Simple chat app with functions

I am a recent graduate at the beginning of my software development career. I enjoy documenting my learnings through my blogs
I created a simple chat app that can talk to OpenAI, and I added to this by adding function calling. Meaning the AI can do more than just browse the internet, you can add some logic to your app.
The aim of Microsoft.Extensions.AI is to make the code more reusable, meaning you can easily swap out the LLMs being used with one line of code rather than multiple.
IChatClient innerChatClient = new OpenAIClient(Environment.GetEnvironmentVariable("OPENAI_API_KEY"))
.AsChatClient("gpt-4o-mini");
I have previously created an AI assistant using OpenAI in a Blazor app sample-ai-chatbot-blazor. I wanted to test out the new ways to add LLMs to my app. Although I only created this current app as a console app, I aim to add this in to my current Blazor app which is more user friendly.
I followed the examples in this YouTube video which was featured on .NET Conf event in November 2024, but specifically this video AI Building Blocks - A new, unified AI layer.
The chat app is simple, add the LLM, hardcode in a message and write out the AI’s response to the console.
Adding the function calling was a bit more complex, in the sense of more code was needed for setup. Function calling needed to be enabled on the IChatClient, I did this through a ChatClientBuilder.
var chatClientBuilder = new ChatClientBuilder(innerChatClient).UseFunctionInvocation();
IChatClient client = chatClientBuilder.Build();
I then added a class for my function, which had a method for “adding socks to a cart” and a method for getting a price. These function are then added to the ChatOptions under Tools, and then the ChatOptions is called with the AI’s response.
ChatOptions chatOptions = new()
{
Tools = [AIFunctionFactory.Create(cart.GetPrice), AIFunctionFactory.Create(cart.AddSocksToCart)]
};
var response = await client.CompleteAsync(messages, chatOptions);
You also need to add some context to the AI’s prompt for it to understand the function.
For the example I followed in this video, the AI assistant essentially promotes socks and tries to sell socks to the user. The user can ask how much for X pairs or per pair and if the user agrees to buy an amount of socks the AI with simulate adding these socks to a cart and give the user a total price.
Here is an example of the AI assistant calling the function




