Chatbot — full example

Complete streaming chatbot with history buffer, system prompt, and abort control.

Python Backend Script

import asyncio, json, os
from fastapi import FastAPI, Request
from fastapi.responses import StreamingResponse
from openai import AsyncOpenAI

app = FastAPI()
client = AsyncOpenAI(api_key=os.getenv("OPENAI_API_KEY"))

SYSTEM_PROMPT = (
    "You are Nimbus Assistant, a helpful AI for the Nimbus "
    "desktop application. Answer concisely and accurately."
)
MAX_HISTORY = 20

@app.post("/api/chat")
async def chat(req: Request):
    body = await req.json()
    raw_messages = body.get("messages", [])

    history = raw_messages[-MAX_HISTORY:]
    messages = [{"role": "system", "content": SYSTEM_PROMPT}] + history

    async def event_stream():
        try:
            stream = await client.chat.completions.create(
                model="gpt-4o-mini",
                messages=messages,
                stream=True,
                temperature=0.7,
                max_tokens=1024,
            )
            async for chunk in stream:
                delta = chunk.choices[0].delta
                if delta.content:
                    yield f"data: {delta.content}\n\n"
            yield "data: [DONE]\n\n"
        except Exception as e:
            yield f"data: [ERROR] {str(e)}\n\n"

    return StreamingResponse(
        event_stream(),
        media_type="text/event-stream",
        headers={
            "Cache-Control": "no-cache",
            "Connection": "keep-alive",
            "X-Accel-Buffering": "no",
        },
    )

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)

Send a message to start the conversation.