review and fix changelogs overview, uncomplete ...
This commit is contained in:
@@ -26,6 +26,7 @@ namespace xAiApi.Constants
|
||||
|
||||
//
|
||||
// Knowledge (Persistent RAG) ...
|
||||
public const string XAiDefaultKnowledgeType = "XAiKnowledge";
|
||||
public const string XAiKnowledgeSeparator = "/";
|
||||
public const string XAiKnowledgeOwnerSeparator = "/";
|
||||
public const string XAiKnowledgeChunkSeparator = "#";
|
||||
|
||||
@@ -4,6 +4,7 @@ using xAiModels.Models.Dtos;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.Collections.Generic;
|
||||
using xIdentityModels.Models;
|
||||
|
||||
namespace xAiApi.Interfaces
|
||||
{
|
||||
@@ -14,16 +15,21 @@ namespace xAiApi.Interfaces
|
||||
public interface IXAiRAGService
|
||||
{
|
||||
/// <summary>
|
||||
/// Ingest Files into Owner's Knowledge Base ...
|
||||
/// Ingest Files into Owner's Knowledge Base:
|
||||
/// Extract -> Chunk -> Embed -> Store (XAiVector) ...
|
||||
/// </summary>
|
||||
/// <param name="files"></param>
|
||||
/// <param name="ownerId"></param>
|
||||
/// <param name="replaceExisting">Remove Previously Stored Vectors of Same File before Store</param>
|
||||
/// <param name="knowledgeType"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <param name="replaceExisting"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<XAiKnowledgeIngestResult> IngestAsync(
|
||||
IFormFileCollection files,
|
||||
string ownerId,
|
||||
string knowledgeType = null,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
string connectionId = null,
|
||||
bool replaceExisting = true,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
@@ -32,7 +38,7 @@ namespace xAiApi.Interfaces
|
||||
/// Embed Prompt and Retrieve Relevant Chunks from Owner's Knowledge Base ...
|
||||
/// </summary>
|
||||
/// <param name="prompt"></param>
|
||||
/// <param name="ownerId"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="topK"></param>
|
||||
/// <param name="minScore"></param>
|
||||
/// <param name="fileName">Optional: Restrict to a Specific File</param>
|
||||
@@ -40,7 +46,7 @@ namespace xAiApi.Interfaces
|
||||
/// <returns></returns>
|
||||
Task<IList<XAiVectorSearchResult>> RetrieveAsync(
|
||||
string prompt,
|
||||
string ownerId,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
int? topK = null,
|
||||
float? minScore = null,
|
||||
string fileName = null,
|
||||
@@ -51,12 +57,12 @@ namespace xAiApi.Interfaces
|
||||
/// Ask a Question with RAG (Retrieve from DB then Generate) ...
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <param name="ownerId"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<XAiDocumentAnswer> AskWithRAGAsync(
|
||||
XAiKnowledgeAskRequest request,
|
||||
string ownerId,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
|
||||
@@ -65,12 +71,12 @@ namespace xAiApi.Interfaces
|
||||
/// 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="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
IAsyncEnumerable<XAiDocumentAnswerChunk> AskWithRAGAsEnumerable(
|
||||
XAiKnowledgeAskRequest request,
|
||||
string ownerId,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ using xAiApi.Constants;
|
||||
using xAiModels.Models;
|
||||
using xAiApi.Extensions;
|
||||
using xAiApi.Interfaces;
|
||||
using xAiService.Helpers;
|
||||
using xAiModels.Helpers;
|
||||
using System.ClientModel;
|
||||
using xCommons.Providers;
|
||||
using xCommons.Extensions;
|
||||
|
||||
+61
-18
@@ -16,6 +16,7 @@ using System.Collections.Generic;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using xAiApi.Interfaces.Extractors;
|
||||
using System.Runtime.CompilerServices;
|
||||
using xIdentityModels.Models;
|
||||
|
||||
namespace xAiApi.Providers
|
||||
{
|
||||
@@ -62,21 +63,37 @@ namespace xAiApi.Providers
|
||||
/// Ingest Files into Owner's Knowledge Base:
|
||||
/// Extract -> Chunk -> Embed -> Store (XAiVector) ...
|
||||
/// </summary>
|
||||
/// <param name="files"></param>
|
||||
/// <param name="knowledgeType"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <param name="replaceExisting"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XAiKnowledgeIngestResult> IngestAsync(
|
||||
IFormFileCollection files,
|
||||
string ownerId,
|
||||
string knowledgeType = null,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
string connectionId = null,
|
||||
bool replaceExisting = true,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (!files.HasChild() || ownerId.IsNullOrEmpty())
|
||||
if (!files.HasChild() || userInfo.IsNullOrDefault())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Normalize ...
|
||||
if (knowledgeType.IsNullOrEmpty())
|
||||
{
|
||||
knowledgeType = XAiApiConstants.XAiDefaultKnowledgeType;
|
||||
}
|
||||
|
||||
//
|
||||
var result = new XAiKnowledgeIngestResult();
|
||||
var ingestedAtUtc = DateTime.UtcNow.ToString("O");
|
||||
|
||||
@@ -111,7 +128,7 @@ namespace xAiApi.Providers
|
||||
|
||||
//
|
||||
// Chunk with Owner Scoped Source => Identifier = "{ownerId}/{fileName}#{index}" ...
|
||||
var documentPrefix = GetDocumentPrefix(ownerId, file.FileName);
|
||||
var documentPrefix = GetDocumentPrefix(userInfo.UserId, file.FileName);
|
||||
var chunks = textChunker.Chunk(text, documentPrefix);
|
||||
if (!chunks.HasChild())
|
||||
{
|
||||
@@ -120,15 +137,22 @@ namespace xAiApi.Providers
|
||||
}
|
||||
|
||||
//
|
||||
// TODO: Implement File Save ...
|
||||
// Handle File Save ...
|
||||
await fileProvider.Upload(
|
||||
file: file,
|
||||
userInfo: userInfo,
|
||||
connectionId: connectionId,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
// Embed in Batches ...
|
||||
var vectors = await EmbedChunks(
|
||||
chunks: chunks,
|
||||
ownerId: ownerId,
|
||||
mimeType: mimeType,
|
||||
type: knowledgeType,
|
||||
fileName: file.FileName,
|
||||
ownerId: userInfo.UserId,
|
||||
ingestedAtUtc: ingestedAtUtc,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
@@ -139,8 +163,8 @@ namespace xAiApi.Providers
|
||||
{
|
||||
//
|
||||
result.ReplacedCount += await dataProvider.RemoveVectorsAsync(
|
||||
identifierPrefix: GetDocumentChunkPrefix(ownerId, file.FileName),
|
||||
cancellationToken: cancellationToken
|
||||
cancellationToken: cancellationToken,
|
||||
identifierPrefix: GetDocumentChunkPrefix(userInfo.UserId, file.FileName)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -165,7 +189,7 @@ namespace xAiApi.Providers
|
||||
//
|
||||
logger.LogInformation(
|
||||
"Knowledge: Ingested {FileName} for {OwnerId} with {Chunks} Chunks",
|
||||
file.FileName, ownerId, storedCount
|
||||
file.FileName, userInfo.UserId, storedCount
|
||||
);
|
||||
}
|
||||
|
||||
@@ -180,9 +204,16 @@ namespace xAiApi.Providers
|
||||
/// <summary>
|
||||
/// Embed Prompt and Retrieve Relevant Chunks from Owner's Knowledge Base ...
|
||||
/// </summary>
|
||||
/// <param name="prompt"></param>
|
||||
/// <param name="userInfo"></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>
|
||||
public async Task<IList<XAiVectorSearchResult>> RetrieveAsync(
|
||||
string prompt,
|
||||
string ownerId,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
int? topK = null,
|
||||
float? minScore = null,
|
||||
string fileName = null,
|
||||
@@ -191,7 +222,7 @@ namespace xAiApi.Providers
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (prompt.IsNullOrEmpty() || ownerId.IsNullOrEmpty())
|
||||
if (prompt.IsNullOrEmpty() || userInfo.IsNullOrDefault())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
@@ -215,8 +246,8 @@ namespace xAiApi.Providers
|
||||
//
|
||||
// Search in DB (Scoped to Owner or Owner + File) ...
|
||||
var prefix = fileName.IsNullOrEmpty()
|
||||
? GetOwnerPrefix(ownerId)
|
||||
: GetDocumentChunkPrefix(ownerId, fileName);
|
||||
? GetOwnerPrefix(userInfo.UserId)
|
||||
: GetDocumentChunkPrefix(userInfo.UserId, fileName);
|
||||
var searchResult = await dataProvider.SearchVectorsAsync(
|
||||
topK: effectiveTopK,
|
||||
identifierPrefix: prefix,
|
||||
@@ -233,9 +264,13 @@ namespace xAiApi.Providers
|
||||
/// <summary>
|
||||
/// Ask a Question with RAG (Retrieve from DB then Generate) ...
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XAiDocumentAnswer> AskWithRAGAsync(
|
||||
XAiKnowledgeAskRequest request,
|
||||
string ownerId,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
@@ -249,7 +284,7 @@ namespace xAiApi.Providers
|
||||
//
|
||||
// Retrieve ...
|
||||
var context = await RetrieveAsync(
|
||||
ownerId: ownerId,
|
||||
userInfo: userInfo,
|
||||
topK: request.TopK,
|
||||
prompt: request.Prompt,
|
||||
minScore: request.MinScore,
|
||||
@@ -283,11 +318,17 @@ namespace xAiApi.Providers
|
||||
|
||||
/// <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="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async IAsyncEnumerable<XAiDocumentAnswerChunk> AskWithRAGAsEnumerable(
|
||||
XAiKnowledgeAskRequest request,
|
||||
string ownerId,
|
||||
[EnumeratorCancellation] CancellationToken cancellationToken = default
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
[EnumeratorCancellation]
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
@@ -300,7 +341,7 @@ namespace xAiApi.Providers
|
||||
//
|
||||
// Retrieve ...
|
||||
var context = await RetrieveAsync(
|
||||
ownerId: ownerId,
|
||||
userInfo: userInfo,
|
||||
topK: request.TopK,
|
||||
prompt: request.Prompt,
|
||||
minScore: request.MinScore,
|
||||
@@ -545,6 +586,7 @@ namespace xAiApi.Providers
|
||||
/// </summary>
|
||||
private async Task<IList<XAiVectorDto>> EmbedChunks(
|
||||
IList<XTextChunk> chunks,
|
||||
string type,
|
||||
string ownerId,
|
||||
string fileName,
|
||||
string mimeType,
|
||||
@@ -583,7 +625,8 @@ namespace xAiApi.Providers
|
||||
var chunk = batch[i];
|
||||
result.Add(new XAiVectorDto
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
//
|
||||
Type = type,
|
||||
Text = chunk.Text,
|
||||
Embedding = embeddings[i],
|
||||
Identifier = chunk.Identifier,
|
||||
|
||||
@@ -2,7 +2,7 @@ using System;
|
||||
using System.Linq;
|
||||
using xAiApi.Models;
|
||||
using xAiApi.Interfaces;
|
||||
using xAiService.Helpers;
|
||||
using xAiModels.Helpers;
|
||||
using xAiModels.Models.Dtos;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
Reference in New Issue
Block a user