last ...
This commit is contained in:
@@ -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