last ...
This commit is contained in:
@@ -11,7 +11,16 @@ namespace xAiModels.Configurations.Entities
|
||||
{
|
||||
public override void Configure(EntityTypeBuilder<XAiVector> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -315,6 +315,7 @@ namespace xAiModels.Interfaces
|
||||
string text,
|
||||
ReadOnlyMemory<float> embedding,
|
||||
Dictionary<string, string> metadata = null,
|
||||
string type = null,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
|
||||
@@ -336,6 +337,7 @@ namespace xAiModels.Interfaces
|
||||
/// <param name="topK"></param>
|
||||
/// <param name="minScore"></param>
|
||||
/// <param name="identifierPrefix">Optional: Restrict Search to Vectors whose Identifier Starts with this Prefix</param>
|
||||
/// <param name="type">Optional: Restrict Search to Vectors of Specified Type</param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<IReadOnlyList<XAiVectorSearchResult>> 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
|
||||
/// </summary>
|
||||
/// <param name="identifierPrefix"></param>
|
||||
/// <param name="includeEmbedding"></param>
|
||||
/// <param name="type">Optional: Restrict to Vectors of Specified Type</param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<IReadOnlyList<XAiVectorDto>> 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 ...
|
||||
/// </summary>
|
||||
/// <param name="identifierPrefix"></param>
|
||||
/// <param name="type">Optional: Restrict to Vectors of Specified Type</param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns>Count of Removed Vectors</returns>
|
||||
Task<int> RemoveVectorsAsync(
|
||||
string identifierPrefix,
|
||||
string type = null,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
#endregion
|
||||
|
||||
@@ -19,6 +19,11 @@ namespace xAiModels.Models.Dtos
|
||||
/// </summary>
|
||||
public string Identifier { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Knowledge Base Type / Domain (Optional Filter on Search) ...
|
||||
/// </summary>
|
||||
public string Type { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Embedding vector representation ...
|
||||
/// </summary>
|
||||
|
||||
@@ -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<string, string> Metadata { get; set; } = null;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using xModels.Base;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace xAiModels.Models.Entities
|
||||
{
|
||||
@@ -14,6 +15,13 @@ namespace xAiModels.Models.Entities
|
||||
/// </summary>
|
||||
public string Identifier { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Knowledge Base Type / Domain (e.g. "Legal", "Medical") ...
|
||||
/// Used for Filtering on Search and Retrieve ...
|
||||
/// </summary>
|
||||
[StringLength(100)]
|
||||
public string Type { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// a Json String which Represents IReadOnlyList of float ...
|
||||
/// </summary>
|
||||
|
||||
@@ -1314,6 +1314,7 @@ namespace xAiModels.Providers
|
||||
string text,
|
||||
ReadOnlyMemory<float> embedding,
|
||||
Dictionary<string, string> 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
|
||||
/// <param name="topK"></param>
|
||||
/// <param name="minScore"></param>
|
||||
/// <param name="identifierPrefix"></param>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<IReadOnlyList<XAiVectorSearchResult>> 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<XAiVector> 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
|
||||
/// </summary>
|
||||
/// <param name="identifierPrefix"></param>
|
||||
/// <param name="includeEmbedding"></param>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<IReadOnlyList<XAiVectorDto>> 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 ...
|
||||
/// </summary>
|
||||
/// <param name="identifierPrefix"></param>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns>Count of Removed Vectors</returns>
|
||||
public async Task<int> 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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Build Database Predicate for Vectors by Optional Identifier Prefix and Type ...
|
||||
/// Returns null when no Filter Provided ...
|
||||
/// </summary>
|
||||
/// <param name="identifierPrefix"></param>
|
||||
/// <param name="type"></param>
|
||||
/// <returns></returns>
|
||||
private static Expression<Func<XAiVector, bool>> 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
|
||||
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user