From 47d8a1942330502599b8f9db919e32cda3fca27a Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Sun, 25 Oct 2026 17:58:05 +0330 Subject: [PATCH] last ... --- DI/XDIHelperExtension.cs | 8 ++ Interfaces/IXInMemoryVectorStore.cs | 48 +++++++ Models/XAiDocumentSession.cs | 18 +++ Providers/XInMemoryVectorStore.cs | 197 ++++++++++++++++++++++++++++ Startup.cs | 6 +- 5 files changed, 275 insertions(+), 2 deletions(-) create mode 100644 Interfaces/IXInMemoryVectorStore.cs create mode 100644 Models/XAiDocumentSession.cs create mode 100644 Providers/XInMemoryVectorStore.cs diff --git a/DI/XDIHelperExtension.cs b/DI/XDIHelperExtension.cs index 2dfea23..b5e6020 100644 --- a/DI/XDIHelperExtension.cs +++ b/DI/XDIHelperExtension.cs @@ -128,6 +128,14 @@ namespace xAiApi.DI source.AddSingleton(); } + /// + /// Register In-Memory Vector Store Service ... + /// + public static void AddXAiInMemoryVectorStore(this IServiceCollection source) + { + source.AddSingleton(); + } + /// /// Use Model Proxy Provider ... /// diff --git a/Interfaces/IXInMemoryVectorStore.cs b/Interfaces/IXInMemoryVectorStore.cs new file mode 100644 index 0000000..8f19d55 --- /dev/null +++ b/Interfaces/IXInMemoryVectorStore.cs @@ -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 vectors, + string ownerId = null, + TimeSpan? ttl = null + ); + bool Replace( + Guid sessionId, + IEnumerable vectors, + string ownerId = null + ); + bool Add( + Guid sessionId, + IEnumerable vectors, + string ownerId = null + ); + IList Search( + Guid sessionId, + ReadOnlyMemory 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 + ); + } +} diff --git a/Models/XAiDocumentSession.cs b/Models/XAiDocumentSession.cs new file mode 100644 index 0000000..049a191 --- /dev/null +++ b/Models/XAiDocumentSession.cs @@ -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 Vectors { get; set; } = new List(); + public bool IsExpired(DateTime utcNow) { return utcNow - LastAccessAtUtc >= Ttl; } + } +} diff --git a/Providers/XInMemoryVectorStore.cs b/Providers/XInMemoryVectorStore.cs new file mode 100644 index 0000000..8c58c37 --- /dev/null +++ b/Providers/XInMemoryVectorStore.cs @@ -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 sessions = new ConcurrentDictionary(); + 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 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 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 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 Search( + Guid sessionId, + ReadOnlyMemory queryEmbedding, + int topK = 5, + float minScore = 0f, + string ownerId = null + ) + { + // + var result = new List(); + 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 Copy(IEnumerable vectors) { return vectors == null ? new List() : vectors.Where(x => x != null).ToList(); } + } +} diff --git a/Startup.cs b/Startup.cs index 7b1ccf1..bffd5cc 100644 --- a/Startup.cs +++ b/Startup.cs @@ -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();