文档中心>腾讯混元大模型>操作指南>functioncall 模型调用示例

functioncall 模型调用示例

最近更新时间:2024-07-18 14:52:11

我的收藏
注意:
调用示例仅包括模型调用部分代码,签名鉴权及依赖请下载 SDK 或参考 API Explorer 中的代码示例。下面给出 python 代码示例(定义函数),其中 SecretId、SecretKey 可前往控制台进行获取
import json import types from tencentcloud.common import credential from tencentcloud.common.profile.client_profile import ClientProfile from tencentcloud.common.profile.http_profile import HttpProfile from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException from tencentcloud.hunyuan.v20230901 import hunyuan_client, models def request_hunyuan_funccall(messages, tools): try: # 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密 # 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305 # 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取 cred = credential.Credential("SecretId", "SecretKey") # 实例化一个http选项,可选的,没有特殊需求可以跳过 httpProfile = HttpProfile() httpProfile.endpoint = "hunyuan.tencentcloudapi.com" # 实例化一个client选项,可选的,没有特殊需求可以跳过 clientProfile = ClientProfile() clientProfile.httpProfile = httpProfile # 实例化要请求产品的client对象,clientProfile是可选的 client = hunyuan_client.HunyuanClient(cred, "", clientProfile) # 实例化一个请求对象,每个接口都会对应一个request对象 req = models.ChatCompletionsRequest() params = { "Model": "hunyuan-functioncall", "Messages": messages, "Tools": tools, "ToolChoice": "auto" } req.from_json_string(json.dumps(params)) # 返回的resp是一个ChatCompletionsResponse的实例,与请求对象对应 resp = client.ChatCompletions(req) # 输出json格式的字符串回包 if isinstance(resp, types.GeneratorType): # 流式响应 for event in resp: print(event) else: # 非流式响应 print(resp) except TencentCloudSDKException as err: print(err)

一、单轮对话-单工具调用

1. 用户提问,模型给出要调用的工具

tools = [ { "Type": "function", "Function": { "Name": "get_weather", "Description": "查询天气", "Parameters": "{\\"type\\": \\"object\\"," "\\"properties\\": {" "\\"city\\": {" "\\"description\\": \\"城市名\\"," "\\"type\\": \\"string\\"" "}" "}," "\\"required\\": [" "\\"city\\"" "]" "}" } } ] messages = [ { "Role": "user", "Content": "帮我查下北京天气" } ] res = request_hunyuan_funccall(messages=messages, tools=tools)
输出结果:
{"Created": 1721234069, "Usage": {"PromptTokens": 5, "CompletionTokens": 40, "TotalTokens": 45}, "Note": "以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记", "Id": "cab3b34b-267e-4f2a-8c24-8bd3d49e25ff", "Choices": [{"FinishReason": "tool_calls", "Delta": null, "Message": {"Role": "assistant", "Content": "使用get_weather工具来查询北京的天气。\\n\\t\\n\\t用户想要查询北京的天气情况。用户提供了城市名为北京,根据工具描述,需要使用get_weather工具来查询天气。", "Contents": null, "ToolCallId": null, "ToolCalls": [{"Id": "call_cqbv55c2c3me13jrn8v0", "Type": "function", "Function": {"Name": "get_weather", "Arguments": "{\\"city\\":\\"北京\\"}"}}]}}], "ErrorMsg": null, "RequestId": "cab3b34b-267e-4f2a-8c24-8bd3d49e25ff"}

2. 给出工具执行结果,模型总结回复

