using System; using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.AI; using Microsoft.Extensions.Logging; using xAiApi.AI.Interfaces; using xAiApi.Base; using xCommons.Configurations; using xCommons.Extensions; using xCommons.Providers; using xIdentityHelper; using xIdentityService.Interfaces; namespace xAiApi.Controllers.V1 { public class AIController : XIBaseV1Controller { private readonly IXAIService aiService; public AIController( IXAIService aiService, ILogger logger, XAppConfiguration appConfiguration, IXIdentityProvider identityProvider, XValidationProvider validationProvider ) : base( logger, appConfiguration, identityProvider, validationProvider ) { this.aiService = aiService; } // #region Actions ... [HttpGet("AskAI")] [Authorize(Policy = XPolicies.EnabledUser)] public async Task> 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; } } [HttpGet("AskAIAsChatMessage")] [Authorize(Policy = XPolicies.EnabledUser)] public async Task> AskAIAsChatMessage( [FromQuery] string prompt, CancellationToken cancellationToken = default ) { // // Do ... try { // var result = await aiService .GetResponseAsync( prompt: prompt, cancellationToken: cancellationToken ); // return Ok(result); } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } [HttpGet("AskAIStream")] [Authorize(Policy = XPolicies.EnabledUser)] 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.ToBytes(); await Response.Body.WriteAsync( msgBytes, 0, msgBytes.Length ); await Response.Body.FlushAsync(); } // // Closing Connection ... await Response.Body.FlushAsync(); } catch (Exception) { } } [HttpGet("AskAIStreamAsChatMessage")] [Authorize(Policy = XPolicies.EnabledUser)] public async Task AskAIStreamAsChatMessage( [FromQuery] string prompt, CancellationToken cancellationToken = default ) { // // Do ... try { // // Adding Content Type ... Response.Headers .Append("Content-Type", "text/event-stream"); // // Retrieving Stream ... var stream = aiService .GetReponseStreamAsync( prompt: prompt, cancellationToken: cancellationToken ); // await foreach (var message in stream) { // await Response.Body.WriteModelToStream( model: message, cancellationToken: cancellationToken ); } // // Closing Connection ... await Response.Body.FlushAsync(); } catch (Exception) { } } #endregion } }