Lesson 2: Adding Function Calling with Agent Framework¶
This lesson builds on lesson 1 by adding function calling capabilities to our agent. Function calling allows the AI model to invoke external functions to get real-time data or perform computations.
-
Switch to Lesson 2 directory:
-
Copy the configuration file from the Solutions directory:
-
Run the application to see it works:
-
Observe the plugins in the
Core.Utilities.Pluginsnamespace:TimeInformationPlugin.cs- Provides current UTC timeStockDataPlugin.cs- Provides stock price data
-
Open
Program.csand add the following features:-
TODO: Step 1 - Initialize the chat client and plugins:
-
TODO: Step 2 - Create AI functions using AIFunctionFactory:
-
TODO: Step 3 - Create a ChatClientAgent with function calling capabilities:
string systemInstructions = "You are a friendly financial advisor that only emits financial advice in a creative and funny tone"; ChatClientAgent agent = new( chatClient, instructions: systemInstructions, name: "FinancialAdvisor", description: "A friendly financial advisor with access to time and stock data", tools: tools ); -
TODO: Step 4 - Create thread and use agent:
-
-
Test the application by asking questions that require function calls:
Example questions to test:
User > What time is it? Assistant > (should call time function and provide current time) User > What is the current price of MSFT? Assistant > (should call stock price function and provide current Microsoft stock price) User > What was the price of AAPL on 2023-01-01? Assistant > (should call historical stock price function)
The Agent Framework automatically handles function calling - when the model determines it needs external data, it will invoke the appropriate function and use the result in its response.