tools2 = [ { "Type": "function", "Function": { "Name": "get_weather", "Description": "查询天气", "Parameters": "{\\"type\\": \\"object\\"," "\\"properties\\": {" "\\"city\\": {" "\\"description\\": \\"城市名\\"," "\\"type\\": \\"string\\"" "}" "}," "\\"required\\": [" "\\"city\\"" "]" "}" } } ] # 工具执行结果 tool_execute_result = { "result": { "weather_info": "北京市今天(2024-06-26, 星期三)天气:晴转多云,最高温度:35°C,最低温度:24°C,相对湿度:25%,西南风,风力:1级。" } } messages2 = [ { "Role": "user", "Content" : "帮我查下北京天气" }, { "Role": "assistant", "Content": "使用get_weather工具来查询北京的天气。\\n\\t\\n\\t用户想要知道北京今天的天气情况。用户的请求是查询天气,这需要使用天气查询工具。", "tool_calls": [{'id': 'call_cqbv6gc2c3m5jj7bg6q0', 'type': 'function', 'function': {'name': 'get_weather', 'arguments': '{"city":"北京"}'}}] }, { "Role": "tool", "ToolCallId": "call_cqbv6gc2c3m5jj7bg6q0", "Content": json.dumps(tool_execute_result, ensure_ascii=False) } ] res2 = request_hunyuan_funccall(messages=messages2, tools=tools2)
输出结果:
{"Created": 1721234733, "Usage": {"PromptTokens": 94, "CompletionTokens": 45, "TotalTokens": 139}, "Note": "以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记", "Id": "cb67a9e3-fc99-4167-9584-0b4cf7ac24af", "Choices": [{"FinishReason": "stop", "Delta": null, "Message": {"Role": "assistant", "Content": "北京市今天(2024-06-26, 星期三)天气:晴转多云,最高温度:35°C,最低温度:24°C,相对湿度:25%,西南风,风力:1级。", "Contents": null, "ToolCallId": null, "ToolCalls": null}}], "ErrorMsg": null, "RequestId": "cb67a9e3-fc99-4167-9584-0b4cf7ac24af"}

二、单轮对话-反问

1. 用户提问,缺乏必要参数,模型向用户反问,获取必要信息

tools = [ { "Type": "function", "Function": { "Name": "get_weather", "Description": "查询天气", "Parameters": "{\\"type\\": \\"object\\"," "\\"properties\\": {" "\\"city\\": {" "\\"description\\": \\"城市名\\"," "\\"type\\": \\"string\\"" "}" "}," "\\"required\\": [" "\\"city\\"" "]" "}" } } ] messages = [ { "Role": "user", "Content" : "今天天气如何?" } ] res = request_hunyuan_funccall(messages=messages, tools=tools)

输出结果:
{"Created": 1721235029, "Usage": {"PromptTokens": 4, "CompletionTokens": 9, "TotalTokens": 13}, "Note": "以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记", "Id": "40c9f705-cc3a-4408-881b-7ee442a723c3", "Choices": [{"FinishReason": "stop", "Delta": null, "Message": {"Role": "assistant", "Content": "您想知道哪个城市的今天天气情况呢?", "Contents": null, "ToolCallId": null, "ToolCalls": null}}], "ErrorMsg": null, "RequestId": "40c9f705-cc3a-4408-881b-7ee442a723c3"}

2. 用户补充信息,模型给出要调用的工具

tools = [ { "Type": "function", "Function": { "Name": "get_weather", "Description": "查询天气", "Parameters": "{\\"type\\": \\"object\\"," "\\"properties\\": {" "\\"city\\": {" "\\"description\\": \\"城市名\\"," "\\"type\\": \\"string\\"" "}" "}," "\\"required\\": [" "\\"city\\"" "]" "}" } } ]

messages = [ { "Role": "user", "Content" : "今天天气如何?" }, { "Role": "assistant", "Content": "您想知道哪个城市的今天天气情况呢?" }, { "Role": "user", "Content": "北京" } ]
res = request_hunyuan_funccall(messages=messages, tools=tools)
print(res)
输出结果(后续步骤参考单轮对话-单工具调用):
(200, {'created': 1719403399, 'id': 'ae0435a4afd19c0d56c0227da03e0fe7', 'model': 'hunyuan-funcall', 'version': '202406141500', 'choices': [{'finish_reason': 'tool_calls', 'message': {'role': 'assistant', 'content': '调用天气查询工具,输入参数为用户提供的城市名。\\n\\t\\n\\t用户已经提供了城市名,可以使用工具进行天气查询。根据用户提供的城市名,调用天气查询工具获取天气信息。', 'tool_calls': [{'id': 'call_cpu071s2c3m6825sqrmg', 'type': 'function', 'function': {'name': 'get_weather', 'arguments': '{"city":"北京"}'}}]}}], 'search_info': {}, 'usage': {'prompt_tokens': 16, 'completion_tokens': 41, 'total_tokens': 57}})

