架构师_程序员_码农网

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

搜索
查看: 205|回复: 2

[资料] 【AI】(十六)Semantic Kernel 基于 Qwen 测试 Function Calling

[复制链接]
发表于 2025-3-26 17:46:52 | 显示全部楼层 |阅读模式
需求:利用大模型获取实时的信息,例如:当前时间、今天的天气、查快递等,或者用大模型来分析业务数据,就要使用 Function Calling 功能。网上很多关于 Function Calling 文章写的非常模糊,也没讲明白,自己根据请求信息理解此功能。

先看效果图,利用大模型获取当前的时间、上海的天气、以及帮我打开电脑的计算器,如下图:

function.gif

OpenAI 接口文档:https://platform.openai.com/docs/api-reference/chat/create,如下图:

屏幕截图 2025-03-26 173713.png

Function Calling

大模型在面对实时性问题、数学计算等问题时可能效果不佳。您可以使用 Function Calling 功能,通过引入外部工具,使得大模型可以与外部世界进行交互。

Function Calling 是一种将大模型与外部工具和API相连的新功能,助力大模型向实际产业落地迈进。Function Calling 允许开发者更可靠的从模型中获得结构化数据,无需用户输入复杂的Prompt。

在使用时,您可以用自然语言向模型描述一组 Function 的功能和定义; 在对话过程中,当大模型觉得需要使用某函数时会智能地选择该 Function,并返回调用所需参数,来满足用户的特定需求; 其他情况,大模型不会返回 Function,而是继续对话。大模型不会直接调用 Function,而是返回其对应的入参,您可自行调用该函数/API接口。

简单来说,Function Calling 是介于自然语言和信息接口的「翻译官」

把自然语言翻译成所需使用函数、参数,返回给大模型调用方
大模型调用方执行函数后把结果返回给大模型,大模型能总结为自然语言,或继续规划子任务

工作流程示意图如下所示:

屏幕截图 2025-03-26 173924.png

Semantic Kernel

语义内核是一种轻型开源开发工具包,可用于轻松生成 AI 代理并将最新的 AI 模型集成到 C#、Python 或 Java 代码库中。 它充当一个高效的中间件,可实现企业级解决方案的快速交付。

文档:https://learn.microsoft.com/zh-cn/semantic-kernel/overview/
源码:https://github.com/microsoft/semantic-kernel

C# 调用阿里云通义千问执行 Function Calling

新建 .NET 8 控制台项目,引用如下:

代码如下:

运行程序,就是一开始的效果图,获取当前时间和上海天气、打开计算器。

执行日志:

