摘要:对于一些具体的、需要特定功能实现的任务,如数学计算、数据处理、调用外部 API 获取信息等,大模型自身无法直接完成。函数调用,则可以让大模型触发相应的函数来执行这些任务。例如,在需要获取实时天气信息时,调用天气 API 相关函数获取数据并反馈给用户。
对于一些具体的、需要特定功能实现的任务,如数学计算、数据处理、调用外部 API 获取信息等,大模型自身无法直接完成。函数调用,则可以让大模型触发相应的函数来执行这些任务。例如,在需要获取实时天气信息时,调用天气 API 相关函数获取数据并反馈给用户。
对于开发者而言,函数调用功能极大简化了与大模型间的交互流程,无需再编写复杂的代码来实现各种功能,只需用自然语言向模型描述所需函数的功能和定义,模型就能智能地选择并返回调用所需的参数。
基石智算Coreshub 大模型服务推出函数调用功能,无缝衔接了大模型的智慧与外部世界的实际需求,为用户带来了更便捷、更高效、更智能的使用体验。
函数调用(Function Calling)可以极大地增强大语言模型的功能,增强推理效果或进行其他外部操作,包括信息检索、数据库操作、知识图谱搜索与推理、操作系统、触发外部操作等工具调用场景。在设计各类 bot 或 agent 时非常有用。其基本流程如下:
1.用户与大模型交互,得到函数名称和参数。
2.程序根据第一步得到的函数名称和参数调用本地函数 API,获取结果。
3.将第二步中得到的结果拼入 prompt 形成新的 promptX,在与大模型交互,得到最终结果。
支持的模型
目前平台上的 QwQ-32B 和 DeepSeek-V3 模型支持。
注意事项
用户需提前在
http://docs.coreshub.cn/console/big_model_server/api_key/create_api_key/ 中创建与获取 API KEY。
代码示例
下面给出了函数调用的完整 Python 代码样例。
● 示例中分别创建了两个模拟函数:
● get_current_temperature:返回当前温度。
● get_temperature_date:返回特定日期的温度。
● 示例中 TOOLS 中定义了两个工具,分别用于获取当前温度和特定日期的温度。每个工具包含名称、描述和参数信息。
● function_call_playground 函数中遍历模型 API 返回的工具调用,获取函数名称和参数,调用相应的函数并打印调用信息。将输出添加到消息列表中再次请求大模型获得最终结果。
完整代码如下:
import requests from openai import OpenAI client = OpenAI( api_key="sk-xxxxxxxxxxxxxxxxxxxxxxxxx", # 需替换成提前创建和获取的 API 密钥 base_url="https://openapi.coreshub.cn/v1" ) MODEL = "QwQ-32B" def get_current_temperature(location: str, unit: str = "celsius"): """Get current temperature at a location.""" return { "temperature": 15.1, "location": location, "unit": unit, } def get_temperature_date(location: str, date: str, unit: str = "celsius"): """Get temperature at a location and date.""" return { "temperature": 13.9, "location": location, "date": date, "unit": unit, } def get_function_by_name(name): if name == "get_current_temperature": return get_current_temperature elif name == "get_temperature_date": return get_temperature_date TOOLS = [ { "type": "function", "function": { "name": "get_current_temperature", "description": "Get current temperature at a location.", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": 'The location to get the temperature for, in the format "City, State, Country".', }, "unit": { "type": "string", "enum": ["celsius", "fahrenheit"], "description": 'The unit to return the temperature in. Defaults to "celsius".', }, }, "required": ["location"], }, }, }, { "type": "function", "function": { "name": "get_temperature_date", "description": "Get temperature at a location and date.", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": 'The location to get the temperature for, in the format "City, State, Country".', }, "date": { "type": "string", "description": 'The date to get the temperature for, in the format "Year-Month-Day".', }, "unit": { "type": "string", "enum": ["celsius", "fahrenheit"], "description": 'The unit to return the temperature in. Defaults to "celsius".', }, }, "required": ["location", "date"], }, }, }, ] MESSAGES = [ {"role": "user", "content": "今天是 2025-03-12, 北京当前的温度怎么样? 明天呢?"} ] def function_call_playground: messages = MESSAGES print(f"input: {messages}") # 发送请求到 OpenAI API response = client.chat.completions.create( model=MODEL, temperature=1.0, top_p=1.0, messages=messages, stream=False, tools=TOOLS ) outputs = for tool_call in response.choices[0].message.tool_calls: func_name = tool_call.function.name func_args = tool_call.function.arguments func_out = eval(f'{func_name}(**{func_args})') outputs.append(func_out) print(f"Calling function: {func_name} with arguments: {func_args}, output: {func_out}") messages.append({ 'role': 'tool', 'content': str(func_out), 'tool_call_id': tool_call.id }) response = client.chat.completions.create( model=MODEL, messages=messages, temperature=0.01, top_p=0.95, stream=False, ) return response.choices[0].message.content output=function_call_playground print(f"output: {output}")返回结果
input: [{'role': 'user', 'content': '今天是 2025-03-12, 北京当前的温度怎么样? 明天呢?'}] Calling function: get_current_temperature with arguments: {"location": "北京, 中国"}, output: {'temperature': 15.1, 'location': '北京, 中国', 'unit': 'celsius'} Calling function: get_temperature_date with arguments: {"location": "北京, 中国", "date": "2025-03-13"}, output: {'temperature': 13.9, 'location': '北京, 中国', 'date': '2025-03-13', 'unit': 'celsius'} output: 好的,用户今天问的是北京当前的温度和明天的温度。我需要先看一下用户提供的数据。今天是2025年3月12日,当前温度是15.1摄氏度,明天也就是3月13日的温度是13.9摄氏度。 首先,用户可能想知道现在北京的天气情况,可能是在考虑出门是否需要带外套,或者安排户外活动。明天的温度比今天低,可能有降温的趋势,用户可能需要知道温差,以便准备合适的衣物。 接下来,我需要把数据用自然的中文表达出来,确保信息准确。要注意单位是摄氏度,日期也要明确。用户可能希望回答简洁明了,不需要太多额外信息,但可以稍微提一下温度变化,比如“略有下降”,这样用户能更清楚天气趋势。 另外,检查一下是否有其他可能的用户需求。比如,用户可能还想知道是否有雨或其他天气状况,但根据提供的数据,只有温度信息,所以不能猜测其他因素。保持回答在已知数据范围内。 最后,确保回答的结构清晰,先回答当天的温度,再说明天的,这样符合用户的提问顺序。使用友好的语气,让用户觉得有帮助。 今天(2025年3月12日)北京当前的温度是 **15.1°C**,明天(3月13日)预计会降至 **13.9°C**,气温略有下降,建议根据活动安排适当调整衣物哦!来源:青云QingCloud