Skip to content

Implement function calling with the DevExpress Blazor DxAIChat component.

License

Notifications You must be signed in to change notification settings

DevExpress-Examples/blazor-ai-chat-function-calling

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DevExpress Blazor AI Chat — Implement Function/Tool Calling

This example uses function/tool calling with the DevExpress Blazor DxAIChat component. Function calling allows a model to invoke external functions or APIs in response to user queries.

What's Inside

The solution includes the following projects:

  • Project 1: DXBlazorChatFunctionCalling implements function calling using the IChatClient interface from the Microsoft.Extensions.AI library.
  • Project 2: DXBlazorChatFunctionCalling.Semantic creates a Microsoft Semantic Kernel plugin with a callable function.

How Function Calling Works

  1. The AI model detects when a request requires external execution.
  2. The AI model identifies the appropriate function and necessary parameters. In this example, these are CustomAIFunctions.GetWeatherTool() and WeatherPlugin.MyGetWhether(string city) functions.
  3. The AI model calls the function and processes the result.
  4. The response is formatted and returned to the Blazor AI Chat control.

Register AI Service

Before you run the example, register your AI service. This example uses Azure OpenAI. You’ll need to specify your credentials in Program.cs:

// Replace with your endpoint, API key, and deployed AI model name.
string azureOpenAIEndpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT");
string azureOpenAIKey = Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY");
string deploymentName = string.Empty;

Note

We use the following versions of the Microsoft.Extensions.AI.* libraries in our source code:

v24.2.6+ | 9.3.0-preview.1.25161.3

We do not guarantee compatibility or correct operation with higher versions. Refer to the following announcement for additional information: Microsoft.Extensions.AI.Abstractions NuGet Package Version Upgrade in v24.2.6.

Implementation Details

Project 1: Tool Calling with IChatClient

  • The GetWeatherTool function retrieves weather information for a specified city.

  • The System.ComponentModel.Description attribute helps the AI model select and call the appropriate method (in this example this is the GetWeather method). By using this attribute, the AI-powered system can better analyze, select, and invoke the correct function when responding to user queries.

    using System.ComponentModel;
    using Microsoft.Extensions.AI;
    
    namespace DXBlazorChatFunctionCalling.Services;
    public class CustomAIFunctions {
    
        // Describe the function so the AI service understands its purpose.
        public static AIFunction GetWeatherTool => AIFunctionFactory.Create(GetWeather);
        [Description("Gets the current weather in the city")]
        public static string GetWeather([Description("The name of the city")] string city) {
            switch (city) {
                case "Los Angeles":
                case "LA":
                    return GetTemperatureValue(20);
                case "London":
                    return GetTemperatureValue(15);
                default:
                    return $"The information about the weather in {city} is not available.";
            }
        }
        static string GetTemperatureValue(int value) {
            var valueInFahrenheits = value * 9 / 5 + 32;
            return $"{valueInFahrenheits}\u00b0F ({value}\u00b0C)";
        }
    }
  • To enable function calling in a chat client, you must first configure chat client options. Once configured, call the UseFunctionInvocation() method to activate function invocation.

    using Azure;
    using Azure.AI.OpenAI;
    using Microsoft.Extensions.AI;
    //...
    IChatClient chatClient = new ChatClientBuilder(azureChatClient)
        .ConfigureOptions(x =>
        {
            x.Tools = [CustomAIFunctions.GetWeatherTool];
        })
        .UseFunctionInvocation()
        .Build();
    
    builder.Services.AddChatClient(chatClient);

When a user asks the AI Chat about the weather, the AI model automatically calls the GetWeatherTool function and returns the formatted result to the AI Chat control.

Project 2: Integrate Microsoft Semantic Kernel Plugins

The DXBlazorChatFunctionCalling.Semantic project implements a custom IChatClient to invoke IChatCompletionService methods from the Semantic Kernel. DevExpress AI-powered APIs use this interface to operate with LLMs.

The WeatherPlugin class implements a Microsoft Semantic Kernel plugin. The Microsoft.SemanticKernel.KernelFunction attribute annotates the GetWeather() method as a callable function within the Semantic Kernel runtime.

using Microsoft.SemanticKernel;
using System.ComponentModel;
 
public class WeatherPlugin
{
    [KernelFunction]
    [Description("Gets the current weather in the city, returning a value in Celsius")]
    public static string GetWeather([Description("The name of the city")] string city) {
        switch(city) {
            case "Los Angeles":
            case "LA":
                return GetTemperatureValue(20);
            case "London":
                return GetTemperatureValue(15);
            default:
                return $"The information about the weather in {city} is not available.";
        }
    }
    static string GetTemperatureValue(int value)
    {
      var valueInFahrenheits = value * 9 / 5 + 32;
      return $"{valueInFahrenheits}\u00b0F ({value}\u00b0C)";
    }
}

Files to Review

DXBlazorChatFunctionCalling

DXBlazorChatFunctionCalling.Semantic

Documentation

Online Demo

More Examples

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)