Wemo.Learn

Step 1 of 11· AI Customer Support Chatbot

Setup

Create the project, install the dependencies, configure the OpenAI API key and send a first request.

Purpose#

Create the Python project, install the dependencies, configure settings and confirm that the OpenAI API key works.

In this step:

  • Create the project with uv
  • Configure the API key in .env
  • Add a settings module and a shared OpenAI client
  • Send a first request

Create the project#

Terminal
uv init --python 3.12 --no-package support-desk
cd support-desk
uv add openai "psycopg[binary,pool]" python-dotenv fastapi "uvicorn[standard]"
mkdir -p support_desk/static knowledge_base evals
touch support_desk/__init__.py evals/__init__.py
rm main.py

Dependencies:

PackageUsed for
openaiEmbeddings and answers (official SDK)
psycopg[binary,pool]Postgres driver with a connection pool
python-dotenvLoading settings from .env
fastapi, uvicorn[standard]HTTP API and server (step 8)

uv init creates the project, uv add installs packages into .venv, and uv run runs commands in it. Run every command in this guide from the support-desk folder.

API key#

  1. Create an API key at platform.openai.com under API keys.
  2. Create .env in support-desk with the content below.
  3. Set OPENAI_API_KEY.
.env
# OpenAI (https://platform.openai.com -> API keys)
OPENAI_API_KEY=sk-...
OPENAI_MODEL=gpt-5.4-mini
EMBEDDING_MODEL=text-embedding-3-small
# Optional reasoning effort for the answer model, for example low or medium. Empty = model default.
OPENAI_REASONING_EFFORT=

# Supabase (step 2): Project -> Connect -> Session pooler -> URI
DATABASE_URL=postgresql://postgres.xxxx:password@aws-0-eu-central-1.pooler.supabase.com:5432/postgres

# Search (step 5): keyword, vector or hybrid. Choose with the retrieval eval (step 9).
SEARCH_MODE=vector
TOP_K=5

# Answer cache (step 7): hours a cached answer is reused. 0 turns the cache off.
CACHE_TTL_HOURS=168

# API (step 8): websites that may embed the widget (comma separated)
ALLOWED_ORIGINS=http://localhost:8000
MAX_MESSAGE_CHARS=1000
# A random string. Used to hash visitor IP addresses, so raw IPs are never stored.
CLIENT_SALT=change-me
# Set to true only behind a proxy that sets X-Forwarded-For (Railway, Render, nginx).
TRUST_PROXY_HEADERS=false

Only OPENAI_API_KEY and OPENAI_MODEL are needed for now. The other settings are used in later steps.

Settings module#

All settings are read in one module. Other modules import from it.

support_desk/config.py
"""All settings in one place. Values come from the .env file."""
import os

from dotenv import load_dotenv

load_dotenv()


def _list(name: str) -> list[str]:
    return [x.strip() for x in os.getenv(name, "").split(",") if x.strip()]


COMPANY_NAME = os.getenv("COMPANY_NAME", "Brightside Cleaning Co.")

# OpenAI
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY", "")
OPENAI_MODEL = os.getenv("OPENAI_MODEL", "gpt-5.4-mini")
OPENAI_REASONING_EFFORT = os.getenv("OPENAI_REASONING_EFFORT", "")
EMBEDDING_MODEL = os.getenv("EMBEDDING_MODEL", "text-embedding-3-small")
EMBEDDING_DIM = 1536  # must match vector(1536) in schema.sql

# Supabase Postgres (step 2)
DATABASE_URL = os.getenv("DATABASE_URL", "")

# Knowledge base and search (steps 3 to 5)
KB_DIR = os.getenv("KB_DIR", "knowledge_base")
SEARCH_MODE = os.getenv("SEARCH_MODE", "vector")  # chosen with the retrieval eval (step 9)
TOP_K = int(os.getenv("TOP_K", "5"))

# Answer cache (step 7): hours a cached answer is reused. 0 turns the cache off.
CACHE_TTL_HOURS = int(os.getenv("CACHE_TTL_HOURS", "168"))

# API (step 8)
ALLOWED_ORIGINS = _list("ALLOWED_ORIGINS")
MAX_MESSAGE_CHARS = int(os.getenv("MAX_MESSAGE_CHARS", "1000"))
CLIENT_SALT = os.getenv("CLIENT_SALT", "change-me")
TRUST_PROXY_HEADERS = os.getenv("TRUST_PROXY_HEADERS", "false").lower() == "true"

config.py loads .env and gives every setting a default, so later steps can leave their settings empty until needed.

OpenAI client#

All modules use one client.

support_desk/openai_client.py
"""One shared OpenAI client with a timeout and automatic retries."""
from functools import lru_cache

from openai import OpenAI

from support_desk import config


@lru_cache
def get_client() -> OpenAI:
    # timeout: a chat visitor will not wait longer. max_retries: the SDK retries rate limits,
    # timeouts and server errors with backoff.
    return OpenAI(api_key=config.OPENAI_API_KEY or None, timeout=30, max_retries=2)

One shared OpenAI client for the whole app, with a 30-second timeout and two automatic retries.

First request#

hello_openai.py
"""Step 1 checkpoint: check that the API key and the model work."""
from support_desk import config
from support_desk.openai_client import get_client

response = get_client().responses.create(
    model=config.OPENAI_MODEL,
    input="Reply with one short sentence confirming that this API connection works.",
)
print(response.output_text)

responses.create() sends the prompt to the model, and output_text is its reply.

Checkpoint#

Terminal
uv run python hello_openai.py

Expected output (wording varies):

Output
This API connection works.

Troubleshooting#

  • AuthenticationError: the key is missing or invalid. Check that .env contains the key without quotes or spaces.
  • ModuleNotFoundError: support_desk: run the command from the support-desk directory.
  • NotFoundError for the model: your account has no access to the model. Set OPENAI_MODEL to a model listed in the OpenAI dashboard under Limits.
  • No pyproject.toml found: the command ran outside the project. Run cd support-desk first.
  • Expected a Python module at: src/support_desk/__init__.py: the project was created as a package. Open pyproject.toml, delete the [project.scripts] and [build-system] sections, and delete the src folder. Or delete the project folder and run uv init again with --no-package.