94 lines
2.4 KiB
C#
94 lines
2.4 KiB
C#
using System;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
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
|
|
)
|
|
{
|
|
//
|
|
// Do ...
|
|
try
|
|
{
|
|
//
|
|
var result = await aiService
|
|
.GetTextResponseAsync(prompt);
|
|
|
|
//
|
|
return Ok(result);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//
|
|
var result = GetExceptionActionResult(ex);
|
|
return result;
|
|
}
|
|
}
|
|
|
|
[AllowAnonymous]
|
|
[HttpGet("AskAIStream")]
|
|
public async Task AskAIStream(
|
|
[FromQuery] string prompt
|
|
)
|
|
{
|
|
//
|
|
// Do ...
|
|
try
|
|
{
|
|
//
|
|
var stream = aiService
|
|
.GetTextReponseStreamAsync(prompt);
|
|
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();
|
|
}
|
|
|
|
//
|
|
await Response.Body.FlushAsync();
|
|
}
|
|
catch
|
|
{ }
|
|
}
|
|
#endregion
|
|
}
|
|
} |