Implement Chat With Documents (In-Memory RAG) ...

- Add XAiDocumentModels (IngestResult, AskRequest, Answer, AnswerChunk, Source, SessionInfo, RAGDescriptor DTOs);
- Add AskWithContextAsync and AskWithContextAsEnumerable (Context Actions) to IXAiServiceBase and XAIServiceBase;
- Add GetContextHistory and BuildContextBlock Helpers for Building RAG Chat History without Persisting;
- Rewrite ChatWithDocumentsController with Ingest, Ask, AskStream (SSE), Get Session and Delete Session Endpoints;
- Add XInMemoryVectorStoreCleanupService (BackgroundService) for Removing Expired Sessions Periodically;
- Add AddXAiChatWithDocuments DI Extension, Register Cleanup HostedService and use it in Startup;
- Add RAGDescriptor Property to XAiApiConfiguration and its Section to appsettings.Development.json;
- Add ChatWithDocuments System Prompt to Prompts in appsettings.Development.json;
- Add IsValid Extension Method for XAiDocumentAskRequest;
- Optimize XInMemoryVectorStore.Search (Query Embedding Converted once) and Refactor Formatting;
- Remove Unused Usings in XAIServiceBase and IXAiServiceBase;
- ...
This commit is contained in:
2026-10-25 22:24:39 +03:30
parent 47d8a19423
commit 47da04a594
11 changed files with 1349 additions and 171 deletions
+45 -17
View File
@@ -1,8 +1,8 @@
using System;
using System.Linq;
using xAiApi.Models;
using xAiService.Helpers;
using xAiApi.Interfaces;
using xAiService.Helpers;
using xAiModels.Models.Dtos;
using System.Collections.Generic;
using System.Collections.Concurrent;
@@ -99,10 +99,10 @@ namespace xAiApi.Providers
}
public IList<XAiVectorSearchResult> Search(
Guid sessionId,
ReadOnlyMemory<float> queryEmbedding,
int topK = 5,
float minScore = 0f,
Guid sessionId,
ReadOnlyMemory<float> queryEmbedding,
int topK = 5,
float minScore = 0f,
string ownerId = null
)
{
@@ -121,6 +121,7 @@ namespace xAiApi.Providers
}
//
var query = queryEmbedding.ToArray();
lock (session.SyncRoot)
{
//
@@ -135,7 +136,7 @@ namespace xAiApi.Providers
//
var score = VectorHelper.CosineSimilarity(
queryEmbedding.ToArray(),
query,
vector.Embedding.ToArray()
);
@@ -143,12 +144,13 @@ namespace xAiApi.Providers
if (score >= minScore)
{
//
result.Add(new XAiVectorSearchResult {
result.Add(new XAiVectorSearchResult
{
Score = score,
Id = vector.Id,
Text = vector.Text,
Metadata = vector.Metadata,
Identifier = vector.Identifier,
Id = vector.Id,
Text = vector.Text,
Metadata = vector.Metadata,
Identifier = vector.Identifier,
});
}
}
@@ -161,8 +163,8 @@ namespace xAiApi.Providers
}
public bool TryGet(
Guid sessionId,
out XAiDocumentSession session,
Guid sessionId,
out XAiDocumentSession session,
string ownerId = null
)
{
@@ -176,8 +178,12 @@ namespace xAiApi.Providers
return true;
}
public bool Remove(Guid sessionId, string ownerId = null)
public bool Remove(
Guid sessionId,
string ownerId = null
)
{
//
XAiDocumentSession session;
if (!TryGet(sessionId, out session, ownerId)) return false;
return sessions.TryRemove(sessionId, out session);
@@ -185,13 +191,35 @@ namespace xAiApi.Providers
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(); }
//
#region Private ...
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();
}
#endregion
}
}