- Recall memory for the incoming message.
- Call your model.
- Store the user and assistant turns.
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:Use
deps.py
tex_client() inside FastAPI Depends(...) so every route shares the same connection pool.3
Build the chat route
Derive Replace
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
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
SetTex(timeout=2.0) and catch APITimeoutError. If recall is slow, answer without memory instead of blocking the whole chat request.

