Assistants
Build AI assistants with memory and tools
Assistants (Beta)
The Assistants API allows you to build AI assistants within your own applications. An Assistant has instructions and can leverage models, tools, and knowledge to respond to user queries.
The Assistants API supports persistent threads, so you don't need to manage message history yourself.
Overview
- Create an Assistant: Define its custom instructions and pick a model.
- Create a Thread: A conversation session between a user and the assistant.
- Add Messages: User questions added to the thread.
- Run the Assistant: Trigger the assistant to process the thread and generate a response.
1. Create an Assistant
from openai import OpenAI
client = OpenAI(
api_key="YOUR_API_KEY",
base_url="https://api.aize.dev/v1"
)
assistant = client.beta.assistants.create(
name="Math Tutor",
instructions="You are a personal math tutor. Write and run code to answer math questions.",
tools=[{"type": "code_interpreter"}],
model="gpt-4-turbo"
)2. Create a Thread
thread = client.beta.threads.create()3. Add a Message
Add the user's question to the thread.
message = client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content="I need to solve the equation `3x + 11 = 14`. Can you help me?"
)4. Run the Assistant
Run the assistant on the thread to generate a response.
run = client.beta.threads.runs.create_and_poll(
thread_id=thread.id,
assistant_id=assistant.id,
instructions="Please address the user as Jane Doe. The user has a premium account."
)
if run.status == 'completed':
messages = client.beta.threads.messages.list(
thread_id=thread.id
)
print(messages)
else:
print(run.status)Supported Tools
- Code Interpreter: Write and run Python code.
- File Search: Search for information in uploaded files.
- Function Calling: Call your own custom functions.
Endpoints
| Method | Endpoint | Description |
|---|---|---|
GET | /v1/assistants | List assistants |
POST | /v1/assistants | Create an assistant |
GET | /v1/threads/{thread_id} | Retrieve a thread |
POST | /v1/threads/{thread_id}/messages | Add a message |
POST | /v1/threads/{thread_id}/runs | Create a run |