Guides

FastAPI Integration

Reference pattern for a FastAPI chatbot: ingest user signal, retrieve context, answer, ingest assistant output, record feedback.

Orbit Cloud users can create API keys in Dashboard. Replace <jwt-token> below with your orbit_pk_... key.

main.py
from fastapi import FastAPI
from orbit import MemoryEngine

app = FastAPI()
orbit = MemoryEngine(api_key="<jwt-token>", base_url="http://localhost:8000")

@app.post("/chat")
async def chat(user_id: str, message: str) -> dict[str, str]:
    orbit.ingest(
        content=message,
        event_type="user_question",
        entity_id=user_id,
    )

    context = orbit.retrieve(
        query=f"What should I know about {user_id} for: {message}",
        entity_id=user_id,
        limit=5,
    )

    prompt_context = "\n".join(f"- {m.content}" for m in context.memories)
    answer = f"(LLM answer using context)\n{prompt_context}"

    orbit.ingest(
        content=answer,
        event_type="assistant_response",
        entity_id=user_id,
    )
    return {"response": answer}

@app.post("/feedback")
async def feedback(memory_id: str, helpful: bool) -> dict[str, bool]:
    orbit.feedback(
        memory_id=memory_id,
        helpful=helpful,
        outcome_value=1.0 if helpful else -1.0,
    )
    return {"recorded": True}

Integration guardrails

>

Ingest user and assistant events with the same entity_id scope.

>

Keep retrieve limit small first (5 to 10) and measure quality.

>

Record feedback for both successful and poor responses.

>

Inspect provenance metadata during QA to explain ranking behavior.