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