Function calling
更新时间:2025-04-09
概述
Agent 中的Function calling是指Agent除了可以调用自身已有的工具外,仍保留大模型的Function calling能力,开发者还可将其他自定义函数、外部工具,与Agent连接,增强Agent在多种应用场景中的能力。
tools是Agent对话API中的可选参数,用于向Agent提供函数定义。通过此参数,Agent能够生成符合用户所提供规范的函数参数。请注意,Agent对话API实际上并不会执行任何函数调用,仅返回调用的函数名与调用函数所需要的参数。开发者可以利用Agent输出的参数在系统中进一步执行函数调用。
具体步骤
- 定义工具:定义要调用的函数名称、描述和参数的 JSON Schema。
- 发送请求:发送包含
tools(第一步中定义的工具)
字段的请求到对话接口,以获取Agent的工具选择和输入参数。 - 调用工具:调用对应的工具,获取工具响应结果。
- 上报工具执行结果:使用对话接口上报工具响应结果。
- Agent最终回复:Agent将工具的响应纳入到自己的总结中。
发送请求
tools字段为所定义的工具的内容
JSON
1curl --location --request POST 'https://qianfan.baidubce.com/v2/app/conversation/runs' \
2--header 'Authorization: Bearer <AppBuilder API Key>' \
3--header 'Content-Type: application/json' \
4--data-raw '{
5 "app_id": "4d4b1b27-d607-4d2a-9002-206134217a9f",
6 "query": "今天北京的天气怎么样",
7 "stream": true,
8 "conversation_id": "8c5928f7-a9e7-4826-a027-3eb1f97f6eab",
9 "tools": [
10 {
11 "type": "function",
12 "function": {
13 "name": "get_current_weather",
14 "description": "仅支持中国城市的天气查询,参数location为中国城市名称,其他国家城市不支持天气查询",
15 "parameters": {
16 "type": "object",
17 "properties": {
18 "location": {
19 "type": "string",
20 "description": "The city and state, e.g. Beijing"
21 },
22 "unit": {
23 "type": "string",
24 "enum": [
25 "celsius",
26 "fahrenheit"
27 ]
28 }
29 },
30 "required": [
31 "location",
32 "unit"
33 ]
34 }
35 }
36 }
37 ]
38}'
模型返回工具名称和输入参数
JSON
1{
2 "request_id": "aa89d67e-95cf-4b3c-8587-db10ba8a6b69",
3 "date": "2025-03-18T08:30:02Z",
4 "answer": "",
5 "conversation_id": "57606d3f-45e5-4216-8e9d-eae07124fea3",
6 "message_id": "e371c651-9ce0-4b7d-9037-67a63df99bc0",
7 "is_completion": null,
8 "content": [
9 {
10 "result_type": "",
11 "event_code": 0,
12 "event_message": "",
13 "event_type": "thought",
14 "event_id": "0",
15 "event_status": "done",
16 "content_type": "thought",
17 "visible_scope": "",
18 "outputs": {
19 "text": "用户想要查询北京的今天天气情况,这是一个明确的天气查询需求,可以通过调用get_current_weather工具来获取相关信息。根据工具描述,需要传入城市名称和温度单位作为参数。在这里,城市名称为北京,温度单位可以选择摄氏度。"
20 }
21 },
22 {
23 "result_type": "",
24 "event_code": 0,
25 "event_message": "",
26 "event_type": "function_call",
27 "event_id": "1",
28 "event_status": "done",
29 "content_type": "function_call",
30 "visible_scope": "",
31 "outputs": {
32 "text": {
33 "arguments": {
34 "location": "北京",
35 "unit": "celsius"
36 },
37 "component_code": "get_current_weather",
38 "component_name": "get_current_weather"
39 }
40 },
41 "usage": {
42 "prompt_tokens": 518,
43 "completion_tokens": 102,
44 "total_tokens": 620,
45 "name": "ERNIE-3.5-8K",
46 "type": "plan"
47 }
48 },
49 {
50 "result_type": "",
51 "event_code": 0,
52 "event_message": "",
53 "event_type": "Interrupt",
54 "event_id": "2",
55 "event_status": "interrupt",
56 "content_type": "contexts",
57 "visible_scope": "",
58 "outputs": {
59 "text": {
60 "function_call": {
61 "name": "get_current_weather",
62 "arguments": {
63 "location": "北京",
64 "unit": "celsius"
65 },
66 "thought": "用户想要查询北京的今天天气情况,这是一个明确的天气查询需求,可以通过调用get_current_weather工具来获取相关信息。根据工具描述,需要传入城市名称和温度单位作为参数。在这里,城市名称为北京,温度单位可以选择摄氏度。",
67 "tool_call_id": "69e5f0fd-d01d-4987-9a70-df9430b7aaaa"
68 },
69 "used_tool": []
70 }
71 },
72 "tool_calls": [
73 {
74 "id": "69e5f0fd-d01d-4987-9a70-df9430b7aaaa",
75 "type": "function",
76 "function": {
77 "name": "get_current_weather",
78 "arguments": {
79 "location": "北京",
80 "unit": "celsius"
81 }
82 }
83 }
84 ]
85 }
86 ]
87}
上报工具执行结果
JSON
1curl --location --request POST 'https://qianfan.baidubce.com/v2/app/conversation/runs' \
2--header 'Authorization: Bearer <AppBuilder API Key>' \
3--header 'Content-Type: application/json' \
4--data-raw '{
5 "app_id": "4d4b1b27-d607-4d2a-9002-206134217a9f",
6 "stream": false,
7 "conversation_id": "8c5928f7-a9e7-4826-a027-3eb1f97f6eab",
8 "tool_outputs": [
9 {
10 "tool_call_id": "69e5f0fd-d01d-4987-9a70-df9430b7aaaa",
11 "output": "北京今天天气晴朗,温度32度"
12 }
13 ]
14}'
Agent最终回复
JSON
1{
2 "request_id": "7637baf3-60bc-4158-bc2a-86010a60d4ff",
3 "date": "2025-03-18T08:48:01Z",
4 "answer": "北京今天天气晴朗,温度32摄氏度。如果您需要更多天气信息或有其他问题,请随时告诉我。",
5 "conversation_id": "57606d3f-45e5-4216-8e9d-eae07124fea3",
6 "message_id": "e8d70412-267b-44f7-a8ae-aa8114db895d",
7 "is_completion": null,
8 "content": [
9 {
10 "result_type": "",
11 "event_code": 0,
12 "event_message": "",
13 "event_type": "thought",
14 "event_id": "0",
15 "event_status": "done",
16 "content_type": "thought",
17 "visible_scope": "",
18 "outputs": {
19 "text": "根据已调用的get_current_weather工具结果,已经提供了北京今天的天气情况,即天气晴朗,温度32度。这个信息已经满足了用户查询北京今天天气的需求,因此不需要再次调用工具或规划其他工具的使用。接下来,我将使用chat_agent工具来总结并终止流程。"
20 }
21 },
22 {
23 "result_type": "",
24 "event_code": 0,
25 "event_message": "",
26 "event_type": "function_call",
27 "event_id": "1",
28 "event_status": "done",
29 "content_type": "function_call",
30 "visible_scope": "",
31 "outputs": {
32 "text": {
33 "arguments": {},
34 "component_code": "ChatAgent",
35 "component_name": "聊天助手"
36 }
37 },
38 "usage": {
39 "prompt_tokens": 702,
40 "completion_tokens": 92,
41 "total_tokens": 794,
42 "name": "ERNIE-3.5-8K",
43 "type": "plan"
44 }
45 },
46 {
47 "result_type": "",
48 "event_code": 0,
49 "event_message": "",
50 "event_type": "ChatAgent",
51 "event_id": "2",
52 "event_status": "preparing",
53 "content_type": "status",
54 "visible_scope": "",
55 "outputs": {}
56 },
57 {
58 "result_type": "",
59 "event_code": 0,
60 "event_message": "",
61 "event_type": "ChatAgent",
62 "event_id": "3",
63 "event_status": "done",
64 "content_type": "text",
65 "visible_scope": "all",
66 "outputs": {
67 "text": "北京今天天气晴朗,温度32摄氏度。如果您需要更多天气信息或有其他问题,请随时告诉我。"
68 },
69 "usage": {
70 "prompt_tokens": 230,
71 "completion_tokens": 21,
72 "total_tokens": 251,
73 "name": "Qianfan-Agent-Speed-32K",
74 "type": "chat"
75 }
76 },
77 {
78 "result_type": "",
79 "event_code": 0,
80 "event_message": "",
81 "event_type": "ChatAgent",
82 "event_id": "4",
83 "event_status": "success",
84 "content_type": "status",
85 "visible_scope": "all",
86 "outputs": {}
87 }
88 ]
89}