Back to Devexpress

BlazorChatMessage.Contexts Property

blazor-devexpress-dot-aiintegration-dot-blazor-dot-chat-dot-blazorchatmessage-e5b31697.md

latest5.3 KB
Original Source

BlazorChatMessage.Contexts Property

Returns a collection of AI resources and files attached to the chat message.

Namespace : DevExpress.AIIntegration.Blazor.Chat

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

NuGet Package : DevExpress.AIIntegration.Blazor.Chat

Declaration

csharp
public List<IAIChatMessageContextItem> Contexts { get; }

Property Value

TypeDescription
List<IAIChatMessageContextItem>

The collection of resources and files.

|

Remarks

Use the Contexts property to obtain information about AI resources and files attached to the chat message.

Note

The Contexts collection is populated only for messages in which a user explicitly attaches resources or files. Subsequent messages can reference previously selected resources, but their Contexts collection remains empty.

razor
<DxAIChat Resources="Resources">
    <MessageContentTemplate>
        <div class="ai-resources-wrapper">
            @foreach(var resource in context.Contexts) {
                <span class="ai-resource">@resource.Name</span>
            }
        </div>
        <div class="ai-chat-message">@context.Content</div>
    </MessageContentTemplate>
</DxAIChat>

@code {
    List<AIChatResource> Resources { get; set; } = [];

    protected override async Task OnInitializedAsync() {
        AIChatResource instructions = await FileResourceProvider.GetTextResourceAsync("instructions",
            "Technical Support",
            "An assistant that helps troubleshoot technical issues and provides step-by-step solutions.");
        AIChatResource documentation = await FileResourceProvider.GetTextResourceAsync("api-reference.md",
            "API Reference",
            "DevExpress Blazor AI Chat API Reference.");
        AIChatResource screenshot = await FileResourceProvider.GetBinaryResourceAsync("ai-chat-image.png",
            "AI Chat Screenshot",
            "An annotated screenshot of the AI Chat component.",
            "image/png");
        Resources.Add(instructions);
        Resources.Add(documentation);
        Resources.Add(screenshot);
        await base.OnInitializedAsync();
    }
}
css
.ai-resources-wrapper {
    padding-top: 10px;
}

.ai-resource {
    margin-right: 10px;
    padding: 5px 10px;
    border: solid 1px #cccccc;
    border-radius: 5px;
    background-color: white;
}

.ai-chat-message {
    margin-top: 15px;
}
csharp
public static class FileResourceProvider
{
    public static async Task<AIChatResource> GetTextResourceAsync(string resourceFileName,
                                                                  string title,
                                                                  string description) {
        string filePath = GetResourcePath(resourceFileName);
        string fileContent = await File.ReadAllTextAsync(filePath);
        string mime = Path.GetExtension(resourceFileName).ToLower() switch {
            ".md" => "text/markdown",
            ".html" => "text/html",
            _ => "text/plain"
        };
        AIChatResource resource = new AIChatResource(resourceFileName,
                                                     title,
                                                     fileContent,
                                                     mime,
                                                     description);
        return resource;
    }

    public static async Task<AIChatResource> GetBinaryResourceAsync(string resourceFileName,
                                                                    string title,
                                                                    string description,
                                                                    string mime) {
        string filePath = GetResourcePath(resourceFileName);
        byte[] fileContent = await File.ReadAllBytesAsync(filePath);
        AIChatResource resource = new AIChatResource(resourceFileName,
                                                     title,
                                                     fileContent,
                                                     mime,
                                                     description);
        return resource;
    }

    private static string GetResourcePath(string resourceFileName) =>
        Path.Combine(AppContext.BaseDirectory, "AiResources", resourceFileName);
}

See Also

BlazorChatMessage Class

BlazorChatMessage Members

DevExpress.AIIntegration.Blazor.Chat Namespace