diff --git a/Interfaces/IXAiServiceBase.cs b/Interfaces/IXAiServiceBase.cs index 358208d..46a48cc 100644 --- a/Interfaces/IXAiServiceBase.cs +++ b/Interfaces/IXAiServiceBase.cs @@ -132,6 +132,8 @@ namespace xAiApi.Interfaces /// /// /// + /// + /// /// /// /// @@ -144,6 +146,8 @@ namespace xAiApi.Interfaces Guid? projectId = null, Guid? conversationId = null, string connectionId = null, + bool extractFileContents = false, + bool addFilesToKnowledge = true, IFormFileCollection files = null, XUserClaimsInfoDto userInfo = null, CancellationToken cancellationToken = default @@ -159,6 +163,8 @@ namespace xAiApi.Interfaces /// /// /// + /// + /// /// /// /// @@ -171,6 +177,8 @@ namespace xAiApi.Interfaces Guid? projectId = null, Guid? conversationId = null, string connectionId = null, + bool extractFileContents = false, + bool addFilesToKnowledge = true, IFormFileCollection files = null, XUserClaimsInfoDto userInfo = null, CancellationToken cancellationToken = default diff --git a/Providers/XAIServiceBase.cs b/Providers/XAIServiceBase.cs index 27893bd..9a404d5 100644 --- a/Providers/XAIServiceBase.cs +++ b/Providers/XAIServiceBase.cs @@ -27,6 +27,7 @@ using System.Collections.Generic; using Microsoft.Extensions.Logging; using xAiApi.Interfaces.Extractors; using System.Runtime.CompilerServices; +using System.IO; namespace xAiApi.Providers { @@ -389,7 +390,10 @@ namespace xAiApi.Providers connectionId: null, conversationId: null, persistPrompt: false, + additionalContents: null, + addFilesToKnowledge: false, resolveConversation: false, + extractFileContents: false, cancellationToken: cancellationToken ); @@ -470,7 +474,10 @@ namespace xAiApi.Providers connectionId: null, conversationId: null, persistPrompt: false, + additionalContents: null, resolveConversation: false, + addFilesToKnowledge: false, + extractFileContents: false, cancellationToken: cancellationToken ); @@ -522,6 +529,8 @@ namespace xAiApi.Providers /// /// /// + /// + /// /// /// /// @@ -534,6 +543,8 @@ namespace xAiApi.Providers Guid? projectId = null, Guid? conversationId = null, string connectionId = null, + bool extractFileContents = false, + bool addFilesToKnowledge = true, IFormFileCollection files = null, XUserClaimsInfoDto userInfo = null, CancellationToken cancellationToken = default @@ -558,10 +569,13 @@ namespace xAiApi.Providers userInfo: userInfo, persistPrompt: true, projectId: projectId, + additionalContents: null, resolveConversation: true, connectionId: connectionId, conversationId: conversationId, - cancellationToken: cancellationToken + cancellationToken: cancellationToken, + extractFileContents: extractFileContents, + addFilesToKnowledge: addFilesToKnowledge ); // @@ -625,6 +639,8 @@ namespace xAiApi.Providers /// /// /// + /// + /// /// /// /// @@ -637,6 +653,8 @@ namespace xAiApi.Providers Guid? projectId = null, Guid? conversationId = null, string connectionId = null, + bool extractFileContents = false, + bool addFilesToKnowledge = true, IFormFileCollection files = null, XUserClaimsInfoDto userInfo = null, [EnumeratorCancellation] @@ -662,10 +680,13 @@ namespace xAiApi.Providers userInfo: userInfo, persistPrompt: true, projectId: projectId, + additionalContents: null, resolveConversation: true, connectionId: connectionId, conversationId: conversationId, - cancellationToken: cancellationToken + cancellationToken: cancellationToken, + extractFileContents: extractFileContents, + addFilesToKnowledge: addFilesToKnowledge ); // @@ -1339,7 +1360,7 @@ namespace xAiApi.Providers if (!datetimeContext.IsNullOrEmpty()) { // - systemText += + systemText += $"{datetimeContext}{Environment.NewLine}{Environment.NewLine}"; } @@ -1465,17 +1486,20 @@ namespace xAiApi.Providers /// /// Retrieve Prepared Chat Context ... /// - /// - /// /// + /// + /// /// /// - /// + /// /// + /// + /// /// /// - /// - /// + /// + /// + /// /// /// private async Task PrepareChatRequirementsAsync( @@ -1490,6 +1514,8 @@ namespace xAiApi.Providers bool resolveConversation, IFormFileCollection files, XUserClaimsInfoDto userInfo, + bool extractFileContents = true, + bool addFilesToKnowledge = false, IList additionalContents = null, CancellationToken cancellationToken = default ) @@ -1580,7 +1606,8 @@ namespace xAiApi.Providers // Ingest Files if Provided and also Resolve Conversation Flagged ... if ( files.HasChild() && - resolveConversation + resolveConversation && + addFilesToKnowledge ) { // @@ -1623,6 +1650,53 @@ namespace xAiApi.Providers } } + // + // If there isn't Files need to add Files to Knowledge ... + if (files.HasChild() && + !addFilesToKnowledge + ) + { + // + // Extract File Content and add them to Additional Contents ... + foreach (var file in files) + { + // + var mimeType = file.ContentType?.ToLowerInvariant() ?? string.Empty; + if (mimeType.IsNullOrEmpty() || + !fileContentExtractor.CanExtract(mimeType)) + { + continue; + } + + // + using var stream = file.OpenReadStream(); + + // + // Extract File Content if necessary ... + if (extractFileContents) + { + // + var content = await fileContentExtractor.ExtractAsync( + fileStream: stream, + mimeType: mimeType, + cancellationToken: cancellationToken + ); + if (!content.IsNullOrEmpty()) + { + // + var fileContent = new TextContent($"File:{Environment.NewLine}{file.FileName}{Environment.NewLine}{content}"); + additionalContents = [.. additionalContents, fileContent]; + } + } + else + { + // + var fileContent = await DataContent.LoadFromAsync(stream, mimeType, cancellationToken); + additionalContents = [.. additionalContents, fileContent]; + } + } + } + // // Request Knowledge ... var knowledgeRequest = new XAiKnowledgeAskRequest