last ...
This commit is contained in:
@@ -1,30 +0,0 @@
|
|||||||
using xAiApi.Interfaces;
|
|
||||||
using xCommons.Providers;
|
|
||||||
using xCommons.Configurations;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
using xIdentityService.Interfaces;
|
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using Microsoft.AspNetCore.Authorization;
|
|
||||||
|
|
||||||
namespace xAiApi.Controllers
|
|
||||||
{
|
|
||||||
[Authorize]
|
|
||||||
[Route("[controller]")]
|
|
||||||
public class DefaultEmbeddingController : XAiEmbeddingServiceControllerBase<IXAiEmbeddingService>
|
|
||||||
{
|
|
||||||
public DefaultEmbeddingController(
|
|
||||||
ILogger<DefaultEmbeddingController> logger,
|
|
||||||
IXAiEmbeddingService embeddingService,
|
|
||||||
XAppConfiguration appConfiguration,
|
|
||||||
IXIdentityProvider identityProvider,
|
|
||||||
XValidationProvider validationProvider
|
|
||||||
) : base(
|
|
||||||
logger: logger,
|
|
||||||
appConfiguration: appConfiguration,
|
|
||||||
identityProvider: identityProvider,
|
|
||||||
embeddingService: embeddingService,
|
|
||||||
validationProvider: validationProvider
|
|
||||||
)
|
|
||||||
{ }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
using xAiApi.Interfaces;
|
|
||||||
using xCommons.Providers;
|
|
||||||
using xCommons.Configurations;
|
|
||||||
using xIdentityService.Interfaces;
|
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
|
|
||||||
namespace xAiApi.Controllers
|
|
||||||
{
|
|
||||||
public class DefaultThinkingAiController : XAiServiceControllerBase<IXDefaultThinkingAiService>
|
|
||||||
{
|
|
||||||
public DefaultThinkingAiController(
|
|
||||||
ILogger<DefaultThinkingAiController> logger,
|
|
||||||
IXDefaultThinkingAiService aiService,
|
|
||||||
XAppConfiguration appConfiguration,
|
|
||||||
IXIdentityProvider identityProvider,
|
|
||||||
XValidationProvider validationProvider
|
|
||||||
) : base(
|
|
||||||
logger,
|
|
||||||
aiService,
|
|
||||||
appConfiguration,
|
|
||||||
identityProvider,
|
|
||||||
validationProvider
|
|
||||||
)
|
|
||||||
{ }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,113 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Threading;
|
|
||||||
using xAiApi.Interfaces;
|
|
||||||
using xCommons.Providers;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using xCommons.Configurations;
|
|
||||||
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<TEmbeddingService> : XBaseIdentityApiV1Controller, IXAiEmbeddingServiceControllerBase
|
|
||||||
where TEmbeddingService : IXAiEmbeddingServiceBase
|
|
||||||
{
|
|
||||||
private readonly TEmbeddingService embeddingService;
|
|
||||||
|
|
||||||
protected XAiEmbeddingServiceControllerBase(
|
|
||||||
ILogger logger,
|
|
||||||
XAppConfiguration appConfiguration,
|
|
||||||
IXIdentityProvider identityProvider,
|
|
||||||
XValidationProvider validationProvider,
|
|
||||||
TEmbeddingService embeddingService
|
|
||||||
) : base(
|
|
||||||
logger,
|
|
||||||
appConfiguration,
|
|
||||||
identityProvider,
|
|
||||||
validationProvider
|
|
||||||
)
|
|
||||||
{
|
|
||||||
this.embeddingService = embeddingService;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Request For Embedding Specified String ...
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="content"></param>
|
|
||||||
/// <param name="cancellationToken"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
[AllowAnonymous]
|
|
||||||
[HttpGet("Embedding")]
|
|
||||||
public async Task<ActionResult<float[]>> Embedding(
|
|
||||||
[FromQuery] string content,
|
|
||||||
CancellationToken cancellationToken = default
|
|
||||||
)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
try
|
|
||||||
{
|
|
||||||
//
|
|
||||||
// Validate ...
|
|
||||||
ValidationProvider.NotEmpty(content);
|
|
||||||
|
|
||||||
//
|
|
||||||
var result = await embeddingService.Embedding(
|
|
||||||
content: content,
|
|
||||||
cancellationToken: cancellationToken
|
|
||||||
);
|
|
||||||
|
|
||||||
//
|
|
||||||
return Ok(result);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
var result = GetExceptionActionResult(ex);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Request For Embedding Specified Batch ...
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="batch"></param>
|
|
||||||
/// <param name="cancellationToken"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
[AllowAnonymous]
|
|
||||||
[HttpGet("Embeddings")]
|
|
||||||
public async Task<ActionResult<IReadOnlyList<float[]>>> Embeddings(
|
|
||||||
[FromQuery] string[] batch,
|
|
||||||
CancellationToken cancellationToken = default
|
|
||||||
)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
try
|
|
||||||
{
|
|
||||||
//
|
|
||||||
// Validate ...
|
|
||||||
ValidationProvider.NotZeroChilds(batch);
|
|
||||||
|
|
||||||
//
|
|
||||||
var result = await embeddingService.Embeddings(
|
|
||||||
batch: batch,
|
|
||||||
cancellationToken: cancellationToken
|
|
||||||
);
|
|
||||||
|
|
||||||
//
|
|
||||||
return Ok(result);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
var result = GetExceptionActionResult(ex);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -162,16 +162,282 @@ namespace xAiApi.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Write Validation Error for Streaming Endpoints (before Stream Starts) ...
|
/// Ask Question and Get Response ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="request"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[NonAction]
|
[HttpPost("Ask")]
|
||||||
protected async Task WriteStreamValidationError()
|
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||||
|
public async Task<ActionResult<string>> Ask(
|
||||||
|
[FromBody] XAiResponseRequest request,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
Response.StatusCode = StatusCodes.Status400BadRequest;
|
try
|
||||||
Response.ContentType = "application/json; charset=utf-8";
|
{
|
||||||
await Response.WriteAsync(XException.InvalidArgs.ToXError().ToJSON());
|
//
|
||||||
|
// Validate ...
|
||||||
|
if (!request.IsValid())
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
var userInfo = await GetUserInfo();
|
||||||
|
var connectionId = GetConnectionId();
|
||||||
|
|
||||||
|
//
|
||||||
|
var result = await aiService.AskAsync(
|
||||||
|
files: null,
|
||||||
|
topK: request.TopK,
|
||||||
|
userInfo: userInfo,
|
||||||
|
prompt: request.Prompt,
|
||||||
|
minScore: request.MinScore,
|
||||||
|
connectionId: connectionId,
|
||||||
|
projectId: request.ProjectId,
|
||||||
|
cancellationToken: cancellationToken,
|
||||||
|
conversationId: request.ConversationId
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
return Ok(result);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = GetExceptionActionResult(ex);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Ask Question and Get Response Stream ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="request"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpPost("AskStream")]
|
||||||
|
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||||
|
public async Task AskStream(
|
||||||
|
[FromBody] XAiResponseRequest request,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate (before Stream Starts -> Proper Http Error) ...
|
||||||
|
if (!request.IsValid())
|
||||||
|
{
|
||||||
|
await WriteStreamValidationError();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var userInfo = await GetUserInfo();
|
||||||
|
var connectionId = GetConnectionId();
|
||||||
|
|
||||||
|
//
|
||||||
|
// Configure SSE Response ...
|
||||||
|
Response.PrepareSseResponse();
|
||||||
|
|
||||||
|
//
|
||||||
|
var enumerable = aiService.AskAsEnumerable(
|
||||||
|
files: null,
|
||||||
|
topK: request.TopK,
|
||||||
|
userInfo: userInfo,
|
||||||
|
prompt: request.Prompt,
|
||||||
|
minScore: request.MinScore,
|
||||||
|
connectionId: connectionId,
|
||||||
|
projectId: request.ProjectId,
|
||||||
|
cancellationToken: cancellationToken,
|
||||||
|
conversationId: request.ConversationId
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Format: data: {json}\n\n
|
||||||
|
await foreach (var resp in enumerable)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Skip Empty Responses ...
|
||||||
|
if (resp.IsNullOrDefault())
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
await Response.WriteSseEventAsync(
|
||||||
|
data: resp,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
await Response.WriteSseDoneAsync(cancellationToken);
|
||||||
|
}
|
||||||
|
catch (Exception ex) when (ex.IsClientCancellation(cancellationToken))
|
||||||
|
{
|
||||||
|
// Client Disconnected ...
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
Logger.LogError(ex, "AskStream Failed ...");
|
||||||
|
if (!Response.HasStarted)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = GetExceptionActionResult(ex);
|
||||||
|
await result.ExecuteResultAsync(ControllerContext);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
await Response.WriteSseErrorAsync(ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Ask Question with Files and Get Response ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="request"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpPost("AskWithFiles")]
|
||||||
|
[Consumes("multipart/form-data")]
|
||||||
|
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||||
|
public async Task<ActionResult<string>> AskWithFiles(
|
||||||
|
[FromForm] XAiResponseRequest request,
|
||||||
|
[FromForm] IFormFileCollection files,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate ...
|
||||||
|
if (!request.IsValid())
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
var userInfo = await GetUserInfo();
|
||||||
|
var connectionId = GetConnectionId();
|
||||||
|
|
||||||
|
//
|
||||||
|
var result = await aiService.AskAsync(
|
||||||
|
files: null,
|
||||||
|
topK: request.TopK,
|
||||||
|
userInfo: userInfo,
|
||||||
|
prompt: request.Prompt,
|
||||||
|
minScore: request.MinScore,
|
||||||
|
connectionId: connectionId,
|
||||||
|
projectId: request.ProjectId,
|
||||||
|
cancellationToken: cancellationToken,
|
||||||
|
conversationId: request.ConversationId
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
return Ok(result);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
var result = GetExceptionActionResult(ex);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Ask Question with Files and Get Response Stream ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="request"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpPost("AskWithFilesStream")]
|
||||||
|
[Consumes("multipart/form-data")]
|
||||||
|
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||||
|
public async Task AskWithFilesStream(
|
||||||
|
[FromForm] XAiResponseRequest request,
|
||||||
|
[FromForm] IFormFileCollection files,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate (before Stream Starts -> Proper Http Error) ...
|
||||||
|
if (!request.IsValid())
|
||||||
|
{
|
||||||
|
await WriteStreamValidationError();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var userInfo = await GetUserInfo();
|
||||||
|
var connectionId = GetConnectionId();
|
||||||
|
|
||||||
|
//
|
||||||
|
// Configure SSE Response ...
|
||||||
|
Response.PrepareSseResponse();
|
||||||
|
|
||||||
|
//
|
||||||
|
var enumerable = aiService.AskAsEnumerable(
|
||||||
|
files: files,
|
||||||
|
topK: request.TopK,
|
||||||
|
userInfo: userInfo,
|
||||||
|
prompt: request.Prompt,
|
||||||
|
minScore: request.MinScore,
|
||||||
|
connectionId: connectionId,
|
||||||
|
projectId: request.ProjectId,
|
||||||
|
cancellationToken: cancellationToken,
|
||||||
|
conversationId: request.ConversationId
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Format: data: {json}\n\n
|
||||||
|
await foreach (var resp in enumerable)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Skip Empty Responses ...
|
||||||
|
if (resp.IsNullOrDefault())
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
await Response.WriteSseEventAsync(
|
||||||
|
data: resp,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
await Response.WriteSseDoneAsync(cancellationToken);
|
||||||
|
}
|
||||||
|
catch (Exception ex) when (ex.IsClientCancellation(cancellationToken))
|
||||||
|
{
|
||||||
|
// Client Disconnected ...
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
Logger.LogError(ex, "AskStream Failed ...");
|
||||||
|
if (!Response.HasStarted)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var result = GetExceptionActionResult(ex);
|
||||||
|
await result.ExecuteResultAsync(ControllerContext);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
await Response.WriteSseErrorAsync(ex.Message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@@ -556,9 +822,10 @@ namespace xAiApi.Controllers
|
|||||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||||
public async Task<ActionResult<string>> ExtractContent(
|
public async Task<ActionResult<string>> ExtractContent(
|
||||||
[FromForm] IFormFileCollection files,
|
[FromForm] IFormFileCollection files,
|
||||||
[FromForm] string extractors = "*",
|
[FromForm] string extractors = null,
|
||||||
CancellationToken cancellationToken = default
|
CancellationToken cancellationToken = default
|
||||||
) {
|
)
|
||||||
|
{
|
||||||
//
|
//
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -599,192 +866,18 @@ namespace xAiApi.Controllers
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
//
|
//
|
||||||
#region Data Actions ...
|
#region Non Actions ...
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Ask Question and Get Response ...
|
/// Write Validation Error for Streaming Endpoints (before Stream Starts) ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="request"></param>
|
|
||||||
/// <param name="cancellationToken"></param>
|
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpPost("Ask")]
|
[NonAction]
|
||||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
protected async Task WriteStreamValidationError()
|
||||||
public async Task<ActionResult<string>> Ask(
|
|
||||||
[FromBody] XAiResponseRequest request,
|
|
||||||
CancellationToken cancellationToken = default
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
try
|
Response.StatusCode = StatusCodes.Status400BadRequest;
|
||||||
{
|
Response.ContentType = "application/json; charset=utf-8";
|
||||||
//
|
await Response.WriteAsync(XException.InvalidArgs.ToXError().ToJSON());
|
||||||
// Validate ...
|
|
||||||
if (!request.IsValid())
|
|
||||||
{
|
|
||||||
XException.InvalidArgs.Throw();
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
var userInfo = await GetUserInfo();
|
|
||||||
var connectionId = GetConnectionId();
|
|
||||||
|
|
||||||
//
|
|
||||||
// TODO: Reading Fiels Form Collection and Attach it ...
|
|
||||||
var result = await aiService.AskAsync(
|
|
||||||
files: null,
|
|
||||||
prompt: request.Prompt,
|
|
||||||
ownerId: userInfo.UserId,
|
|
||||||
connectionId: connectionId,
|
|
||||||
projectId: request.ProjectId,
|
|
||||||
cancellationToken: cancellationToken,
|
|
||||||
conversationId: request.ConversationId
|
|
||||||
);
|
|
||||||
|
|
||||||
//
|
|
||||||
return Ok(result);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
var result = GetExceptionActionResult(ex);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Ask Question and Get Response Stream ...
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="request"></param>
|
|
||||||
/// <param name="cancellationToken"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
[HttpPost("AskStream")]
|
|
||||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
|
||||||
public async Task AskStream(
|
|
||||||
[FromBody] XAiResponseRequest request,
|
|
||||||
CancellationToken cancellationToken = default
|
|
||||||
)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
// Validate (before Stream Starts -> Proper Http Error) ...
|
|
||||||
if (!request.IsValid())
|
|
||||||
{
|
|
||||||
await WriteStreamValidationError();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
try
|
|
||||||
{
|
|
||||||
//
|
|
||||||
var userInfo = await GetUserInfo();
|
|
||||||
var connectionId = GetConnectionId();
|
|
||||||
|
|
||||||
//
|
|
||||||
// Configure SSE Response ...
|
|
||||||
Response.PrepareSseResponse();
|
|
||||||
|
|
||||||
//
|
|
||||||
// TODO: Reading Fiels Form Collection and Attach it ...
|
|
||||||
var enumerable = aiService.AskAsEnumerable(
|
|
||||||
files: null,
|
|
||||||
prompt: request.Prompt,
|
|
||||||
ownerId: userInfo.UserId,
|
|
||||||
connectionId: connectionId,
|
|
||||||
projectId: request.ProjectId.Value,
|
|
||||||
cancellationToken: cancellationToken,
|
|
||||||
conversationId: request.ConversationId.Value
|
|
||||||
);
|
|
||||||
|
|
||||||
//
|
|
||||||
// Format: data: {json}\n\n
|
|
||||||
await foreach (var resp in enumerable)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
// Skip Empty Responses ...
|
|
||||||
if (resp.IsNullOrDefault())
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
await Response.WriteSseEventAsync(
|
|
||||||
data: resp,
|
|
||||||
cancellationToken: cancellationToken
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
await Response.WriteSseDoneAsync(cancellationToken);
|
|
||||||
}
|
|
||||||
catch (Exception ex) when (ex.IsClientCancellation(cancellationToken))
|
|
||||||
{
|
|
||||||
// Client Disconnected ...
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
Logger.LogError(ex, "AskStream Failed");
|
|
||||||
if (!Response.HasStarted)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
var result = GetExceptionActionResult(ex);
|
|
||||||
await result.ExecuteResultAsync(ControllerContext);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
await Response.WriteSseErrorAsync(ex.Message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Ask Question with Files and Get Response ...
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="request"></param>
|
|
||||||
/// <param name="cancellationToken"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
[HttpPost("AskWithFiles")]
|
|
||||||
[Consumes("multipart/form-data")]
|
|
||||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
|
||||||
public async Task<ActionResult<string>> AskWithFiles(
|
|
||||||
[FromForm] XAiResponseRequest request,
|
|
||||||
[FromForm] IFormFileCollection files,
|
|
||||||
CancellationToken cancellationToken = default
|
|
||||||
)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
try
|
|
||||||
{
|
|
||||||
//
|
|
||||||
// Validate ...
|
|
||||||
if (!request.IsValid())
|
|
||||||
{
|
|
||||||
XException.InvalidArgs.Throw();
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
var userInfo = await GetUserInfo();
|
|
||||||
var connectionId = GetConnectionId();
|
|
||||||
|
|
||||||
//
|
|
||||||
var result = await aiService.AskAsync(
|
|
||||||
files: files,
|
|
||||||
userInfo: userInfo,
|
|
||||||
prompt: request.Prompt,
|
|
||||||
ownerId: userInfo.UserId,
|
|
||||||
connectionId: connectionId,
|
|
||||||
projectId: request.ProjectId,
|
|
||||||
cancellationToken: cancellationToken,
|
|
||||||
conversationId: request.ConversationId
|
|
||||||
);
|
|
||||||
|
|
||||||
//
|
|
||||||
return Ok(result);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
var result = GetExceptionActionResult(ex);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,11 +50,38 @@ namespace xAiApi.Interfaces
|
|||||||
CancellationToken cancellationToken = default
|
CancellationToken cancellationToken = default
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Ask a Question using Provided Context Chunks (RAG) without Persisting ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="prompt"></param>
|
||||||
|
/// <param name="context"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Task<string> AskWithContextAsync(
|
||||||
|
string prompt,
|
||||||
|
IEnumerable<XAiVectorSearchResult> context,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Ask a Question using Provided Context Chunks (RAG) and Stream Response ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="prompt"></param>
|
||||||
|
/// <param name="context"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
IAsyncEnumerable<string> AskWithContextAsEnumerable(
|
||||||
|
string prompt,
|
||||||
|
IEnumerable<XAiVectorSearchResult> context,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Ask a Question as Prompt and Retrieve Response ...
|
/// Ask a Question as Prompt and Retrieve Response ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="prompt"></param>
|
/// <param name="prompt"></param>
|
||||||
/// <param name="ownerId"></param>
|
/// <param name="topK"></param>
|
||||||
|
/// <param name="minScore"></param>
|
||||||
/// <param name="projectId"></param>
|
/// <param name="projectId"></param>
|
||||||
/// <param name="conversationId"></param>
|
/// <param name="conversationId"></param>
|
||||||
/// <param name="connectionId"></param>
|
/// <param name="connectionId"></param>
|
||||||
@@ -64,9 +91,10 @@ namespace xAiApi.Interfaces
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task<XAiMessageDto> AskAsync(
|
Task<XAiMessageDto> AskAsync(
|
||||||
string prompt,
|
string prompt,
|
||||||
string ownerId,
|
int? topK = null,
|
||||||
Guid? projectId,
|
float? minScore = null,
|
||||||
Guid? conversationId,
|
Guid? projectId = null,
|
||||||
|
Guid? conversationId = null,
|
||||||
string connectionId = null,
|
string connectionId = null,
|
||||||
IFormFileCollection files = null,
|
IFormFileCollection files = null,
|
||||||
XUserClaimsInfoDto userInfo = null,
|
XUserClaimsInfoDto userInfo = null,
|
||||||
@@ -77,20 +105,24 @@ namespace xAiApi.Interfaces
|
|||||||
/// Ask a Question as Prompt and Retrieve Response as Async Enumerable ...
|
/// Ask a Question as Prompt and Retrieve Response as Async Enumerable ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="prompt"></param>
|
/// <param name="prompt"></param>
|
||||||
/// <param name="ownerId"></param>
|
/// <param name="topK"></param>
|
||||||
|
/// <param name="minScore"></param>
|
||||||
/// <param name="projectId"></param>
|
/// <param name="projectId"></param>
|
||||||
/// <param name="conversationId"></param>
|
/// <param name="conversationId"></param>
|
||||||
/// <param name="connectionId"></param>
|
/// <param name="connectionId"></param>
|
||||||
/// <param name="files"></param>
|
/// <param name="files"></param>
|
||||||
|
/// <param name="userInfo"></param>
|
||||||
/// <param name="cancellationToken"></param>
|
/// <param name="cancellationToken"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
IAsyncEnumerable<XAiResonseMessage> AskAsEnumerable(
|
IAsyncEnumerable<XAiResonseMessage> AskAsEnumerable(
|
||||||
string prompt,
|
string prompt,
|
||||||
string ownerId,
|
int? topK = null,
|
||||||
Guid projectId,
|
float? minScore = null,
|
||||||
Guid conversationId,
|
Guid? projectId = null,
|
||||||
|
Guid? conversationId = null,
|
||||||
string connectionId = null,
|
string connectionId = null,
|
||||||
IFormFileCollection files = null,
|
IFormFileCollection files = null,
|
||||||
|
XUserClaimsInfoDto userInfo = null,
|
||||||
CancellationToken cancellationToken = default
|
CancellationToken cancellationToken = default
|
||||||
);
|
);
|
||||||
#endregion
|
#endregion
|
||||||
|
|||||||
+268
-40
@@ -168,11 +168,99 @@ namespace xAiApi.Providers
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Ask a Question using Provided Context Chunks (RAG) without Persisting ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="prompt"></param>
|
||||||
|
/// <param name="context"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public virtual async Task<string> AskWithContextAsync(
|
||||||
|
string prompt,
|
||||||
|
IEnumerable<XAiVectorSearchResult> context,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate Args ...
|
||||||
|
if (prompt.IsNullOrEmpty())
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
using var client = Descriptor.GetClient();
|
||||||
|
var history = GetHistory(prompt, context);
|
||||||
|
var response = await client.GetResponseAsync(
|
||||||
|
messages: history,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Validate Response ...
|
||||||
|
if (!response.IsValid())
|
||||||
|
{
|
||||||
|
client.Dispose();
|
||||||
|
XException.ActionFailed.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
var result = response.Text;
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Ask a Question using Provided Context Chunks (RAG) and Stream Response ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="prompt"></param>
|
||||||
|
/// <param name="context"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public virtual async IAsyncEnumerable<string> AskWithContextAsEnumerable(
|
||||||
|
string prompt,
|
||||||
|
IEnumerable<XAiVectorSearchResult> context,
|
||||||
|
[EnumeratorCancellation]
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// Validate Args ...
|
||||||
|
if (prompt.IsNullOrEmpty())
|
||||||
|
{
|
||||||
|
XException.InvalidArgs.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
using var client = Descriptor.GetClient();
|
||||||
|
var history = GetHistory(prompt, context);
|
||||||
|
var enumerable = client.GetStreamingResponseAsync(
|
||||||
|
options: null,
|
||||||
|
messages: history,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
|
//
|
||||||
|
await foreach (var res in enumerable)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (cancellationToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
yield break;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
yield return res.Text;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Ask a Question as Prompt and Retrieve Response ...
|
/// Ask a Question as Prompt and Retrieve Response ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="prompt"></param>
|
/// <param name="prompt"></param>
|
||||||
/// <param name="ownerId"></param>
|
/// <param name="topK"></param>
|
||||||
|
/// <param name="minScore"></param>
|
||||||
/// <param name="projectId"></param>
|
/// <param name="projectId"></param>
|
||||||
/// <param name="conversationId"></param>
|
/// <param name="conversationId"></param>
|
||||||
/// <param name="connectionId"></param>
|
/// <param name="connectionId"></param>
|
||||||
@@ -182,9 +270,10 @@ namespace xAiApi.Providers
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public virtual async Task<XAiMessageDto> AskAsync(
|
public virtual async Task<XAiMessageDto> AskAsync(
|
||||||
string prompt,
|
string prompt,
|
||||||
string ownerId,
|
int? topK = null,
|
||||||
Guid? projectId,
|
float? minScore = null,
|
||||||
Guid? conversationId,
|
Guid? projectId = null,
|
||||||
|
Guid? conversationId = null,
|
||||||
string connectionId = null,
|
string connectionId = null,
|
||||||
IFormFileCollection files = null,
|
IFormFileCollection files = null,
|
||||||
XUserClaimsInfoDto userInfo = null,
|
XUserClaimsInfoDto userInfo = null,
|
||||||
@@ -196,9 +285,36 @@ namespace xAiApi.Providers
|
|||||||
validationProvider
|
validationProvider
|
||||||
.GroupValidationBuilder()
|
.GroupValidationBuilder()
|
||||||
.AddNotEmpty(prompt)
|
.AddNotEmpty(prompt)
|
||||||
.AddNotEmpty(ownerId)
|
.AddNotNull(userInfo)
|
||||||
|
.AddNotEmpty(userInfo.UserId)
|
||||||
.ValidateGroup();
|
.ValidateGroup();
|
||||||
|
|
||||||
|
//
|
||||||
|
// Normalize ...
|
||||||
|
if (!topK.HasValue ||
|
||||||
|
topK.Value < 1 ||
|
||||||
|
topK.Value > configuration.RAGDescriptor.MaxTopK
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (!topK.HasValue ||
|
||||||
|
(topK.HasValue && topK.Value < 1))
|
||||||
|
{
|
||||||
|
topK = configuration.RAGDescriptor.DefaultTopK;
|
||||||
|
}
|
||||||
|
else if (topK.HasValue && topK.Value > configuration.RAGDescriptor.MaxTopK)
|
||||||
|
{
|
||||||
|
topK = configuration.RAGDescriptor.MaxTopK;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!minScore.HasValue ||
|
||||||
|
minScore.Value <= 0 ||
|
||||||
|
minScore.Value > 1
|
||||||
|
)
|
||||||
|
{
|
||||||
|
minScore = configuration.RAGDescriptor.DefaultMinScore;
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Check Project ...
|
// Check Project ...
|
||||||
XAiProjectDto project = null;
|
XAiProjectDto project = null;
|
||||||
@@ -206,7 +322,7 @@ namespace xAiApi.Providers
|
|||||||
{
|
{
|
||||||
//
|
//
|
||||||
project = await dataProvider.GetDefaultProject(
|
project = await dataProvider.GetDefaultProject(
|
||||||
ownerId: ownerId,
|
ownerId: userInfo.UserId,
|
||||||
connectionId: connectionId,
|
connectionId: connectionId,
|
||||||
cancellationToken: cancellationToken
|
cancellationToken: cancellationToken
|
||||||
);
|
);
|
||||||
@@ -236,7 +352,7 @@ namespace xAiApi.Providers
|
|||||||
{
|
{
|
||||||
//
|
//
|
||||||
conversation = await dataProvider.GetDefaultConversation(
|
conversation = await dataProvider.GetDefaultConversation(
|
||||||
ownerId: ownerId,
|
ownerId: userInfo.UserId,
|
||||||
projectId: projectId.Value,
|
projectId: projectId.Value,
|
||||||
connectionId: connectionId,
|
connectionId: connectionId,
|
||||||
cancellationToken: cancellationToken
|
cancellationToken: cancellationToken
|
||||||
@@ -288,8 +404,10 @@ namespace xAiApi.Providers
|
|||||||
// Prepare Knowledge Request ...
|
// Prepare Knowledge Request ...
|
||||||
var knowledgeRequest = new XAiKnowledgeAskRequest
|
var knowledgeRequest = new XAiKnowledgeAskRequest
|
||||||
{
|
{
|
||||||
|
TopK = topK,
|
||||||
Prompt = prompt,
|
Prompt = prompt,
|
||||||
IncludeUser = true,
|
IncludeUser = true,
|
||||||
|
MinScore = minScore,
|
||||||
IncludeGlobal = true,
|
IncludeGlobal = true,
|
||||||
ProjectId = projectId,
|
ProjectId = projectId,
|
||||||
ConversationId = conversationId
|
ConversationId = conversationId
|
||||||
@@ -321,8 +439,8 @@ namespace xAiApi.Providers
|
|||||||
var promptMessage = new XAiMessageDto
|
var promptMessage = new XAiMessageDto
|
||||||
{
|
{
|
||||||
Content = prompt,
|
Content = prompt,
|
||||||
OwnerId = ownerId,
|
|
||||||
Role = XAiChatRole.User,
|
Role = XAiChatRole.User,
|
||||||
|
OwnerId = userInfo.UserId,
|
||||||
CreatedOn = DateTime.UtcNow,
|
CreatedOn = DateTime.UtcNow,
|
||||||
ConversationId = conversationId.Value,
|
ConversationId = conversationId.Value,
|
||||||
ConversationTitle = conversation.Title
|
ConversationTitle = conversation.Title
|
||||||
@@ -355,7 +473,7 @@ namespace xAiApi.Providers
|
|||||||
var result = new XAiMessageDto
|
var result = new XAiMessageDto
|
||||||
{
|
{
|
||||||
Content = answer,
|
Content = answer,
|
||||||
OwnerId = ownerId,
|
OwnerId = userInfo.UserId,
|
||||||
CreatedOn = DateTime.UtcNow,
|
CreatedOn = DateTime.UtcNow,
|
||||||
Role = XAiChatRole.Assistant,
|
Role = XAiChatRole.Assistant,
|
||||||
ConversationId = conversationId.Value,
|
ConversationId = conversationId.Value,
|
||||||
@@ -376,20 +494,24 @@ namespace xAiApi.Providers
|
|||||||
/// Ask a Question as Prompt and Retrieve Response as Async Enumerable ...
|
/// Ask a Question as Prompt and Retrieve Response as Async Enumerable ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="prompt"></param>
|
/// <param name="prompt"></param>
|
||||||
/// <param name="ownerId"></param>
|
/// <param name="topK"></param>
|
||||||
|
/// <param name="minScore"></param>
|
||||||
/// <param name="projectId"></param>
|
/// <param name="projectId"></param>
|
||||||
/// <param name="conversationId"></param>
|
/// <param name="conversationId"></param>
|
||||||
/// <param name="connectionId"></param>
|
/// <param name="connectionId"></param>
|
||||||
/// <param name="files"></param>
|
/// <param name="files"></param>
|
||||||
|
/// <param name="userInfo"></param>
|
||||||
/// <param name="cancellationToken"></param>
|
/// <param name="cancellationToken"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public virtual async IAsyncEnumerable<XAiResonseMessage> AskAsEnumerable(
|
public virtual async IAsyncEnumerable<XAiResonseMessage> AskAsEnumerable(
|
||||||
string prompt,
|
string prompt,
|
||||||
string ownerId,
|
int? topK = null,
|
||||||
Guid projectId,
|
float? minScore = null,
|
||||||
Guid conversationId,
|
Guid? projectId = null,
|
||||||
|
Guid? conversationId = null,
|
||||||
string connectionId = null,
|
string connectionId = null,
|
||||||
IFormFileCollection files = null,
|
IFormFileCollection files = null,
|
||||||
|
XUserClaimsInfoDto userInfo = null,
|
||||||
[EnumeratorCancellation]
|
[EnumeratorCancellation]
|
||||||
CancellationToken cancellationToken = default
|
CancellationToken cancellationToken = default
|
||||||
)
|
)
|
||||||
@@ -399,17 +521,44 @@ namespace xAiApi.Providers
|
|||||||
validationProvider
|
validationProvider
|
||||||
.GroupValidationBuilder()
|
.GroupValidationBuilder()
|
||||||
.AddNotEmpty(prompt)
|
.AddNotEmpty(prompt)
|
||||||
.AddNotEmpty(ownerId)
|
.AddNotNull(userInfo)
|
||||||
|
.AddNotEmpty(userInfo.UserId)
|
||||||
.ValidateGroup();
|
.ValidateGroup();
|
||||||
|
|
||||||
|
//
|
||||||
|
// Normalize ...
|
||||||
|
if (!topK.HasValue ||
|
||||||
|
topK.Value < 1 ||
|
||||||
|
topK.Value > configuration.RAGDescriptor.MaxTopK
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
if (!topK.HasValue ||
|
||||||
|
(topK.HasValue && topK.Value < 1))
|
||||||
|
{
|
||||||
|
topK = configuration.RAGDescriptor.DefaultTopK;
|
||||||
|
}
|
||||||
|
else if (topK.HasValue && topK.Value > configuration.RAGDescriptor.MaxTopK)
|
||||||
|
{
|
||||||
|
topK = configuration.RAGDescriptor.MaxTopK;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!minScore.HasValue ||
|
||||||
|
minScore.Value <= 0 ||
|
||||||
|
minScore.Value > 1
|
||||||
|
)
|
||||||
|
{
|
||||||
|
minScore = configuration.RAGDescriptor.DefaultMinScore;
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Check Project ...
|
// Check Project ...
|
||||||
XAiProjectDto project = null;
|
XAiProjectDto project = null;
|
||||||
if (projectId.IsDefaultGuid())
|
if (!projectId.HasValue || projectId.Value.IsDefaultGuid())
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
project = await dataProvider.GetDefaultProject(
|
project = await dataProvider.GetDefaultProject(
|
||||||
ownerId: ownerId,
|
ownerId: userInfo.UserId,
|
||||||
connectionId: connectionId,
|
connectionId: connectionId,
|
||||||
cancellationToken: cancellationToken
|
cancellationToken: cancellationToken
|
||||||
);
|
);
|
||||||
@@ -421,7 +570,7 @@ namespace xAiApi.Providers
|
|||||||
{
|
{
|
||||||
//
|
//
|
||||||
project = await dataProvider.GetProject(
|
project = await dataProvider.GetProject(
|
||||||
id: projectId,
|
id: projectId.Value,
|
||||||
fillConversations: false,
|
fillConversations: false,
|
||||||
fillConversationMessages: false,
|
fillConversationMessages: false,
|
||||||
cancellationToken: cancellationToken
|
cancellationToken: cancellationToken
|
||||||
@@ -435,12 +584,12 @@ namespace xAiApi.Providers
|
|||||||
//
|
//
|
||||||
// Check Conversation ...
|
// Check Conversation ...
|
||||||
XAiConversationDto conversation = null;
|
XAiConversationDto conversation = null;
|
||||||
if (conversationId.IsDefaultGuid())
|
if (!conversationId.HasValue || conversationId.Value.IsDefaultGuid())
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
conversation = await dataProvider.GetDefaultConversation(
|
conversation = await dataProvider.GetDefaultConversation(
|
||||||
ownerId: ownerId,
|
ownerId: userInfo.UserId,
|
||||||
projectId: projectId,
|
projectId: projectId.Value,
|
||||||
connectionId: connectionId,
|
connectionId: connectionId,
|
||||||
cancellationToken: cancellationToken
|
cancellationToken: cancellationToken
|
||||||
);
|
);
|
||||||
@@ -452,8 +601,8 @@ namespace xAiApi.Providers
|
|||||||
{
|
{
|
||||||
//
|
//
|
||||||
conversation = await dataProvider.GetConversation(
|
conversation = await dataProvider.GetConversation(
|
||||||
id: conversationId,
|
|
||||||
fillMessages: false,
|
fillMessages: false,
|
||||||
|
id: conversationId.Value,
|
||||||
cancellationToken: cancellationToken
|
cancellationToken: cancellationToken
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -463,20 +612,60 @@ namespace xAiApi.Providers
|
|||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Reading Messages ...
|
// Ingest Files based on Conversation ...
|
||||||
var messages = await dataProvider.LoadMessages(
|
XAiKnowledgeIngestResult ingestResult;
|
||||||
conversationId: conversationId,
|
if (files.HasChild())
|
||||||
|
{
|
||||||
|
//
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//
|
||||||
|
ingestResult = await ragProvider
|
||||||
|
.IngestAsync(
|
||||||
|
files: files,
|
||||||
|
userInfo: userInfo,
|
||||||
|
replaceExisting: true,
|
||||||
|
connectionId: connectionId,
|
||||||
|
cancellationToken: cancellationToken,
|
||||||
|
scope: XAiKnowledgeScopeDescriptor.Conversation(
|
||||||
|
type: null,
|
||||||
|
conversationId: conversationId.Value
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Prepare Knowledge Request ...
|
||||||
|
var knowledgeRequest = new XAiKnowledgeAskRequest
|
||||||
|
{
|
||||||
|
TopK = topK,
|
||||||
|
Prompt = prompt,
|
||||||
|
IncludeUser = true,
|
||||||
|
MinScore = minScore,
|
||||||
|
IncludeGlobal = true,
|
||||||
|
ProjectId = projectId,
|
||||||
|
ConversationId = conversationId
|
||||||
|
};
|
||||||
|
var context = await ragProvider.RetrieveAsync(
|
||||||
|
userInfo: userInfo,
|
||||||
|
request: knowledgeRequest,
|
||||||
cancellationToken: cancellationToken
|
cancellationToken: cancellationToken
|
||||||
);
|
);
|
||||||
|
|
||||||
//
|
//
|
||||||
// TODO: Parse Files ...
|
// Reading Messages ...
|
||||||
// for Attached them into Prompt Message ...
|
var messages = await dataProvider.LoadMessages(
|
||||||
|
conversationId: conversationId.Value,
|
||||||
|
cancellationToken: cancellationToken
|
||||||
|
);
|
||||||
|
|
||||||
//
|
//
|
||||||
// Handle Memory ...
|
// Handle Memory ...
|
||||||
IList<ChatMessage> history = await PrepareMemory(
|
IList<ChatMessage> history = await PrepareMemory(
|
||||||
project: project,
|
project: project,
|
||||||
|
context: context,
|
||||||
messages: messages,
|
messages: messages,
|
||||||
cancellationToken: cancellationToken
|
cancellationToken: cancellationToken
|
||||||
);
|
);
|
||||||
@@ -486,33 +675,33 @@ namespace xAiApi.Providers
|
|||||||
var promptMessage = new XAiMessageDto
|
var promptMessage = new XAiMessageDto
|
||||||
{
|
{
|
||||||
Content = prompt,
|
Content = prompt,
|
||||||
OwnerId = ownerId,
|
|
||||||
Role = XAiChatRole.User,
|
Role = XAiChatRole.User,
|
||||||
|
OwnerId = userInfo.UserId,
|
||||||
CreatedOn = DateTime.UtcNow,
|
CreatedOn = DateTime.UtcNow,
|
||||||
ConversationId = conversationId,
|
ConversationId = conversationId.Value,
|
||||||
|
ConversationTitle = conversation.Title
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//
|
||||||
|
// Save Prompt Message ...
|
||||||
promptMessage = await dataProvider.AddMessage(
|
promptMessage = await dataProvider.AddMessage(
|
||||||
item: promptMessage,
|
item: promptMessage,
|
||||||
connectionId: connectionId,
|
connectionId: connectionId,
|
||||||
conversationId: conversationId,
|
conversationId: conversationId.Value,
|
||||||
cancellationToken: cancellationToken
|
|
||||||
);
|
|
||||||
promptMessage = await dataProvider.Enrich(
|
|
||||||
source: promptMessage,
|
|
||||||
cancellationToken: cancellationToken
|
cancellationToken: cancellationToken
|
||||||
);
|
);
|
||||||
|
|
||||||
//
|
//
|
||||||
// Attach Uploaded Files ...
|
// Converts to ChatMessage ...
|
||||||
var promptChatMessage = promptMessage
|
var promptChatMessage = promptMessage
|
||||||
.ToChatMessages();
|
.ToChatMessages(additionalContents: []);
|
||||||
history.Add(promptChatMessage);
|
history.Add(promptChatMessage);
|
||||||
|
|
||||||
//
|
//
|
||||||
// Ask Questions From LLM ...
|
// Ask Questions From LLM ...
|
||||||
using var client = Descriptor.GetClient();
|
using var client = Descriptor.GetClient();
|
||||||
var enumerable = client.GetStreamingResponseAsync(
|
var enumerable = client.GetStreamingResponseAsync(
|
||||||
options: null,
|
options: Options,
|
||||||
messages: history
|
messages: history
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -531,9 +720,9 @@ namespace xAiApi.Providers
|
|||||||
//
|
//
|
||||||
var resp = new XAiResonseMessage
|
var resp = new XAiResonseMessage
|
||||||
{
|
{
|
||||||
OwnerId = ownerId,
|
|
||||||
Content = res.Text,
|
Content = res.Text,
|
||||||
Sequence = sequence,
|
Sequence = sequence,
|
||||||
|
OwnerId = userInfo.UserId,
|
||||||
Role = XAiChatRole.System,
|
Role = XAiChatRole.System,
|
||||||
RespondTo = promptMessage.Id,
|
RespondTo = promptMessage.Id,
|
||||||
RespondedOn = DateTime.UtcNow,
|
RespondedOn = DateTime.UtcNow,
|
||||||
@@ -552,15 +741,15 @@ namespace xAiApi.Providers
|
|||||||
var result = new XAiMessageDto
|
var result = new XAiMessageDto
|
||||||
{
|
{
|
||||||
Content = answer,
|
Content = answer,
|
||||||
OwnerId = ownerId,
|
OwnerId = userInfo.UserId,
|
||||||
Role = XAiChatRole.System,
|
Role = XAiChatRole.System,
|
||||||
CreatedOn = DateTime.UtcNow,
|
CreatedOn = DateTime.UtcNow,
|
||||||
ConversationId = conversationId
|
ConversationId = conversationId.Value
|
||||||
};
|
};
|
||||||
result = await dataProvider.AddMessage(
|
result = await dataProvider.AddMessage(
|
||||||
item: result,
|
item: result,
|
||||||
connectionId: connectionId,
|
connectionId: connectionId,
|
||||||
conversationId: conversationId,
|
conversationId: conversationId.Value,
|
||||||
cancellationToken: cancellationToken
|
cancellationToken: cancellationToken
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -1084,6 +1273,44 @@ namespace xAiApi.Providers
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Build Chat History for RAG:
|
||||||
|
/// [System: Introduction + ChatWithDocuments Prompt + Context Block] + [User: Prompt] ...
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="prompt"></param>
|
||||||
|
/// <param name="context"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
protected virtual IList<ChatMessage> GetHistory(
|
||||||
|
string prompt,
|
||||||
|
IEnumerable<XAiVectorSearchResult> context
|
||||||
|
)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
var introductionPrompt = configuration.GetPrompt();
|
||||||
|
var ragPrompt = configuration.GetPrompt(configuration.RAGDescriptor.PromptName);
|
||||||
|
if (ragPrompt.IsNullOrEmpty())
|
||||||
|
{
|
||||||
|
XException.InvalidConfiguration.Throw();
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
var contextBlock = BuildContextBlock(context);
|
||||||
|
var systemText =
|
||||||
|
$"{introductionPrompt}{Environment.NewLine}{Environment.NewLine}" +
|
||||||
|
$"{ragPrompt}{Environment.NewLine}{Environment.NewLine}" +
|
||||||
|
$"Context:{Environment.NewLine}{contextBlock}";
|
||||||
|
|
||||||
|
//
|
||||||
|
List<ChatMessage> result =
|
||||||
|
[
|
||||||
|
new ChatMessage(ChatRole.System, systemText),
|
||||||
|
new ChatMessage(ChatRole.User, prompt),
|
||||||
|
];
|
||||||
|
|
||||||
|
//
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Implementing Memory Strategy here ...
|
/// Implementing Memory Strategy here ...
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -1222,6 +1449,7 @@ namespace xAiApi.Providers
|
|||||||
history.Add(prompt);
|
history.Add(prompt);
|
||||||
using var client = Descriptor.GetClient();
|
using var client = Descriptor.GetClient();
|
||||||
var response = await client.GetResponseAsync(
|
var response = await client.GetResponseAsync(
|
||||||
|
options: Options,
|
||||||
messages: history,
|
messages: history,
|
||||||
cancellationToken: cancellationToken
|
cancellationToken: cancellationToken
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user