|
|
|
|
# 一、概念
|
|
|
|
|
function Calling 是一种机制,允许大语言模型[动态调用](https://zhida.zhihu.com/search?content_id=254823414&content_type=Article&match_order=1&q=%E5%8A%A8%E6%80%81%E8%B0%83%E7%94%A8&zd_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ6aGlkYV9zZXJ2ZXIiLCJleHAiOjE3NDIzNzc4MTgsInEiOiLliqjmgIHosIPnlKgiLCJ6aGlkYV9zb3VyY2UiOiJlbnRpdHkiLCJjb250ZW50X2lkIjoyNTQ4MjM0MTQsImNvbnRlbnRfdHlwZSI6IkFydGljbGUiLCJtYXRjaF9vcmRlciI6MSwiemRfdG9rZW4iOm51bGx9.SFQ4VSfJ2VWn-0GlIyT2_oZcB3dIPRDbBWFy9g7gYmM&zhida_source=entity)外部函数或API,以完成特定任务。例如,当用户提问“今天的天气如何?”时,模型可以通过调用天气API获取实时数据并返回结果。这种方式不仅提升了模型的实用性,还使其能够处理复杂的多步骤任务。
|
|
|
|
|
|
|
|
|
|
流程如下:
|
|
|
|
|
![[Pasted image 20250317180245.png]]
|
|
|
|
|
|
|
|
|
|
1. 用户输入问题或指令。
|
|
|
|
|
2. 模型分析输入,生成 [JSON 格式](https://zhida.zhihu.com/search?content_id=254823414&content_type=Article&match_order=1&q=JSON+%E6%A0%BC%E5%BC%8F&zd_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ6aGlkYV9zZXJ2ZXIiLCJleHAiOjE3NDIzNzc4MTgsInEiOiJKU09OIOagvOW8jyIsInpoaWRhX3NvdXJjZSI6ImVudGl0eSIsImNvbnRlbnRfaWQiOjI1NDgyMzQxNCwiY29udGVudF90eXBlIjoiQXJ0aWNsZSIsIm1hdGNoX29yZGVyIjoxLCJ6ZF90b2tlbiI6bnVsbH0.CqcXsaw3SWw2cR64Zo3MC3hEnVXL7SwKfAfkt-xau9I&zhida_source=entity)的函数调用请求。
|
|
|
|
|
3. 后端解析请求,调用对应函数或API。
|
|
|
|
|
4. 将函数返回的结果传递回模型。
|
|
|
|
|
5. 模型基于结果生成最终回答。
|
|
|
|
|
|
|
|
|
|
# 二、Deepseek function calling
|
|
|
|
|
|
|
|
|
|
`仅限于火山引擎,Deepseek-r1 原版不支持function calling`
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
1. tools定义
|
|
|
|
|
deepseek 官方定义的格式(最多支持 128 个 function),参考 https://api-docs.deepseek.com/zh-cn/guides/function_calling
|
|
|
|
|
```json
|
|
|
|
|
TOOLS = [
|
|
|
|
|
{
|
|
|
|
|
"type": "function",
|
|
|
|
|
"function": {
|
|
|
|
|
"name": "get_current_weather",
|
|
|
|
|
"description": "Get weather of an location, the user shoud supply a location first",
|
|
|
|
|
"parameters": {
|
|
|
|
|
"type": "object",
|
|
|
|
|
"properties": {
|
|
|
|
|
"location": {
|
|
|
|
|
"type": "string",
|
|
|
|
|
"description": "The city and state, e.g. San Francisco, CA",
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
"required": ["location"]
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"type": "function",
|
|
|
|
|
"function": {
|
|
|
|
|
"name": "play_video",
|
|
|
|
|
"description": "播放、查看、打开实时视频",
|
|
|
|
|
"parameters": {
|
|
|
|
|
"type": "object",
|
|
|
|
|
"properties": {
|
|
|
|
|
"camera_name": {
|
|
|
|
|
"type": "string",
|
|
|
|
|
"description": "相机名称,例如:南卡口1号相机",
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
"required": ["camera_name"]
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"type": "function",
|
|
|
|
|
"function": {
|
|
|
|
|
"name": "split_screen",
|
|
|
|
|
"description": "切换分屏",
|
|
|
|
|
"parameters": {
|
|
|
|
|
"type": "object",
|
|
|
|
|
"properties": {
|
|
|
|
|
"split_n": {
|
|
|
|
|
"type": "int",
|
|
|
|
|
"description": "要切换的分屏数量,整数并且大于0,例如:1分屏、2分屏",
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
"required": ["split_n"]
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"type": "function",
|
|
|
|
|
"function": {
|
|
|
|
|
"name": "play_video_record",
|
|
|
|
|
"description": "播放、打开录像",
|
|
|
|
|
"parameters": {
|
|
|
|
|
"type": "object",
|
|
|
|
|
"properties": {
|
|
|
|
|
"camera_name": {
|
|
|
|
|
"type": "string",
|
|
|
|
|
"description": "相机名称,例如:南卡口1号相机",
|
|
|
|
|
},
|
|
|
|
|
"start_time": {
|
|
|
|
|
"type": "string",
|
|
|
|
|
"description": "录像开始时间,格式为yyyy-MM-dd hh:mm:ss,例 2025-03-16 01:00:00",
|
|
|
|
|
},
|
|
|
|
|
"end_time": {
|
|
|
|
|
"type": "string",
|
|
|
|
|
"description": "录像结束时间,格式为yyyy-MM-dd hh:mm:ss,例 2025-03-16 02:09:31",
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
"required": ["camera_name", "start_time", "end_time"]
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"type": "function",
|
|
|
|
|
"function": {
|
|
|
|
|
"name": "switch_page",
|
|
|
|
|
"description": "打开、跳转页面",
|
|
|
|
|
"parameters": {
|
|
|
|
|
"type": "object",
|
|
|
|
|
"properties": {
|
|
|
|
|
"page_name": {
|
|
|
|
|
"type": "string",
|
|
|
|
|
"description": "页面中文名称或者缩写,例:人员核查、系统日志、设备管理、首页",
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
"required": ["page_name"]
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"type": "function",
|
|
|
|
|
"function": {
|
|
|
|
|
"name": "zoom_in",
|
|
|
|
|
"description": "放大电子地图",
|
|
|
|
|
"parameters": {
|
|
|
|
|
"type": "object",
|
|
|
|
|
"properties": {
|
|
|
|
|
"level_n": {
|
|
|
|
|
"type": "int",
|
|
|
|
|
"description": "放大等级,整数并且大于0小于5,例如:放大1级、放大2级",
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
"required": ["level_n"]
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"type": "function",
|
|
|
|
|
"function": {
|
|
|
|
|
"name": "view_flight_details",
|
|
|
|
|
"description": "查询指定机场指定航班及时间的出入境人员明细",
|
|
|
|
|
"parameters": {
|
|
|
|
|
"type": "object",
|
|
|
|
|
"properties": {
|
|
|
|
|
"airport_name": {
|
|
|
|
|
"type": "string",
|
|
|
|
|
"description": "机场名称,简体中文,可以是缩写,例如:成都天府机场、天府机场、长水机场、上海浦东机场",
|
|
|
|
|
},
|
|
|
|
|
"flight_code": {
|
|
|
|
|
"type": "string",
|
|
|
|
|
"description": "航班编号,由字母+数字组成的完整编号,若编号包含多余字符(如标点符号),需过滤后保留有效部分",
|
|
|
|
|
},
|
|
|
|
|
"flight_date": {
|
|
|
|
|
"type": "string",
|
|
|
|
|
"description": "提取完整日期(年月日),自动补零至标准格式 yyyy-MM-dd, 例:2025-03-16",
|
|
|
|
|
},
|
|
|
|
|
"ie_type": {
|
|
|
|
|
"type": "string",
|
|
|
|
|
"description": "出入境类型,仅识别'入境'或'出境'两种类型",
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
"required": ["airport_name", "flight_code", "flight_date"]
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
2. 函数绑定及调用
|
|
|
|
|
```python
|
|
|
|
|
模型初始化
|
|
|
|
|
llm = ChatOpenAI(
|
|
|
|
|
model='deepseek-r1-250120', api_key='4eefc827-187f-4756-9637-7e0153c93d81',
|
|
|
|
|
base_url='https://ark.cn-beijing.volces.com/api/v3/', max_tokens=4096, temperature=0.5, streaming=True
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def invoke_with_tool(user_input: str):
|
|
|
|
|
"""
|
|
|
|
|
工具链调用,function calling时system prompt不会生效
|
|
|
|
|
:param user_input: :return: 这里返回的是LLM推理出的tool信息,格式如下:
|
|
|
|
|
[{'name': 'get_current_weather', 'args': {'location': 'Beijing, China'}, 'id': 'call_xeeq4q52fw9x61lkrqwy9cr6', 'type': 'tool_call'}] """ llm_with_tools = llm.bind_tools(TOOLS)
|
|
|
|
|
return llm_with_tools.invoke(user_input).tool_calls
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
3. 返回样例,包含函数名和参数
|
|
|
|
|
```json
|
|
|
|
|
[{'name': 'get_current_weather', 'args': {'location': 'Beijing, China'}, 'id': 'call_xeeq4q52fw9x61lkrqwy9cr6', 'type': 'tool_call'}]
|
|
|
|
|
```
|