Skip to main content
This recipe puts the quickstart loop behind one HTTP route:
  1. Recall memory for the incoming message.
  2. Call your model.
  3. Store the user and assistant turns.
Create one Tex client per process and reuse it across requests.

Layout

1

Lay out the package

Create this structure:
Rename app/ if you want. Keep the import paths consistent in uvicorn.
2

Cache your Tex client

Read secrets from the environment and construct Tex once:
deps.py
Use tex_client() inside FastAPI Depends(...) so every route shares the same connection pool.
3

Build the chat route

Derive session_id from the user and the chat. Recall with a small top_k. If Tex times out or quota is exhausted, answer without memory. Then store both sides of the turn:
chat.py
Replace your_llm.complete(...) with your model call.
4

Expose the app

Mount the router once:
main.py
5

Run locally

Export your key and launch uvicorn:
Send POST /chat with JSON {"text":"...","session_id":"..."} and header x-user-id.

Full files

If you prefer one copy block, paste these files:

Production tweaks

Run remember in the background

Do not make the user wait for remember. Enqueue it in the background:

Bound recall latency

Set Tex(timeout=2.0) and catch APITimeoutError. If recall is slow, answer without memory instead of blocking the whole chat request.

Add a health probe