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>
|
||||
/// Write Validation Error for Streaming Endpoints (before Stream Starts) ...
|
||||
/// Ask Question and Get Response ...
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[NonAction]
|
||||
protected async Task WriteStreamValidationError()
|
||||
[HttpPost("Ask")]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
public async Task<ActionResult<string>> Ask(
|
||||
[FromBody] XAiResponseRequest request,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
Response.StatusCode = StatusCodes.Status400BadRequest;
|
||||
Response.ContentType = "application/json; charset=utf-8";
|
||||
await Response.WriteAsync(XException.InvalidArgs.ToXError().ToJSON());
|
||||
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 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
|
||||
|
||||
@@ -541,7 +807,7 @@ namespace xAiApi.Controllers
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
//
|
||||
#region Content Extraction ...
|
||||
/// <summary>
|
||||
@@ -556,9 +822,10 @@ namespace xAiApi.Controllers
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
public async Task<ActionResult<string>> ExtractContent(
|
||||
[FromForm] IFormFileCollection files,
|
||||
[FromForm] string extractors = "*",
|
||||
CancellationToken cancellationToken = default
|
||||
) {
|
||||
[FromForm] string extractors = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
@@ -599,192 +866,18 @@ namespace xAiApi.Controllers
|
||||
#endregion
|
||||
|
||||
//
|
||||
#region Data Actions ...
|
||||
#region Non Actions ...
|
||||
/// <summary>
|
||||
/// Ask Question and Get Response ...
|
||||
/// Write Validation Error for Streaming Endpoints (before Stream Starts) ...
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost("Ask")]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
public async Task<ActionResult<string>> Ask(
|
||||
[FromBody] XAiResponseRequest request,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
[NonAction]
|
||||
protected async Task WriteStreamValidationError()
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// 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;
|
||||
}
|
||||
Response.StatusCode = StatusCodes.Status400BadRequest;
|
||||
Response.ContentType = "application/json; charset=utf-8";
|
||||
await Response.WriteAsync(XException.InvalidArgs.ToXError().ToJSON());
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user