last ...
This commit is contained in:
@@ -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<XAiDataProviderHub> 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<XAiDataProviderHub> 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;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Query Messages ...
|
||||
/// </summary>
|
||||
@@ -1283,12 +1294,245 @@ namespace xAiModels.Providers
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
|
||||
//
|
||||
#region Vectors ...
|
||||
/// <summary>
|
||||
/// Insert or Update ...
|
||||
/// </summary>
|
||||
/// <param name="identifier"></param>
|
||||
/// <param name="text"></param>
|
||||
/// <param name="embedding"></param>
|
||||
/// <param name="metadata"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task UpsertAsync(
|
||||
string identifier,
|
||||
string text,
|
||||
ReadOnlyMemory<float> embedding,
|
||||
Dictionary<string, string> 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
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Search Cosine Similarity based on Top K ...
|
||||
/// </summary>
|
||||
/// <param name="queryEmbedding"></param>
|
||||
/// <param name="topK"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<IReadOnlyList<XAiVectorSearchResult>> SearchAsync(
|
||||
ReadOnlyMemory<float> 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 ...
|
||||
/// <summary>
|
||||
/// Enrich Specified Item ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XAiVectorDto> Enrich(
|
||||
XAiVector source,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
var result = await vectorEnricher.Enrich(
|
||||
source: source,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Enrich Specified Items ...
|
||||
/// </summary>
|
||||
/// <param name="sources"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<IEnumerable<XAiVectorDto>> EnrichRange(
|
||||
IEnumerable<XAiVector> sources,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
var result = await vectorEnricher.EnrichRange(
|
||||
sources: sources,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Enrich Specified Item ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XAiVectorDto> Enrich(
|
||||
XAiVectorDto source,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
var result = await vectorEnricher.Enrich(
|
||||
source: source,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Enrich Specified Items ...
|
||||
/// </summary>
|
||||
/// <param name="sources"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<IEnumerable<XAiVectorDto>> EnrichRange(
|
||||
IEnumerable<XAiVectorDto> sources,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
var result = await vectorEnricher.EnrichRange(
|
||||
sources: sources,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Enrich Specified Item ...
|
||||
/// </summary>
|
||||
@@ -1848,6 +2092,33 @@ namespace xAiModels.Providers
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Search Similarity ...
|
||||
/// </summary>
|
||||
/// <param name="a"></param>
|
||||
/// <param name="b"></param>
|
||||
/// <returns></returns>
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user