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:
@@ -0,0 +1,181 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace xAiApi.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// Result of Ingesting Documents into an In-Memory Session ...
|
||||
/// </summary>
|
||||
public class XAiDocumentIngestResult
|
||||
{
|
||||
/// <summary>
|
||||
/// Session Identifier which must be used in Ask Requests ...
|
||||
/// </summary>
|
||||
public Guid SessionId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Count of Successfully Processed Files ...
|
||||
/// </summary>
|
||||
public int FilesCount { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Count of Chunks (Vectors) Added to Session ...
|
||||
/// </summary>
|
||||
public int ChunksCount { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Total Vectors Count in Session after Ingest ...
|
||||
/// </summary>
|
||||
public int TotalVectorsCount { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Files which Skipped (Unsupported / Empty / Failed) ...
|
||||
/// </summary>
|
||||
public IList<string> SkippedFiles { get; set; } = new List<string>();
|
||||
|
||||
/// <summary>
|
||||
/// Session Expiration Time (UTC) based on Last Access ...
|
||||
/// </summary>
|
||||
public DateTime ExpiresAtUtc { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Request for Asking a Question over Ingested Documents ...
|
||||
/// </summary>
|
||||
public class XAiDocumentAskRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// Session Identifier ...
|
||||
/// </summary>
|
||||
public Guid SessionId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// User Question ...
|
||||
/// </summary>
|
||||
public string Prompt { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Count of Top Chunks used as Context (Optional) ...
|
||||
/// </summary>
|
||||
public int? TopK { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Minimum Similarity Score of Chunks (Optional) ...
|
||||
/// </summary>
|
||||
public float? MinScore { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// a Source Chunk which used for Generating Answer ...
|
||||
/// </summary>
|
||||
public class XAiDocumentSource
|
||||
{
|
||||
/// <summary>
|
||||
/// Doc and Chunk Identifier (e.g. "file.pdf#3") ...
|
||||
/// </summary>
|
||||
public string Identifier { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Source File Name ...
|
||||
/// </summary>
|
||||
public string FileName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Chunk Index in Source File ...
|
||||
/// </summary>
|
||||
public int? ChunkIndex { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Similarity Score ...
|
||||
/// </summary>
|
||||
public float Score { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Short Snippet of Chunk Text ...
|
||||
/// </summary>
|
||||
public string Snippet { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Answer of a Chat With Documents Request ...
|
||||
/// </summary>
|
||||
public class XAiDocumentAnswer
|
||||
{
|
||||
/// <summary>
|
||||
/// Session Identifier ...
|
||||
/// </summary>
|
||||
public Guid SessionId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Generated Answer ...
|
||||
/// </summary>
|
||||
public string Answer { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// True when no Relevant Chunk Found in Session ...
|
||||
/// </summary>
|
||||
public bool NoContextFound { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Sources used for Generating Answer ...
|
||||
/// </summary>
|
||||
public IList<XAiDocumentSource> Sources { get; set; } = new List<XAiDocumentSource>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Streaming Chunk of a Chat With Documents Answer ...
|
||||
/// </summary>
|
||||
public class XAiDocumentAnswerChunk
|
||||
{
|
||||
/// <summary>
|
||||
/// Text Delta ...
|
||||
/// </summary>
|
||||
public string Text { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// True for Last Chunk ...
|
||||
/// </summary>
|
||||
public bool IsFinal { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// True when no Relevant Chunk Found in Session ...
|
||||
/// </summary>
|
||||
public bool NoContextFound { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Sources (Only Filled in First Chunk) ...
|
||||
/// </summary>
|
||||
public IList<XAiDocumentSource> Sources { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Public Info of a Document Session (without Embeddings) ...
|
||||
/// </summary>
|
||||
public class XAiDocumentSessionInfo
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public TimeSpan Ttl { get; set; }
|
||||
public int VectorsCount { get; set; }
|
||||
public DateTime CreatedAtUtc { get; set; }
|
||||
public DateTime LastAccessAtUtc { get; set; }
|
||||
public DateTime ExpiresAtUtc { get; set; }
|
||||
public IList<string> Files { get; set; } = new List<string>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Document RAG Configuration ...
|
||||
/// </summary>
|
||||
public class XAiDocumentRAGDescriptor
|
||||
{
|
||||
public int MaxTopK { get; set; } = 20;
|
||||
public int DefaultTopK { get; set; } = 5;
|
||||
public int DefaultTtlMinutes { get; set; } = 30;
|
||||
public int EmbeddingBatchSize { get; set; } = 32;
|
||||
public int SourceSnippetLength { get; set; } = 200;
|
||||
public float DefaultMinScore { get; set; } = 0.35f;
|
||||
public string MetadataFileName { get; set; } = "fileName";
|
||||
public string MetadataMimeType { get; set; } = "mimeType";
|
||||
public string PromptName { get; set; } = "ChatWithDocuments";
|
||||
public string MetadataChunkIndex { get; set; } = "chunkIndex";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user