Function calling
更新时间:2025-04-18
能力介绍
Function call是能将大模型与外部工具或代码相连的功能,可通过该功能增强大模型在实时性、数据计算等应用场景的推理效果或进行其他外部操作,包括信息检索、数据库操作、图谱搜索与处理等工具调用场景。
tools是模型服务API中的可选参数,用于向模型提供函数定义。通过此参数,模型能够生成符合用户所提供规范的函数参数。请注意,模型服务API实际上并不会执行任何函数调用,仅返回是否调用函数,以及调用的函数名与调用函数所需要的参数。开发者可以利用模型输出的参数在系统中进一步执行函数调用。
支持模型范围
- ernie-4.0-turbo-128k
- ernie-4.0-turbo-8k
- ernie-3.5-128k
- ernie-3.5-8k
- ernie-speed-pro-128k
- ernie-lite-pro-128k
- qwq-32b
- deepseek-v3
快速开始
调用步骤说明
- 使用JSON Schema格式定义函数;
- 通过tools参数将定义好的函数提交给支持function call的大模型,可一次提交多个函数;
- 大模型将根据当前聊天上下文,决定使用哪个函数,也可能不使用函数;
- 若大模型决定使用函数,大模型会将调用函数所需要的参数和信息通过JSON格式返回;
- 使用大模型输出的参数,执行对应的函数,并将此函数执行结果提交给大模型;
- 大模型将根据函数执行结果,给予用户回复。
SDK调用示例
Plain Text
1from openai import OpenAI
2
3client = OpenAI(
4 api_key=api_key ##
5 base_url="https://qianfan.baidubce.com/v2", ##
6)
7
8tools = [
9 {
10 "type": "function",
11 "function": {
12 "name": "get_current_weather",
13 "description": "Get the current weather in a given location",
14 "parameters": {
15 "type": "object",
16 "properties": {
17 "location": {
18 "type": "string",
19 "description": "The city and state, e.g. San Francisco, CA",
20 },
21 "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
22 },
23 "required": ["location"],
24 },
25 }
26 }
27]
28messages = [{"role": "user", "content": "What's the weather like in Boston today?"}]
29completion = client.chat.completions.create(
30 model="ernie-3.5-8k",
31 messages=messages,
32 tools=tools,
33 tool_choice="auto"
34)
35
36print(completion)
典型案例
查询天气
请求示例
Plain Text
1curl --location 'https://qianfan.baidubce.com/v2/chat/completions' \
2--header 'Content-Type: application/json' \
3--header 'Authorization: Bearer bce-v3/ALTAK-IpGwCWpMaKK9kfz44OK2A/' \
4--header 'Cookie: BAIDUID=6FC98A94EA43253A9B06FB79783DA833:FG=1' \
5--data '{
6 "model": "ernie-lite-pro-128k",
7 "messages": [
8 {
9 "role": "user",
10 "content": "查一下上海和北京现在的天气"
11 }
12 ],
13 "tools": [{
14 "type": "function",
15 "function": {
16 "name": "get_current_weather",
17 "description": "天气查询工具",
18 "parameters": {
19 "properties": {
20 "location": {
21 "description": "地理位置,精确到区县级别",
22 "type": "string"
23 },
24 "time": {
25 "description": "时间,格式为YYYY-MM-DD",
26 "type": "string"
27 }
28 },
29 "type": "object"
30 }
31 }
32
33 }],
34 "stream": false,
35 "stream_options": {
36 "include_usage": true
37 },
38 "tool_choice" : "auto",
39 "tool_options" : {"thoughts_output" : true}
40}'
返回示例
Plain Text
1{
2 "id": "as-7jqstcp9ce",
3 "object": "chat.completion",
4 "created": 1732885634,
5 "model": "ernie-lite-pro-128k",
6 "choices": [
7 {
8 "index": 0,
9 "message": {
10 "role": "assistant",
11 "content": "",
12 "tool_calls": [
13 {
14 "id": "call_tmvd94n0t8",
15 "type": "function",
16 "function": {
17 "name": "get_current_weather",
18 "arguments": "{\"location\":\"上海\"}"
19 }
20 }
21 ]
22 },
23 "finish_reason": "tool_calls",
24 "flag": 0
25 }
26 ],
27 "usage": {
28 "prompt_tokens": 5,
29 "completion_tokens": 49,
30 "total_tokens": 54
31 }
32}
询问车票
请求示例
Plain Text
1curl --location 'https://qianfan.baidubce.com/v2/chat/completions' \
2--header 'Content-Type: application/json' \
3--header 'Authorization: Bearer bce-v3/ALTAK-IpGwCWpMaKK9kfz44OK2A/' \
4--header 'Cookie: BAIDUID=6FC98A94EA43253A9B06FB79783DA833:FG=1' \
5--data '{
6 "model": "ernie-3.5-8k",
7 "messages": [
8 {
9 "role": "system",
10 "content": "你是一个智能助手"
11 },
12 {
13 "role": "user",
14 "content": "你能帮我查询2024年1月1日从北京南站到上海的火车票吗?"
15 }
16 ],
17 "stream": false,
18 "tools": [
19 {
20 "type": "function",
21 "function": {
22 "name": "query_train_info",
23 "description": "根据用户提供的信息,查询对应的车次",
24 "parameters": {
25 "type": "object",
26 "properties": {
27 "departure": {
28 "type": "string",
29 "description": "出发城市或车站"
30 },
31 "destination": {
32 "type": "string",
33 "description": "目的地城市或车站"
34 },
35 "date": {
36 "type": "string",
37 "description": "要查询的车次日期"
38 }
39 },
40 "required": [
41 "departure",
42 "destination",
43 "date"
44 ]
45 }
46 }
47 }
48 ]
49}'
返回示例
Plain Text
1{
2 "id": "as-ubsiihj8d5",
3 "object": "chat.completion",
4 "created": 1732887366,
5 "model": "ernie-3.5-8k",
6 "choices": [
7 {
8 "index": 0,
9 "message": {
10 "role": "assistant",
11 "content": "",
12 "tool_calls": [
13 {
14 "id": "19d8e571660dd000",
15 "type": "function",
16 "function": {
17 "name": "query_train_info",
18 "arguments": "{\"departure\": \"北京南站\", \"destination\": \"上海\", \"date\": \"2024-01-01\"}"
19 }
20 }
21 ]
22 },
23 "finish_reason": "tool_calls",
24 "flag": 0
25 }
26 ],
27 "usage": {
28 "prompt_tokens": 19,
29 "completion_tokens": 33,
30 "total_tokens": 52
31 }
32}