三、单轮对话-多工具调用

1. 用户提问,模型给出要调用的工具

tools = [ { "Type": "function", "Function": { "Name": "get_weather", "Description": "查询天气", "Parameters": "{\\"type\\": \\"object\\"," "\\"properties\\": {" "\\"city\\": {" "\\"description\\": \\"城市名\\"," "\\"type\\": \\"string\\"" "}" "}," "\\"required\\": [" "\\"city\\"" "]" "}" } } ]
messages = [ { "Role": "user", "Content" : "帮我查下北京和上海的天气" } ] res = request_hunyuan_funccall(messages=messages, tools=tools)
输出结果:
{"Created": 1721235220, "Usage": {"PromptTokens": 7, "CompletionTokens": 66, "TotalTokens": 73}, "Note": "以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记", "Id": "f1ec8ad7-49b1-4f84-ab48-0c284f87a595", "Choices": [{"FinishReason": "tool_calls", "Delta": null, "Message": {"Role": "assistant", "Content": "计划使用get_weather工具来查询北京的天气,然后使用相同的工具查询上海的天气。\\n\\t\\n\\t用户想要查询北京和上海的天气情况,需要调用天气查询工具来完成这个任务。根据用户的请求,我们需要分别查询两个城市的天气。由于用户没有提供具体的查询时间,我们可以默认查询当前时间的天气。", "Contents": null, "ToolCallId": null, "ToolCalls": [{"Id": "call_cqbve542c3m9v673dovg", "Type": "function", "Function": {"Name": "get_weather", "Arguments": "{\\"city\\":\\"北京\\"}"}}]}}], "ErrorMsg": null, "RequestId": "f1ec8ad7-49b1-4f84-ab48-0c284f87a595"}

2. 给出第一个工具执行结果,模型给出下一个要调用的工具

Messages= [ { "Role": "user", "Content": "北京和深圳今天天气如何" }, { "Role": "assistant", "Content": "使用get_current_weather工具来获取北京和深圳的当前天气。\\n\\t\\n\\t用户想要知道北京和深圳今天的天气情况。用户提供了两个城市名称,但没有指定温度单位,根据常识,默认用户需要的是摄氏度。", "ToolCalls": [ { "Id": "call_cq16e7k2c3m1v7ep35c0", "Type": "function", "Function": { "Name": "get_current_weather", "Arguments": "{\\"location\\":\\"北京\\",\\"unit\\":\\"celsius\\"}" } } ] }, { "Role": "tool", "ToolCallId": "call_cq16e7k2c3m1v7ep35c0", "Content": "{\\"temperature\\": 35, \\"wind\\": \\"南\\", \\"condition\\": \\"暴雨\\"}" } ] tools = [ { "Type": "function", "Function": { "Name": "get_weather", "Description": "查询天气", "Parameters": "{\\"type\\": \\"object\\"," "\\"properties\\": {" "\\"city\\": {" "\\"description\\": \\"城市名\\"," "\\"type\\": \\"string\\"" "}" "}," "\\"required\\": [" "\\"city\\"" "]" "}" } } ] res = request_hunyuan_funccall(messages=Messages, tools=tools)

