助理 API
助理 API 允许您在自己的应用程序中构建 AI 助手。助手有指示,并且可以利用模型、工具和文件来回答用户的查询。Assistants API 当前支持三种类型的工具:代码解释器、文件搜索和函数调用。
您可以使用 助理 游乐场探索 助理 API 的功能,或者按照本指南中的步骤构建集成。
概述
典型的 助理 API 集成流程如下:
- 创建助手,定义其自定义指示,选择一个模型,并且如果有必要,添加文件并启用工具,如代码解释器。
- 当用户开始会话时,创建一个线程。
- 将消息添加到线程,以便用户提出问题。
- 在线程上运行助手,调用模型和工具以生成响应。
在本指南中,我们将逐步介绍创建和运行使用代码解释器工具的助手的关键步骤。在本例中,我们将创建一个名为“数学辅导”的助手,其指示是“您是一位个人数学辅导员。编写和运行代码以回答数学问题。”,并且代码解释器工具已启用。
步骤 1:创建助手
助手是一个可以配置为使用几个参数(如 model
、instructions
和 tools
)来回答用户消息的实体。
使用 Python 库创建助手:
from openai import OpenAI
client = OpenAI()
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-4o",
)
使用 Node.js 库创建助手:
const { OpenAI } = require("openai");
const client = new OpenAI();
const assistant = await 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-4o",
});
使用 cURL 创建助手:
curl https://api.openai.com/v1/beta/assistants \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"name": "Math Tutor",
"instructions": "You are a personal math tutor. Write and run code to answer math questions.",
"tools": [{"type": "code_interpreter"}],
"model": "gpt-4o"
}'
注意:调用 Assistants API 需要传递 beta HTTP 头。这在使用 OpenAI 的官方 Python 或 Node.js SDK 时将自动处理。
步骤 2:创建线程
线程表示用户与一个或多个助手的会话。您可以在用户(或您的 AI 应用程序)开始与您的助手交谈时创建一个线程。
使用 Python 库创建线程:
thread = client.beta.threads.create()
使用 Node.js 库创建线程:
const thread = await client.beta.threads.create();
使用 cURL 创建线程:
curl https://api.openai.com/v1/beta/threads \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{}'
步骤 3:将消息添加到线程
用户或应用程序创建的消息的内容被添加为消息对象到线程中。消息可以包含文本和文件。没有最大消息数量的限制 — 我们会智能地截断任何不适合模型上下文窗口的内容。
使用 Python 库将消息添加到线程:
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?",
)
使用 Node.js 库将消息添加到线程:
const message = await 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?",
});
使用 cURL 将消息添加到线程:
curl https://api.openai.com/v1/beta/threads/${THREAD_ID}/messages \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"role": "user",
"content": "I need to solve the equation `3x + 11 = 14`. Can you help me?"
}'
请注意,在此处将 THREAD_ID
替换为实际的线程 ID。
步骤 4:创建运行
一旦将所有用户消息添加到线程,您可以使用助手的模型和工具在线程上创建运行以生成响应。这些响应被添加到线程中作为助手消息。
使用 Python 库创建运行:
run = client.beta.threads.runs.create(
thread_id=thread.id,
assistant_id=assistant.id,
)
使用 Node.js 库创建运行:
const run = await client.beta.threads.runs.create({
thread_id: thread.id,
assistant_id: assistant.id,
});
使用 cURL 创建运行:
curl https://api.openai.com/v1/beta/threads/${THREAD_ID}/runs \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"assistant_id": "${ASSISTANT_ID}"
}'
请注意,在此处将 THREAD_ID
替换为实际的线程 ID,将 ASSISTANT_ID
替换为实际的助手 ID。
后续步骤
了解有关助手的工作原理的更多信息,并探索 Assistants API 的其他功能:
- 深入了解如何使用助手工作
- 了解有关工具的更多信息
- 在 Assistants 游乐场中探索更多可能性