- Add IXAiRAGService and XAiRAGService for Extract -> Chunk -> Embed -> Store;
- Add synchronous and streaming RAG answers with sources;
- Add Knowledge models and KnowledgeController endpoints;
- Register IXAiRAGService as scoped;
- Keep current identifier: {ownerId}/{fileName}#{chunkIndex}.
111 lines
3.9 KiB
C#
111 lines
3.9 KiB
C#
using xAiApi.Models;
|
|
using System.Threading;
|
|
using xAiModels.Models.Dtos;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Http;
|
|
using System.Collections.Generic;
|
|
|
|
namespace xAiApi.Interfaces
|
|
{
|
|
/// <summary>
|
|
/// Persistent RAG Service over XAiVector Table:
|
|
/// Ingest (Extract -> Chunk -> Embed -> Store) and Ask (Embed -> Search -> Generate) ...
|
|
/// </summary>
|
|
public interface IXAiRAGService
|
|
{
|
|
/// <summary>
|
|
/// Ingest Files into Owner's Knowledge Base ...
|
|
/// </summary>
|
|
/// <param name="files"></param>
|
|
/// <param name="ownerId"></param>
|
|
/// <param name="replaceExisting">Remove Previously Stored Vectors of Same File before Store</param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
Task<XAiKnowledgeIngestResult> IngestAsync(
|
|
IFormFileCollection files,
|
|
string ownerId,
|
|
bool replaceExisting = true,
|
|
CancellationToken cancellationToken = default
|
|
);
|
|
|
|
/// <summary>
|
|
/// Embed Prompt and Retrieve Relevant Chunks from Owner's Knowledge Base ...
|
|
/// </summary>
|
|
/// <param name="prompt"></param>
|
|
/// <param name="ownerId"></param>
|
|
/// <param name="topK"></param>
|
|
/// <param name="minScore"></param>
|
|
/// <param name="fileName">Optional: Restrict to a Specific File</param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
Task<IList<XAiVectorSearchResult>> RetrieveAsync(
|
|
string prompt,
|
|
string ownerId,
|
|
int? topK = null,
|
|
float? minScore = null,
|
|
string fileName = null,
|
|
CancellationToken cancellationToken = default
|
|
);
|
|
|
|
/// <summary>
|
|
/// Ask a Question with RAG (Retrieve from DB then Generate) ...
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <param name="ownerId"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
Task<XAiDocumentAnswer> AskWithRAGAsync(
|
|
XAiKnowledgeAskRequest request,
|
|
string ownerId,
|
|
CancellationToken cancellationToken = default
|
|
);
|
|
|
|
/// <summary>
|
|
/// Ask a Question with RAG and Stream Response ...
|
|
/// First Item Carries Sources, Next Items Carry Text Deltas, Last Item has IsFinal = true ...
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <param name="ownerId"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
IAsyncEnumerable<XAiDocumentAnswerChunk> AskWithRAGAsEnumerable(
|
|
XAiKnowledgeAskRequest request,
|
|
string ownerId,
|
|
CancellationToken cancellationToken = default
|
|
);
|
|
|
|
/// <summary>
|
|
/// List Ingested Documents of Owner ...
|
|
/// </summary>
|
|
/// <param name="ownerId"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
Task<IList<XAiKnowledgeDocumentInfo>> LoadDocumentsAsync(
|
|
string ownerId,
|
|
CancellationToken cancellationToken = default
|
|
);
|
|
|
|
/// <summary>
|
|
/// Remove All Vectors of a Document ...
|
|
/// </summary>
|
|
/// <param name="ownerId"></param>
|
|
/// <param name="fileName"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns>Count of Removed Vectors</returns>
|
|
Task<int> RemoveDocumentAsync(
|
|
string ownerId,
|
|
string fileName,
|
|
CancellationToken cancellationToken = default
|
|
);
|
|
|
|
/// <summary>
|
|
/// Map Search Results to Public Sources ...
|
|
/// </summary>
|
|
/// <param name="context"></param>
|
|
/// <returns></returns>
|
|
IList<XAiDocumentSource> ToSources(
|
|
IEnumerable<XAiVectorSearchResult> context
|
|
);
|
|
}
|
|
}
|