Files
xSaherElmAiApi/Models/XAiKnowledgeModels.cs
T
saherelm ad8ea9c1dd Integrate RAG into Chat Pipeline and Decouple Embedding Service
- AskAsync: accept Guid? projectId / conversationId; resolve defaults;
- AskAsync: ingest attached files into Conversation scope (replaceExisting)
  via IXAiRAGService instead of Upload + Extract + raw content attachment;
- AskAsync: retrieve context (Conversation -> Project -> User -> Global)
  and build a single System message: Introduction + ChatWithDocuments prompt
  + Context block (+ Project prompt);
- XAIServiceBase / XDefaultAiService / XDefaultThinkingAiService:
  inject IXAiRAGService instead of IXFileProvider;
- Add IXAiEmbeddingService + XAiEmbeddingService; remove
  IXDefaultEmbeddingService + XDefaultEmbeddingService;
- XAiEmbeddingServiceBase no longer inherits XAIServiceBase;
  IXAiEmbeddingServiceBase / IXAiEmbeddingServiceControllerBase no longer
  extend chat interfaces; XAiEmbeddingServiceControllerBase derives from
  XBaseIdentityApiV1Controller;
- XAiRAGService: drop IXDefaultAiService dependency (removes circular
  dependency); remove AskWithRAGAsync / AskWithRAGAsEnumerable; add file
  reference (AddReference) for scoped ingests;
- KnowledgeController: remove POST Ask and POST AskStream;
- DI: add AddXRagService() and call it from Startup before AddXAiServices();
- Add XAiApiRagPromptName = "ChatWithDocuments";
- Knowledge scope factories default Type to XAiDefaultKnowledgeType;
- ChatWithDocuments ask request: TopK and MinScore are now required.
BREAKING: /Knowledge/Ask and /Knowledge/AskStream removed;
          IXDefaultEmbeddingService replaced by IXAiEmbeddingService.
2026-10-30 19:46:20 +03:30

256 lines
7.6 KiB
C#

