Function Calling
Chat Completions API(流式)
- Python
- JavaScript
- curl
from openai import OpenAI
import json
client = OpenAI(api_key="your-api-key", base_url="https://www.cheapertoken.work/v1")
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "获取指定城市的天气",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "城市名称"}
},
"required": ["city"]
}
}
}
]
stream = client.chat.completions.create(
model="gpt-5.4",
messages=[{"role": "user", "content": "北京今天天气怎么样?"}],
tools=tools,
tool_choice="auto",
stream=True,
)
tool_call_args = ""
tool_call_name = ""
for chunk in stream:
delta = chunk.choices[0].delta
if delta.tool_calls:
for tc in delta.tool_calls:
if tc.function.name:
tool_call_name = tc.function.name
if tc.function.arguments:
tool_call_args += tc.function.arguments
if chunk.choices[0].finish_reason == "tool_calls":
args = json.loads(tool_call_args)
print(f"调用函数: {tool_call_name}, 参数: {args}")
import OpenAI from "openai";
const client = new OpenAI({
apiKey: "your-api-key",
baseURL: "https://www.cheapertoken.work/v1",
});
const tools = [
{
type: "function",
function: {
name: "get_weather",
description: "获取指定城市的天气",
parameters: {
type: "object",
properties: {
city: { type: "string", description: "城市名称" },
},
required: ["city"],
},
},
},
];
const stream = await client.chat.completions.create({
model: "gpt-5.4",
messages: [{ role: "user", content: "北京今天天气怎么样?" }],
tools,
tool_choice: "auto",
stream: true,
});
let toolCallName = "";
let toolCallArgs = "";
for await (const chunk of stream) {
const delta = chunk.choices[0]?.delta;
if (delta?.tool_calls) {
for (const tc of delta.tool_calls) {
if (tc.function?.name) toolCallName = tc.function.name;
if (tc.function?.arguments) toolCallArgs += tc.function.arguments;
}
}
if (chunk.choices[0]?.finish_reason === "tool_calls") {
const args = JSON.parse(toolCallArgs);
console.log(`调用函数: ${toolCallName}, 参数:`, args);
}
}
curl https://www.cheapertoken.work/v1/chat/completions \
-H "Authorization: Bearer your-api-key" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5.4",
"messages": [{"role": "user", "content": "北京今天天气怎么样?"}],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "获取指定城市的天气",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "城市名称"}
},
"required": ["city"]
}
}
}
],
"tool_choice": "auto",
"stream": true
}'
Responses API
- Python
- JavaScript
- curl
from openai import OpenAI
client = OpenAI(api_key="your-api-key", base_url="https://www.cheapertoken.work/v1")
tools = [
{
"type": "function",
"name": "get_weather",
"description": "获取指定城市的天气",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string"}
},
"required": ["city"]
}
}
]
response = client.responses.create(
model="gpt-5.4",
input="上海今天天气怎么样?",
tools=tools,
)
for output in response.output:
if output.type == "function_call":
print(f"调用函数: {output.name}, 参数: {output.arguments}")
import OpenAI from "openai";
const client = new OpenAI({
apiKey: "your-api-key",
baseURL: "https://www.cheapertoken.work/v1",
});
const tools = [
{
type: "function",
name: "get_weather",
description: "获取指定城市的天气",
parameters: {
type: "object",
properties: { city: { type: "string" } },
required: ["city"],
},
},
];
const response = await client.responses.create({
model: "gpt-5.4",
input: "上海今天天气怎么样?",
tools,
});
for (const output of response.output) {
if (output.type === "function_call") {
console.log(`调用函数: ${output.name}, 参数: ${output.arguments}`);
}
}
curl https://www.cheapertoken.work/v1/responses \
-H "Authorization: Bearer your-api-key" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5.4",
"input": "上海今天天气怎么样?",
"tools": [
{
"type": "function",
"name": "get_weather",
"description": "获取指定城市的天气",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"]
}
}
]
}'