diff --git a/Constants/XAiApiConstants.cs b/Constants/XAiApiConstants.cs index db96048..ad5a38e 100644 --- a/Constants/XAiApiConstants.cs +++ b/Constants/XAiApiConstants.cs @@ -26,6 +26,7 @@ namespace xAiApi.Constants // // Knowledge (Persistent RAG) ... + public const string XAiDefaultKnowledgeType = "XAiKnowledge"; public const string XAiKnowledgeSeparator = "/"; public const string XAiKnowledgeOwnerSeparator = "/"; public const string XAiKnowledgeChunkSeparator = "#"; diff --git a/Interfaces/IXAiRAGService.cs b/Interfaces/IXAiRAGService.cs index f47a7ef..5bbb921 100644 --- a/Interfaces/IXAiRAGService.cs +++ b/Interfaces/IXAiRAGService.cs @@ -4,6 +4,7 @@ using xAiModels.Models.Dtos; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using System.Collections.Generic; +using xIdentityModels.Models; namespace xAiApi.Interfaces { @@ -14,16 +15,21 @@ namespace xAiApi.Interfaces public interface IXAiRAGService { /// - /// Ingest Files into Owner's Knowledge Base ... + /// Ingest Files into Owner's Knowledge Base: + /// Extract -> Chunk -> Embed -> Store (XAiVector) ... /// /// - /// - /// Remove Previously Stored Vectors of Same File before Store + /// + /// + /// + /// /// /// Task IngestAsync( IFormFileCollection files, - string ownerId, + string knowledgeType = null, + XUserClaimsInfoDto userInfo = null, + string connectionId = null, bool replaceExisting = true, CancellationToken cancellationToken = default ); @@ -32,7 +38,7 @@ namespace xAiApi.Interfaces /// Embed Prompt and Retrieve Relevant Chunks from Owner's Knowledge Base ... /// /// - /// + /// /// /// /// Optional: Restrict to a Specific File @@ -40,7 +46,7 @@ namespace xAiApi.Interfaces /// Task> RetrieveAsync( string prompt, - string ownerId, + XUserClaimsInfoDto userInfo = null, int? topK = null, float? minScore = null, string fileName = null, @@ -51,12 +57,12 @@ namespace xAiApi.Interfaces /// Ask a Question with RAG (Retrieve from DB then Generate) ... /// /// - /// + /// /// /// Task AskWithRAGAsync( XAiKnowledgeAskRequest request, - string ownerId, + XUserClaimsInfoDto userInfo = null, CancellationToken cancellationToken = default ); @@ -65,12 +71,12 @@ namespace xAiApi.Interfaces /// First Item Carries Sources, Next Items Carry Text Deltas, Last Item has IsFinal = true ... /// /// - /// + /// /// /// IAsyncEnumerable AskWithRAGAsEnumerable( XAiKnowledgeAskRequest request, - string ownerId, + XUserClaimsInfoDto userInfo = null, CancellationToken cancellationToken = default ); diff --git a/Providers/XAiEmbeddingServiceBase.cs b/Providers/XAiEmbeddingServiceBase.cs index 97e9d35..dd12d20 100644 --- a/Providers/XAiEmbeddingServiceBase.cs +++ b/Providers/XAiEmbeddingServiceBase.cs @@ -7,7 +7,7 @@ using xAiApi.Constants; using xAiModels.Models; using xAiApi.Extensions; using xAiApi.Interfaces; -using xAiService.Helpers; +using xAiModels.Helpers; using System.ClientModel; using xCommons.Providers; using xCommons.Extensions; diff --git a/Providers/XAiRAGService.cs b/Providers/XAiRAGService.cs index b21ffb3..d388b18 100644 --- a/Providers/XAiRAGService.cs +++ b/Providers/XAiRAGService.cs @@ -16,6 +16,7 @@ using System.Collections.Generic; using Microsoft.Extensions.Logging; using xAiApi.Interfaces.Extractors; using System.Runtime.CompilerServices; +using xIdentityModels.Models; namespace xAiApi.Providers { @@ -62,21 +63,37 @@ namespace xAiApi.Providers /// Ingest Files into Owner's Knowledge Base: /// Extract -> Chunk -> Embed -> Store (XAiVector) ... /// + /// + /// + /// + /// + /// + /// + /// public async Task IngestAsync( IFormFileCollection files, - string ownerId, + string knowledgeType = null, + XUserClaimsInfoDto userInfo = null, + string connectionId = null, bool replaceExisting = true, CancellationToken cancellationToken = default ) { // // Validate ... - if (!files.HasChild() || ownerId.IsNullOrEmpty()) + if (!files.HasChild() || userInfo.IsNullOrDefault()) { XException.InvalidArgs.Throw(); } // + // Normalize ... + if (knowledgeType.IsNullOrEmpty()) + { + knowledgeType = XAiApiConstants.XAiDefaultKnowledgeType; + } + + // var result = new XAiKnowledgeIngestResult(); var ingestedAtUtc = DateTime.UtcNow.ToString("O"); @@ -111,7 +128,7 @@ namespace xAiApi.Providers // // Chunk with Owner Scoped Source => Identifier = "{ownerId}/{fileName}#{index}" ... - var documentPrefix = GetDocumentPrefix(ownerId, file.FileName); + var documentPrefix = GetDocumentPrefix(userInfo.UserId, file.FileName); var chunks = textChunker.Chunk(text, documentPrefix); if (!chunks.HasChild()) { @@ -120,15 +137,22 @@ namespace xAiApi.Providers } // - // TODO: Implement File Save ... + // Handle File Save ... + await fileProvider.Upload( + file: file, + userInfo: userInfo, + connectionId: connectionId, + cancellationToken: cancellationToken + ); // // Embed in Batches ... var vectors = await EmbedChunks( chunks: chunks, - ownerId: ownerId, mimeType: mimeType, + type: knowledgeType, fileName: file.FileName, + ownerId: userInfo.UserId, ingestedAtUtc: ingestedAtUtc, cancellationToken: cancellationToken ); @@ -139,8 +163,8 @@ namespace xAiApi.Providers { // result.ReplacedCount += await dataProvider.RemoveVectorsAsync( - identifierPrefix: GetDocumentChunkPrefix(ownerId, file.FileName), - cancellationToken: cancellationToken + cancellationToken: cancellationToken, + identifierPrefix: GetDocumentChunkPrefix(userInfo.UserId, file.FileName) ); } @@ -165,7 +189,7 @@ namespace xAiApi.Providers // logger.LogInformation( "Knowledge: Ingested {FileName} for {OwnerId} with {Chunks} Chunks", - file.FileName, ownerId, storedCount + file.FileName, userInfo.UserId, storedCount ); } @@ -180,9 +204,16 @@ namespace xAiApi.Providers /// /// Embed Prompt and Retrieve Relevant Chunks from Owner's Knowledge Base ... /// + /// + /// + /// + /// + /// Optional: Restrict to a Specific File + /// + /// public async Task> RetrieveAsync( string prompt, - string ownerId, + XUserClaimsInfoDto userInfo = null, int? topK = null, float? minScore = null, string fileName = null, @@ -191,7 +222,7 @@ namespace xAiApi.Providers { // // Validate ... - if (prompt.IsNullOrEmpty() || ownerId.IsNullOrEmpty()) + if (prompt.IsNullOrEmpty() || userInfo.IsNullOrDefault()) { XException.InvalidArgs.Throw(); } @@ -215,8 +246,8 @@ namespace xAiApi.Providers // // Search in DB (Scoped to Owner or Owner + File) ... var prefix = fileName.IsNullOrEmpty() - ? GetOwnerPrefix(ownerId) - : GetDocumentChunkPrefix(ownerId, fileName); + ? GetOwnerPrefix(userInfo.UserId) + : GetDocumentChunkPrefix(userInfo.UserId, fileName); var searchResult = await dataProvider.SearchVectorsAsync( topK: effectiveTopK, identifierPrefix: prefix, @@ -233,9 +264,13 @@ namespace xAiApi.Providers /// /// Ask a Question with RAG (Retrieve from DB then Generate) ... /// + /// + /// + /// + /// public async Task AskWithRAGAsync( XAiKnowledgeAskRequest request, - string ownerId, + XUserClaimsInfoDto userInfo = null, CancellationToken cancellationToken = default ) { @@ -249,7 +284,7 @@ namespace xAiApi.Providers // // Retrieve ... var context = await RetrieveAsync( - ownerId: ownerId, + userInfo: userInfo, topK: request.TopK, prompt: request.Prompt, minScore: request.MinScore, @@ -283,11 +318,17 @@ namespace xAiApi.Providers /// /// Ask a Question with RAG and Stream Response ... + /// First Item Carries Sources, Next Items Carry Text Deltas, Last Item has IsFinal = true ... /// + /// + /// + /// + /// public async IAsyncEnumerable AskWithRAGAsEnumerable( XAiKnowledgeAskRequest request, - string ownerId, - [EnumeratorCancellation] CancellationToken cancellationToken = default + XUserClaimsInfoDto userInfo = null, + [EnumeratorCancellation] + CancellationToken cancellationToken = default ) { // @@ -300,7 +341,7 @@ namespace xAiApi.Providers // // Retrieve ... var context = await RetrieveAsync( - ownerId: ownerId, + userInfo: userInfo, topK: request.TopK, prompt: request.Prompt, minScore: request.MinScore, @@ -545,6 +586,7 @@ namespace xAiApi.Providers /// private async Task> EmbedChunks( IList chunks, + string type, string ownerId, string fileName, string mimeType, @@ -583,7 +625,8 @@ namespace xAiApi.Providers var chunk = batch[i]; result.Add(new XAiVectorDto { - Id = Guid.NewGuid(), + // + Type = type, Text = chunk.Text, Embedding = embeddings[i], Identifier = chunk.Identifier, diff --git a/Providers/XInMemoryVectorStore.cs b/Providers/XInMemoryVectorStore.cs index d00f63e..342cf9f 100644 --- a/Providers/XInMemoryVectorStore.cs +++ b/Providers/XInMemoryVectorStore.cs @@ -2,7 +2,7 @@ using System; using System.Linq; using xAiApi.Models; using xAiApi.Interfaces; -using xAiService.Helpers; +using xAiModels.Helpers; using xAiModels.Models.Dtos; using System.Collections.Generic; using System.Collections.Concurrent;