From c8ad34944a9b68440d67f3a1dc2d8c94c2fd9fe6 Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Wed, 21 Oct 2026 17:54:52 +0330 Subject: [PATCH] last ... --- Configurations/XAiModelMappersProfile.cs | 7 +- DI/XDIHelperExtension.cs | 54 +++++ Extensions/XEmbeddingExtensions.cs | 58 +++++ Helpers/XAiModelsHelper.cs | 267 ++++++++++++++++++++++ Models/Dtos/XAiVectorDto.cs | 7 +- Models/Dtos/XAiVectorSearchResult.cs | 1 + Models/Entities/XAiDocument.cs | 61 ----- Models/Entities/XAiVector.cs | 22 +- Providers/Enrichers/XAiVectorEnricher.cs | 8 + Providers/XAiDataProvider.cs | 277 ++++++++++++++++++++++- 10 files changed, 692 insertions(+), 70 deletions(-) create mode 100644 Extensions/XEmbeddingExtensions.cs delete mode 100644 Models/Entities/XAiDocument.cs diff --git a/Configurations/XAiModelMappersProfile.cs b/Configurations/XAiModelMappersProfile.cs index 42801b4..6c81e4d 100644 --- a/Configurations/XAiModelMappersProfile.cs +++ b/Configurations/XAiModelMappersProfile.cs @@ -14,7 +14,12 @@ namespace xAiModels.Configurations // // XAiVector ... - CreateMap(); + CreateMap() + .ForMember(x => x.Embedding, opt => opt.Ignore()) + .ForMember(x => x.Metadata, opt => opt.Ignore()); + CreateMap() + .ForMember(x => x.Embedding, opt => opt.Ignore()) + .ForMember(x => x.Metadata, opt => opt.Ignore()); // // XAiProject ... diff --git a/DI/XDIHelperExtension.cs b/DI/XDIHelperExtension.cs index 3e9d9a4..f4c010b 100644 --- a/DI/XDIHelperExtension.cs +++ b/DI/XDIHelperExtension.cs @@ -85,6 +85,11 @@ namespace xAiModels.DI // var pushHelper = new XPushServiceHelper(); + // + // XAiVector ... + pushHelper.AddHub(XAiModelsConstants.XAiVectorDtoHub); + pushHelper.AddHub(XAiModelsConstants.XAiVectorEntityHub); + // // XAiMessage ... pushHelper.AddHub(XAiModelsConstants.XAiMessageDtoHub); @@ -245,6 +250,7 @@ namespace xAiModels.DI // #region Private ... + private static XRepositoryDescriptor aiVectorDescriptor = null; private static XRepositoryDescriptor aiMessageDescriptor = null; private static XRepositoryDescriptor aiProjectDescriptor = null; private static XRepositoryDescriptor aiConversationDescriptor = null; @@ -322,6 +328,15 @@ namespace xAiModels.DI { // case XRepositoryType.EF: + // + // XAiVector ... + aiVectorDescriptor = typeof(XAiModelsHelper) + .InvokeGenericMethod( + args: null, + runtimeType: contextType, + methodName: XAiModelsConstants.GetXAiVectorEFRepositoryDescriptor + ); + // // XAiMessage ... aiMessageDescriptor = typeof(XAiModelsHelper) @@ -352,6 +367,10 @@ namespace xAiModels.DI // case XRepositoryType.Mongo: + // + // XAiVector ... + aiVectorDescriptor = XAiModelsHelper.GetXAiVectorMongoRepositoryDescriptor(); + // // XAiMessage ... aiMessageDescriptor = XAiModelsHelper.GetXAiMessageMongoRepositoryDescriptor(); @@ -367,6 +386,10 @@ namespace xAiModels.DI // case XRepositoryType.InMemory: + // + // XAiVector ... + aiVectorDescriptor = XAiModelsHelper.GetXAiVectorInMemoryRepositoryDescriptor(); + // // XAiMessage ... aiMessageDescriptor = XAiModelsHelper.GetXAiMessageInMemoryRepositoryDescriptor(); @@ -383,6 +406,17 @@ namespace xAiModels.DI // #region Register Repository Descriptor ... + // + // XAiVector ... + if (!aiVectorDescriptor.IsNull()) + { + // + services.AddXRepository( + lifeTime: lifeTime, + descriptor: aiVectorDescriptor + ); + } + // // XAiMessage ... if (!aiMessageDescriptor.IsNull()) @@ -419,6 +453,26 @@ namespace xAiModels.DI // #region Register Services ... + // + #region AiVector ... + // + services.Add( + new ServiceDescriptor(typeof(IXAiVectorServiceProvider), typeof(XAiVectorServiceProvider), lifeTime) + ); + services.Add( + new ServiceDescriptor(typeof(IXAiVectorRepositoryService), typeof(XAiVectorRepositoryService), lifeTime) + ); + services.Add( + new ServiceDescriptor(typeof(IXAiVectorRepositoryProvider), typeof(XAiVectorRepositoryProvider), lifeTime) + ); + + // + // Enrichers ... + services.Add( + new ServiceDescriptor(typeof(IXAiVectorEnricher), typeof(XAiVectorEnricher), lifeTime) + ); + #endregion + // #region AiMessage ... // diff --git a/Extensions/XEmbeddingExtensions.cs b/Extensions/XEmbeddingExtensions.cs new file mode 100644 index 0000000..02c035e --- /dev/null +++ b/Extensions/XEmbeddingExtensions.cs @@ -0,0 +1,58 @@ +using xCommons.Extensions; +using System.Collections.Generic; + +namespace xAiModels.Extensions +{ + public static class XEmbeddingExtensions + { + /// + /// Converts String to Embeddings JSON ... + /// + /// + /// + public static float[] AsEmbedding(this string source) + { + // + // Validate ... + if (source.IsNullOrEmpty()) { + return []; + } + + // + // JSON is expected to be like: [0.12, 0.34, ...] ... + var result = source.FromJSON(); + if (!result.HasChild()) + { + result = []; + } + + // + return result; + } + + /// + /// Converts to Meta Data Representation ... + /// + /// + /// + public static Dictionary AsMetadat(this string source) + { + // + // Validate ... + if (source.IsNullOrEmpty()) { + return []; + } + + // + // JSON is expected to be like: [0.12, 0.34, ...] ... + var result = source.FromJSON>(); + if (!result.HasChild()) + { + result = []; + } + + // + return result; + } + } +} \ No newline at end of file diff --git a/Helpers/XAiModelsHelper.cs b/Helpers/XAiModelsHelper.cs index 596bccb..31807c4 100644 --- a/Helpers/XAiModelsHelper.cs +++ b/Helpers/XAiModelsHelper.cs @@ -811,6 +811,273 @@ namespace xAiModels.Helpers return result; } #endregion + #endregion + + // + #region XAiVector ... + // + #region EF Repository Descriptor Provider(s) ... + public static XEFRepositoryDescriptor< + XAiVector, + Guid, + IXAiVectorKeyGenerator, + XAiVectorKeyGenerator, + IXAiVectorRepositoryEvents, + XAiVectorRepositoryEvents, + XAiVectorGraphType, + GuidGraphType, + XAiVectorGraphQuery, + IXAiVectorGraphQLTypeHelper, + XAiVectorGraphQLTypeHelper, + XAiVectorGraphSchema, + IXAiVectorRepository, + XAiVectorEFRepository, + TContext + > GetXAiVectorEFRepositoryDescriptor() + where TContext : XDbContext + { + // + var result = new XEFRepositoryDescriptor< + XAiVector, + Guid, + IXAiVectorKeyGenerator, + XAiVectorKeyGenerator, + IXAiVectorRepositoryEvents, + XAiVectorRepositoryEvents, + XAiVectorGraphType, + GuidGraphType, + XAiVectorGraphQuery, + IXAiVectorGraphQLTypeHelper, + XAiVectorGraphQLTypeHelper, + XAiVectorGraphSchema, + IXAiVectorRepository, + XAiVectorEFRepository, + TContext + >(); + + // + return result; + } + + public static XEFRepositoryDescriptor< + XAiVector, + Guid, + IXAiVectorKeyGenerator, + XAiVectorKeyGenerator, + IXAiVectorRepositoryEvents, + XAiVectorRepositoryEvents, + XAiVectorGraphType, + GuidGraphType, + XAiVectorGraphQuery, + IXAiVectorGraphQLTypeHelper, + XAiVectorGraphQLTypeHelper, + XAiVectorGraphSchema, + IXAiVectorRepository, + XAiVectorEFRepository, + TContext, + IXAiVectorSeeder, + TSeederImplementation + > GetXAiVectorEFRepositoryDescriptor() + where TContext : XDbContext + where TSeederImplementation : XBaseDbSeeder + { + // + var result = new XEFRepositoryDescriptor< + XAiVector, + Guid, + IXAiVectorKeyGenerator, + XAiVectorKeyGenerator, + IXAiVectorRepositoryEvents, + XAiVectorRepositoryEvents, + XAiVectorGraphType, + GuidGraphType, + XAiVectorGraphQuery, + IXAiVectorGraphQLTypeHelper, + XAiVectorGraphQLTypeHelper, + XAiVectorGraphSchema, + IXAiVectorRepository, + XAiVectorEFRepository, + TContext, + IXAiVectorSeeder, + TSeederImplementation + >(); + + // + return result; + } + #endregion + + // + #region Mongo Repository Descriptor Provider(s) ... + public static XMongoRepositoryDescriptor< + XAiVector, + Guid, + IXAiVectorKeyGenerator, + XAiVectorKeyGenerator, + IXAiVectorRepositoryEvents, + XAiVectorRepositoryEvents, + XAiVectorGraphType, + GuidGraphType, + XAiVectorGraphQuery, + IXAiVectorGraphQLTypeHelper, + XAiVectorGraphQLTypeHelper, + XAiVectorGraphSchema, + IXAiVectorRepository, + XAiVectorMongoRepository + > GetXAiVectorMongoRepositoryDescriptor() + { + // + var result = new XMongoRepositoryDescriptor< + XAiVector, + Guid, + IXAiVectorKeyGenerator, + XAiVectorKeyGenerator, + IXAiVectorRepositoryEvents, + XAiVectorRepositoryEvents, + XAiVectorGraphType, + GuidGraphType, + XAiVectorGraphQuery, + IXAiVectorGraphQLTypeHelper, + XAiVectorGraphQLTypeHelper, + XAiVectorGraphSchema, + IXAiVectorRepository, + XAiVectorMongoRepository + >(); + + // + return result; + } + + public static XMongoRepositoryDescriptor< + XAiVector, + Guid, + IXAiVectorKeyGenerator, + XAiVectorKeyGenerator, + IXAiVectorRepositoryEvents, + XAiVectorRepositoryEvents, + XAiVectorGraphType, + GuidGraphType, + XAiVectorGraphQuery, + IXAiVectorGraphQLTypeHelper, + XAiVectorGraphQLTypeHelper, + XAiVectorGraphSchema, + IXAiVectorRepository, + XAiVectorMongoRepository, + IXAiVectorSeeder, + TSeederImplementation + > GetXAiVectorMongoRepositoryDescriptor() + where TSeederImplementation : XBaseDbSeeder + { + // + var result = new XMongoRepositoryDescriptor< + XAiVector, + Guid, + IXAiVectorKeyGenerator, + XAiVectorKeyGenerator, + IXAiVectorRepositoryEvents, + XAiVectorRepositoryEvents, + XAiVectorGraphType, + GuidGraphType, + XAiVectorGraphQuery, + IXAiVectorGraphQLTypeHelper, + XAiVectorGraphQLTypeHelper, + XAiVectorGraphSchema, + IXAiVectorRepository, + XAiVectorMongoRepository, + IXAiVectorSeeder, + TSeederImplementation + >(); + + // + return result; + } + #endregion + + // + #region InMemory Repository Descriptor Provider(s) ... + public static XInMemoryRepositoryDescriptor< + XAiVector, + Guid, + IXAiVectorKeyGenerator, + XAiVectorKeyGenerator, + IXAiVectorRepositoryEvents, + XAiVectorRepositoryEvents, + XAiVectorGraphType, + GuidGraphType, + XAiVectorGraphQuery, + IXAiVectorGraphQLTypeHelper, + XAiVectorGraphQLTypeHelper, + XAiVectorGraphSchema, + IXAiVectorRepository, + XAiVectorInMemoryRepository + > GetXAiVectorInMemoryRepositoryDescriptor() + { + // + var result = new XInMemoryRepositoryDescriptor< + XAiVector, + Guid, + IXAiVectorKeyGenerator, + XAiVectorKeyGenerator, + IXAiVectorRepositoryEvents, + XAiVectorRepositoryEvents, + XAiVectorGraphType, + GuidGraphType, + XAiVectorGraphQuery, + IXAiVectorGraphQLTypeHelper, + XAiVectorGraphQLTypeHelper, + XAiVectorGraphSchema, + IXAiVectorRepository, + XAiVectorInMemoryRepository + >(); + + // + return result; + } + + public static XInMemoryRepositoryDescriptor< + XAiVector, + Guid, + IXAiVectorKeyGenerator, + XAiVectorKeyGenerator, + IXAiVectorRepositoryEvents, + XAiVectorRepositoryEvents, + XAiVectorGraphType, + GuidGraphType, + XAiVectorGraphQuery, + IXAiVectorGraphQLTypeHelper, + XAiVectorGraphQLTypeHelper, + XAiVectorGraphSchema, + IXAiVectorRepository, + XAiVectorInMemoryRepository, + IXAiVectorSeeder, + TSeederImplementation + > GetXAiVectorInMemoryRepositoryDescriptor() + where TSeederImplementation : XBaseDbSeeder + { + // + var result = new XInMemoryRepositoryDescriptor< + XAiVector, + Guid, + IXAiVectorKeyGenerator, + XAiVectorKeyGenerator, + IXAiVectorRepositoryEvents, + XAiVectorRepositoryEvents, + XAiVectorGraphType, + GuidGraphType, + XAiVectorGraphQuery, + IXAiVectorGraphQLTypeHelper, + XAiVectorGraphQLTypeHelper, + XAiVectorGraphSchema, + IXAiVectorRepository, + XAiVectorInMemoryRepository, + IXAiVectorSeeder, + TSeederImplementation + >(); + + // + return result; + } + #endregion #endregion } } \ No newline at end of file diff --git a/Models/Dtos/XAiVectorDto.cs b/Models/Dtos/XAiVectorDto.cs index 65dac37..4363a23 100644 --- a/Models/Dtos/XAiVectorDto.cs +++ b/Models/Dtos/XAiVectorDto.cs @@ -14,10 +14,15 @@ namespace xAiModels.Models.Dtos /// public string Text { get; set; } + /// + /// Doc and Chunk Identifier ... + /// + public string Identifier { get; set; } + /// /// Embedding vector representation ... /// - public ReadOnlyMemory Embedding { get; set; } + public virtual ReadOnlyMemory Embedding { get; set; } = null; /// /// Metadata associated with the vector ... diff --git a/Models/Dtos/XAiVectorSearchResult.cs b/Models/Dtos/XAiVectorSearchResult.cs index 316d36d..0e3feef 100644 --- a/Models/Dtos/XAiVectorSearchResult.cs +++ b/Models/Dtos/XAiVectorSearchResult.cs @@ -8,6 +8,7 @@ namespace xAiModels.Models.Dtos public Guid Id { get; set; } public string Text { get; set; } public float Score { get; set; } + public string Identifier { get; set; } public Dictionary Metadata { get; set; } = null; } } \ No newline at end of file diff --git a/Models/Entities/XAiDocument.cs b/Models/Entities/XAiDocument.cs deleted file mode 100644 index d68ac67..0000000 --- a/Models/Entities/XAiDocument.cs +++ /dev/null @@ -1,61 +0,0 @@ -using System; -using System.ComponentModel.DataAnnotations; -using xModels.Base; - -namespace xAiModels.Models.Entities -{ - /// - /// Represents an embedd document in the AI system. - /// - public class XAiDocument : XBaseGuidIDEntity - { - /// - /// User Identifier ... - /// - /// - [Required] - [StringLength(255)] - public string OwnerId { get; set; } - - /// - /// Title of Document ... - /// - [StringLength(255)] - public string Title { get; set; } - - /// - /// Content of Document ... - /// - public string Content { get; set; } - - /// - /// Hash of the Document Content ... - /// - public string ContentHash { get; set; } = string.Empty; - - /// - /// Embedding Model Identifier ... - /// - public string EmbeddingModel { get; set; } = string.Empty; - - /// - /// Dimensions of the Embedding ... - /// - public int Dimensions { get; set; } - - /// - /// Vector representation of the Document ... - /// - public byte[] Vector { get; set; } = []; - - /// - /// Created Time ... - /// - public DateTime CreatedOn { get; set; } - - /// - /// Updated Time ... - /// - public DateTime UpdatedAt { get; set; } - } -} \ No newline at end of file diff --git a/Models/Entities/XAiVector.cs b/Models/Entities/XAiVector.cs index 9802b50..a1915cf 100644 --- a/Models/Entities/XAiVector.cs +++ b/Models/Entities/XAiVector.cs @@ -1,13 +1,27 @@ -using System; using xModels.Base; -using System.Collections.Generic; namespace xAiModels.Models.Entities { public class XAiVector : XBaseGuidIDEntity { + /// + /// Thge Content Which Embedded ... + /// public string Text { get; set; } - public ReadOnlyMemory Embedding { get; set; } - public virtual Dictionary Metadata { get; set; } = null; + + /// + /// Doc and Chunk Identifier ... + /// + public string Identifier { get; set; } + + /// + /// a Json String which Represents IReadOnlyList of float ... + /// + public string Embedding { get; set; } + + /// + /// a Json String Which Represents MetaData ... + /// + public string Metadata { get; set; } = string.Empty; } } \ No newline at end of file diff --git a/Providers/Enrichers/XAiVectorEnricher.cs b/Providers/Enrichers/XAiVectorEnricher.cs index 14544ef..cf10670 100644 --- a/Providers/Enrichers/XAiVectorEnricher.cs +++ b/Providers/Enrichers/XAiVectorEnricher.cs @@ -1,6 +1,7 @@ using System.Linq; using System.Threading; using xCommons.Extensions; +using xAiModels.Extensions; using xAiModels.Models.Dtos; using System.Threading.Tasks; using xAiModels.Models.Entities; @@ -30,6 +31,13 @@ namespace xAiModels.Providers.Enrichers // // Mapping Default ... var result = source.MapConvert(); + + // + // Here we Converts Embeddings ... + result.Metadata = source.Metadata.AsMetadat(); + result.Embedding = source.Embedding.AsEmbedding(); + + // result = await Enrich( source: result, cancellationToken: cancellationToken diff --git a/Providers/XAiDataProvider.cs b/Providers/XAiDataProvider.cs index d5dadc5..14c98a4 100644 --- a/Providers/XAiDataProvider.cs +++ b/Providers/XAiDataProvider.cs @@ -12,12 +12,14 @@ using System.Threading.Tasks; using xCommons.Configurations; using System.Linq.Expressions; using xDataService.Extensions; +using System.Numerics.Tensors; using xAiModels.Providers.Hubs; using xAiModels.Interfaces.Dtos; using xStringService.Interfaces; using xAiModels.Models.Entities; using System.Collections.Generic; using Microsoft.AspNetCore.SignalR; +using xAiModels.Interfaces.Entities; using xAiModels.Interfaces.Enrichers; namespace xAiModels.Providers @@ -30,11 +32,14 @@ namespace xAiModels.Providers // #region Properties ... private readonly IXStringProvider stringProvider; + private readonly IXAiVectorEnricher vectorEnricher; private readonly XAppConfiguration appConfiguration; private readonly IHubContext hub; private readonly IXAiMessageEnricher messageEnricher; private readonly IXAiProjectEnricher projectEnricher; + private readonly IXAiVectorRepository vectorRepository; private readonly IXAiConversationEnricher conversationEnricher; + private readonly IXAiVectorRepositoryService vectorRepositoryService; private readonly IXAiMessageRepositoryService messageRepositoryService; private readonly IXAiProjectRepositoryService projectRepositoryService; private readonly IXAiProjectTitleResourceProvider projectTitleResourceProvider; @@ -45,11 +50,14 @@ namespace xAiModels.Providers public XAiDataProvider( IXStringProvider stringProvider, + IXAiVectorEnricher vectorEnricher, XAppConfiguration appConfiguration, IHubContext hub, IXAiProjectEnricher projectEnricher, IXAiMessageEnricher messageEnricher, + IXAiVectorRepository vectorRepository, IXAiConversationEnricher conversationEnricher, + IXAiVectorRepositoryService vectorRepositoryService, IXAiMessageRepositoryService messageRepositoryService, IXAiProjectRepositoryService projectRepositoryService, IXAiProjectTitleResourceProvider projectTitleResourceProvider, @@ -60,10 +68,13 @@ namespace xAiModels.Providers { this.hub = hub; this.stringProvider = stringProvider; + this.vectorEnricher = vectorEnricher; this.projectEnricher = projectEnricher; this.messageEnricher = messageEnricher; this.appConfiguration = appConfiguration; + this.vectorRepository = vectorRepository; this.conversationEnricher = conversationEnricher; + this.vectorRepositoryService = vectorRepositoryService; this.messageRepositoryService = messageRepositoryService; this.projectRepositoryService = projectRepositoryService; this.projectTitleResourceProvider = projectTitleResourceProvider; @@ -1216,7 +1227,7 @@ namespace xAiModels.Providers // return result; } - + /// /// Query Messages ... /// @@ -1283,12 +1294,245 @@ namespace xAiModels.Providers } // - return result; - } + return result; + } + #endregion + + // + #region Vectors ... + /// + /// Insert or Update ... + /// + /// + /// + /// + /// + /// + /// + public async Task UpsertAsync( + string identifier, + string text, + ReadOnlyMemory embedding, + Dictionary metadata = null, + CancellationToken cancellationToken = default + ) + { + // + // Validate ... + if ( + embedding.IsEmpty || + text.IsNullOrEmpty() || + identifier.IsNullOrEmpty() + ) + { + XException.InvalidArgs.Throw(); + } + + // + var exists = await vectorRepository.FindOneAsync( + includeBuilder: null, + ignoreSoftDeleteds: true, + cancellationToken: cancellationToken, + predicate: x => x.Identifier == identifier + ); + var isExists = !exists.IsNullOrDefault(); + if (isExists) + { + // + exists.Text = text; + exists.Embedding = embedding.ToJSON(); + } + else + { + // + exists = new XAiVector + { + Text = text, + Identifier = identifier, + Metadata = metadata.ToJSON(), + Embedding = embedding.ToJSON(), + }; + } + + // + await vectorRepository.AddOrUpdateAsync( + item: exists, + saveChanges: true, + cancellationToken: cancellationToken + ); + } + + /// + /// Search Cosine Similarity based on Top K ... + /// + /// + /// + /// + /// + public async Task> SearchAsync( + ReadOnlyMemory queryEmbedding, + int topK = 5, + CancellationToken cancellationToken = default + ) + { + // + // Validate ... + if ( + topK <= 0 || + queryEmbedding.IsEmpty + ) + { + XException.InvalidArgs.Throw(); + } + + // + // Converts Question Vector to Vector Array ... + var queryVector = queryEmbedding.ToArray(); + var scoredResult = new List<(XAiVectorSearchResult Result, float Score)>(); + + // + var vectorsEnumerator = vectorRepository.GetAllAsAsyncEnumerable(); + await foreach (var vector in vectorsEnumerator) + { + // + // Validate Vector ... + if (vector.Embedding.IsNullOrEmpty()) + { + continue; + } + + // + // Check Cancellation Token ... + if (cancellationToken.IsCancellationRequested) + { + break; + } + + // + // Ignore Unsame Vectors ... + var embd = vector.Embedding.AsEmbedding(); + if ( + embd.IsNull() || + embd.Length != queryEmbedding.Length + ) + { + continue; + } + + // + // Calculate Cosine Similarity Score ... + var score = ComputeCosineSimilarity(queryVector, embd); + + // + scoredResult.Add((new XAiVectorSearchResult + { + // + Score = score, + Id = vector.Id, + Text = vector.Text, + Identifier = vector.Identifier, + Metadata = vector.Metadata.AsMetadat(), + }, score)); + } + + // + var result = scoredResult + .OrderByDescending(x => x.Score) + .Take(topK) + .Select(x => x.Result) + .ToList(); + + // + return result; + } #endregion // #region Enrichers ... + /// + /// Enrich Specified Item ... + /// + /// + /// + /// + public async Task Enrich( + XAiVector source, + CancellationToken cancellationToken = default + ) + { + // + var result = await vectorEnricher.Enrich( + source: source, + cancellationToken: cancellationToken + ); + + // + return result; + } + + /// + /// Enrich Specified Items ... + /// + /// + /// + /// + public async Task> EnrichRange( + IEnumerable sources, + CancellationToken cancellationToken = default + ) + { + // + var result = await vectorEnricher.EnrichRange( + sources: sources, + cancellationToken: cancellationToken + ); + + // + return result; + } + + /// + /// Enrich Specified Item ... + /// + /// + /// + /// + public async Task Enrich( + XAiVectorDto source, + CancellationToken cancellationToken = default + ) + { + // + var result = await vectorEnricher.Enrich( + source: source, + cancellationToken: cancellationToken + ); + + // + return result; + } + + /// + /// Enrich Specified Items ... + /// + /// + /// + /// + public async Task> EnrichRange( + IEnumerable sources, + CancellationToken cancellationToken = default + ) + { + // + var result = await vectorEnricher.EnrichRange( + sources: sources, + cancellationToken: cancellationToken + ); + + // + return result; + } + /// /// Enrich Specified Item ... /// @@ -1848,6 +2092,33 @@ namespace xAiModels.Providers } catch { } } + + /// + /// Search Similarity ... + /// + /// + /// + /// + private float ComputeCosineSimilarity(float[] a, float[] b) + { + // + // Validate ... + if (a.Length != b.Length) + { + XException.InvalidArgs.Throw(); + } + + // + // Check Similarity ... + var result = TensorPrimitives + .CosineSimilarity( + a.AsSpan(), + b.AsSpan() + ); + + // + return result; + } #endregion } } \ No newline at end of file