Back to Devexpress

DxAIChat.ResponseReceived Event

blazor-devexpress-dot-aiintegration-dot-blazor-dot-chat-dot-dxaichat-564ee263.md

latest3.8 KB
Original Source

DxAIChat.ResponseReceived Event

Fires after the AI provider returns a response, before the message is displayed in the chat.

Namespace : DevExpress.AIIntegration.Blazor.Chat

Assembly : DevExpress.AIIntegration.Blazor.Chat.v25.2.dll

NuGet Package : DevExpress.AIIntegration.Blazor.Chat

Declaration

csharp
[Parameter]
public EventCallback<ResponseReceivedEventArgs> ResponseReceived { get; set; }

Parameters

TypeDescription
ResponseReceivedEventArgs

An object that contains event data.

|

Remarks

Handle the ResponseReceived event to process the AI provider’s response immediately before it is rendered. Use this event to analyze content, save an audit trail, or trigger business logic.

The following code snippet handles the ResponseReceived event to remove specific tool call details from the response:

razor
@using DevExpress.AIIntegration.Blazor.Chat
@using DevExpress.AIIntegration.Tools
@inject AIToolsContextContainer aiToolsContextContainer

<DxAIChat IncludeFunctionCallInfo="true"
          ResponseReceived="OnResponseReceived">
</DxAIChat>

@code {
    private void OnResponseReceived(ResponseReceivedEventArgs args) {
        args.Message.FunctionCalls.RemoveAll(
            fc => fc?.Request?.Name != null &&
            fc.Request.Name == "Generic_Greeting");
    }

    protected override void OnAfterRender(bool firstRender) {
        if(firstRender) {
            var context = new AIToolsContextBuilder()
                .WithToolMethods(MyAITools.Greet, MyAITools.AddNumbers)
                .Build();
            aiToolsContextContainer.Add(context);
        }
    }
}
csharp
using DevExpress.AIIntegration;
using System.ComponentModel;

public class MyAITools
{
    [AIIntegrationTool("Generic_Greeting")]
    [Description("Greets a user with their name.")]
    public static string Greet(
        [Description("The name of the user.")] string name) {
        var now = DateTime.Now;
        if(now.Hour < 7)
            return $"Good night, {name}!";
        else if(now.Hour < 12)
            return $"Good morning, {name}!";
        else if(now.Hour < 18)
            return $"Good afternoon, {name}!";
        else
            return $"Good evening, {name}!";
    }

    [AIIntegrationTool("Generic_Calculator")]
    [Description("Adds two numbers.")]
    public static int AddNumbers(
        [Description("First number")] int a,
        [Description("Second number")] int b) {
        return a + b;
    }
}
csharp
using Azure.AI.OpenAI;
using DevExpress.AIIntegration;
using Microsoft.Extensions.AI;
using System.ClientModel;

/* ... */

AzureOpenAIClient azureOpenAIClient = new(
      new Uri(azureOpenAiEndpoint),
      new ApiKeyCredential(azureOpenAiKey)
);
IChatClient azureOpenAIChatClient = azureOpenAIClient.GetChatClient(azureOpenAiModel).AsIChatClient();
builder.Services.AddScoped((sp) => {
    return azureOpenAIChatClient.AsBuilder()
                                .UseDXTools()
                                .UseFunctionInvocation()
                                .Build(sp);
});
builder.Services.AddDevExpressAI();
builder.Services.AddDevExpressBlazor();

See Also

DxAIChat Class

DxAIChat Members

DevExpress.AIIntegration.Blazor.Chat Namespace