输出结果:
{"Created": 1721236839, "Usage": {"PromptTokens": 71, "CompletionTokens": 47, "TotalTokens": 118}, "Note": "以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记", "Id": "3ec25066-f03c-41d4-aa38-cfc8a952286d", "Choices": [{"FinishReason": "tool_calls", "Delta": null, "Message": {"Role": "assistant", "Content": "继续使用get_current_weather工具获取深圳的天气。\\n\\t\\n\\t已经获取了北京的天气情况,现在需要获取深圳的天气情况。用户请求了两个城市的天气,已经获取了一个城市的,应该继续获取另一个城市的天气。", "Contents": null, "ToolCallId": null, "ToolCalls": [{"Id": "call_cqbvqps2c3mfe0g5qg2g", "Type": "function", "Function": {"Name": "get_current_weather", "Arguments": "{\\"location\\":\\"深圳\\",\\"unit\\":\\"celsius\\"}"}}]}}], "ErrorMsg": null, "RequestId": "3ec25066-f03c-41d4-aa38-cfc8a952286d"}

3. 给出第二个工具执行结果,模型给出总结回复

tools = [ { "Type": "function", "Function": { "Name": "get_weather", "Description": "查询天气", "Parameters": "{\\"type\\": \\"object\\"," "\\"properties\\": {" "\\"city\\": {" "\\"description\\": \\"城市名\\"," "\\"type\\": \\"string\\"" "}" "}," "\\"required\\": [" "\\"city\\"" "]" "}" } } ] tool_execute_result1 = { "status_code": 200, "result": { "weather_info": "北京市今天(2024-06-26, 星期三)天气:晴转多云,最高温度:35°C,最低温度:24°C,相对湿度:25%,西南风,风力:1级。" } } tool_execute_result2 = { "status_code": 200, "result": { "weather_info": "上海市今天(2024-06-26, 星期三)天气:大雨,最高温度:25°C,最低温度:22°C,相对湿度:91%,北风,风力:1级。" } } messages = [ { "Role": "user", "Content" : "帮我查下北京和上海的天气" }, { "Role": "assistant", "Content": "使用get_weather工具来查询北京和上海的天气。\\n\\t\\n\\t用户想要查询北京和上海的天气情况。用户提供了两个具体的城市名,需要查询这两个城市的天气信息。", "tool_calls": [{'id': 'call_cpu024k2c3m9olr9jt00', 'type': 'function', 'function': {'name': 'get_weather', 'arguments': '{"city":"北京"}'}}] }, { "Role": "tool", "ToolCallId": "call_cpu024k2c3m9olr9jt00", "Content": json.dumps(tool_execute_result1, ensure_ascii=False) }, { "Role": "assistant", "Content": "调用get_weather工具查询上海的天气。\\n\\t\\n\\t已经获取了北京的天气信息,接下来需要查询上海的天气信息。根据用户的任务描述,需要查询两个城市的天气,现在已经完成了北京天气的查询。", "tool_calls": [{'id': 'call_cpu04m42c3m6cjl33ipg', 'type': 'function', 'function': {'name': 'get_weather', 'arguments': '{"city":"上海"}'}}] }, { "Role": "tool", "ToolCallId": "call_cpu04m42c3m6cjl33ipg", "Content": json.dumps(tool_execute_result2, ensure_ascii=False) } ] res = request_hunyuan_funccall(messages=messages, tools=tools)

输出结果:
{"Created": 1721237311, "Usage": {"PromptTokens": 210, "CompletionTokens": 129, "TotalTokens": 339}, "Note": "以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记", "Id": "0cb937dc-ba11-42a7-8d84-7bf3f1604836", "Choices": [{"FinishReason": "stop", "Delta": null, "Message": {"Role": "assistant", "Content": "北京今天的天气情况如下:\\n- 日期:2024-06-26,星期三\\n- 天气:晴转多云\\n- 最高温度:35°C\\n- 最低温度:24°C\\n- 相对湿度:25%\\n- 风向:西南风\\n- 风力:1级\\n\\n上海今天的天气情况如下:\\n- 日期:2024-06-26,星期三\\n- 天气:大雨\\n- 最高温度:25°C\\n- 最低温度:22°C\\n- 相对湿度:91%\\n- 风向:北风\\n- 风力:1级\\n\\n请问还有其他可以帮助您的吗?", "Contents": null, "ToolCallId": null, "ToolCalls": null}}], "ErrorMsg": null, "RequestId": "0cb937dc-ba11-42a7-8d84-7bf3f1604836"}

