alexa
menu

ASP.NET Core - EJ2

  • Code Examples
  • Upgrade Guide
  • User Guide
  • Demos
  • Support
  • Forums
  • Download

    Show / Hide Table of Contents

    Class SyncfusionAIService

    Represents a service that provides AI chat inference capabilities using Microsoft.Extensions.AI abstractions. This service works with any IChatClient implementation provided by the user.

    Inheritance
    System.Object
    SyncfusionAIService
    Implements
    IChatInferenceService
    Namespace: Syncfusion.EJ2.AI
    Assembly: Syncfusion.EJ2.dll
    Syntax
    public class SyncfusionAIService : Object, IChatInferenceService
    Examples
    // Example configuration in Program.cs for OpenAI
    using OpenAI;
    using Microsoft.Extensions.AI;
    
    string openAiApiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY") 
        ?? throw new InvalidOperationException("OPENAI_API_KEY environment variable is not set.");
    string openAiModel = Environment.GetEnvironmentVariable("OPENAI_MODEL") ?? "gpt-3.5-turbo";
    
    OpenAIClient openAIClient = new OpenAIClient(openAiApiKey);
    IChatClient openAiChatClient = openAIClient.GetChatClient(openAiModel).AsIChatClient();
    builder.Services.AddChatClient(openAiChatClient);
    builder.Services.AddSingleton<IChatInferenceService, SyncfusionAIService>();
    
    // Example configuration for Azure OpenAI
    using Azure.AI.OpenAI;
    using Microsoft.Extensions.AI;
    using OpenAI;
    
    string azureApiKey = Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY");
    string azureEndpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT");
    string deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME");
    
    AzureOpenAIClient azureClient = new AzureOpenAIClient(new Uri(azureEndpoint), new ApiKeyCredential(azureApiKey));
    IChatClient azureChatClient = azureClient.GetChatClient(deploymentName).AsIChatClient();
    builder.Services.AddChatClient(azureChatClient);
    builder.Services.AddSingleton<IChatInferenceService, SyncfusionAIService>();

    Constructors

    SyncfusionAIService(IChatClient)

    Initializes a new instance of the SyncfusionAIService class using the specified IChatClient.

    Declaration
    public SyncfusionAIService(IChatClient chatClient)
    Parameters
    Type Name Description
    Microsoft.Extensions.AI.IChatClient chatClient

    The IChatClient instance to use for AI inference operations.

    Exceptions
    Type Condition
    System.ArgumentNullException

    Thrown when chatClient is null.

    Methods

    GenerateResponseAsync(ChatParameters)

    Retrieves the chat response from the AI service based on the provided parameters. Sends a request to the AI service and processes the response.

    Declaration
    public Task<string> GenerateResponseAsync(ChatParameters options)
    Parameters
    Type Name Description
    ChatParameters options

    The ChatParameters containing the chat request details such as messages and other options.

    Returns
    Type Description
    System.Threading.Tasks.Task<System.String>

    A task that represents the asynchronous operation. The task result is a string containing the AI response.

    Exceptions
    Type Condition
    System.ArgumentNullException

    Thrown when options is null.

    System.InvalidOperationException

    Thrown when the chat client is not properly configured.

    Implements

    IChatInferenceService
    Back to top Generated by DocFX
    Copyright © 2001 - 2025 Syncfusion Inc. All Rights Reserved