This article is a mirror article of machine translation, please click here to jump to the original article.

View: 1326|Reply: 3

[Source] [AI] (16) Semantic Kernel is based on Qwen to test Function Calling

[Copy link]
Posted on 2025-3-26 17:46:52 | | | |
Requirements: Use large models to obtain real-time information, such as current time, today's weather, check express delivery, etc., or use large models to analyze business data, you need to use the Function Calling function. Many articles on the Internet about Function Calling are very vague and do not explain clearly, and I understand this function based on the request information.

Let's take a look at the renderings first and use the large modelGet the current time, the weather in Shanghai, and the calculator that helped me turn on my computer, as shown in the figure below:



OpenAI Interface Documentation:The hyperlink login is visible., as shown in the figure below:



Function Calling

Large models may not be effective when facing real-time problems, mathematical calculations and other problems. You can use the Function Calling function to enable large models to interact with the outside world by introducing external tools.

Function Calling is a new function that connects large models with external tools and APIs to help large models move towards actual industrial implementation. Function Calling allows developers to obtain structured data from models more reliably without the need for users to input complex prompts.

When used, you can describe the functions and definitions of a set of functions to the model in natural language; During the conversation, when the large model feels that it needs to use a function, it will intelligently select the function and return to call the required parameters to meet the specific needs of the user. In other cases, the large model will not return a function and will continue the conversation. The large model does not directly call the function, but returns its corresponding input parameters, and you can call the function/API interface yourself.

In simple terms, Function Calling is a "translator" between natural language and information interfaces

Translate the natural language into the required functions and parameters, and return it to the large model caller
After the large model caller executes the function, the result is returned to the large model, and the large model can be summarized into natural language or continue to plan subtasks

The workflow diagram is shown below:



Semantic Kernel

The Semantic Kernel is a lightweight, open-source development kit that can be used to easily generate AI agents and integrate the latest AI models into C#, Python, or Java codebases. It acts as an efficient middleware, enabling rapid delivery of enterprise-level solutions.

Documentation:The hyperlink login is visible.
Source:The hyperlink login is visible.

C# calls Alibaba Cloud Tongyi Qianwen to execute Function Calling

Create a new .NET 8 console project with the following references:

The code is as follows:

Run the program, which is the rendering at the beginning, get the current time and Shanghai weather, and open the calculator.

Execution log:

=============================================================================
Request:
Method: POST
Uri: The hyperlink login is visible.
Request Content: {"tools":[{"function":{"description":"Very useful when you want to know what time it is." ,"name":"itsvse-get_current_time","strict":false,"parameters":{"type":"object","required":[],"properties":{}}},"type":"function"},{"function":{"description":" This is useful when you want to check the weather for a given city today. ","name":"itsvse-get_current_weather","strict":false,"parameters":{"type":"object","required":["location"],"properties":{"location":{"description":"City or county, such as Beijing, Shanghai, Hangzhou City, Yuhang District, etc. ","type":"string"}}}},"type":"function"},{"function":{"description":"This is very useful when you want to query the specified express logistics information." ","name":"itsvse-get_current_test","strict":false,"parameters":{"type":"object","required":["no"],"properties":{"no":{"description":"Courier tracking number, e.g., SF, ZTO, and Postal courier tracking numbers are acceptable." ","type":"string"}}}},"type":"function"},{"function":{"description":"Very useful when you want to open the calculator." ,"name":"itsvse-open_calculator","strict":false,"parameters":{"type":"object","required":[],"properties":{}}},"type":"function"}],"messages":[{"role":"user","content" "Hello, please help me check the current time and today's weather in Shanghai." Then, help me with the clock-in calculator, I want to calculate today's income. "}],"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"}
=============================================================================
----------------------- call the method of getting time-----------------------
----------------------------------------------------------------
----------------------- call the method of getting the weather-----------------------
Parameter: Shanghai
----------------------------------------------------------------
----------------------- Open the computer calculator-----------------------
=============================================================================
Request:
Method: POST
Uri: The hyperlink login is visible.
Request Content: {"tools":[{"function":{"description":"Very useful when you want to know what time it is." ,"name":"itsvse-get_current_time","strict":false,"parameters":{"type":"object","required":[],"properties":{}}},"type":"function"},{"function":{"description":" This is useful when you want to check the weather for a given city today. ","name":"itsvse-get_current_weather","strict":false,"parameters":{"type":"object","required":["location"],"properties":{"location":{"description":"City or county, such as Beijing, Shanghai, Hangzhou City, Yuhang District, etc. ","type":"string"}}}},"type":"function"},{"function":{"description":"This is very useful when you want to query the specified express logistics information." ","name":"itsvse-get_current_test","strict":false,"parameters":{"type":"object","required":["no"],"properties":{"no":{"description":"Courier tracking number, e.g., SF, ZTO, and Postal courier tracking numbers are acceptable." ","type":"string"}}}},"type":"function"},{"function":{"description":"Very useful when you want to open the calculator." ,"name":"itsvse-open_calculator","strict":false,"parameters":{"type":"object","required":[],"properties":{}}},"type":"function"}],"messages":[{"role":"user","content" "Hello, please help me check the current time and today's weather in Shanghai." Then, help me with the clock-in calculator, I want to calculate today's income. "},{"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\": \"Shanghai\"}"},"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":"The weather conditions in Shanghai are as follows: cloudy with thunderstorms, temperatures between 10°C and 20°C"},{" role":"tool","tool_call_id":"call_6023df74d7bd48c789e876","content":"Calculator opened." }],"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 > is now 17:19:40, 26 March 2025.

The weather conditions in Shanghai today are as follows: cloudy to thunderstorms, temperatures between 10°C and 20°C, please remember to bring an umbrella.

I've opened the calculator for you and you can start calculating your earnings today.

Reference:

The hyperlink login is visible.
The hyperlink login is visible.
The hyperlink login is visible.
The hyperlink login is visible.
The hyperlink login is visible.

Score

Number of participants1MB+1 Collapse reason
GoodTime + 1 Support the owner to post a good post, and I will also post a good post!.

See all ratings





Previous:toPlainString, toEngineeringString, toString for BigDecimal in Java
Next:.NET/C# parent-child processes communicate using standard input/output streams
 Landlord| Posted on 2025-3-27 08:59:23 |
Multi-turn conversation code:


secondThe information for the initiated request is as follows:



 Landlord| Posted on 2025-3-27 08:58:04 |
About streaming output

In stream mode, tool_calls also applies, but there are a few additional points to note, as enumerated below:

During the streaming output, since finish_reason will appear in the final chunk, it is recommended to use the presence or absence of the delta.tool_calls field to determine whether the current reply contains a tool call;
During the streaming output, delta.content will be output first, and then the delta.tool_calls, so you must wait for the delta.content output to complete before you can judge and identify tool_calls;
In the process of streaming output, we will specify the tool_call.id and tool_call.function.name of the current call tool_calls in the initial data block, and only tool_call.function.arguments will be output in the subsequent data block.
During the streaming output process, if the Kimi model returns multiple tool_calls at once, we will use an additional field called index to identify the current tool_call index, so that you can correctly stitch the tool_call.function.arguments parameter.

The hyperlink login is visible.

An example of a streaming response is as follows:

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]


 Landlord| Posted on 2025-10-22 11:30:02 |
MCP (1) Model Context Protocol Introductory Tutorial
https://www.itsvse.com/thread-11104-1-1.html
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com