四、单轮对话-拒识

用户提问,模型发现没有需要调用的工具,直接回复

tools = [ { "Type": "function", "Function": { "Name": "get_weather", "Description": "查询天气", "Parameters": "{\\"type\\": \\"object\\"," "\\"properties\\": {" "\\"city\\": {" "\\"description\\": \\"城市名\\"," "\\"type\\": \\"string\\"" "}" "}," "\\"required\\": [" "\\"city\\"" "]" "}" } } ] messages = [ { "Role": "user", "Content" : "明天去故宫玩,帮我写一个待办清单" } ] res = request_hunyuan_funccall(messages=messages, tools=tools)
输出结果:
{"Created": 1721237428, "Usage": {"PromptTokens": 11, "CompletionTokens": 279, "TotalTokens": 290}, "Note": "以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记", "Id": "5bafa6c7-3208-4788-bfe0-f004ca4bcddb", "Choices": [{"FinishReason": "stop", "Delta": null, "Message": {"Role": "assistant", "Content": "明天去故宫玩,以下是一个待办清单,希望能帮助你规划行程:\\n\\n1. 提前购买故宫门票:可以通过故宫官网、官方授权的第三方平台或者景区附近的售票窗口购买。\\n\\n2. 准备必要的证件:身份证是必须的,如果是学生、老年人等可能有优惠政策的群体,记得带上相关证件。\\n\\n3. 规划路线:故宫规模较大,建议提前规划好路线,可以避开人流量大的区域。\\n\\n4. 穿着舒适:故宫内道路不平,建议穿着舒适的鞋子。\\n\\n5. 防晒措施:故宫位于北京,夏季阳光强烈,建议携带防晒用品,如太阳帽、太阳镜、防晒霜等。\\n\\n6. 携带水杯:故宫内没有提供饮用水,建议携带水杯。\\n\\n7. 遵守景区规定:不破坏文物,不攀爬古迹,不在非指定区域吸烟等。\\n\\n8. 携带小零食:故宫内没有餐饮设施,可以携带一些小零食和水。\\n\\n9. 安排休息时间:故宫较大,建议安排适当的休息时间。\\n\\n10. 留意闭馆时间:故宫有固定的闭馆时间,记得在闭馆前离开。\\n\\n11. 拍照留念:故宫是中国古代建筑的瑰宝,不要错过拍照留念的机会。\\n\\n以上就是明天去故宫玩的一个待办清单,希望对你有所帮助。", "Contents": null, "ToolCallId": null, "ToolCalls": null}}], "ErrorMsg": null, "RequestId": "5bafa6c7-3208-4788-bfe0-f004ca4bcddb"}

五、多轮对话-单工具调用

1. 用户提问,模型给出要调用的工具

tools = [ { "Type": "function", "Function": { "Name": "get_weather", "Description": "查询天气", "Parameters": "{\\"type\\": \\"object\\"," "\\"properties\\": {" "\\"city\\": {" "\\"description\\": \\"城市名\\"," "\\"type\\": \\"string\\"" "}" "}," "\\"required\\": [" "\\"city\\"" "]" "}" } } ] messages = [ { "Role": "user", "Content": "帮我查下北京天气" }, { "Role": "assistant", "Content": "北京市今天(2024-06-26, 星期三)天气:晴转多云,最高温度:35°C,最低温度:24°C,相对湿度:25%,西南风,风力:1级。" }, { "Role": "user", "Content" : "帮我查下上海天气" } ] res = request_hunyuan_funccall(messages=messages, tools=tools)
输出结果:
{"Created": 1721237519, "Usage": {"PromptTokens": 55, "CompletionTokens": 35, "TotalTokens": 90}, "Note": "以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记", "Id": "61cf289c-5410-4348-999b-c5ede22e7cc2", "Choices": [{"FinishReason": "tool_calls", "Delta": null, "Message": {"Role": "assistant", "Content": "使用get_weather工具来查询上海的天气。\\n\\t\\n\\t用户想要查询上海的天气情况。用户提供了城市名'上海',这是查询天气所必需的信息。", "Contents": null, "ToolCallId": null, "ToolCalls": [{"Id": "call_cqc003s2c3md17b1nm7g", "Type": "function", "Function": {"Name": "get_weather", "Arguments": "{\\"city\\":\\"上海\\"}"}}]}}], "ErrorMsg": null, "RequestId": "61cf289c-5410-4348-999b-c5ede22e7cc2"}


