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. |