last ...
This commit is contained in:
@@ -128,6 +128,14 @@ namespace xAiApi.DI
|
||||
source.AddSingleton<IXTextChunker, XTextChunker>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register In-Memory Vector Store Service ...
|
||||
/// </summary>
|
||||
public static void AddXAiInMemoryVectorStore(this IServiceCollection source)
|
||||
{
|
||||
source.AddSingleton<IXInMemoryVectorStore, XInMemoryVectorStore>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Use Model Proxy Provider ...
|
||||
/// </summary>
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using xAiApi.Models;
|
||||
using xAiModels.Models.Dtos;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace xAiApi.Interfaces
|
||||
{
|
||||
public interface IXInMemoryVectorStore
|
||||
{
|
||||
int Count { get; }
|
||||
int RemoveExpired();
|
||||
Guid CreateSession(
|
||||
string ownerId = null,
|
||||
TimeSpan? ttl = null
|
||||
);
|
||||
Guid CreateSession(
|
||||
IEnumerable<XAiVectorDto> vectors,
|
||||
string ownerId = null,
|
||||
TimeSpan? ttl = null
|
||||
);
|
||||
bool Replace(
|
||||
Guid sessionId,
|
||||
IEnumerable<XAiVectorDto> vectors,
|
||||
string ownerId = null
|
||||
);
|
||||
bool Add(
|
||||
Guid sessionId,
|
||||
IEnumerable<XAiVectorDto> vectors,
|
||||
string ownerId = null
|
||||
);
|
||||
IList<XAiVectorSearchResult> Search(
|
||||
Guid sessionId,
|
||||
ReadOnlyMemory<float> queryEmbedding,
|
||||
int topK = 5,
|
||||
float minScore = 0f,
|
||||
string ownerId = null
|
||||
);
|
||||
bool TryGet(
|
||||
Guid sessionId,
|
||||
out XAiDocumentSession session,
|
||||
string ownerId = null
|
||||
);
|
||||
bool Remove(
|
||||
Guid sessionId,
|
||||
string ownerId = null
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using xAiModels.Models.Dtos;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace xAiApi.Models
|
||||
{
|
||||
public class XAiDocumentSession
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public TimeSpan Ttl { get; set; }
|
||||
public string OwnerId { get; set; }
|
||||
public DateTime CreatedAtUtc { get; set; }
|
||||
public DateTime LastAccessAtUtc { get; set; }
|
||||
internal object SyncRoot { get; } = new object();
|
||||
public IList<XAiVectorDto> Vectors { get; set; } = new List<XAiVectorDto>();
|
||||
public bool IsExpired(DateTime utcNow) { return utcNow - LastAccessAtUtc >= Ttl; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,197 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using xAiApi.Models;
|
||||
using xAiService.Helpers;
|
||||
using xAiApi.Interfaces;
|
||||
using xAiModels.Models.Dtos;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace xAiApi.Providers
|
||||
{
|
||||
public class XInMemoryVectorStore : IXInMemoryVectorStore
|
||||
{
|
||||
private const int DefaultTtlMinutes = 30;
|
||||
private readonly ConcurrentDictionary<Guid, XAiDocumentSession> sessions = new ConcurrentDictionary<Guid, XAiDocumentSession>();
|
||||
public int Count { get { RemoveExpired(); return sessions.Count; } }
|
||||
|
||||
public Guid CreateSession(
|
||||
string ownerId = null,
|
||||
TimeSpan? ttl = null
|
||||
)
|
||||
{
|
||||
//
|
||||
var result = CreateSession(null, ownerId, ttl);
|
||||
return result;
|
||||
}
|
||||
|
||||
public Guid CreateSession(
|
||||
IEnumerable<XAiVectorDto> vectors,
|
||||
string ownerId = null,
|
||||
TimeSpan? ttl = null
|
||||
)
|
||||
{
|
||||
//
|
||||
var now = DateTime.UtcNow;
|
||||
var session = new XAiDocumentSession
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
OwnerId = ownerId,
|
||||
CreatedAtUtc = now,
|
||||
LastAccessAtUtc = now,
|
||||
Ttl = NormalizeTtl(ttl),
|
||||
Vectors = Copy(vectors)
|
||||
};
|
||||
sessions[session.Id] = session;
|
||||
var result = session.Id;
|
||||
return result;
|
||||
}
|
||||
|
||||
public bool Replace(
|
||||
Guid sessionId,
|
||||
IEnumerable<XAiVectorDto> vectors,
|
||||
string ownerId = null
|
||||
)
|
||||
{
|
||||
//
|
||||
XAiDocumentSession session;
|
||||
if (!TryGet(sessionId, out session, ownerId))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
//
|
||||
lock (session.SyncRoot)
|
||||
{
|
||||
session.Vectors = Copy(vectors);
|
||||
session.LastAccessAtUtc = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
//
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Add(
|
||||
Guid sessionId,
|
||||
IEnumerable<XAiVectorDto> vectors,
|
||||
string ownerId = null
|
||||
)
|
||||
{
|
||||
//
|
||||
XAiDocumentSession session;
|
||||
if (!TryGet(sessionId, out session, ownerId))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
var additions = Copy(vectors);
|
||||
lock (session.SyncRoot)
|
||||
{
|
||||
//
|
||||
foreach (var vector in additions)
|
||||
{
|
||||
session.Vectors.Add(vector);
|
||||
}
|
||||
|
||||
//
|
||||
session.LastAccessAtUtc = DateTime.UtcNow;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public IList<XAiVectorSearchResult> Search(
|
||||
Guid sessionId,
|
||||
ReadOnlyMemory<float> queryEmbedding,
|
||||
int topK = 5,
|
||||
float minScore = 0f,
|
||||
string ownerId = null
|
||||
)
|
||||
{
|
||||
//
|
||||
var result = new List<XAiVectorSearchResult>();
|
||||
if (topK <= 0 || queryEmbedding.Length == 0)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
XAiDocumentSession session;
|
||||
if (!TryGet(sessionId, out session, ownerId))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
lock (session.SyncRoot)
|
||||
{
|
||||
//
|
||||
session.LastAccessAtUtc = DateTime.UtcNow;
|
||||
foreach (var vector in session.Vectors)
|
||||
{
|
||||
//
|
||||
if (vector == null || vector.Embedding.Length == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
//
|
||||
var score = VectorHelper.CosineSimilarity(
|
||||
queryEmbedding.ToArray(),
|
||||
vector.Embedding.ToArray()
|
||||
);
|
||||
|
||||
//
|
||||
if (score >= minScore)
|
||||
{
|
||||
//
|
||||
result.Add(new XAiVectorSearchResult {
|
||||
Score = score,
|
||||
Id = vector.Id,
|
||||
Text = vector.Text,
|
||||
Metadata = vector.Metadata,
|
||||
Identifier = vector.Identifier,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return [.. result
|
||||
.OrderByDescending(x => x.Score)
|
||||
.Take(topK)];
|
||||
}
|
||||
|
||||
public bool TryGet(
|
||||
Guid sessionId,
|
||||
out XAiDocumentSession session,
|
||||
string ownerId = null
|
||||
)
|
||||
{
|
||||
//
|
||||
session = null;
|
||||
XAiDocumentSession found;
|
||||
if (!sessions.TryGetValue(sessionId, out found) || !OwnerMatches(found, ownerId)) return false;
|
||||
if (found.IsExpired(DateTime.UtcNow)) { sessions.TryRemove(sessionId, out found); return false; }
|
||||
found.LastAccessAtUtc = DateTime.UtcNow;
|
||||
session = found;
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Remove(Guid sessionId, string ownerId = null)
|
||||
{
|
||||
XAiDocumentSession session;
|
||||
if (!TryGet(sessionId, out session, ownerId)) return false;
|
||||
return sessions.TryRemove(sessionId, out session);
|
||||
}
|
||||
|
||||
public int RemoveExpired()
|
||||
{
|
||||
var now = DateTime.UtcNow; var removed = 0;
|
||||
foreach (var pair in sessions) { if (pair.Value.IsExpired(now) && sessions.TryRemove(pair.Key, out _)) removed++; }
|
||||
return removed;
|
||||
}
|
||||
|
||||
private static bool OwnerMatches(XAiDocumentSession session, string ownerId) { return string.IsNullOrEmpty(session.OwnerId) || string.Equals(session.OwnerId, ownerId, StringComparison.Ordinal); }
|
||||
private static TimeSpan NormalizeTtl(TimeSpan? ttl) { return !ttl.HasValue || ttl.Value <= TimeSpan.Zero ? TimeSpan.FromMinutes(DefaultTtlMinutes) : ttl.Value; }
|
||||
private static IList<XAiVectorDto> Copy(IEnumerable<XAiVectorDto> vectors) { return vectors == null ? new List<XAiVectorDto>() : vectors.Where(x => x != null).ToList(); }
|
||||
}
|
||||
}
|
||||
+4
-2
@@ -11,11 +11,9 @@ using xPushService.DI;
|
||||
using xFileService.DI;
|
||||
using xDataService.DI;
|
||||
using Newtonsoft.Json;
|
||||
using xAiApi.Providers;
|
||||
using System.Reflection;
|
||||
using xAiApi.Extensions;
|
||||
using xStringService.DI;
|
||||
using xAiApi.Interfaces;
|
||||
using xStorageService.DI;
|
||||
using xIdentityService.DI;
|
||||
using xDataService.Models;
|
||||
@@ -193,6 +191,10 @@ namespace xAiApi
|
||||
// Register Text Chunker Service ...
|
||||
services.AddXAiTextChunker();
|
||||
|
||||
//
|
||||
// Register In-Memory Vector Store Service ...
|
||||
services.AddXAiInMemoryVectorStore();
|
||||
|
||||
//
|
||||
// Register Default AI Services ...
|
||||
services.AddXAiServices();
|
||||
|
||||
Reference in New Issue
Block a user