OpenAI Assistants API v2 2026: Files, Code Interpreter, and Threads
Build persistent AI assistants with built-in RAG, code execution, function calling
OpenAI Assistants API v2 2026: Files, Code Interpreter, and Threads
Build persistent AI assistants with built-in RAG, code execution, function calling
Complete OpenAI Assistants API tutorial. Create assistants, manage threads, upload files for RAG, use Code Interpreter for data analysis, stream responses, handle function calling.
OpenAI Assistants API v2 2026: Files, Threads, Code Interpreter
The Assistants API provides managed infrastructure: threads, file search (RAG), code execution, and function calling.
Core Concepts
Create an Assistant
python
from openai import OpenAIclient = OpenAI()
assistant = client.beta.assistants.create(
name='Data Analyst',
instructions='Analyze data with Code Interpreter. Show Python code before results.',
model='gpt-4o',
tools=[{'type': 'code_interpreter'}, {'type': 'file_search'}]
)
print(f'Assistant ID: {assistant.id}') # Save this!
File Search (Managed RAG)
python
import timewith open('handbook.pdf', 'rb') as f:
file = client.files.create(file=f, purpose='assistants')
vs = client.beta.vector_stores.create(name='Company Knowledge')
client.beta.vector_stores.files.create(vector_store_id=vs.id, file_id=file.id)
while client.beta.vector_stores.retrieve(vs.id).status != 'completed':
time.sleep(1)
client.beta.assistants.update(
assistant_id=assistant.id,
tool_resources={'file_search': {'vector_store_ids': [vs.id]}}
)
Multi-Turn Conversations
python
thread = client.beta.threads.create()client.beta.threads.messages.create(
thread_id=thread.id, role='user',
content='What is our parental leave policy?'
)
run = client.beta.threads.runs.create_and_poll(
thread_id=thread.id, assistant_id=assistant.id
)
for msg in client.beta.threads.messages.list(thread.id).data:
if msg.role == 'assistant':
for c in msg.content:
if c.type == 'text':
print(c.text.value)
break
Code Interpreter: Data Analysis
python
with open('sales.csv', 'rb') as f:
csv = client.files.create(file=f, purpose='assistants')thread = client.beta.threads.create()
client.beta.threads.messages.create(
thread_id=thread.id, role='user',
content='Analyze data. Find top 5 products and create a bar chart.',
attachments=[{'file_id': csv.id, 'tools': [{'type': 'code_interpreter'}]}]
)
run = client.beta.threads.runs.create_and_poll(thread_id=thread.id, assistant_id=assistant.id)
for msg in client.beta.threads.messages.list(thread.id).data:
if msg.role == 'assistant':
for c in msg.content:
if c.type == 'image_file':
data = client.files.content(c.image_file.file_id)
with open('chart.png', 'wb') as f:
f.write(data.read())
print('Saved: chart.png')
Streaming
python
with client.beta.threads.runs.stream(
thread_id=thread.id,
assistant_id=assistant.id,
) as stream:
for text in stream.text_deltas:
print(text, end='', flush=True)
Conclusion
The Assistants API is ideal when you want OpenAI managing RAG infrastructure, thread storage, and code execution. Best for rapid prototyping.
相关工具
相关教程
Automatically classify, summarize, and draft replies to emails using AI
Build voice AI applications with natural-sounding TTS and custom voice cloning
Transcribe audio files, meetings, and real-time speech with Whisper