This repository contains a FastAPI application that demonstrates the OpenAI Assistants QuickStart guide, as shown in the official documentation. It implements the step-by-step instructions for creating an Assistant (including Code Interpreter), creating Threads for conversation, adding Messages, and streaming a Run using Server-Sent Events (SSE).
- OpenAI Assistants (Beta)
This app uses the OpenAI Python Beta SDK to create, configure, and run a GPT-based Assistant. - FastAPI
We wrap the Assistants API logic in a FastAPI server to provide an HTTP interface, including SSE endpoints for real-time token streaming. - Streaming Chat UI
A simple front-end using Alpine.js & TailwindCSS to let users enter a prompt, see the streamed tokens in real time, and stop the generation if desired.
This application follows the four major steps from OpenAI’s Assistants QuickStart:
- Create an Assistant
Configure a custom name, instructions, and attach any tools (e.g., Code Interpreter) to the GPT model. - Create a Thread
Start a conversation “session” for the user. - Add Messages
As the user talks to the Assistant, each message is stored in the Thread. - Run the Assistant
Generate a response by calling the model (and any tools) on the entire conversation context. In this example, we use streaming to emit tokens to the client in real time.
- Real-Time Streaming
Uses Server-Sent Events (SSE) viasse_starlette
to stream tokens as they are generated by the Assistant. - Code Interpreter Tool
Demonstrates how to enable the built-incode_interpreter
tool for advanced math, scripting, and code execution. - FastAPI + Alpine.js
Lightweight Python backend with a minimal, reactive JavaScript frontend. - Stop Generation
A Stop button in the UI closes the SSE connection, immediately stopping further token streaming.
-
Clone the repository:
git clone https://github.com/your-username/fastapi_ai_assistant.git cd fastapi_ai_assistant
-
Install dependencies (e.g.,
requirements.txt
):pip install -r requirements.txt
-
Configure OpenAI (Beta) environment:
- Set your
OPENAI_API_KEY
environment variable:export OPENAI_API_KEY="sk-..."
- Set your
-
Run the FastAPI application:
python main.py
or
uvicorn main:app --host 0.0.0.0 --port 8000
-
Navigate to:
http://localhost:8000/
-
Chat with the AI Assistant:
- Enter a message and press Send.
- Watch as the response arrives token-by-token.
- Click Stop to end the generation at any time.
fastapi_ai_assistant/
├── templates/
│ └── index.html # Contains the chat UI (HTML, Alpine.js, TailwindCSS)
├── main.py # FastAPI app with SSE and Assistants logic
├── README.md # This file
└── requirements.txt # Python dependencies (optional)
-
main.py
implements:- Assistant Creation:
assistant = client.beta.assistants.create(...)
- Thread Management:
thread = client.beta.threads.create()
- Message Creation:
client.beta.threads.messages.create(...)
- Run Streaming: Uses the
runs.stream(...)
context manager and a custom SSE event handler. - FastAPI Endpoints:
GET /
: Serves the chat UI.GET /chat-stream?prompt=...
: Streams the AI response via SSE.
- Assistant Creation:
-
index.html
:- A Tailwind-styled chat interface with Alpine.js for reactivity.
- Subscribes to the
/chat-stream
endpoint usingEventSource
. - Displays real-time tokens as they arrive and offers a Stop button.
- Model and Tools:
Change themodel
parameter or add more tools inmain.py
to suit your needs.
E.g.,tools=[{"type": "code_interpreter"}, {"type": "another_tool"}]
. - Instructions:
Customize the Assistant’s global instructions (role: system) to define its personality or domain knowledge. - UI/Styling:
Modifyindex.html
or integrate a different frontend approach.
Feel free to open issues or submit pull requests if you have suggestions or improvements.
This project is provided under the MIT License.
See the LICENSE file for more details (if present).
Happy coding! Check out the OpenAI Assistants QuickStart docs for more information. If you have questions or run into any issues, please open an issue.