OpenAI API 为最先进的 AI 提供了简单的接口, 视觉 涵盖文本生成、自然语言处理、计算机视觉等功能。请先创建一个 API 密钥并运行你的首个 API 调用,以此为起点。探索如何生成文本、分析图像、构建智能体以及更多功能。
创建 API 密钥
在开始之前,请在控制台中创建一个 API 密钥,你将使用它来安全地 访问 API。请将密钥存储在安全的位置,例如 .zshrc
文件 或计算机上的其他文本文件。生成 API 密钥后,将其导出为 环境变量
in your terminal.
macOS / Linux
1
export OPENAI_API_KEY="your_api_key_here"
Windows
1
setx OPENAI_API_KEY "your_api_key_here"
OpenAI SDK 已配置为自动从系统环境中读取您的 API 密钥。
JavaScript
要在 Node.js、Deno 或 Bun 等服务端 JavaScript 环境中使用 OpenAI API,您可以使用官方的 适用于 TypeScript 和 JavaScript 的 OpenAI SDK。通过安装以下 SDK 开始使用 npm 或您首选的包管理器:
安装 OpenAI SDK 后,创建一个名为 example.mjs 的文件,并将示例代码复制到其中:
1
2
3
4
5
6
7
8
9
import OpenAI from "openai";
const client = new OpenAI();
const response = await client.responses.create({
model: "gpt-5.5",
input: "Write a one-sentence bedtime story about a unicorn."
});
console.log(response.output_text);
使用 node example.mjs 执行代码(或 Deno 和 Bun 的等效命令)。片刻之后,您应该就能看到 API 请求的输出结果。
前往该库的 GitHub README 探索更多 SDK 的功能与选项。
Python
要在 Python 中使用 OpenAI API,您可以使用官方的 适用于 Python 的 OpenAI SDK。通过安装以下 SDK 开始使用 pip:
安装 OpenAI SDK 后,创建一个名为 example.py 的文件,并将示例代码复制到其中:
1
2
3
4
5
6
7
8
9
from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model="gpt-5.5",
input="Write a one-sentence bedtime story about a unicorn."
)
print(response.output_text)
使用 python example.py。稍后,你应该会看到 API 请求的输出。
前往该库的 GitHub README 探索更多 SDK 的功能与选项。
.NET
OpenAI 与 Microsoft 合作,提供了官方支持的 C# API 客户端。你可以使用 .NET CLI 从 NuGet.
dotnet add package OpenAI
一个简单的对 Responses API 的 API 请求如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using System;
using System.Threading.Tasks;
using OpenAI;
class Program
{
static async Task Main()
{
var client = new OpenAIClient(
Environment.GetEnvironmentVariable("OPENAI_API_KEY")
);
var response = await client.Responses.CreateAsync(new ResponseCreateRequest
{
Model = "gpt-5.5",
Input = "Say 'this is a test.'"
});
Console.WriteLine($"[ASSISTANT]: {response.OutputText()}");
}
}
Java
OpenAI 为 Java 编程语言提供了一个 API 助手,目前处于测试阶段。你可以使用以下配置添加 Maven 依赖:
<dependency>
<groupId>com.openai</groupId>
<artifactId>openai-java</artifactId>
<version>4.0.0</version>
</dependency>
一个简单的对 Responses API 的 API 请求如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.responses.Response;
import com.openai.models.responses.ResponseCreateParams;
public class Main {
public static void main(String[] args) {
OpenAIClient client = OpenAIOkHttpClient.fromEnv();
ResponseCreateParams params = ResponseCreateParams.builder()
.input("Say this is a test")
.model("gpt-5.5")
.build();
Response response = client.responses().create(params);
System.out.println(response.outputText());
}
}
要了解更多关于在 Java 中使用 OpenAI API 的信息,请查看下面链接的 GitHub 仓库!
前往该库的 GitHub README 探索更多 SDK 的功能与选项。
Go
OpenAI 为 Go 编程语言提供了一个 API 助手,目前处于测试阶段。你可以使用以下代码导入该库:
import (
"github.com/openai/openai-go" // imported as openai
)
一个简单的对 Responses API 的 API 请求如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package main
import (
"context"
"fmt"
"github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/option"
"github.com/openai/openai-go/v3/responses"
)
func main() {
client := openai.NewClient(
option.WithAPIKey("My API Key"),
)
resp, err := client.Responses.New(context.TODO(), openai.ResponseNewParams{
Model: "gpt-5.5",
Input: responses.ResponseNewParamsInputUnion{OfString: openai.String("Say this is a test")},
})
if err != nil {
panic(err.Error())
}
fmt.Println(resp.OutputText())
}
要了解更多关于在 Go 中使用 OpenAI API 的信息,请查看下面链接的 GitHub 仓库!
前往该库的 GitHub README 探索更多 SDK 的功能与选项。
了解更多关于提示词、消息角色以及构建对话应用的知识。
前往账单
恭喜你成功运行了一个免费的测试 API 请求!开始构建具有更高限额的实际应用,并使用
我们的模型 来生成文本、音频、图像、视频等。
将图像 URL、上传的文件或 PDF 文档直接发送给模型,以提取文本、对内容进行分类或检测视觉元素。
图像 URL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import OpenAI from "openai";
const client = new OpenAI();
const response = await client.responses.create({
model: "gpt-5.5",
input: [
{
role: "user",
content: [
{
type: "input_text",
text: "What is in this image?",
},
{
type: "input_image",
image_url: "https://openai-documentation.vercel.app/images/cat_and_otter.png",
},
],
},
],
});
console.log(response.output_text);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
curl "https://api.openai.com/v1/responses" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-5.5",
"input": [
{
"role": "user",
"content": [
{
"type": "input_text",
"text": "What is in this image?"
},
{
"type": "input_image",
"image_url": "https://openai-documentation.vercel.app/images/cat_and_otter.png"
}
]
}
]
}'
1
2
3
4
5
6
7
8
9
10
11
12
openai responses create \
--model gpt-5.5 \
--raw-output \
--transform 'output.#(type=="message").content.0.text' <<'YAML'
input:
- role: user
content:
- type: input_text
text: What is in this image?
- type: input_image
image_url: https://openai-documentation.vercel.app/images/cat_and_otter.png
YAML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model="gpt-5.5",
input=[
{
"role": "user",
"content": [
{
"type": "input_text",
"text": "What teams are playing in this image?",
},
{
"type": "input_image",
"image_url": "https://api.nga.gov/iiif/a2e6da57-3cd1-4235-b20e-95dcaefed6c8/full/!800,800/0/default.jpg"
}
]
}
]
)
print(response.output_text)
1
2
3
4
5
6
7
8
9
10
11
12
13
using OpenAI.Responses;
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
OpenAIResponseClient client = new(model: "gpt-5.5", apiKey: key);
OpenAIResponse response = (OpenAIResponse)client.CreateResponse([
ResponseItem.CreateUserMessageItem([
ResponseContentPart.CreateInputTextPart("What is in this image?"),
ResponseContentPart.CreateInputImagePart(new Uri("https://openai-documentation.vercel.app/images/cat_and_otter.png")),
]),
]);
Console.WriteLine(response.GetOutputText());
文件 URL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
curl "https://api.openai.com/v1/responses" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-5.5",
"input": [
{
"role": "user",
"content": [
{
"type": "input_text",
"text": "Analyze the letter and provide a summary of the key points."
},
{
"type": "input_file",
"file_url": "https://www.berkshirehathaway.com/letters/2024ltr.pdf"
}
]
}
]
}'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import OpenAI from "openai";
const client = new OpenAI();
const response = await client.responses.create({
model: "gpt-5.5",
input: [
{
role: "user",
content: [
{
type: "input_text",
text: "Analyze the letter and provide a summary of the key points.",
},
{
type: "input_file",
file_url: "https://www.berkshirehathaway.com/letters/2024ltr.pdf",
},
],
},
],
});
console.log(response.output_text);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model="gpt-5.5",
input=[
{
"role": "user",
"content": [
{
"type": "input_text",
"text": "Analyze the letter and provide a summary of the key points.",
},
{
"type": "input_file",
"file_url": "https://www.berkshirehathaway.com/letters/2024ltr.pdf",
},
],
},
]
)
print(response.output_text)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using OpenAI.Files;
using OpenAI.Responses;
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
OpenAIResponseClient client = new(model: "gpt-5.5", apiKey: key);
using HttpClient http = new();
using Stream stream = await http.GetStreamAsync("https://www.berkshirehathaway.com/letters/2024ltr.pdf");
OpenAIFileClient files = new(key);
OpenAIFile file = files.UploadFile(stream, "2024ltr.pdf", FileUploadPurpose.UserData);
OpenAIResponse response = (OpenAIResponse)client.CreateResponse([
ResponseItem.CreateUserMessageItem([
ResponseContentPart.CreateInputTextPart("Analyze the letter and provide a summary of the key points."),
ResponseContentPart.CreateInputFilePart(file.Id),
]),
]);
Console.WriteLine(response.GetOutputText());
上传文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
curl https://api.openai.com/v1/files \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-F purpose="user_data" \
-F file="@draconomicon.pdf"
curl "https://api.openai.com/v1/responses" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-5.5",
"input": [
{
"role": "user",
"content": [
{
"type": "input_file",
"file_id": "file-6F2ksmvXxt4VdoqmHRw6kL"
},
{
"type": "input_text",
"text": "What is the first dragon in the book?"
}
]
}
]
}'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import fs from "fs";
import OpenAI from "openai";
const client = new OpenAI();
const file = await client.files.create({
file: fs.createReadStream("draconomicon.pdf"),
purpose: "user_data",
});
const response = await client.responses.create({
model: "gpt-5.5",
input: [
{
role: "user",
content: [
{
type: "input_file",
file_id: file.id,
},
{
type: "input_text",
text: "What is the first dragon in the book?",
},
],
},
],
});
console.log(response.output_text);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from openai import OpenAI
client = OpenAI()
file = client.files.create(
file=open("draconomicon.pdf", "rb"),
purpose="user_data"
)
response = client.responses.create(
model="gpt-5.5",
input=[
{
"role": "user",
"content": [
{
"type": "input_file",
"file_id": file.id,
},
{
"type": "input_text",
"text": "What is the first dragon in the book?",
},
]
}
]
)
print(response.output_text)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using OpenAI.Files;
using OpenAI.Responses;
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
OpenAIResponseClient client = new(model: "gpt-5.5", apiKey: key);
OpenAIFileClient files = new(key);
OpenAIFile file = files.UploadFile("draconomicon.pdf", FileUploadPurpose.UserData);
OpenAIResponse response = (OpenAIResponse)client.CreateResponse([
ResponseItem.CreateUserMessageItem([
ResponseContentPart.CreateInputFilePart(file.Id),
ResponseContentPart.CreateInputTextPart("What is the first dragon in the book?"),
]),
]);
Console.WriteLine(response.GetOutputText());
学习如何使用模型的文件输入功能并从文档中提取含义。
通过附加工具,让模型能够访问外部数据和函数 工具。使用内置工具(如网页搜索或文件搜索),或定义您自己的工具,以用于调用 API、运行代码或与第三方系统集成。
网络搜索
1
2
3
4
5
6
7
8
9
10
11
12
import OpenAI from "openai";
const client = new OpenAI();
const response = await client.responses.create({
model: "gpt-5.5",
tools: [
{ type: "web_search" },
],
input: "What was a positive news story from today?",
});
console.log(response.output_text);
1
2
3
4
5
6
7
8
9
10
from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model="gpt-5.5",
tools=[{"type": "web_search"}],
input="What was a positive news story from today?"
)
print(response.output_text)
1
2
3
4
5
6
7
8
curl "https://api.openai.com/v1/responses" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-5.5",
"tools": [{"type": "web_search"}],
"input": "what was a positive news story from today?"
}'
1
2
3
4
5
6
7
8
openai responses create \
--model gpt-5.5 \
--raw-output \
--transform 'output.#(type=="message").content.0.text' <<'YAML'
tools:
- type: web_search
input: What was a positive news story from today?
YAML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using OpenAI.Responses;
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
OpenAIResponseClient client = new(model: "gpt-5.5", apiKey: key);
ResponseCreationOptions options = new();
options.Tools.Add(ResponseTool.CreateWebSearchTool());
OpenAIResponse response = (OpenAIResponse)client.CreateResponse([
ResponseItem.CreateUserMessageItem([
ResponseContentPart.CreateInputTextPart("What was a positive news story from today?"),
]),
], options);
Console.WriteLine(response.GetOutputText());
文件搜索
1
2
3
4
5
6
7
8
9
10
11
12
from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model="gpt-5.5",
input="What is deep research by OpenAI?",
tools=[{
"type": "file_search",
"vector_store_ids": ["<vector_store_id>"]
}]
)
print(response)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import OpenAI from "openai";
const openai = new OpenAI();
const response = await openai.responses.create({
model: "gpt-5.5",
input: "What is deep research by OpenAI?",
tools: [
{
type: "file_search",
vector_store_ids: ["<vector_store_id>"],
},
],
});
console.log(response);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using OpenAI.Responses;
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
OpenAIResponseClient client = new(model: "gpt-5.5", apiKey: key);
ResponseCreationOptions options = new();
options.Tools.Add(ResponseTool.CreateFileSearchTool(["<vector_store_id>"]));
OpenAIResponse response = (OpenAIResponse)client.CreateResponse([
ResponseItem.CreateUserMessageItem([
ResponseContentPart.CreateInputTextPart("What is deep research by OpenAI?"),
]),
], options);
Console.WriteLine(response.GetOutputText());
函数调用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import OpenAI from "openai";
const client = new OpenAI();
const tools = [
{
type: "function",
name: "get_weather",
description: "Get current temperature for a given location.",
parameters: {
type: "object",
properties: {
location: {
type: "string",
description: "City and country e.g. Bogotá, Colombia",
},
},
required: ["location"],
additionalProperties: false,
},
strict: true,
},
];
const response = await client.responses.create({
model: "gpt-5",
input: [
{ role: "user", content: "What is the weather like in Paris today?" },
],
tools,
});
console.log(response.output[0].to_json());
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from openai import OpenAI
client = OpenAI()
tools = [
{
"type": "function",
"name": "get_weather",
"description": "Get current temperature for a given location.",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City and country e.g. Bogotá, Colombia",
}
},
"required": ["location"],
"additionalProperties": False,
},
"strict": True,
},
]
response = client.responses.create(
model="gpt-5",
input=[
{"role": "user", "content": "What is the weather like in Paris today?"},
],
tools=tools,
)
print(response.output[0].to_json())
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using System.Text.Json;
using OpenAI.Responses;
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
OpenAIResponseClient client = new(model: "gpt-5", apiKey: key);
ResponseCreationOptions options = new();
options.Tools.Add(ResponseTool.CreateFunctionTool(
functionName: "get_weather",
functionDescription: "Get current temperature for a given location.",
functionParameters: BinaryData.FromObjectAsJson(new
{
type = "object",
properties = new
{
location = new
{
type = "string",
description = "City and country e.g. Bogotá, Colombia"
}
},
required = new[] { "location" },
additionalProperties = false
}),
strictModeEnabled: true
)
);
OpenAIResponse response = (OpenAIResponse)client.CreateResponse([
ResponseItem.CreateUserMessageItem([
ResponseContentPart.CreateInputTextPart("What is the weather like in Paris today?")
])
], options);
Console.WriteLine(JsonSerializer.Serialize(response.OutputItems[0]));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
curl -X POST https://api.openai.com/v1/responses \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5",
"input": [
{"role": "user", "content": "What is the weather like in Paris today?"}
],
"tools": [
{
"type": "function",
"name": "get_weather",
"description": "Get current temperature for a given location.",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City and country e.g. Bogotá, Colombia"
}
},
"required": ["location"],
"additionalProperties": false
},
"strict": true
}
]
}'
远程 MCP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
curl https://api.openai.com/v1/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-5.5",
"tools": [
{
"type": "mcp",
"server_label": "dmcp",
"server_description": "A Dungeons and Dragons MCP server to assist with dice rolling.",
"server_url": "https://dmcp-server.deno.dev/sse",
"require_approval": "never"
}
],
"input": "Roll 2d4+1"
}'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import OpenAI from "openai";
const client = new OpenAI();
const resp = await client.responses.create({
model: "gpt-5.5",
tools: [
{
type: "mcp",
server_label: "dmcp",
server_description: "A Dungeons and Dragons MCP server to assist with dice rolling.",
server_url: "https://dmcp-server.deno.dev/sse",
require_approval: "never",
},
],
input: "Roll 2d4+1",
});
console.log(resp.output_text);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from openai import OpenAI
client = OpenAI()
resp = client.responses.create(
model="gpt-5.5",
tools=[
{
"type": "mcp",
"server_label": "dmcp",
"server_description": "A Dungeons and Dragons MCP server to assist with dice rolling.",
"server_url": "https://dmcp-server.deno.dev/sse",
"require_approval": "never",
},
],
input="Roll 2d4+1",
)
print(resp.output_text)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using OpenAI.Responses;
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
OpenAIResponseClient client = new(model: "gpt-5.5", apiKey: key);
ResponseCreationOptions options = new();
options.Tools.Add(ResponseTool.CreateMcpTool(
serverLabel: "dmcp",
serverUri: new Uri("https://dmcp-server.deno.dev/sse"),
toolCallApprovalPolicy: new McpToolCallApprovalPolicy(GlobalMcpToolCallApprovalPolicy.NeverRequireApproval)
));
OpenAIResponse response = (OpenAIResponse)client.CreateResponse([
ResponseItem.CreateUserMessageItem([
ResponseContentPart.CreateInputTextPart("Roll 2d4+1")
])
], options);
Console.WriteLine(response.GetOutputText());
使用服务器发送的 流式事件 以在结果生成时即时展示,或使用 Realtime API 构建交互式语音和多模态应用。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import { OpenAI } from "openai";
const client = new OpenAI();
const stream = await client.responses.create({
model: "gpt-5.5",
input: [
{
role: "user",
content: "Say 'double bubble bath' ten times fast.",
},
],
stream: true,
});
for await (const event of stream) {
console.log(event);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from openai import OpenAI
client = OpenAI()
stream = client.responses.create(
model="gpt-5.5",
input=[
{
"role": "user",
"content": "Say 'double bubble bath' ten times fast.",
},
],
stream=True,
)
for event in stream:
print(event)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using OpenAI.Responses;
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
OpenAIResponseClient client = new(model: "gpt-5.5", apiKey: key);
var responses = client.CreateResponseStreamingAsync([
ResponseItem.CreateUserMessageItem([
ResponseContentPart.CreateInputTextPart("Say 'double bubble bath' ten times fast."),
]),
]);
await foreach (var response in responses)
{
if (response is StreamingResponseOutputTextDeltaUpdate delta)
{
Console.Write(delta.Delta);
}
}
使用服务器发送事件,将模型响应快速流式传输给用户。
使用 WebRTC 或 WebSockets 构建超低延迟的端到端语音 AI 应用。
使用 OpenAI 平台构建能够代表用户执行操作(例如 控制计算机 )的 智能体。使用 Apps SDK 在后端创建编排逻辑。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import { Agent, run } from '@openai/agents';
const spanishAgent = new Agent({
name: 'Spanish agent',
instructions: 'You only speak Spanish.',
});
const englishAgent = new Agent({
name: 'English agent',
instructions: 'You only speak English',
});
const triageAgent = new Agent({
name: 'Triage agent',
instructions:
'Handoff to the appropriate agent based on the language of the request.',
handoffs: [spanishAgent, englishAgent],
});
const result = await run(triageAgent, 'Hola, ¿cómo estás?');
console.log(result.finalOutput);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from agents import Agent, Runner
import asyncio
spanish_agent = Agent(
name="Spanish agent",
instructions="You only speak Spanish.",
)
english_agent = Agent(
name="English agent",
instructions="You only speak English",
)
triage_agent = Agent(
name="Triage agent",
instructions="Handoff to the appropriate agent based on the language of the request.",
handoffs=[spanish_agent, english_agent],
)
async def main():
result = await Runner.run(triage_agent, input="Hola, ¿cómo estás?")
print(result.final_output)
if __name__ == "__main__":
asyncio.run(main())
了解如何使用 OpenAI 平台构建强大且能力卓越的 AI 智能体。