using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using xAiService.Interfaces;
using xCommons.Configurations;
using xCommons.Providers;
using xIdentityService.Controllers;
using xIdentityService.Interfaces;
namespace xAiService.Controllers
{
///
/// a Controller base Class which implement based Actions to Provides AI Service Access ...
///
public abstract class XAiServiceControllerBase : XIBaseProviderController, IXAiServiceControllerBase
{
private readonly IXAiService aiService;
protected XAiServiceControllerBase(
ILogger logger,
IXAiService aiService,
XAppConfiguration appConfiguration,
IXIdentityProvider identityProvider,
XValidationProvider validationProvider
) : base(
logger,
appConfiguration,
identityProvider,
validationProvider
)
{
this.aiService = aiService;
}
//
#region Text Actions ...
[HttpGet("Ask")]
public virtual async Task> Ask(
[FromQuery] string prompt,
CancellationToken cancellationToken = default
)
{
//
// Do ...
try
{
//
var userInfo = await GetUserInfo();
var result = await aiService
.GetTextResponseAsync(
prompt: prompt,
userInfo: userInfo,
cancellationToken: cancellationToken
);
//
return Ok(result);
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
[HttpGet("AskStream")]
public async Task AskAIStream(
[FromQuery] string prompt,
CancellationToken cancellationToken = default
)
{
//
// Do ...
try
{
//
var userInfo = await GetUserInfo();
var result = aiService
.GetTextReponseStreamAsync(
prompt: prompt,
userInfo: userInfo,
cancellationToken: cancellationToken
);
//
return Ok(result);
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
#endregion
}
}