如何使用Python SDK与OpenAI Assistants API构建助手?
OpenAI和其Assistant功能旨在为制造商提供一个SDK,用于开发有状态、无提示的Assistant。
其目标是简化虚拟助理的创建。目前,Assistant可以使用三种类型的工具:函数、RAG和代码解释器。
在这篇文章中,我们将介绍如何使用Python SDK构建最基本的Assistant,你只需要在代码中添加你自己的OpenAI API密钥即可。
一些注意事项
OpenAI推出的Assistant能力适用于实验、探索和作为短期的解决方案,也可以充当更大的自治代理实例的扩展。然而,为了实现高度可扩展、可检查和可观察的目标,需要更加详细的方法论。
基于安全的角度,一个项目组织不会选择在OpenAI的环境中管理和存储对话记录,而是更更倾向于隐私性强的大语言模型(LLM)。此外,OpenAI Assistant的无状态特性要求开发者手动管理会话状态、工具定义、文档检索和代码执行,这同样造成了一些使用阻碍。
解决上述的挑战,或许可以通过实现LLM和框架的独立性,摆脱对特定框架的依赖。
如果将对话记录管理交给OpenAI,虽然减轻了开发者的管理负担,但每次运行都会被收取整个对话历史记录的token费用,这就给框架内的token使用带来了一定程度的不透明性,容易引起误解。
与Chat Completions API中的完成不同,创建Run是一个异步操作。要想得知Assistant何时完成处理,需要在循环中轮询运行,尽管这种方法会增加复杂性和支出,但优势在于运行时可以查询状态值,可用于管理会话和通知用户。
完整的OpenAI Assistant开发流程
接下来,我们将详细展示使用Python SDK构建Assistant的过程:
!pip install — upgrade openai
!pip show openai | grep Version
Assistant API的Python SDK要求OpenAI版本>1.2.3。
Version: 1.3.7
定义API密钥。
import json
import os
def show_json(obj):
display(json.loads(obj.model_dump_json()))
os.environ['OPENAI_API_KEY'] = str("Your OpenAI API Key goes here.")
代理已创建完成。
# You can also create Assistants directly through the Assistants API
from openai import OpenAI
client = OpenAI()
assistant = client.beta.assistants.create(
name="History Tutor",
instructions="You are a personal history tutor. Answer questions briefly, in three sentence or less.",
model="gpt-4-1106-preview",
)
show_json(assistant)
在输出的JSON中。创建代理后,您将看到ID、型号、Assistant命名和其他详细信息。
{'id': 'asst_qlaTYRSyl9EWeftjKSskdaco',
'created_at': 1702009585,
'description': None,
'file_ids': [],
'instructions': 'You are a personal history tutor. Answer questions briefly, in three sentence or less.',
'metadata': {},
'model': 'gpt-4-1106-preview',
'name': 'History Tutor',
'object': 'assistant',
'tools': []
}
一旦创建了Assistant,就可以通过OpenAI仪表板看到它,并显示其名称、说明和ID。
无论您是通过仪表板还是使用API创建Assistant,都需要跟踪AssistantI D。

