Persist Vector Storage and Prefix-Scoped Vector Search
- Add IXAiDataProvider contracts for batch upsert, prefix search, loading, and removal; - Persist chunks in existing XAiVector schema without migration; - Support topK, minimum score, prefix filtering, batch persistence, and replacement. Files: M Interfaces/IXAiDataProvider.cs; M Providers/XAiDataProvider.cs
This commit is contained in:
@@ -298,5 +298,77 @@ namespace xAiModels.Interfaces
|
|||||||
CancellationToken cancellationToken = default
|
CancellationToken cancellationToken = default
|
||||||
);
|
);
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
//
|
||||||
|
#region Vectors ...
|
||||||
|
/// <summary>
|
||||||
|
/// Insert or Update a Single Vector (Matched by Identifier) ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="identifier"></param>
|
||||||
|
/// <param name="text"></param>
|
||||||
|
/// <param name="embedding"></param>
|
||||||
|
/// <param name="metadata"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task UpsertVectorAsync(
|
||||||
|
string identifier,
|
||||||
|
string text,
|
||||||
|
ReadOnlyMemory<float> embedding,
|
||||||
|
Dictionary<string, string> metadata = null,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Insert or Update a Batch of Vectors (Matched by Identifier) ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="items"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns>Count of Stored Vectors</returns>
|
||||||
|
Task<int> UpsertVectorsAsync(
|
||||||
|
IEnumerable<XAiVectorDto> items,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Search Vectors by Cosine Similarity (Top K) ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="queryEmbedding"></param>
|
||||||
|
/// <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="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<IReadOnlyList<XAiVectorSearchResult>> SearchVectorsAsync(
|
||||||
|
ReadOnlyMemory<float> queryEmbedding,
|
||||||
|
int topK = 5,
|
||||||
|
float minScore = 0f,
|
||||||
|
string identifierPrefix = null,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Load Vectors whose Identifier Starts with Specified Prefix ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="identifierPrefix"></param>
|
||||||
|
/// <param name="includeEmbedding"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<IReadOnlyList<XAiVectorDto>> LoadVectorsAsync(
|
||||||
|
string identifierPrefix,
|
||||||
|
bool includeEmbedding = false,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Remove Vectors whose Identifier Starts with Specified Prefix ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="identifierPrefix"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns>Count of Removed Vectors</returns>
|
||||||
|
Task<int> RemoveVectorsAsync(
|
||||||
|
string identifierPrefix,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1301,7 +1301,7 @@ namespace xAiModels.Providers
|
|||||||
//
|
//
|
||||||
#region Vectors ...
|
#region Vectors ...
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Insert or Update ...
|
/// Insert or Update a Single Vector (Matched by Identifier) ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="identifier"></param>
|
/// <param name="identifier"></param>
|
||||||
/// <param name="text"></param>
|
/// <param name="text"></param>
|
||||||
@@ -1309,7 +1309,7 @@ namespace xAiModels.Providers
|
|||||||
/// <param name="metadata"></param>
|
/// <param name="metadata"></param>
|
||||||
/// <param name="cancellationToken"></param>
|
/// <param name="cancellationToken"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task UpsertAsync(
|
public async Task UpsertVectorAsync(
|
||||||
string identifier,
|
string identifier,
|
||||||
string text,
|
string text,
|
||||||
ReadOnlyMemory<float> embedding,
|
ReadOnlyMemory<float> embedding,
|
||||||
@@ -1341,6 +1341,7 @@ namespace xAiModels.Providers
|
|||||||
//
|
//
|
||||||
exists.Text = text;
|
exists.Text = text;
|
||||||
exists.Embedding = embedding.ToJSON();
|
exists.Embedding = embedding.ToJSON();
|
||||||
|
exists.Metadata = metadata.IsNullOrDefault() ? exists.Metadata : metadata.ToJSON();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -1363,15 +1364,19 @@ namespace xAiModels.Providers
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Search Cosine Similarity based on Top K ...
|
/// Search Vectors by Cosine Similarity (Top K) ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="queryEmbedding"></param>
|
/// <param name="queryEmbedding"></param>
|
||||||
/// <param name="topK"></param>
|
/// <param name="topK"></param>
|
||||||
|
/// <param name="minScore"></param>
|
||||||
|
/// <param name="identifierPrefix"></param>
|
||||||
/// <param name="cancellationToken"></param>
|
/// <param name="cancellationToken"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task<IReadOnlyList<XAiVectorSearchResult>> SearchAsync(
|
public async Task<IReadOnlyList<XAiVectorSearchResult>> SearchVectorsAsync(
|
||||||
ReadOnlyMemory<float> queryEmbedding,
|
ReadOnlyMemory<float> queryEmbedding,
|
||||||
int topK = 5,
|
int topK = 5,
|
||||||
|
float minScore = 0f,
|
||||||
|
string identifierPrefix = null,
|
||||||
CancellationToken cancellationToken = default
|
CancellationToken cancellationToken = default
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
@@ -1391,8 +1396,26 @@ namespace xAiModels.Providers
|
|||||||
var scoredResult = new List<(XAiVectorSearchResult Result, float Score)>();
|
var scoredResult = new List<(XAiVectorSearchResult Result, float Score)>();
|
||||||
|
|
||||||
//
|
//
|
||||||
var vectorsEnumerator = vectorRepository.GetAllAsAsyncEnumerable();
|
// Load Candidates (Restricted by Prefix when Provided) ...
|
||||||
await foreach (var vector in vectorsEnumerator)
|
IEnumerable<XAiVector> candidates;
|
||||||
|
if (identifierPrefix.IsNullOrEmpty())
|
||||||
|
{
|
||||||
|
candidates = await vectorRepository.GetAllAsync(
|
||||||
|
ignoreSoftDeleteds: true,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
candidates = await vectorRepository.FindManyAsync(
|
||||||
|
ignoreSoftDeleteds: true,
|
||||||
|
cancellationToken: cancellationToken,
|
||||||
|
predicate: x => x.Identifier.StartsWith(identifierPrefix)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
foreach (var vector in candidates ?? [])
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
// Validate Vector ...
|
// Validate Vector ...
|
||||||
@@ -1422,6 +1445,10 @@ namespace xAiModels.Providers
|
|||||||
//
|
//
|
||||||
// Calculate Cosine Similarity Score ...
|
// Calculate Cosine Similarity Score ...
|
||||||
var score = ComputeCosineSimilarity(queryVector, embd);
|
var score = ComputeCosineSimilarity(queryVector, embd);
|
||||||
|
if (score < minScore)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
scoredResult.Add((new XAiVectorSearchResult
|
scoredResult.Add((new XAiVectorSearchResult
|
||||||
@@ -1445,6 +1472,191 @@ namespace xAiModels.Providers
|
|||||||
//
|
//
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Insert or Update a Batch of Vectors (Matched by Identifier) ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="items"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns>Count of Stored Vectors</returns>
|
||||||
|
public async Task<int> UpsertVectorsAsync(
|
||||||
|
IEnumerable<XAiVectorDto> items,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate & Normalize (Last One Wins on Duplicate Identifiers) ...
|
||||||
|
var validItems = (items ?? [])
|
||||||
|
.Where(x =>
|
||||||
|
!x.IsNullOrDefault() &&
|
||||||
|
!x.Embedding.IsEmpty &&
|
||||||
|
!x.Text.IsNullOrEmpty() &&
|
||||||
|
!x.Identifier.IsNullOrEmpty()
|
||||||
|
)
|
||||||
|
.GroupBy(x => x.Identifier)
|
||||||
|
.Select(g => g.Last())
|
||||||
|
.ToList();
|
||||||
|
if (!validItems.HasChild())
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Load Existing Vectors with Same Identifiers ...
|
||||||
|
var identifiers = validItems
|
||||||
|
.Select(x => x.Identifier)
|
||||||
|
.ToList();
|
||||||
|
var existings = await vectorRepository.FindManyAsync(
|
||||||
|
ignoreSoftDeleteds: true,
|
||||||
|
cancellationToken: cancellationToken,
|
||||||
|
predicate: x => identifiers.Contains(x.Identifier)
|
||||||
|
);
|
||||||
|
var existingMap = (existings ?? [])
|
||||||
|
.GroupBy(x => x.Identifier)
|
||||||
|
.ToDictionary(g => g.Key, g => g.First());
|
||||||
|
|
||||||
|
//
|
||||||
|
// Split to Insert / Update ...
|
||||||
|
var toAdd = new List<XAiVector>();
|
||||||
|
var toUpdate = new List<XAiVector>();
|
||||||
|
foreach (var item in validItems)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (existingMap.TryGetValue(item.Identifier, out var exists))
|
||||||
|
{
|
||||||
|
//
|
||||||
|
exists.Text = item.Text;
|
||||||
|
exists.Embedding = item.Embedding.ToJSON();
|
||||||
|
exists.Metadata = item.Metadata.IsNullOrDefault() ? exists.Metadata : item.Metadata.ToJSON();
|
||||||
|
toUpdate.Add(exists);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//
|
||||||
|
toAdd.Add(new XAiVector
|
||||||
|
{
|
||||||
|
Text = item.Text,
|
||||||
|
Identifier = item.Identifier,
|
||||||
|
Metadata = item.Metadata.ToJSON(),
|
||||||
|
Embedding = item.Embedding.ToJSON(),
|
||||||
|
Id = item.Id.IsDefaultGuid() ? Guid.NewGuid() : item.Id,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
if (toUpdate.HasChild())
|
||||||
|
{
|
||||||
|
await vectorRepository.UpdateRangeAsync(
|
||||||
|
items: toUpdate,
|
||||||
|
saveChanges: false,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (toAdd.HasChild())
|
||||||
|
{
|
||||||
|
await vectorRepository.AddRangeAsync(
|
||||||
|
items: toAdd,
|
||||||
|
saveChanges: false,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
await vectorRepository.SaveChangesAsync(cancellationToken);
|
||||||
|
|
||||||
|
//
|
||||||
|
var result = toAdd.Count + toUpdate.Count;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Load Vectors whose Identifier Starts with Specified Prefix ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="identifierPrefix"></param>
|
||||||
|
/// <param name="includeEmbedding"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<IReadOnlyList<XAiVectorDto>> LoadVectorsAsync(
|
||||||
|
string identifierPrefix,
|
||||||
|
bool includeEmbedding = false,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate ...
|
||||||
|
if (identifierPrefix.IsNullOrEmpty())
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
var vectors = await vectorRepository.FindManyAsync(
|
||||||
|
ignoreSoftDeleteds: true,
|
||||||
|
cancellationToken: cancellationToken,
|
||||||
|
predicate: x => x.Identifier.StartsWith(identifierPrefix)
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
var result = (vectors ?? [])
|
||||||
|
.Select(v => new XAiVectorDto
|
||||||
|
{
|
||||||
|
Id = v.Id,
|
||||||
|
Text = v.Text,
|
||||||
|
Identifier = v.Identifier,
|
||||||
|
Metadata = v.Metadata.AsMetadat(),
|
||||||
|
Embedding = includeEmbedding ? v.Embedding.AsEmbedding() : null,
|
||||||
|
})
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Remove Vectors whose Identifier Starts with Specified Prefix ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="identifierPrefix"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns>Count of Removed Vectors</returns>
|
||||||
|
public async Task<int> RemoveVectorsAsync(
|
||||||
|
string identifierPrefix,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate ...
|
||||||
|
if (identifierPrefix.IsNullOrEmpty())
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
var vectors = await vectorRepository.FindManyAsync(
|
||||||
|
ignoreSoftDeleteds: true,
|
||||||
|
cancellationToken: cancellationToken,
|
||||||
|
predicate: x => x.Identifier.StartsWith(identifierPrefix)
|
||||||
|
);
|
||||||
|
var toRemove = (vectors ?? []).ToList();
|
||||||
|
if (!toRemove.HasChild())
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Hard Delete (Vectors are Disposable Data) ...
|
||||||
|
await vectorRepository.RemoveRangeAsync(
|
||||||
|
items: toRemove,
|
||||||
|
softDelete: false,
|
||||||
|
saveChanges: true,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
var result = toRemove.Count;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|||||||
Reference in New Issue
Block a user