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.
This commit is contained in:
@@ -54,7 +54,7 @@ namespace xAiApi.Controllers
|
||||
/// <summary>
|
||||
/// Embedding Service ...
|
||||
/// </summary>
|
||||
private readonly IXDefaultEmbeddingService embeddingService;
|
||||
private readonly IXAiEmbeddingService embeddingService;
|
||||
|
||||
/// <summary>
|
||||
/// File Context Extractor Service ...
|
||||
@@ -68,8 +68,8 @@ namespace xAiApi.Controllers
|
||||
XAppConfiguration appConfiguration,
|
||||
IXIdentityProvider identityProvider,
|
||||
XAiApiConfiguration apiConfiguration,
|
||||
IXAiEmbeddingService embeddingService,
|
||||
XValidationProvider validationProvider,
|
||||
IXDefaultEmbeddingService embeddingService,
|
||||
ILogger<ChatWithDocumentsController> logger,
|
||||
IXFileContentExtractor fileContentExtractor
|
||||
) : base(logger, appConfiguration, identityProvider, validationProvider)
|
||||
|
||||
@@ -10,20 +10,20 @@ namespace xAiApi.Controllers
|
||||
{
|
||||
[Authorize]
|
||||
[Route("[controller]")]
|
||||
public class DefaultEmbeddingController : XAiEmbeddingServiceControllerBase<IXDefaultEmbeddingService>
|
||||
public class DefaultEmbeddingController : XAiEmbeddingServiceControllerBase<IXAiEmbeddingService>
|
||||
{
|
||||
public DefaultEmbeddingController(
|
||||
ILogger<DefaultEmbeddingController> logger,
|
||||
IXDefaultEmbeddingService aiService,
|
||||
IXAiEmbeddingService embeddingService,
|
||||
XAppConfiguration appConfiguration,
|
||||
IXIdentityProvider identityProvider,
|
||||
XValidationProvider validationProvider
|
||||
) : base(
|
||||
logger,
|
||||
aiService,
|
||||
appConfiguration,
|
||||
identityProvider,
|
||||
validationProvider
|
||||
logger: logger,
|
||||
appConfiguration: appConfiguration,
|
||||
identityProvider: identityProvider,
|
||||
embeddingService: embeddingService,
|
||||
validationProvider: validationProvider
|
||||
)
|
||||
{ }
|
||||
}
|
||||
|
||||
@@ -40,7 +40,6 @@ namespace xAiApi.Controllers
|
||||
XValidationProvider validationProvider
|
||||
) : base(logger, appConfiguration, identityProvider, validationProvider)
|
||||
{
|
||||
//
|
||||
this.ragService = ragService;
|
||||
}
|
||||
|
||||
@@ -104,137 +103,6 @@ namespace xAiApi.Controllers
|
||||
}
|
||||
#endregion
|
||||
|
||||
//
|
||||
#region Ask ...
|
||||
/// <summary>
|
||||
/// Ask a Question over Knowledge Base (RAG) ...
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost("Ask")]
|
||||
public async Task<ActionResult<XAiDocumentAnswer>> Ask(
|
||||
[FromBody] XAiKnowledgeAskRequest request,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (request.IsNullOrDefault() || request.Prompt.IsNullOrEmpty())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var userInfo = await GetUserInfo();
|
||||
|
||||
//
|
||||
var result = await ragService.AskWithRAGAsync(
|
||||
request: request,
|
||||
userInfo: userInfo,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ask a Question over Knowledge Base and Stream Response (SSE) ...
|
||||
/// First Event Carries Sources, Next Events Carry Text Deltas, Last Event has isFinal = true ...
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost("AskStream")]
|
||||
public async Task AskStream(
|
||||
[FromBody] XAiKnowledgeAskRequest request,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate (before Stream Starts -> Proper Http Error) ...
|
||||
if (request.IsNullOrDefault() || request.Prompt.IsNullOrEmpty())
|
||||
{
|
||||
Response.StatusCode = StatusCodes.Status400BadRequest;
|
||||
Response.ContentType = "application/json; charset=utf-8";
|
||||
await Response.WriteAsync(XException.InvalidArgs.ToXError().ToJSON());
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
var userInfo = await GetUserInfo();
|
||||
|
||||
//
|
||||
await using var enumerator = ragService
|
||||
.AskWithRAGAsEnumerable(
|
||||
request: request,
|
||||
userInfo: userInfo,
|
||||
cancellationToken: cancellationToken
|
||||
)
|
||||
.GetAsyncEnumerator(cancellationToken);
|
||||
|
||||
//
|
||||
// Pull First Item before Starting Stream, so Scope / Ownership /
|
||||
// Retrieval Errors are Returned as Regular Http Errors ...
|
||||
var hasItem = await enumerator.MoveNextAsync();
|
||||
|
||||
//
|
||||
// Configure SSE Response ...
|
||||
Response.PrepareSseResponse();
|
||||
|
||||
//
|
||||
while (hasItem)
|
||||
{
|
||||
//
|
||||
var chunk = enumerator.Current;
|
||||
if (!chunk.IsNullOrDefault())
|
||||
{
|
||||
await WriteChunk(chunk, cancellationToken);
|
||||
}
|
||||
|
||||
//
|
||||
hasItem = await enumerator.MoveNextAsync();
|
||||
}
|
||||
|
||||
//
|
||||
await Response.WriteSseDoneAsync(cancellationToken);
|
||||
}
|
||||
catch (Exception ex) when (ex.IsClientCancellation(cancellationToken))
|
||||
{
|
||||
// Client Disconnected ...
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
Logger.LogError(ex, "Knowledge: AskStream Failed");
|
||||
if (!Response.HasStarted)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
await result.ExecuteResultAsync(ControllerContext);
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
await Response.WriteSseErrorAsync(ex.Message);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
//
|
||||
#region Documents ...
|
||||
/// <summary>
|
||||
|
||||
@@ -8,32 +8,32 @@ using Microsoft.AspNetCore.Mvc;
|
||||
using System.Collections.Generic;
|
||||
using xIdentityService.Interfaces;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using xIdentityService.Controllers;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
|
||||
namespace xAiApi.Controllers
|
||||
{
|
||||
[Authorize]
|
||||
[Route("[controller]")]
|
||||
public abstract class XAiEmbeddingServiceControllerBase<TAiService> : XAiServiceControllerBase<TAiService>, IXAiEmbeddingServiceControllerBase
|
||||
where TAiService : IXAiEmbeddingServiceBase
|
||||
public abstract class XAiEmbeddingServiceControllerBase<TEmbeddingService> : XBaseIdentityApiV1Controller, IXAiEmbeddingServiceControllerBase
|
||||
where TEmbeddingService : IXAiEmbeddingServiceBase
|
||||
{
|
||||
private readonly TAiService aiService;
|
||||
private readonly TEmbeddingService embeddingService;
|
||||
|
||||
protected XAiEmbeddingServiceControllerBase(
|
||||
ILogger logger,
|
||||
TAiService aiService,
|
||||
XAppConfiguration appConfiguration,
|
||||
IXIdentityProvider identityProvider,
|
||||
XValidationProvider validationProvider
|
||||
XValidationProvider validationProvider,
|
||||
TEmbeddingService embeddingService
|
||||
) : base(
|
||||
logger,
|
||||
aiService,
|
||||
appConfiguration,
|
||||
identityProvider,
|
||||
validationProvider
|
||||
)
|
||||
{
|
||||
this.aiService = aiService;
|
||||
this.embeddingService = embeddingService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -57,7 +57,7 @@ namespace xAiApi.Controllers
|
||||
ValidationProvider.NotEmpty(content);
|
||||
|
||||
//
|
||||
var result = await aiService.Embedding(
|
||||
var result = await embeddingService.Embedding(
|
||||
content: content,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
@@ -94,7 +94,7 @@ namespace xAiApi.Controllers
|
||||
ValidationProvider.NotZeroChilds(batch);
|
||||
|
||||
//
|
||||
var result = await aiService.Embeddings(
|
||||
var result = await embeddingService.Embeddings(
|
||||
batch: batch,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
@@ -689,9 +689,9 @@ namespace xAiApi.Controllers
|
||||
prompt: request.Prompt,
|
||||
ownerId: userInfo.UserId,
|
||||
connectionId: connectionId,
|
||||
projectId: request.ProjectId,
|
||||
projectId: request.ProjectId.Value,
|
||||
cancellationToken: cancellationToken,
|
||||
conversationId: request.ConversationId
|
||||
conversationId: request.ConversationId.Value
|
||||
);
|
||||
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user