last ...
This commit is contained in:
@@ -6,22 +6,34 @@ using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.AI;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using OpenAI.Realtime;
|
||||
using xAiApi.AI.Interfaces;
|
||||
using xAiApi.Base;
|
||||
using xAiModels.Models;
|
||||
using xCommons.Configurations;
|
||||
using xCommons.Extensions;
|
||||
using xCommons.Providers;
|
||||
using xIdentityHelper;
|
||||
using xIdentityService.Constants;
|
||||
using xIdentityService.Interfaces;
|
||||
|
||||
namespace xAiApi.Controllers.V1
|
||||
{
|
||||
/// <summary>
|
||||
/// a Controller Which Provides Actions for
|
||||
/// using Ai Service ...
|
||||
/// </summary>
|
||||
public class AIController : XIBaseV1Controller
|
||||
{
|
||||
private readonly IXAIService aiService;
|
||||
/// <summary>
|
||||
/// Ai Provider ...
|
||||
/// </summary>
|
||||
private readonly IXAiProvider aiService;
|
||||
|
||||
//
|
||||
#region Constructor ...
|
||||
public AIController(
|
||||
IXAIService aiService,
|
||||
IXAiProvider aiService,
|
||||
ILogger<XIBaseV1Controller> logger,
|
||||
XAppConfiguration appConfiguration,
|
||||
IXIdentityProvider identityProvider,
|
||||
@@ -35,13 +47,26 @@ namespace xAiApi.Controllers.V1
|
||||
{
|
||||
this.aiService = aiService;
|
||||
}
|
||||
#endregion
|
||||
|
||||
//
|
||||
#region Actions ...
|
||||
/// <summary>
|
||||
/// As a Message from Ai ...
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// </remarks>
|
||||
/// <param name="prompt">specified prompt</param>
|
||||
/// <param name="projectId">active Project Id</param>
|
||||
/// <param name="conversationId">active Conversation Id</param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns>Ai Model's answer <see cref="XAiMessageDto"/></returns>
|
||||
[HttpGet("AskAI")]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
public async Task<ActionResult<string>> AskAI(
|
||||
public async Task<ActionResult<XAiMessageDto>> AskAI(
|
||||
[FromQuery] string prompt,
|
||||
[FromQuery] string projectId = null,
|
||||
[FromQuery] string conversationId = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
@@ -50,43 +75,18 @@ namespace xAiApi.Controllers.V1
|
||||
try
|
||||
{
|
||||
//
|
||||
var userInfo = await GetUserInfo();
|
||||
var result = await aiService
|
||||
.GetTextResponseAsync(
|
||||
.Ask(
|
||||
prompt: prompt,
|
||||
userInfo: userInfo,
|
||||
projectId: projectId,
|
||||
conversationId: conversationId,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("AskAIAsChatMessage")]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
public async Task<ActionResult<ChatMessage>> AskAIAsChatMessage(
|
||||
[FromQuery] string prompt,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Do ...
|
||||
try
|
||||
{
|
||||
//
|
||||
var result = await aiService
|
||||
.GetResponseAsync(
|
||||
prompt: prompt,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result);
|
||||
return Ok(result.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -100,6 +100,8 @@ namespace xAiApi.Controllers.V1
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
public async Task AskAIStream(
|
||||
[FromQuery] string prompt,
|
||||
[FromQuery] string projectId = null,
|
||||
[FromQuery] string conversationId = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
@@ -113,69 +115,37 @@ namespace xAiApi.Controllers.V1
|
||||
.Append("Content-Type", "text/event-stream");
|
||||
|
||||
//
|
||||
// Retrieving Stream ...
|
||||
var userInfo = await GetUserInfo();
|
||||
var stream = aiService
|
||||
.GetTextReponseStreamAsync(
|
||||
.AskStream(
|
||||
prompt: prompt,
|
||||
userInfo: userInfo,
|
||||
projectId: projectId,
|
||||
conversationId: conversationId,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
await foreach (var message in stream)
|
||||
{
|
||||
//
|
||||
// Do What we Have to Do with Message ...
|
||||
// Converts Model to Json String ...
|
||||
var json = message.ToJSON();
|
||||
|
||||
//
|
||||
var msgBytes = message.ToBytes();
|
||||
// Generates Json byte[] ...
|
||||
var bytes = json.ToBytes();
|
||||
|
||||
//
|
||||
// Write Bytes to Stream ...
|
||||
await Response.Body.WriteAsync(
|
||||
msgBytes,
|
||||
bytes,
|
||||
0,
|
||||
msgBytes.Length
|
||||
bytes.Length,
|
||||
cancellationToken
|
||||
);
|
||||
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
|
||||
);
|
||||
// Flushing Stream ...
|
||||
await Response.Body.FlushAsync(cancellationToken);
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user