AIChatUploadFileInfo Class
Contains information about a file to be uploaded to a chat message.
Namespace: DevExpress.AIIntegration.Blazor.Chat
Assembly: DevExpress.AIIntegration.Blazor.Chat.v25.1.dll
NuGet Package: DevExpress.AIIntegration.Blazor.Chat
Declaration
public class AIChatUploadFileInfo
Remarks
Use the MessageSentEventArgs.Files argument property in a MessageSent event handler to obtain information about files attached to the message.
<DxAIChat MessageSent="MessageSent"
FileUploadEnabled="true" />
@code {
async Task MessageSent(MessageSentEventArgs args) {
var fileNames = args.Files.Select(x => x.Name);
var response = $"Processed message content: {args.Content}.{System.Environment.NewLine}";
response += $"The message contains {args.Files.Count} attached file(s):{System.Environment.NewLine}";
response += string.Join(System.Environment.NewLine, fileNames);
await args.Chat.SendMessage(response, ChatRole.Assistant);
}
}
You can also use AIChatUploadFileInfo
class instances in the BlazorChatMessage.Files property and the SendMessage(String, ChatRole, List<AIChatUploadFileInfo>) method.
@using DevExpress.AIIntegration.Blazor.Chat
@using System.IO
<DxAIChat @ref="Chat" />
<DxButton Text="Start dialog" Click="StartDialog" />
@code {
DxAIChat Chat { get; set; }
void StartDialog(MouseEventArgs args) {
var filePath = "C:\\Users\\Public\\AlbertEinstein.jpg";
var fileBytes = File.ReadAllBytes(filePath);
var testFiles = new List<AIChatUploadFileInfo> {
new AIChatUploadFileInfo(
name: Path.GetFileName(filePath),
type: "image/jpeg",
size: fileBytes.Length,
data: new ReadOnlyMemory<byte>(fileBytes)
)
};
Chat.SendMessage("What is on the image?", Microsoft.Extensions.AI.ChatRole.User, testFiles);
}
}
Supported File Types
The DevExpress Blazor AI Chat component allows users to upload files, which are then processed and interpreted by the connected AI model. The ability to understand a specific file format is a function of the selected Large Language Model (LLM), not the chat component. If the model does not support the uploaded file’s format, it will be unable to extract file content and will ignore it in responses. Check the specific LLM’s documentation to understand its file handling capabilities:
Some models understand multiple file formats, while others may be more limited. Therefore, for the file upload feature to be effective, the chosen LLM must be compatible with the file type uploaded by the user. To ensure compatibility between user-uploaded files and model capabilities, it is important to specify allowed file types. You can use the AllowedFileExtensions property to restrict uploads by file extension. Additionally, you can specify file types available for selection in the file dialog using the FileTypeFilter option.
If you need to support a file type that the selected AI model cannot natively handle, implement a manual pre-conversion step:
- Intercept the file upload.
- Decode file content to a supported format or extract it as plain text.
- Pass the converted content to the model for analysis.
The following code snippet extracts text from a Word or text file and passes it to the AI model as a chat message:
public class FilesToContextChatClient(IChatClient client) :
DelegatingChatClient(client) {
private IEnumerable<Microsoft.Extensions.AI.ChatMessage> ConvertDataFilesToTextContent(
IEnumerable<Microsoft.Extensions.AI.ChatMessage> messages) {
var result = new List<Microsoft.Extensions.AI.ChatMessage>();
foreach(var item in messages) {
for(int i = 0; i < item.Contents.Count; i++) {
if(item.Contents[i] is DataContent dataContent) {
switch(dataContent.MediaType) {
case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
item.Contents.Remove(dataContent);
string resultFileContent = string.Empty;
var data = dataContent.Data;
MemoryStream stream = new MemoryStream(data.ToArray());
stream.Seek(0, SeekOrigin.Begin);
using(var wordProcessor = new RichEditDocumentServer()) {
wordProcessor.LoadDocument(stream, DocumentFormat.Docx);
resultFileContent = wordProcessor.Document.Text;
}
result.Add(new Microsoft.Extensions.AI.ChatMessage(ChatRole.System,
"FileDATA: " + resultFileContent));
break;
case "text/plain":
item.Contents.Remove(dataContent);
string txtContent = Encoding.UTF8.GetString(dataContent.Data.ToArray());
result.Add(new Microsoft.Extensions.AI.ChatMessage(ChatRole.System,
"FileDATA: " + txtContent));
break;
default:
break;
}
}
}
result.Add(item);
}
return result;
}
public override Task<ChatResponse> GetResponseAsync(
IEnumerable<Microsoft.Extensions.AI.ChatMessage> messages,
ChatOptions options = null,
CancellationToken cancellationToken = default) {
return base.GetResponseAsync(ConvertDataFilesToTextContent(messages),
options,
cancellationToken);
}
public override IAsyncEnumerable<ChatResponseUpdate> GetStreamingResponseAsync(
IEnumerable<Microsoft.Extensions.AI.ChatMessage> messages,
ChatOptions options = null,
CancellationToken cancellationToken = default) {
return base.GetStreamingResponseAsync(ConvertDataFilesToTextContent(messages),
options,
cancellationToken);
}
}
You can extend the code to support additional formats such as Excel, PDF, and archives. To extract content from these files, use Office File API or similar libraries.
Note
The size of an uploaded file, regardless of its format, affects performance and cost because large files increase token consumption. To control this, use the MaxFileSize property to restrict the upload size for the DxAIChat component.