diff --git a/Controllers/KnowledgeController.cs b/Controllers/KnowledgeController.cs index 9488a27..b7af596 100644 --- a/Controllers/KnowledgeController.cs +++ b/Controllers/KnowledgeController.cs @@ -56,6 +56,7 @@ namespace xAiApi.Controllers [Consumes("multipart/form-data")] public async Task> Ingest( [FromForm] IFormFileCollection files, + [FromForm] string knowledgeType = null, [FromForm] bool replaceExisting = true, CancellationToken cancellationToken = default ) @@ -72,11 +73,14 @@ namespace xAiApi.Controllers // var userInfo = await GetUserInfo(); + var connectionId = GetConnectionId(); // var result = await ragService.IngestAsync( files: files, - ownerId: userInfo.UserId, + userInfo: userInfo, + connectionId: connectionId, + knowledgeType: knowledgeType, replaceExisting: replaceExisting, cancellationToken: cancellationToken ); @@ -123,7 +127,7 @@ namespace xAiApi.Controllers // var result = await ragService.AskWithRAGAsync( request: request, - ownerId: userInfo.UserId, + userInfo: userInfo, cancellationToken: cancellationToken ); @@ -180,7 +184,7 @@ namespace xAiApi.Controllers // var enumerable = ragService.AskWithRAGAsEnumerable( request: request, - ownerId: userInfo.UserId, + userInfo: userInfo, cancellationToken: cancellationToken ); @@ -242,7 +246,7 @@ namespace xAiApi.Controllers // var result = await ragService.LoadDocumentsAsync( - ownerId: userInfo.UserId, + userInfo: userInfo, cancellationToken: cancellationToken ); @@ -282,7 +286,7 @@ namespace xAiApi.Controllers // var result = await ragService.RemoveDocumentAsync( fileName: fileName, - ownerId: userInfo.UserId, + userInfo: userInfo, cancellationToken: cancellationToken ); diff --git a/Interfaces/IXAiRAGService.cs b/Interfaces/IXAiRAGService.cs index 5bbb921..fd4ce85 100644 --- a/Interfaces/IXAiRAGService.cs +++ b/Interfaces/IXAiRAGService.cs @@ -2,9 +2,9 @@ using xAiApi.Models; using System.Threading; using xAiModels.Models.Dtos; using System.Threading.Tasks; +using xIdentityModels.Models; using Microsoft.AspNetCore.Http; using System.Collections.Generic; -using xIdentityModels.Models; namespace xAiApi.Interfaces { @@ -83,24 +83,24 @@ namespace xAiApi.Interfaces /// /// List Ingested Documents of Owner ... /// - /// + /// /// /// Task> LoadDocumentsAsync( - string ownerId, + XUserClaimsInfoDto userInfo = null, CancellationToken cancellationToken = default ); /// /// Remove All Vectors of a Document ... /// - /// /// + /// /// /// Count of Removed Vectors Task RemoveDocumentAsync( - string ownerId, string fileName, + XUserClaimsInfoDto userInfo = null, CancellationToken cancellationToken = default ); diff --git a/Providers/XAiRAGService.cs b/Providers/XAiRAGService.cs index a9b87eb..03dbd41 100644 --- a/Providers/XAiRAGService.cs +++ b/Providers/XAiRAGService.cs @@ -10,13 +10,13 @@ using xAiApi.Configurations; using xExceptions.Constants; using xAiModels.Models.Dtos; using System.Threading.Tasks; +using xIdentityModels.Models; using xFileService.Interfaces; using Microsoft.AspNetCore.Http; using System.Collections.Generic; using Microsoft.Extensions.Logging; using xAiApi.Interfaces.Extractors; using System.Runtime.CompilerServices; -using xIdentityModels.Models; namespace xAiApi.Providers { @@ -249,6 +249,7 @@ namespace xAiApi.Providers ? GetOwnerPrefix(userInfo.UserId) : GetDocumentChunkPrefix(userInfo.UserId, fileName); var searchResult = await dataProvider.SearchVectorsAsync( + type: null, topK: effectiveTopK, identifierPrefix: prefix, minScore: effectiveMinScore, @@ -402,8 +403,11 @@ namespace xAiApi.Providers // #region Documents ... /// - /// List Ingested Documents of Owner (Grouped by File Name) ... + /// List Ingested Documents of Owner ... /// + /// + /// + /// public async Task> LoadDocumentsAsync( XUserClaimsInfoDto userInfo = null, CancellationToken cancellationToken = default @@ -426,19 +430,19 @@ namespace xAiApi.Providers // var rag = configuration.RAGDescriptor; var result = (vectors ?? []) - .GroupBy(v => GetMetadata(v.Metadata, rag.MetadataFileName) ?? v.Identifier) + .GroupBy(v => v.Metadata.GetMetadata(rag.MetadataFileName) ?? v.Identifier) .Select(g => { // var first = g.First(); - var ingestedText = GetMetadata(first.Metadata, XAiApiConstants.XAiKnowledgeMetadataIngestedAtUtc); + var ingestedText = first.Metadata.GetMetadata(XAiApiConstants.XAiKnowledgeMetadataIngestedAtUtc); // return new XAiKnowledgeDocumentInfo { FileName = g.Key, ChunksCount = g.Count(), - MimeType = GetMetadata(first.Metadata, rag.MetadataMimeType), + MimeType = first.Metadata.GetMetadata(rag.MetadataMimeType), IngestedAtUtc = DateTime.TryParse( ingestedText, null, System.Globalization.DateTimeStyles.RoundtripKind, out var dt ) ? dt : null @@ -454,15 +458,23 @@ namespace xAiApi.Providers /// /// Remove All Vectors of a Document ... /// + /// + /// + /// + /// Count of Removed Vectors public async Task RemoveDocumentAsync( - string ownerId, string fileName, + XUserClaimsInfoDto userInfo = null, CancellationToken cancellationToken = default ) { // // Validate ... - if (ownerId.IsNullOrEmpty() || fileName.IsNullOrEmpty()) + if ( + fileName.IsNullOrEmpty() || + userInfo.IsNullOrDefault() || + userInfo.UserId.IsNullOrEmpty() + ) { XException.InvalidArgs.Throw(); } @@ -470,7 +482,7 @@ namespace xAiApi.Providers // var result = await dataProvider.RemoveVectorsAsync( cancellationToken: cancellationToken, - identifierPrefix: GetDocumentChunkPrefix(ownerId, fileName) + identifierPrefix: GetDocumentChunkPrefix(userInfo.UserId, fileName) ); // @@ -480,6 +492,8 @@ namespace xAiApi.Providers /// /// Map Search Results to Public Sources ... /// + /// + /// public IList ToSources( IEnumerable context ) @@ -491,8 +505,8 @@ namespace xAiApi.Providers { // var text = c.Text ?? string.Empty; - var chunkIndexText = GetMetadata(c.Metadata, rag.MetadataChunkIndex); var snippetLength = Math.Min(text.Length, rag.SourceSnippetLength); + var chunkIndexText = c.Metadata.GetMetadata(rag.MetadataChunkIndex); // return new XAiDocumentSource @@ -500,7 +514,7 @@ namespace xAiApi.Providers Score = c.Score, Identifier = c.Identifier, Snippet = text.Substring(0, snippetLength), - FileName = GetMetadata(c.Metadata, rag.MetadataFileName), + FileName = c.Metadata.GetMetadata(rag.MetadataFileName), ChunkIndex = int.TryParse(chunkIndexText, out var idx) ? idx : null }; }) @@ -645,24 +659,6 @@ namespace xAiApi.Providers // return result; } - - /// - /// Safe Metadata Reader ... - /// - private static string GetMetadata( - IDictionary metadata, - string key - ) - { - // - if (metadata.IsNullOrDefault() || !metadata.TryGetValue(key, out var value)) - { - return null; - } - - // - return value; - } #endregion } }