using System;
using xAiModels.Constants;
using System.Collections.Generic;
namespace xAiApi.Models
{
/// <summary>
/// Describes Target Knowledge Base Scope of an Operation ...
/// </summary>
public class XAiKnowledgeScopeDescriptor
{
/// <summary>
/// Scope Kind ...
/// </summary>
public XAiKnowledgeScope Scope { get; set; } = XAiKnowledgeScope.User;
/// <summary>
/// Scope Identifier (ProjectId / ConversationId) - Null for Global and User ...
/// </summary>
public Guid? ScopeId { get; set; }
/// <summary>
/// Optional Knowledge Type / Domain Filter ...
/// </summary>
public string Type { get; set; }
/// <summary>
/// Create User Scope Knowledge Descriptor ...
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public static XAiKnowledgeScopeDescriptor User(
string type = null
) => new()
{
//
Type = type ?? XAiModelsConstants.XAiDefaultKnowledgeType,
ScopeId = null,
Scope = XAiKnowledgeScope.User,
};
/// <summary>
/// Create Global Scope Knowledge Descriptor ...
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public static XAiKnowledgeScopeDescriptor Global(
string type = null
) => new()
{
Type = type ?? XAiModelsConstants.XAiDefaultKnowledgeType,
ScopeId = null,
Scope = XAiKnowledgeScope.Global,
};
/// <summary>
/// Create Project Scope Knowledge Descriptor ...
/// </summary>
/// <param name="projectId"></param>
/// <param name="type"></param>
/// <returns></returns>
public static XAiKnowledgeScopeDescriptor Project(
Guid projectId,
string type = null
) => new()
{
Type = type ?? XAiModelsConstants.XAiDefaultKnowledgeType,
ScopeId = projectId,
Scope = XAiKnowledgeScope.Project,
};
/// <summary>
/// Create Conversational Scope Knowledge Descriptor ...
/// </summary>
/// <param name="conversationId"></param>
/// <param name="type"></param>
/// <returns></returns>
public static XAiKnowledgeScopeDescriptor Conversation(
Guid conversationId,
string type = null
) => new()
{
Type = type ?? XAiModelsConstants.XAiDefaultKnowledgeType,
ScopeId = conversationId,
Scope = XAiKnowledgeScope.Conversation,
};
}
/// <summary>
/// Result of Ingesting Files into Persistent Knowledge Base (XAiVector Table) ...
/// </summary>
public class XAiKnowledgeIngestResult
{
/// <summary>
/// Target Scope ...
/// </summary>
public XAiKnowledgeScope Scope { get; set; }
/// <summary>
/// Target Scope Identifier (Null for Global) ...
/// </summary>
public Guid? ScopeId { get; set; }
/// <summary>
/// Knowledge Type of Ingested Vectors ...
/// </summary>
public string Type { get; set; }
/// <summary>
/// Count of Successfully Ingested Files ...
/// </summary>
public int FilesCount { get; set; }
/// <summary>
/// Count of Stored Chunks (Vectors) in this Request ...
/// </summary>
public int ChunksCount { get; set; }
/// <summary>
/// Count of Removed Stale Vectors (Re-Ingest) ...
/// </summary>
public int ReplacedCount { get; set; }
/// <summary>
/// Skipped Files (Empty, Unsupported or Extraction Failed) ...
/// </summary>
public IList<string> SkippedFiles { get; set; } = new List<string>();
/// <summary>
/// Ingested Documents Info ...
/// </summary>
public IList<XAiKnowledgeDocumentInfo> Documents { get; set; } = new List<XAiKnowledgeDocumentInfo>();
}
/// <summary>
/// Ask Request over Persistent Knowledge Base ...
/// Retrieval is Tiered: Conversation -> Project -> User -> Global (Each Tier Fills Remaining TopK) ...
/// </summary>
public class XAiKnowledgeAskRequest
{
/// <summary>
/// Question ...
/// </summary>
public string Prompt { get; set; }
/// <summary>
/// Optional: Conversation Scope (Caller must be Conversation Owner) ...
/// When Provided, Conversation's Project is used for Second Tier Automatically ...
/// </summary>
public Guid? ConversationId { get; set; }
/// <summary>
/// Optional: Project Scope (Caller must be Project Owner) ...
/// </summary>
public Guid? ProjectId { get; set; }
/// <summary>
/// Include Personal (User) Knowledge Tier (default true) ...
/// </summary>
public bool IncludeUser { get; set; } = true;
/// <summary>
/// Include Global Knowledge Tier (default true) ...
/// </summary>
public bool IncludeGlobal { get; set; } = true;
/// <summary>
/// Optional: Restrict Retrieval to a Knowledge Type / Domain ...
/// </summary>
public string Type { get; set; }
/// <summary>
/// Optional: Restrict Retrieval to a Specific File Name ...
/// </summary>
public string FileName { get; set; }
/// <summary>
/// Optional: Max Retrieved Chunks ...
/// </summary>
public int? TopK { get; set; }
/// <summary>
/// Optional: Minimum Cosine Similarity Score ...
/// </summary>
public float? MinScore { get; set; }
}
/// <summary>
/// Ingested Document Info ...
/// </summary>
public class XAiKnowledgeDocumentInfo
{
/// <summary>
/// Scope ...
/// </summary>
public XAiKnowledgeScope Scope { get; set; }
/// <summary>
/// Scope Identifier (Null for Global) ...
/// </summary>
public Guid? ScopeId { get; set; }
/// <summary>
/// Knowledge Type ...
/// </summary>
public string Type { get; set; }
/// <summary>
/// File Name ...
/// </summary>
public string FileName { get; set; }
/// <summary>
/// Mime Type ...
/// </summary>
public string MimeType { get; set; }
/// <summary>
/// Ingesting User Identifier ...
/// </summary>
public string OwnerId { get; set; }
/// <summary>
/// Count of Stored Chunks ...
/// </summary>
public int ChunksCount { get; set; }
/// <summary>
/// Ingestion Time (UTC) ...
/// </summary>
public DateTime? IngestedAtUtc { get; set; }
}
/// <summary>
/// Public Vector Info (Export) ...
/// </summary>
public class XAiKnowledgeVectorInfo
{
public Guid Id { get; set; }
public string Text { get; set; }
public string Type { get; set; }
public Guid? ScopeId { get; set; }
public string FileName { get; set; }
public int? ChunkIndex { get; set; }
public string Identifier { get; set; }
public XAiKnowledgeScope Scope { get; set; }
public Dictionary<string, string> Metadata { get; set; }
/// <summary>
/// Embedding (Only Filled when Requested) ...
/// </summary>
public float[] Embedding { get; set; }
}
}