last works ...
This commit is contained in:
@@ -8,11 +8,13 @@ using Microsoft.Extensions.AI;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using OpenAI.Realtime;
|
||||
using xAiApi.AI.Interfaces;
|
||||
using xAiApi.AI.Models.Dtos;
|
||||
using xAiApi.Base;
|
||||
using xAiModels.Models;
|
||||
using xCommons.Configurations;
|
||||
using xCommons.Extensions;
|
||||
using xCommons.Providers;
|
||||
using xExceptions.Constants;
|
||||
using xIdentityHelper;
|
||||
using xIdentityService.Constants;
|
||||
using xIdentityService.Interfaces;
|
||||
@@ -51,6 +53,62 @@ namespace xAiApi.Controllers.V1
|
||||
|
||||
//
|
||||
#region Actions ...
|
||||
/// <summary>
|
||||
/// As a Message from Ai ...
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// </remarks>
|
||||
/// <param name="request"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("Ask")]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
public async Task<ActionResult<XAiMessageDto>> Ask(
|
||||
[FromQuery] XAiAskRequestDto request,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Do ...
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate Model ...
|
||||
var isValid =
|
||||
ModelState.IsValid &&
|
||||
!request.IsNull() &&
|
||||
!request.Prompt.IsNullOrEmpty();
|
||||
if (!isValid)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Retrieve User Info ...
|
||||
var userInfo = await GetUserInfo();
|
||||
|
||||
//
|
||||
// Retrieve Result ...
|
||||
var result = await aiService
|
||||
.Ask(
|
||||
userInfo: userInfo,
|
||||
prompt: request.Prompt,
|
||||
projectId: request.ProjectId,
|
||||
cancellationToken: cancellationToken,
|
||||
conversationId: request.ConversationId
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// As a Message from Ai ...
|
||||
/// </summary>
|
||||
@@ -96,6 +154,98 @@ namespace xAiApi.Controllers.V1
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ask a Message from Ai using Streamin Pattern ...
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// </remarks>
|
||||
/// <param name="request"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("AskStream")]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
public async Task AskStream(
|
||||
[FromQuery] XAiAskRequestDto request,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Do ...
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate Model ...
|
||||
var isValid =
|
||||
ModelState.IsValid &&
|
||||
!request.IsNull() &&
|
||||
!request.Prompt.IsNullOrEmpty();
|
||||
if (!isValid)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Adding Content Type ...
|
||||
Response.Headers
|
||||
.Append("Content-Type", "text/event-stream");
|
||||
|
||||
//
|
||||
// Retrieve User Info ...
|
||||
var userInfo = await GetUserInfo();
|
||||
|
||||
//
|
||||
// Access Async Enumerable ...
|
||||
var stream = aiService
|
||||
.AskStream(
|
||||
userInfo: userInfo,
|
||||
prompt: request.Prompt,
|
||||
projectId: request.ProjectId,
|
||||
cancellationToken: cancellationToken,
|
||||
conversationId: request.ConversationId
|
||||
);
|
||||
|
||||
//
|
||||
// Loop through Enumerable ...
|
||||
await foreach (var message in stream)
|
||||
{
|
||||
//
|
||||
// Converts Model to Json String ...
|
||||
var json = message.ToJSON();
|
||||
|
||||
//
|
||||
// Generates Json byte[] ...
|
||||
var bytes = json.ToBytes();
|
||||
|
||||
//
|
||||
// Write Bytes to Stream ...
|
||||
await Response.Body.WriteAsync(
|
||||
bytes,
|
||||
0,
|
||||
bytes.Length,
|
||||
cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
// Flushing Stream ...
|
||||
await Response.Body.FlushAsync(cancellationToken);
|
||||
}
|
||||
|
||||
//
|
||||
// Closing Connection ...
|
||||
await Response.Body.FlushAsync();
|
||||
}
|
||||
catch (Exception)
|
||||
{ }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ask a Message from Ai using Streamin Pattern ...
|
||||
/// </summary>
|
||||
/// <param name="prompt"></param>
|
||||
/// <param name="projectId"></param>
|
||||
/// <param name="conversationId"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("AskAIStream")]
|
||||
[Authorize(Policy = XPolicies.EnabledUser)]
|
||||
public async Task AskAIStream(
|
||||
@@ -124,6 +274,9 @@ namespace xAiApi.Controllers.V1
|
||||
conversationId: conversationId,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
// Loop through Enumerable ...
|
||||
await foreach (var message in stream)
|
||||
{
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user