=============================================================================
Request:
Method: POST
Uri: https://dashscope.aliyuncs.com/c ... v1/chat/completions
Request Content: {"tools":[{"function":{"description":"当你想知道现在的时间时非常有用。","name":"itsvse-get_current_time","strict":false,"parameters":{"type":"object","required":[],"properties":{}}},"type":"function"},{"function":{"description":"当你想查询指定城市今天的天气时非常有用。","name":"itsvse-get_current_weather","strict":false,"parameters":{"type":"object","required":["location"],"properties":{"location":{"description":"城市或县区,比如北京市、上海市、杭州市、余杭区等。","type":"string"}}}},"type":"function"},{"function":{"description":"当你想查询指定快递物流信息时非常有用。","name":"itsvse-get_current_test","strict":false,"parameters":{"type":"object","required":["no"],"properties":{"no":{"description":"快递单号,例如:顺丰、中通、邮政快递单号都可以。","type":"string"}}}},"type":"function"},{"function":{"description":"当你想打开计算器的时候非常有用。","name":"itsvse-open_calculator","strict":false,"parameters":{"type":"object","required":[],"properties":{}}},"type":"function"}],"messages":[{"role":"user","content":"你好,帮忙查询一下现在的时间和今天上海的天气。然后,再帮我打卡计算器,我要计算今天的收入。"}],"model":"qwen-max","tool_choice":"auto","parallel_tool_calls":true}
Response:
Status Code: OK
Body: {"choices":[{"message":{"content":"","role":"assistant","tool_calls":[{"function":{"name":"itsvse-get_current_time","arguments":"{}"},"index":0,"id":"call_6e6cade782f54c9ca87186","type":"function"},{"function":{"name":"itsvse-get_current_weather","arguments":"{\"location\": \"上海市\"}"},"index":1,"id":"call_cc957be34f5741f4b2a969","type":"function"},{"function":{"name":"itsvse-open_calculator","arguments":"{}"},"index":2,"id":"call_6023df74d7bd48c789e876","type":"function"}]},"finish_reason":"tool_calls","index":0,"logprobs":null}],"object":"chat.completion","usage":{"prompt_tokens":500,"completion_tokens":53,"total_tokens":553,"prompt_tokens_details":{"cached_tokens":256}},"created":1742980780,"system_fingerprint":null,"model":"qwen-max","id":"chatcmpl-1761acb9-717a-997c-9f17-7284e82fd121"}
=============================================================================
-----------------------调用获取时间的方法-----------------------
----------------------------------------------------------------
-----------------------调用获取天气的方法-----------------------
参数:上海市
----------------------------------------------------------------
-----------------------打开电脑计算器-----------------------
=============================================================================
Request:
Method: POST
Uri: https://dashscope.aliyuncs.com/c ... v1/chat/completions
Request Content: {"tools":[{"function":{"description":"当你想知道现在的时间时非常有用。","name":"itsvse-get_current_time","strict":false,"parameters":{"type":"object","required":[],"properties":{}}},"type":"function"},{"function":{"description":"当你想查询指定城市今天的天气时非常有用。","name":"itsvse-get_current_weather","strict":false,"parameters":{"type":"object","required":["location"],"properties":{"location":{"description":"城市或县区,比如北京市、上海市、杭州市、余杭区等。","type":"string"}}}},"type":"function"},{"function":{"description":"当你想查询指定快递物流信息时非常有用。","name":"itsvse-get_current_test","strict":false,"parameters":{"type":"object","required":["no"],"properties":{"no":{"description":"快递单号,例如:顺丰、中通、邮政快递单号都可以。","type":"string"}}}},"type":"function"},{"function":{"description":"当你想打开计算器的时候非常有用。","name":"itsvse-open_calculator","strict":false,"parameters":{"type":"object","required":[],"properties":{}}},"type":"function"}],"messages":[{"role":"user","content":"你好,帮忙查询一下现在的时间和今天上海的天气。然后,再帮我打卡计算器,我要计算今天的收入。"},{"role":"assistant","content":"","tool_calls":[{"id":"call_6e6cade782f54c9ca87186","function":{"name":"itsvse-get_current_time","arguments":"{}"},"type":"function","index":0},{"id":"call_cc957be34f5741f4b2a969","function":{"name":"itsvse-get_current_weather","arguments":"{\"location\": \"上海市\"}"},"type":"function","index":1},{"id":"call_6023df74d7bd48c789e876","function":{"name":"itsvse-open_calculator","arguments":"{}"},"type":"function","index":2}]},{"role":"tool","tool_call_id":"call_6e6cade782f54c9ca87186","content":"2025/3/26 17:19:40"},{"role":"tool","tool_call_id":"call_cc957be34f5741f4b2a969","content":"上海市的天气情况如下:多云转雷阵雨,气温介于10°C到20°C之间"},{"role":"tool","tool_call_id":"call_6023df74d7bd48c789e876","content":"已经打开计算器。"}],"model":"qwen-max","tool_choice":"auto","parallel_tool_calls":true}
Response:
Status Code: OK
Body: {"choices":[{"message":{"content":"现在的时间是2025年3月26日17点19分40秒。\n\n今天上海市的天气情况如下:多云转雷阵雨,气温介于10°C到20°C之间,请记得带伞哦。\n\n我已经 帮你打开了计算器,你可以开始计算今天的收入了。","role":"assistant"},"finish_reason":"stop","index":0,"logprobs":null}],"object":"chat.completion","usage":{"prompt_tokens":621,"completion_tokens":70,"total_tokens":691,"prompt_tokens_details":{"cached_tokens":0}},"created":1742980783,"system_fingerprint":null,"model":"qwen-max","id":"chatcmpl-b0bd149e-3f5b-9a53-91d7-7bb26487d822"}
=============================================================================
Assistant > 现在的时间是2025年3月26日17点19分40秒。

今天上海市的天气情况如下:多云转雷阵雨,气温介于10°C到20°C之间,请记得带伞哦。

我已经帮你打开了计算器,你可以开始计算今天的收入了。

参考:

https://qwen.readthedocs.io/zh-cn/latest/framework/function_call.html
https://learn.microsoft.com/en-us/semantic-kernel/get-started/quick-start-guide
https://learn.microsoft.com/zh-cn/azure/ai-services/openai/
https://help.aliyun.com/zh/model-studio/developer-reference/compatibility-of-openai-with-dashscope
https://help.aliyun.com/zh/model-studio/user-guide/qwen-function-calling

评分

参与人数 1MB +1 收起 理由
GoodTime + 1 支持楼主发好帖,我也会发好帖!.

查看全部评分





上一篇:Java 中 BigDecimal 的 toPlainString、toEngineeringString、toString
下一篇:.NET/C# 父子进程使用标准输入输出流通信
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
 楼主| 发表于 2025-3-27 08:59:23 | 显示全部楼层
多轮对话代码:


第二次发起的请求信息如下:



码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
 楼主| 发表于 2025-3-27 08:58:04 | 显示全部楼层
关于流式输出

在流式输出模式(stream)下,tool_calls 同样适用,但有一些需要额外注意的地方,列举如下:

在流式输出的过程中,由于 finish_reason 将会在最后的数据块中出现,因此建议使用 delta.tool_calls 字段是否存在来判断当前回复是否包含工具调用;
在流式输出的过程中,会先输出 delta.content,再输出 delta.tool_calls,因此你必须等待 delta.content 输出完成后,才能判断和识别 tool_calls;
在流式输出的过程中,我们会在最初的数据块中,指明当前调用 tool_calls 的 tool_call.id 和 tool_call.function.name,在后续的数据块中将只输出 tool_call.function.arguments;
在流式输出的过程中,如果 Kimi 大模型一次性返回多个 tool_calls,那么我们会额外使用一个名为 index 的字段来标识当前 tool_call 的索引,以便于你能正确拼接 tool_call.function.arguments 参数;

https://platform.moonshot.cn/docs/guide/use-kimi-api-to-complete-tool-calls

流式响应示例如下:

data: {"choices":[{"delta":{"content":null,"tool_calls":[{"function":{"name":"itsvse-get_current_time","arguments":""},"index":0,"id":"call_deb0063d315441b18b50d8","type":"function"}],"role":"assistant"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1743037194,"system_fingerprint":null,"model":"qwen-max","id":"chatcmpl-c4e2a989-10a3-9604-bdae-0b58b4f876a7"}

data: {"choices":[{"delta":{"content":null,"tool_calls":[{"function":{"arguments":"{}"},"index":0,"id":"","type":"function"}]},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1743037194,"system_fingerprint":null,"model":"qwen-max","id":"chatcmpl-c4e2a989-10a3-9604-bdae-0b58b4f876a7"}

data: {"choices":[{"delta":{"content":null,"tool_calls":[{"function":{},"index":0,"id":"","type":"function"}]},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1743037194,"system_fingerprint":null,"model":"qwen-max","id":"chatcmpl-c4e2a989-10a3-9604-bdae-0b58b4f876a7"}

data: {"choices":[{"delta":{"content":null,"tool_calls":[{"function":{},"index":0,"id":"","type":"function"}]},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1743037194,"system_fingerprint":null,"model":"qwen-max","id":"chatcmpl-c4e2a989-10a3-9604-bdae-0b58b4f876a7"}

data: {"choices":[{"delta":{"content":null,"tool_calls":[{"function":{},"index":0,"id":"","type":"function"},{"function":{"name":"itsvse-get_current_weather","arguments":""},"index":1,"id":"call_9790fb45e2b7419097d578","type":"function"}]},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1743037194,"system_fingerprint":null,"model":"qwen-max","id":"chatcmpl-c4e2a989-10a3-9604-bdae-0b58b4f876a7"}

data: {"choices":[{"delta":{"content":null,"tool_calls":[{"function":{"arguments":"{\"location\": \""},"index":1,"id":"","type":"function"}]},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1743037194,"system_fingerprint":null,"model":"qwen-max","id":"chatcmpl-c4e2a989-10a3-9604-bdae-0b58b4f876a7"}

data: {"choices":[{"delta":{"content":null,"tool_calls":[{"function":{"arguments":"上海市\"}"},"index":1,"id":"","type":"function"}]},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1743037194,"system_fingerprint":null,"model":"qwen-max","id":"chatcmpl-c4e2a989-10a3-9604-bdae-0b58b4f876a7"}

data: {"choices":[{"delta":{"content":null,"tool_calls":[{"function":{},"index":1,"id":"","type":"function"}]},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1743037194,"system_fingerprint":null,"model":"qwen-max","id":"chatcmpl-c4e2a989-10a3-9604-bdae-0b58b4f876a7"}

data: {"choices":[{"delta":{"content":null,"tool_calls":[{"function":{},"index":1,"id":"","type":"function"}]},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1743037194,"system_fingerprint":null,"model":"qwen-max","id":"chatcmpl-c4e2a989-10a3-9604-bdae-0b58b4f876a7"}

data: {"choices":[{"delta":{"content":null,"tool_calls":[{"function":{},"index":1,"id":"","type":"function"}]},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1743037194,"system_fingerprint":null,"model":"qwen-max","id":"chatcmpl-c4e2a989-10a3-9604-bdae-0b58b4f876a7"}

data: {"choices":[{"delta":{"tool_calls":[{"function":{"arguments":null},"index":1,"id":"","type":"function"},{"function":{"arguments":"{}","name":"itsvse-open_calculator"},"index":2,"id":"call_3ad6478075f04021ab9ea1","type":"function"}]},"index":0}],"object":"chat.completion.chunk","usage":null,"created":1743037194,"system_fingerprint":null,"model":"qwen-max","id":"chatcmpl-c4e2a989-10a3-9604-bdae-0b58b4f876a7"}

data: {"choices":[{"finish_reason":"tool_calls","delta":{},"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1743037194,"system_fingerprint":null,"model":"qwen-max","id":"chatcmpl-c4e2a989-10a3-9604-bdae-0b58b4f876a7"}

data: {"choices":[],"object":"chat.completion.chunk","usage":{"prompt_tokens":500,"completion_tokens":53,"total_tokens":553,"prompt_tokens_details":{"cached_tokens":0}},"created":1743037194,"system_fingerprint":null,"model":"qwen-max","id":"chatcmpl-c4e2a989-10a3-9604-bdae-0b58b4f876a7"}

data: [DONE]


码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|手机版|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2025-4-20 09:06

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表