Python Chat Application with Memory
Build a Python chat backend that uses Papr for persistent conversation memory.
Scenario
You are building a support assistant in Python and need:
- cross-session memory,
- compressed history for model prompts,
- graph-aware recall of relevant past context.
What You Will Build
- FastAPI endpoint for chat messages
- Messages API storage and compression
- Memory search integration before response generation
Prerequisites
- Python 3.10+
papr_memory,fastapi,uvicorn- Papr API key
Step 1: Install Dependencies
pip install papr_memory fastapi uvicornStep 2: Build the Chat Backend
import os
from fastapi import FastAPI
from pydantic import BaseModel
from papr_memory import Papr
app = FastAPI()
client = Papr(x_api_key=os.environ.get("PAPR_MEMORY_API_KEY"))
class ChatRequest(BaseModel):
session_id: str
external_user_id: str
message: str
@app.post("/chat")
def chat(req: ChatRequest):
client.messages.store(
sessionId=req.session_id,
role="user",
content=req.message,
external_user_id=req.external_user_id,
process_messages=True,
)
compressed = client.messages.compress_session(session_id=req.session_id)
memory_results = client.memory.search(
query=f"Retrieve context relevant to: {req.message}",
external_user_id=req.external_user_id,
enable_agentic_graph=True,
)
# Replace with your model provider call.
assistant_reply = (
"Stub response. Connect your model here using "
"compressed context and memory search results."
)
client.messages.store(
sessionId=req.session_id,
role="assistant",
content=assistant_reply,
external_user_id=req.external_user_id,
process_messages=True,
)
return {
"reply": assistant_reply,
"context_for_llm": compressed.context_for_llm,
"memory_count": len(memory_results.data.get("memories", []))
if hasattr(memory_results, "data")
else None,
}Step 3: Run
uvicorn app:app --reloadStep 4: Test
curl -X POST http://127.0.0.1:8000/chat \
-H "Content-Type: application/json" \
-d '{
"session_id": "session_001",
"external_user_id": "user_123",
"message": "I prefer weekly reports by email."
}'Production Considerations
- Add retry and timeout wrappers around network calls.
- Persist your own session metadata in app storage.
- Use
organization_idandnamespace_idin multi-tenant systems. - Add
memory_policy.aclfor scoped memory access.