From 34539cd764d76167432eaf11d6d4baeb68fe0972 Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Tue, 27 Oct 2026 05:55:53 +0330 Subject: [PATCH] last ... --- .../Entities/XAiVectorEntityConfiguration.cs | 9 +++ Interfaces/IXAiDataProvider.cs | 7 +++ Models/Dtos/XAiVectorDto.cs | 5 ++ Models/Dtos/XAiVectorSearchResult.cs | 1 + Models/Entities/XAiVector.cs | 8 +++ Providers/XAiDataProvider.cs | 60 +++++++++++++++++-- 6 files changed, 84 insertions(+), 6 deletions(-) diff --git a/Configurations/Entities/XAiVectorEntityConfiguration.cs b/Configurations/Entities/XAiVectorEntityConfiguration.cs index 3c6659c..c27dde9 100644 --- a/Configurations/Entities/XAiVectorEntityConfiguration.cs +++ b/Configurations/Entities/XAiVectorEntityConfiguration.cs @@ -11,7 +11,16 @@ namespace xAiModels.Configurations.Entities { public override void Configure(EntityTypeBuilder builder) { + // builder.ToTable(XAiModelsConstants.XAiVectorTable); + + // + builder.Property(x => x.Type).HasMaxLength(100); + + // + // Indexes for Prefix Scoped Search and Type Filtering ... + builder.HasIndex(x => x.Identifier); + builder.HasIndex(x => x.Type); } } } diff --git a/Interfaces/IXAiDataProvider.cs b/Interfaces/IXAiDataProvider.cs index abe9f35..21bbdc7 100644 --- a/Interfaces/IXAiDataProvider.cs +++ b/Interfaces/IXAiDataProvider.cs @@ -315,6 +315,7 @@ namespace xAiModels.Interfaces string text, ReadOnlyMemory embedding, Dictionary metadata = null, + string type = null, CancellationToken cancellationToken = default ); @@ -336,6 +337,7 @@ namespace xAiModels.Interfaces /// /// /// Optional: Restrict Search to Vectors whose Identifier Starts with this Prefix + /// Optional: Restrict Search to Vectors of Specified Type /// /// Task> SearchVectorsAsync( @@ -343,6 +345,7 @@ namespace xAiModels.Interfaces int topK = 5, float minScore = 0f, string identifierPrefix = null, + string type = null, CancellationToken cancellationToken = default ); @@ -351,11 +354,13 @@ namespace xAiModels.Interfaces /// /// /// + /// Optional: Restrict to Vectors of Specified Type /// /// Task> LoadVectorsAsync( string identifierPrefix, bool includeEmbedding = false, + string type = null, CancellationToken cancellationToken = default ); @@ -363,10 +368,12 @@ namespace xAiModels.Interfaces /// Remove Vectors whose Identifier Starts with Specified Prefix ... /// /// + /// Optional: Restrict to Vectors of Specified Type /// /// Count of Removed Vectors Task RemoveVectorsAsync( string identifierPrefix, + string type = null, CancellationToken cancellationToken = default ); #endregion diff --git a/Models/Dtos/XAiVectorDto.cs b/Models/Dtos/XAiVectorDto.cs index 4363a23..996c542 100644 --- a/Models/Dtos/XAiVectorDto.cs +++ b/Models/Dtos/XAiVectorDto.cs @@ -19,6 +19,11 @@ namespace xAiModels.Models.Dtos /// public string Identifier { get; set; } + /// + /// Knowledge Base Type / Domain (Optional Filter on Search) ... + /// + public string Type { get; set; } + /// /// Embedding vector representation ... /// diff --git a/Models/Dtos/XAiVectorSearchResult.cs b/Models/Dtos/XAiVectorSearchResult.cs index 0e3feef..ae0471a 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 Type { get; set; } public string Identifier { get; set; } public Dictionary Metadata { get; set; } = null; } diff --git a/Models/Entities/XAiVector.cs b/Models/Entities/XAiVector.cs index a1915cf..197a9f3 100644 --- a/Models/Entities/XAiVector.cs +++ b/Models/Entities/XAiVector.cs @@ -1,4 +1,5 @@ using xModels.Base; +using System.ComponentModel.DataAnnotations; namespace xAiModels.Models.Entities { @@ -14,6 +15,13 @@ namespace xAiModels.Models.Entities /// public string Identifier { get; set; } + /// + /// Knowledge Base Type / Domain (e.g. "Legal", "Medical") ... + /// Used for Filtering on Search and Retrieve ... + /// + [StringLength(100)] + public string Type { get; set; } + /// /// a Json String which Represents IReadOnlyList of float ... /// diff --git a/Providers/XAiDataProvider.cs b/Providers/XAiDataProvider.cs index fe81612..28ef684 100644 --- a/Providers/XAiDataProvider.cs +++ b/Providers/XAiDataProvider.cs @@ -1314,6 +1314,7 @@ namespace xAiModels.Providers string text, ReadOnlyMemory embedding, Dictionary metadata = null, + string type = null, CancellationToken cancellationToken = default ) { @@ -1341,6 +1342,7 @@ namespace xAiModels.Providers // exists.Text = text; exists.Embedding = embedding.ToJSON(); + exists.Type = type.IsNullOrEmpty() ? exists.Type : type; exists.Metadata = metadata.IsNullOrDefault() ? exists.Metadata : metadata.ToJSON(); } else @@ -1349,6 +1351,7 @@ namespace xAiModels.Providers exists = new XAiVector { Text = text, + Type = type, Identifier = identifier, Metadata = metadata.ToJSON(), Embedding = embedding.ToJSON(), @@ -1370,6 +1373,7 @@ namespace xAiModels.Providers /// /// /// + /// /// /// public async Task> SearchVectorsAsync( @@ -1377,6 +1381,7 @@ namespace xAiModels.Providers int topK = 5, float minScore = 0f, string identifierPrefix = null, + string type = null, CancellationToken cancellationToken = default ) { @@ -1396,9 +1401,10 @@ namespace xAiModels.Providers var scoredResult = new List<(XAiVectorSearchResult Result, float Score)>(); // - // Load Candidates (Restricted by Prefix when Provided) ... + // Load Candidates (Restricted by Prefix / Type when Provided) ... + var predicate = BuildVectorPredicate(identifierPrefix, type); IEnumerable candidates; - if (identifierPrefix.IsNullOrEmpty()) + if (predicate.IsNull()) { candidates = await vectorRepository.GetAllAsync( ignoreSoftDeleteds: true, @@ -1408,9 +1414,9 @@ namespace xAiModels.Providers else { candidates = await vectorRepository.FindManyAsync( + predicate: predicate, ignoreSoftDeleteds: true, - cancellationToken: cancellationToken, - predicate: x => x.Identifier.StartsWith(identifierPrefix) + cancellationToken: cancellationToken ); } @@ -1457,6 +1463,7 @@ namespace xAiModels.Providers Score = score, Id = vector.Id, Text = vector.Text, + Type = vector.Type, Identifier = vector.Identifier, Metadata = vector.Metadata.AsMetadat(), }, score)); @@ -1527,6 +1534,7 @@ namespace xAiModels.Providers // exists.Text = item.Text; exists.Embedding = item.Embedding.ToJSON(); + exists.Type = item.Type.IsNullOrEmpty() ? exists.Type : item.Type; exists.Metadata = item.Metadata.IsNullOrDefault() ? exists.Metadata : item.Metadata.ToJSON(); toUpdate.Add(exists); } @@ -1536,6 +1544,7 @@ namespace xAiModels.Providers toAdd.Add(new XAiVector { Text = item.Text, + Type = item.Type, Identifier = item.Identifier, Metadata = item.Metadata.ToJSON(), Embedding = item.Embedding.ToJSON(), @@ -1575,11 +1584,13 @@ namespace xAiModels.Providers /// /// /// + /// /// /// public async Task> LoadVectorsAsync( string identifierPrefix, bool includeEmbedding = false, + string type = null, CancellationToken cancellationToken = default ) { @@ -1594,7 +1605,7 @@ namespace xAiModels.Providers var vectors = await vectorRepository.FindManyAsync( ignoreSoftDeleteds: true, cancellationToken: cancellationToken, - predicate: x => x.Identifier.StartsWith(identifierPrefix) + predicate: BuildVectorPredicate(identifierPrefix, type) ); // @@ -1603,6 +1614,7 @@ namespace xAiModels.Providers { Id = v.Id, Text = v.Text, + Type = v.Type, Identifier = v.Identifier, Metadata = v.Metadata.AsMetadat(), Embedding = includeEmbedding ? v.Embedding.AsEmbedding() : null, @@ -1617,10 +1629,12 @@ namespace xAiModels.Providers /// Remove Vectors whose Identifier Starts with Specified Prefix ... /// /// + /// /// /// Count of Removed Vectors public async Task RemoveVectorsAsync( string identifierPrefix, + string type = null, CancellationToken cancellationToken = default ) { @@ -1635,7 +1649,7 @@ namespace xAiModels.Providers var vectors = await vectorRepository.FindManyAsync( ignoreSoftDeleteds: true, cancellationToken: cancellationToken, - predicate: x => x.Identifier.StartsWith(identifierPrefix) + predicate: BuildVectorPredicate(identifierPrefix, type) ); var toRemove = (vectors ?? []).ToList(); if (!toRemove.HasChild()) @@ -1657,6 +1671,40 @@ namespace xAiModels.Providers return result; } + /// + /// Build Database Predicate for Vectors by Optional Identifier Prefix and Type ... + /// Returns null when no Filter Provided ... + /// + /// + /// + /// + private static Expression> BuildVectorPredicate( + string identifierPrefix, + string type + ) + { + // + var hasType = !type.IsNullOrEmpty(); + var hasPrefix = !identifierPrefix.IsNullOrEmpty(); + + // + if (hasPrefix && hasType) + { + return x => x.Identifier.StartsWith(identifierPrefix) && x.Type == type; + } + if (hasPrefix) + { + return x => x.Identifier.StartsWith(identifierPrefix); + } + if (hasType) + { + return x => x.Type == type; + } + + // + return null; + } + #endregion //