Skip to main content
All docs
V25.1
  • IAIChat.SetupAssistantAsync(String, String) Method

    Connects the chat to an existing OpenAI Assistant.

    Namespace: DevExpress.AIIntegration.Blazor.Chat

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

    NuGet Package: DevExpress.AIIntegration.Blazor.Chat

    Declaration

    Task SetupAssistantAsync(
        string assistantName,
        string threadId = null
    )

    Parameters

    Name Type Description
    assistantName String

    The name associated with the specified model.

    Optional Parameters

    Name Type Default Description
    threadId String null

    A thread identifier.

    Returns

    Type Description
    Task

    The task that is completed when the assistant is connected.

    Remarks

    The DxAIChat component supports OpenAI Assistants. You can use an OpenAI Assistant instance to initiate multiple tasks within a single application. To connect the chat to an existing assistant, pass the assistant and thread IDs to the SetupAssistantAsync method as parameters.

    The following code snippet creates an OpenAI Assistant, obtains the assistant and thread IDs, and connects the Blazor AI Chat to the assistant in a DxAIChat.Initialized event handler:

    @using DevExpress.AIIntegration.OpenAI.Services
    @using DevExpress.AIIntegration.Blazor.Chat
    @using AIIntegration.Services.Chat
    @using Markdig
    
    @inject ReportingApp.AIAssistantCreator assistantCreator
    
    <DxAIChat CssClass="my-chat" 
            Initialized="Initialized" 
            ResponseContentFormat="ResponseContentFormat.Markdown">
        <MessageContentTemplate>
            <div class="my-chat-content">
                @ToHtml(context.Content)
            </div>
        </MessageContentTemplate>
    </DxAIChat>
    
    @code {
        const string DocumentResourceName = "DevExpress.AI.Samples.Blazor.Data.Restaurant Menu.pdf";
        const string prompt = "You are an analytics assistant specialized in analyzing PDF files. Your role is to assist users by providing accurate answers to their questions about data contained within these files.\n \n### Tasks:\n- Perform various types of data analyses, including summaries, calculations, data filtering, and trend identification.\n- Clearly explain your analysis process to ensure users understand how you arrived at your answers.\n- Always provide precise and accurate information based on the Excel data.\n- If you cannot find an answer based on the provided data, explicitly state: \"The requested information cannot be found in the data provided.\"\n \n### Examples:\n1. **Summarization:**\n   - **User Question:** \"What is the average sales revenue for Q1?\"\n   - **Response:** \"The average sales revenue for Q1 is calculated as $45,000, based on the data in Sheet1, Column C.\"\n \n2. **Data Filtering:**\n   - **User Question:** \"Which products had sales over $10,000 in June?\"\n   - **Response:** \"The products with sales over $10,000 in June are listed in Sheet2, Column D, and they include Product A, Product B, and Product C.\"\n \n3. **Insufficient Data:**\n   - **User Question:** \"What is the market trend for Product Z over the past 5 years?\"\n   - **Response:** \"The requested information cannot be found in the data provided, as the dataset only includes data for the current year.\"\n \n### Additional Instructions:\n- Format your responses to clearly indicate which sheet and column the data was extracted from when necessary.\n- Avoid providing any answers if the data in the file is insufficient for a reliable response.\n- Ask clarifying questions if the user's query is ambiguous or lacks detail.\n \nRemember, your primary goal is to provide helpful, data-driven insights that directly answer the user's questions. Do not assume or infer information not present in the dataset.";
    
        async Task Initialized(IAIChat chat) {
            (string assistantId, string threadId) = await assistantCreator.CreateAssistantAsync(
                Assembly.GetExecutingAssembly().GetManifestResourceStream(DocumentResourceName)!,
                $"{Guid.NewGuid().ToString("N")}.pdf",
                prompt);
    
            await chat.SetupAssistantAsync(assistantId, threadId);
        }
        MarkupString ToHtml(string text) {
            return (MarkupString)Markdown.ToHtml(text);
        }
    }
    

    View Example: AI Chat for Blazor - How to add DxAIChat component in Blazor, MAUI, WPF, and WinForms applications

    See Also