last ...
This commit is contained in:
@@ -11,7 +11,16 @@ namespace xAiModels.Configurations.Entities
|
|||||||
{
|
{
|
||||||
public override void Configure(EntityTypeBuilder<XAiVector> builder)
|
public override void Configure(EntityTypeBuilder<XAiVector> builder)
|
||||||
{
|
{
|
||||||
|
//
|
||||||
builder.ToTable(XAiModelsConstants.XAiVectorTable);
|
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,
|
string text,
|
||||||
ReadOnlyMemory<float> embedding,
|
ReadOnlyMemory<float> embedding,
|
||||||
Dictionary<string, string> metadata = null,
|
Dictionary<string, string> metadata = null,
|
||||||
|
string type = null,
|
||||||
CancellationToken cancellationToken = default
|
CancellationToken cancellationToken = default
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -336,6 +337,7 @@ namespace xAiModels.Interfaces
|
|||||||
/// <param name="topK"></param>
|
/// <param name="topK"></param>
|
||||||
/// <param name="minScore"></param>
|
/// <param name="minScore"></param>
|
||||||
/// <param name="identifierPrefix">Optional: Restrict Search to Vectors whose Identifier Starts with this Prefix</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>
|
/// <param name="cancellationToken"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task<IReadOnlyList<XAiVectorSearchResult>> SearchVectorsAsync(
|
Task<IReadOnlyList<XAiVectorSearchResult>> SearchVectorsAsync(
|
||||||
@@ -343,6 +345,7 @@ namespace xAiModels.Interfaces
|
|||||||
int topK = 5,
|
int topK = 5,
|
||||||
float minScore = 0f,
|
float minScore = 0f,
|
||||||
string identifierPrefix = null,
|
string identifierPrefix = null,
|
||||||
|
string type = null,
|
||||||
CancellationToken cancellationToken = default
|
CancellationToken cancellationToken = default
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -351,11 +354,13 @@ namespace xAiModels.Interfaces
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="identifierPrefix"></param>
|
/// <param name="identifierPrefix"></param>
|
||||||
/// <param name="includeEmbedding"></param>
|
/// <param name="includeEmbedding"></param>
|
||||||
|
/// <param name="type">Optional: Restrict to Vectors of Specified Type</param>
|
||||||
/// <param name="cancellationToken"></param>
|
/// <param name="cancellationToken"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task<IReadOnlyList<XAiVectorDto>> LoadVectorsAsync(
|
Task<IReadOnlyList<XAiVectorDto>> LoadVectorsAsync(
|
||||||
string identifierPrefix,
|
string identifierPrefix,
|
||||||
bool includeEmbedding = false,
|
bool includeEmbedding = false,
|
||||||
|
string type = null,
|
||||||
CancellationToken cancellationToken = default
|
CancellationToken cancellationToken = default
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -363,10 +368,12 @@ namespace xAiModels.Interfaces
|
|||||||
/// Remove Vectors whose Identifier Starts with Specified Prefix ...
|
/// Remove Vectors whose Identifier Starts with Specified Prefix ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="identifierPrefix"></param>
|
/// <param name="identifierPrefix"></param>
|
||||||
|
/// <param name="type">Optional: Restrict to Vectors of Specified Type</param>
|
||||||
/// <param name="cancellationToken"></param>
|
/// <param name="cancellationToken"></param>
|
||||||
/// <returns>Count of Removed Vectors</returns>
|
/// <returns>Count of Removed Vectors</returns>
|
||||||
Task<int> RemoveVectorsAsync(
|
Task<int> RemoveVectorsAsync(
|
||||||
string identifierPrefix,
|
string identifierPrefix,
|
||||||
|
string type = null,
|
||||||
CancellationToken cancellationToken = default
|
CancellationToken cancellationToken = default
|
||||||
);
|
);
|
||||||
#endregion
|
#endregion
|
||||||
|
|||||||
@@ -19,6 +19,11 @@ namespace xAiModels.Models.Dtos
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public string Identifier { get; set; }
|
public string Identifier { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Knowledge Base Type / Domain (Optional Filter on Search) ...
|
||||||
|
/// </summary>
|
||||||
|
public string Type { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Embedding vector representation ...
|
/// Embedding vector representation ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ namespace xAiModels.Models.Dtos
|
|||||||
public Guid Id { get; set; }
|
public Guid Id { get; set; }
|
||||||
public string Text { get; set; }
|
public string Text { get; set; }
|
||||||
public float Score { get; set; }
|
public float Score { get; set; }
|
||||||
|
public string Type { get; set; }
|
||||||
public string Identifier { get; set; }
|
public string Identifier { get; set; }
|
||||||
public Dictionary<string, string> Metadata { get; set; } = null;
|
public Dictionary<string, string> Metadata { get; set; } = null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using xModels.Base;
|
using xModels.Base;
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
namespace xAiModels.Models.Entities
|
namespace xAiModels.Models.Entities
|
||||||
{
|
{
|
||||||
@@ -14,6 +15,13 @@ namespace xAiModels.Models.Entities
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public string Identifier { get; set; }
|
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>
|
/// <summary>
|
||||||
/// a Json String which Represents IReadOnlyList of float ...
|
/// a Json String which Represents IReadOnlyList of float ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -1314,6 +1314,7 @@ namespace xAiModels.Providers
|
|||||||
string text,
|
string text,
|
||||||
ReadOnlyMemory<float> embedding,
|
ReadOnlyMemory<float> embedding,
|
||||||
Dictionary<string, string> metadata = null,
|
Dictionary<string, string> metadata = null,
|
||||||
|
string type = null,
|
||||||
CancellationToken cancellationToken = default
|
CancellationToken cancellationToken = default
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
@@ -1341,6 +1342,7 @@ namespace xAiModels.Providers
|
|||||||
//
|
//
|
||||||
exists.Text = text;
|
exists.Text = text;
|
||||||
exists.Embedding = embedding.ToJSON();
|
exists.Embedding = embedding.ToJSON();
|
||||||
|
exists.Type = type.IsNullOrEmpty() ? exists.Type : type;
|
||||||
exists.Metadata = metadata.IsNullOrDefault() ? exists.Metadata : metadata.ToJSON();
|
exists.Metadata = metadata.IsNullOrDefault() ? exists.Metadata : metadata.ToJSON();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -1349,6 +1351,7 @@ namespace xAiModels.Providers
|
|||||||
exists = new XAiVector
|
exists = new XAiVector
|
||||||
{
|
{
|
||||||
Text = text,
|
Text = text,
|
||||||
|
Type = type,
|
||||||
Identifier = identifier,
|
Identifier = identifier,
|
||||||
Metadata = metadata.ToJSON(),
|
Metadata = metadata.ToJSON(),
|
||||||
Embedding = embedding.ToJSON(),
|
Embedding = embedding.ToJSON(),
|
||||||
@@ -1370,6 +1373,7 @@ namespace xAiModels.Providers
|
|||||||
/// <param name="topK"></param>
|
/// <param name="topK"></param>
|
||||||
/// <param name="minScore"></param>
|
/// <param name="minScore"></param>
|
||||||
/// <param name="identifierPrefix"></param>
|
/// <param name="identifierPrefix"></param>
|
||||||
|
/// <param name="type"></param>
|
||||||
/// <param name="cancellationToken"></param>
|
/// <param name="cancellationToken"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task<IReadOnlyList<XAiVectorSearchResult>> SearchVectorsAsync(
|
public async Task<IReadOnlyList<XAiVectorSearchResult>> SearchVectorsAsync(
|
||||||
@@ -1377,6 +1381,7 @@ namespace xAiModels.Providers
|
|||||||
int topK = 5,
|
int topK = 5,
|
||||||
float minScore = 0f,
|
float minScore = 0f,
|
||||||
string identifierPrefix = null,
|
string identifierPrefix = null,
|
||||||
|
string type = null,
|
||||||
CancellationToken cancellationToken = default
|
CancellationToken cancellationToken = default
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
@@ -1396,9 +1401,10 @@ namespace xAiModels.Providers
|
|||||||
var scoredResult = new List<(XAiVectorSearchResult Result, float Score)>();
|
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;
|
IEnumerable<XAiVector> candidates;
|
||||||
if (identifierPrefix.IsNullOrEmpty())
|
if (predicate.IsNull())
|
||||||
{
|
{
|
||||||
candidates = await vectorRepository.GetAllAsync(
|
candidates = await vectorRepository.GetAllAsync(
|
||||||
ignoreSoftDeleteds: true,
|
ignoreSoftDeleteds: true,
|
||||||
@@ -1408,9 +1414,9 @@ namespace xAiModels.Providers
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
candidates = await vectorRepository.FindManyAsync(
|
candidates = await vectorRepository.FindManyAsync(
|
||||||
|
predicate: predicate,
|
||||||
ignoreSoftDeleteds: true,
|
ignoreSoftDeleteds: true,
|
||||||
cancellationToken: cancellationToken,
|
cancellationToken: cancellationToken
|
||||||
predicate: x => x.Identifier.StartsWith(identifierPrefix)
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1457,6 +1463,7 @@ namespace xAiModels.Providers
|
|||||||
Score = score,
|
Score = score,
|
||||||
Id = vector.Id,
|
Id = vector.Id,
|
||||||
Text = vector.Text,
|
Text = vector.Text,
|
||||||
|
Type = vector.Type,
|
||||||
Identifier = vector.Identifier,
|
Identifier = vector.Identifier,
|
||||||
Metadata = vector.Metadata.AsMetadat(),
|
Metadata = vector.Metadata.AsMetadat(),
|
||||||
}, score));
|
}, score));
|
||||||
@@ -1527,6 +1534,7 @@ namespace xAiModels.Providers
|
|||||||
//
|
//
|
||||||
exists.Text = item.Text;
|
exists.Text = item.Text;
|
||||||
exists.Embedding = item.Embedding.ToJSON();
|
exists.Embedding = item.Embedding.ToJSON();
|
||||||
|
exists.Type = item.Type.IsNullOrEmpty() ? exists.Type : item.Type;
|
||||||
exists.Metadata = item.Metadata.IsNullOrDefault() ? exists.Metadata : item.Metadata.ToJSON();
|
exists.Metadata = item.Metadata.IsNullOrDefault() ? exists.Metadata : item.Metadata.ToJSON();
|
||||||
toUpdate.Add(exists);
|
toUpdate.Add(exists);
|
||||||
}
|
}
|
||||||
@@ -1536,6 +1544,7 @@ namespace xAiModels.Providers
|
|||||||
toAdd.Add(new XAiVector
|
toAdd.Add(new XAiVector
|
||||||
{
|
{
|
||||||
Text = item.Text,
|
Text = item.Text,
|
||||||
|
Type = item.Type,
|
||||||
Identifier = item.Identifier,
|
Identifier = item.Identifier,
|
||||||
Metadata = item.Metadata.ToJSON(),
|
Metadata = item.Metadata.ToJSON(),
|
||||||
Embedding = item.Embedding.ToJSON(),
|
Embedding = item.Embedding.ToJSON(),
|
||||||
@@ -1575,11 +1584,13 @@ namespace xAiModels.Providers
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="identifierPrefix"></param>
|
/// <param name="identifierPrefix"></param>
|
||||||
/// <param name="includeEmbedding"></param>
|
/// <param name="includeEmbedding"></param>
|
||||||
|
/// <param name="type"></param>
|
||||||
/// <param name="cancellationToken"></param>
|
/// <param name="cancellationToken"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task<IReadOnlyList<XAiVectorDto>> LoadVectorsAsync(
|
public async Task<IReadOnlyList<XAiVectorDto>> LoadVectorsAsync(
|
||||||
string identifierPrefix,
|
string identifierPrefix,
|
||||||
bool includeEmbedding = false,
|
bool includeEmbedding = false,
|
||||||
|
string type = null,
|
||||||
CancellationToken cancellationToken = default
|
CancellationToken cancellationToken = default
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
@@ -1594,7 +1605,7 @@ namespace xAiModels.Providers
|
|||||||
var vectors = await vectorRepository.FindManyAsync(
|
var vectors = await vectorRepository.FindManyAsync(
|
||||||
ignoreSoftDeleteds: true,
|
ignoreSoftDeleteds: true,
|
||||||
cancellationToken: cancellationToken,
|
cancellationToken: cancellationToken,
|
||||||
predicate: x => x.Identifier.StartsWith(identifierPrefix)
|
predicate: BuildVectorPredicate(identifierPrefix, type)
|
||||||
);
|
);
|
||||||
|
|
||||||
//
|
//
|
||||||
@@ -1603,6 +1614,7 @@ namespace xAiModels.Providers
|
|||||||
{
|
{
|
||||||
Id = v.Id,
|
Id = v.Id,
|
||||||
Text = v.Text,
|
Text = v.Text,
|
||||||
|
Type = v.Type,
|
||||||
Identifier = v.Identifier,
|
Identifier = v.Identifier,
|
||||||
Metadata = v.Metadata.AsMetadat(),
|
Metadata = v.Metadata.AsMetadat(),
|
||||||
Embedding = includeEmbedding ? v.Embedding.AsEmbedding() : null,
|
Embedding = includeEmbedding ? v.Embedding.AsEmbedding() : null,
|
||||||
@@ -1617,10 +1629,12 @@ namespace xAiModels.Providers
|
|||||||
/// Remove Vectors whose Identifier Starts with Specified Prefix ...
|
/// Remove Vectors whose Identifier Starts with Specified Prefix ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="identifierPrefix"></param>
|
/// <param name="identifierPrefix"></param>
|
||||||
|
/// <param name="type"></param>
|
||||||
/// <param name="cancellationToken"></param>
|
/// <param name="cancellationToken"></param>
|
||||||
/// <returns>Count of Removed Vectors</returns>
|
/// <returns>Count of Removed Vectors</returns>
|
||||||
public async Task<int> RemoveVectorsAsync(
|
public async Task<int> RemoveVectorsAsync(
|
||||||
string identifierPrefix,
|
string identifierPrefix,
|
||||||
|
string type = null,
|
||||||
CancellationToken cancellationToken = default
|
CancellationToken cancellationToken = default
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
@@ -1635,7 +1649,7 @@ namespace xAiModels.Providers
|
|||||||
var vectors = await vectorRepository.FindManyAsync(
|
var vectors = await vectorRepository.FindManyAsync(
|
||||||
ignoreSoftDeleteds: true,
|
ignoreSoftDeleteds: true,
|
||||||
cancellationToken: cancellationToken,
|
cancellationToken: cancellationToken,
|
||||||
predicate: x => x.Identifier.StartsWith(identifierPrefix)
|
predicate: BuildVectorPredicate(identifierPrefix, type)
|
||||||
);
|
);
|
||||||
var toRemove = (vectors ?? []).ToList();
|
var toRemove = (vectors ?? []).ToList();
|
||||||
if (!toRemove.HasChild())
|
if (!toRemove.HasChild())
|
||||||
@@ -1657,6 +1671,40 @@ namespace xAiModels.Providers
|
|||||||
return result;
|
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
|
#endregion
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|||||||
Reference in New Issue
Block a user