2. 给出工具执行结果,模型总结回复

tools = [ { "Type": "function", "Function": { "Name": "get_weather", "Description": "查询天气", "Parameters": "{\\"type\\": \\"object\\"," "\\"properties\\": {" "\\"city\\": {" "\\"description\\": \\"城市名\\"," "\\"type\\": \\"string\\"" "}" "}," "\\"required\\": [" "\\"city\\"" "]" "}" } } ] tool_execute_result = { "result": { "weather_info": "上海市今天(2024-06-26, 星期三)天气:大雨,最高温度:25°C,最低温度:22°C,相对湿度:91%,北风,风力:1级。" } } messages = [ { "Role": "user", "Content": "帮我查下北京天气" }, { "Role": "assistant", "Content": "北京市今天(2024-06-26, 星期三)天气:晴转多云,最高温度:35°C,最低温度:24°C,相对湿度:25%,西南风,风力:1级。" }, { "Role": "user", "Content" : "帮我查下上海天气" }, { "Role": "assistant", "Content": "计划使用get_weather工具来查询上海的天气。\\n\\t\\n\\t用户想要查询上海的天气情况。用户的请求与工具描述相匹配,需要使用get_weather工具来查询上海的天气。", "tool_calls": [{'id': 'call_cqc003s2c3md17b1nm7g', 'type': 'function', 'function': {'name': 'get_weather', 'arguments': '{"city":"上海"}'}}] }, { "Role": "tool", "ToolCallId":"call_cqc003s2c3md17b1nm7g", "Content": json.dumps(tool_execute_result, ensure_ascii=False) } ] res = request_hunyuan_funccall(messages=messages, tools=tools)
输出结果:
{"Created": 1721237740, "Usage": {"PromptTokens": 146, "CompletionTokens": 42, "TotalTokens": 188}, "Note": "以上内容为AI生成,不代表开发者立场,请勿删除或修改本标记", "Id": "a0c70538-d031-4c29-a03f-c75e909ffbdc", "Choices": [{"FinishReason": "stop", "Delta": null, "Message": {"Role": "assistant", "Content": "上海市今天(2024-06-26, 星期三)天气:大雨,最高温度:25°C,最低温度:22°C,相对湿度:91%,北风,风力:1级。", "Contents": null, "ToolCallId": null, "ToolCalls": null}}], "ErrorMsg": null, "RequestId": "a0c70538-d031-4c29-a03f-c75e909ffbdc"}

六、多轮对话-反问

多轮对话信息填写可参考五、多轮对话-单工具调用,以多对 {"role": "user", "content": "xxx"}, {"role": "assistant", "content": "xxx"} 给出历史对话信息即可,其余部分可参考二、单轮对话-反问

七、多轮对话-多工具调用

多轮对话信息填写可参考五、多轮对话-单工具调用,以多对 {"role": "user", "content": "xxx"}, {"role": "assistant", "content": "xxx"} 给出历史对话信息即可,其余部分可参考三、单轮对话-多工具调用

八、多轮对话-拒识

多轮对话信息填写可参考五、多轮对话-单工具调用,以多对 {"role": "user", "content": "xxx"}, {"role": "assistant", "content": "xxx"} 给出历史对话信息即可,其余部分可参考四、单轮对话-拒识