Files
xSaherElmAiApi/Controllers/V1/AIController.cs
T
2026-04-10 23:51:44 +03:30

115 lines
3.1 KiB
C#

using System;
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.AI.Interfaces;
using xAiApi.Base;
using xCommons.Configurations;
using xCommons.Extensions;
using xCommons.Providers;
using xIdentityService.Interfaces;
namespace xAiApi.Controllers.V1
{
public class AIController : XIBaseV1Controller
{
private readonly IXAIService aiService;
public AIController(
IXAIService aiService,
ILogger<XIBaseV1Controller> logger,
XAppConfiguration appConfiguration,
IXIdentityProvider identityProvider,
XValidationProvider validationProvider
) : base(
logger,
appConfiguration,
identityProvider,
validationProvider
)
{
this.aiService = aiService;
}
//
#region Actions ...
[AllowAnonymous]
[HttpGet("AskAI")]
public async Task<ActionResult<string>> AskAI(
[FromQuery] string prompt,
CancellationToken cancellationToken = default
)
{
//
// Do ...
try
{
//
var result = await aiService
.GetTextResponseAsync(
prompt: prompt,
cancellationToken: cancellationToken
);
//
return Ok(result);
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
[AllowAnonymous]
[HttpGet("AskAIStream")]
public async Task AskAIStream(
[FromQuery] string prompt,
CancellationToken cancellationToken = default
)
{
//
// Do ...
try
{
//
// Adding Content Type ...
Response.Headers
.Append("Content-Type", "text/event-stream");
//
// Retrieving Stream ...
var stream = aiService
.GetTextReponseStreamAsync(
prompt: prompt,
cancellationToken: cancellationToken
);
await foreach (var message in stream)
{
//
// Do What we Have to Do with Message ...
//
var msgBytes = message.Text.ToBytes();
await Response.Body.WriteAsync(
msgBytes,
0,
msgBytes.Length
);
await Response.Body.FlushAsync();
}
//
// Closing Connection ...
await Response.Body.FlushAsync();
}
catch
{ }
}
#endregion
}
}