refactor for better support of streaming and etc ...
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using xAiApi.Interfaces;
|
||||
@@ -12,7 +14,7 @@ using xCommons.Providers;
|
||||
namespace xAiApi.Controllers
|
||||
{
|
||||
[Authorize]
|
||||
[Route ("[controller]")]
|
||||
[Route("[controller]")]
|
||||
public abstract class XAiServiceControllerBase<TAiService> : XBaseController, IXAiServiceControllerBase
|
||||
where TAiService : IXAiServiceBase
|
||||
{
|
||||
@@ -39,8 +41,8 @@ namespace xAiApi.Controllers
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[AllowAnonymous]
|
||||
[HttpGet("Ask")]
|
||||
public virtual async Task<ActionResult<string>> Ask(
|
||||
[HttpGet("AskText")]
|
||||
public virtual async Task<ActionResult<string>> AskText(
|
||||
[FromQuery] string question,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
@@ -53,7 +55,7 @@ namespace xAiApi.Controllers
|
||||
ValidationProvider.NotEmpty(question);
|
||||
|
||||
//
|
||||
var result = await aiService.AskAsync(
|
||||
var result = await aiService.AskTextAsync(
|
||||
prompt: question,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
@@ -68,5 +70,85 @@ namespace xAiApi.Controllers
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ask Question and Get Response Stream ...
|
||||
/// </summary>
|
||||
/// <param name="question"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[AllowAnonymous]
|
||||
[HttpGet("AskTextStream")]
|
||||
public virtual async Task AskTextStream(
|
||||
[FromQuery] string question,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
ValidationProvider.NotEmpty(question);
|
||||
|
||||
//
|
||||
// Configure SSE Response Headers ...
|
||||
Response.Headers["X-Accel-Buffering"] = "no";
|
||||
Response.Headers["Connection"] = "keep-alive";
|
||||
Response.Headers["Content-Type"] = "text/event-stream";
|
||||
Response.Headers["Cache-Control"] = "no-cache, no-store";
|
||||
|
||||
//
|
||||
// Disable response buffering ...
|
||||
var responseBodyFeature = HttpContext.Features
|
||||
.Get<Microsoft.AspNetCore.Http.Features.IHttpResponseBodyFeature>();
|
||||
responseBodyFeature?.DisableBuffering();
|
||||
|
||||
//
|
||||
// Stream Response ...
|
||||
var jsonOptions = new JsonSerializerOptions
|
||||
{
|
||||
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
|
||||
};
|
||||
|
||||
//
|
||||
var enumerable = aiService.AskTextAsEnumerable(
|
||||
prompt: question,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
await foreach (var chunk in enumerable)
|
||||
{
|
||||
//
|
||||
// Skip empty chunks ...
|
||||
if (string.IsNullOrEmpty(chunk))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
//
|
||||
// Build SSE message ...
|
||||
// Format: data: <json>\n\n
|
||||
// var payload = JsonSerializer.Serialize(
|
||||
// new { text = chunk },
|
||||
// jsonOptions
|
||||
// );
|
||||
var payload = chunk;
|
||||
|
||||
//
|
||||
// Write SSE Event ...
|
||||
await Response.WriteAsync($"{payload}\n\n", cancellationToken);
|
||||
await Response.Body.FlushAsync(cancellationToken);
|
||||
}
|
||||
|
||||
//
|
||||
await Response.Body.FlushAsync(cancellationToken);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
await Response.Body.FlushAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user