# Simple chat app with functions

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](http://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.

```csharp
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](https://github.com/rachkeenan/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](https://www.youtube.com/watch?v=qcp6ufe_XYo).

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

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

```csharp
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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1737443988955/5c121c68-c7ce-4b84-aa4c-968f926d6b6d.gif align="center")

### Repos

* [samples-microsoft-extensions-ai](https://github.com/rachkeenan/samples-microsoft-extensions-ai)
    
* [sample-ai-chatbot-blazor](https://github.com/rachkeenan/sample-ai-chatbot-blazor)
    

### [References](https://github.com/rachkeenan/samples-microsoft-extensions-ai)

* [https://www.youtube.com/watc](https://github.com/rachkeenan/samples-microsoft-extensions-ai)[h?v=qcp6ufe\_XYo](https://www.youtube.com/watch?v=qcp6ufe_XYo)
    
* [https://www.nuget.org/packages/Microsoft.Extensions.AI.OpenAI/9.0.0-preview.9.24556.5](https://www.nuget.org/packages/Microsoft.Extensions.AI.OpenAI/9.0.0-preview.9.24556.5)