首先创建线程。
# Creating a new thread:
thread = client.beta.threads.create()
show_json(thread)
下面是输出,包括线程ID等。
{'id': 'thread_1flknQB4C8KH4BDYPWsyl0no',
'created_at': 1702009588,
'metadata': {},
'object': 'thread'}
在这里,一条消息被添加到线程中。
# Now we add a message to the thread:
message = client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content="What year was the USA founded?",
)
show_json(message)
结果如下。
在这里,您需要注意的是,即使每次都没有发送会话历史记录,您仍要为每次运行整个会话历史记录支付token费用。
{'id': 'msg_5xOq4FV38cS98ohBpQPbpUiE',
'assistant_id': None,
'content': [{'text': {'annotations': [],
'value': 'What year was the USA founded?'},
'type': 'text'}],
'created_at': 1702009591,
'file_ids': [],
'metadata': {},
'object': 'thread.message',
'role': 'user',
'run_id': None,
'thread_id': 'thread_1flknQB4C8KH4BDYPWsyl0no'}
当前面提到定义run时,必须同时指定Assistant和Thread。
run = client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistant.id,
)
show_json(run)
再次输出:
{'id': 'run_PnwSECkqDDdjWkQ5P7Hcyfor',
'assistant_id': 'asst_qlaTYRSyl9EWeftjKSskdaco',
'cancelled_at': None,
'completed_at': None,
'created_at': 1702009598,
'expires_at': 1702010198,
'failed_at': None,
'file_ids': [],
'instructions': 'You are a personal history tutor. Answer questions briefly, in three sentence or less.',
'last_error': None,
'metadata': {},
'model': 'gpt-4-1106-preview',
'object': 'thread.run',
'required_action': None,
'started_at': None,
'status': 'queued',
'thread_id': 'thread_1flknQB4C8KH4BDYPWsyl0no',
'tools': []}
与Chat Completions API中的完成不同,创建Run是一个异步操作。它将立即返回运行元数据,其中包括一个 status ,初始设置为 queued 。该值将随着Assistant执行操作而更新。
下面的循环检查while循环中的运行状态,直到运行状态达到完整状态。
import time
def wait_on_run(run, thread):
while run.status == "queued" or run.status == "in_progress":
run = client.beta.threads.runs.retrieve(
thread_id=thread.id,
run_id=run.id,
)
time.sleep(0.5)
return run
run = wait_on_run(run, thread)
show_json(run)
低于运行结果。
{'id': 'run_PnwSECkqDDdjWkQ5P7Hcyfor',
'assistant_id': 'asst_qlaTYRSyl9EWeftjKSskdaco',
'cancelled_at': None,
'completed_at': 1702009605,
'created_at': 1702009598,
'expires_at': None,
'failed_at': None,
'file_ids': [],
'instructions': 'You are a personal history tutor. Answer questions briefly, in three sentence or less.',
'last_error': None,
'metadata': {},
'model': 'gpt-4-1106-preview',
'object': 'thread.run',
'required_action': None,
'started_at': 1702009598,
'status': 'completed',
'thread_id': 'thread_1flknQB4C8KH4BDYPWsyl0no',
'tools': []}
一旦运行完成,我们就可以列出线程中的所有消息。
# Now that the Run has completed, list the Messages in the Thread to
# see what got added by the Assistant.
messages = client.beta.threads.messages.list(thread_id=thread.id)
show_json(messages)
再次输出如下…
{'data': [{'id': 'msg_WhzkHcPnszsmbdrn0H5Ugl7I',
'assistant_id': 'asst_qlaTYRSyl9EWeftjKSskdaco',
'content': [{'text': {'annotations': [],
'value': 'The United States of America was founded in 1776, with the adoption of the Declaration of Independence on July 4th of that year.'},
'type': 'text'}],
'created_at': 1702009604,
'file_ids': [],
'metadata': {},
'object': 'thread.message',
'role': 'assistant',
'run_id': 'run_PnwSECkqDDdjWkQ5P7Hcyfor',
'thread_id': 'thread_1flknQB4C8KH4BDYPWsyl0no'},
{'id': 'msg_5xOq4FV38cS98ohBpQPbpUiE',
'assistant_id': None,
'content': [{'text': {'annotations': [],
'value': 'What year was the USA founded?'},
'type': 'text'}],
'created_at': 1702009591,
'file_ids': [],
'metadata': {},
'object': 'thread.message',
'role': 'user',
'run_id': None,
'thread_id': 'thread_1flknQB4C8KH4BDYPWsyl0no'}],
'object': 'list',
'first_id': 'msg_WhzkHcPnszsmbdrn0H5Ugl7I',
'last_id': 'msg_5xOq4FV38cS98ohBpQPbpUiE',
'has_more': False}
一条消息被附加到线程…
# Create a message to append to our thread
message = client.beta.threads.messages.create(
thread_id=thread.id, role="user", content="Could you give me a little more detail on this?"
)
# Execute our run
run = client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistant.id,
)
# Wait for completion
wait_on_run(run, thread)
# Retrieve all the messages added after our last user message
messages = client.beta.threads.messages.list(
thread_id=thread.id, order="asc", after=message.id
)
show_json(messages)
根据结果,考虑内容价值…
{'data': [{'id': 'msg_oIOfuARjk20zZRn6lAytf0Hz',
'assistant_id': 'asst_qlaTYRSyl9EWeftjKSskdaco',
'content': [{'text': {'annotations': [],
'value': 'Certainly! The founding of the USA is marked by the Declaration of Independence, which was ratified by the Continental Congress on July 4, 1776. This act declared the thirteen American colonies free and independent states, breaking away from British rule.'},
'type': 'text'}],
'created_at': 1702009645,
'file_ids': [],
'metadata': {},
'object': 'thread.message',
'role': 'assistant',
'run_id': 'run_9dWR1QFrN983q1AG1cjcQ9Le',
'thread_id': 'thread_1flknQB4C8KH4BDYPWsyl0no'}],
'object': 'list',
'first_id': 'msg_oIOfuARjk20zZRn6lAytf0Hz',
'last_id': 'msg_oIOfuARjk20zZRn6lAytf0Hz',
'has_more': False}
运行完成后,可以在线程中列出消息。
# Now that the Run has completed, list the Messages in the Thread to see
# what got added by the Assistant.
messages = client.beta.threads.messages.list(thread_id=thread.id)
show_json(messages)
结果再次出现。
{'data': [{'id': 'msg_oIOfuARjk20zZRn6lAytf0Hz',
'assistant_id': 'asst_qlaTYRSyl9EWeftjKSskdaco',
'content': [{'text': {'annotations': [],
'value': 'Certainly! The founding of the USA is marked by the Declaration of Independence, which was ratified by the Continental Congress on July 4, 1776. This act declared the thirteen American colonies free and independent states, breaking away from British rule.'},
'type': 'text'}],
'created_at': 1702009645,
'file_ids': [],
'metadata': {},
'object': 'thread.message',
'role': 'assistant',
'run_id': 'run_9dWR1QFrN983q1AG1cjcQ9Le',
'thread_id': 'thread_1flknQB4C8KH4BDYPWsyl0no'},
{'id': 'msg_dDeGGSj4w3CIVRd5hsQpGHmF',
'assistant_id': None,
'content': [{'text': {'annotations': [],
'value': 'Could you give me a little more detail on this?'},
'type': 'text'}],
'created_at': 1702009643,
'file_ids': [],
'metadata': {},
'object': 'thread.message',
'role': 'user',
'run_id': None,
'thread_id': 'thread_1flknQB4C8KH4BDYPWsyl0no'},
{'id': 'msg_WhzkHcPnszsmbdrn0H5Ugl7I',
'assistant_id': 'asst_qlaTYRSyl9EWeftjKSskdaco',
'content': [{'text': {'annotations': [],
'value': 'The United States of America was founded in 1776, with the adoption of the Declaration of Independence on July 4th of that year.'},
'type': 'text'}],
'created_at': 1702009604,
'file_ids': [],
'metadata': {},
'object': 'thread.message',
'role': 'assistant',
'run_id': 'run_PnwSECkqDDdjWkQ5P7Hcyfor',
'thread_id': 'thread_1flknQB4C8KH4BDYPWsyl0no'},
{'id': 'msg_5xOq4FV38cS98ohBpQPbpUiE',
'assistant_id': None,
'content': [{'text': {'annotations': [],
'value': 'What year was the USA founded?'},
'type': 'text'}],
'created_at': 1702009591,
'file_ids': [],
'metadata': {},
'object': 'thread.message',
'role': 'user',
'run_id': None,
'thread_id': 'thread_1flknQB4C8KH4BDYPWsyl0no'}],
'object': 'list',
'first_id': 'msg_oIOfuARjk20zZRn6lAytf0Hz',
'last_id': 'msg_5xOq4FV38cS98ohBpQPbpUiE',
'has_more': False}
热门API
- 1. AI文本生成
- 2. AI图片生成_文生图
- 3. AI图片生成_图生图
- 4. AI图像编辑
- 5. AI视频生成_文生视频
- 6. AI视频生成_图生视频
- 7. AI语音合成_文生语音
- 8. AI文本生成(中国)
最新文章
- DRF库详解:用Django轻松搭建功能强大的API服务
- 一文搞懂在 HTTP 如何 one-api 调用,实操指南来袭!
- 探索海洋数据的宝库:Amentum海洋数据探测API的潜力
- 云原生 API 网关 APISIX 入门教程
- API Key 密钥:深入理解与应用
- 2025年国内免费生成式 AI API 平台大盘点:探索国产大模型的开放能力
- 使用DeepSeek和Claude绘制出高质量的SVG 图片
- 精通REST API:解析iOS开发中的核心要点
- ASP.NET Core Web API 的授权指南 – Auth0
- Supertest:如何像专业人士一样测试API – Testim博客
- Next.js API 路由:GET 和 POST 请求示例
- Kimi K2 API 调用全指南:解锁国产大模型的强大能力