函数调用

Enable models to fetch data and take actions.

使模型能够获取数据并采取行动。

Function calling provides a powerful and flexible way for OpenAI models to interface with your code or external services, and has two primary use cases:

函数调用为OpenAI模型提供了一种强大而灵活的方式来与您的代码或外部服务接口,并有两个主要用例:

Fetching Data Retrieve up-to-date information to incorporate into the model's response (RAG). Useful for searching knowledge bases and retrieving specific data from APIs (e.g. current weather data).
Taking Action Perform actions like submitting a form, calling APIs, modifying application state (UI/frontend or backend), or taking agentic workflow actions (like handing off the conversation).
获取数据 检索最新信息以纳入模型的响应中(RAG)。这对于搜索知识库和从API检索特定数据(例如当前天气数据)非常有用。
采取行动 执行诸如提交表单、调用API、修改应用程序状态(UI/前端或后端)或采取代理工作流操作(如交接对话)等操作。

Get weather

获取天气

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",
 "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
 }
}]

completion = client.chat.completions.create(
 model="gpt-4o",
 messages=[{"role": "user", "content": "What is the weather like in Paris today?"}],
 tools=tools
)

print(completion.choices[0].message.tool_calls)
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
import { OpenAI } from "openai";

const openai = new OpenAI();

const tools = [{
 "type": "function",
 "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"...
开通本站会员,查看完整译文。

ホーム - Wiki
Copyright © 2011-2025 iteam. Current version is 2.142.0. UTC+08:00, 2025-02-23 07:05
浙ICP备14020137号-1 $お客様$