This commit is contained in:
2026-04-17 03:04:14 +03:30
parent 43d0433a8e
commit 8868afe656
5 changed files with 307 additions and 6 deletions
+73 -1
View File
@@ -4,6 +4,7 @@ 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;
@@ -66,6 +67,35 @@ namespace xAiApi.Controllers.V1
}
}
[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);
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
[HttpGet("AskAIStream")]
[Authorize(Policy = XPolicies.EnabledUser)]
public async Task AskAIStream(
@@ -97,7 +127,7 @@ namespace xAiApi.Controllers.V1
// Do What we Have to Do with Message ...
//
var msgBytes = message.Text.ToBytes();
var msgBytes = message.ToBytes();
await Response.Body.WriteAsync(
msgBytes,
0,
@@ -113,6 +143,48 @@ namespace xAiApi.Controllers.V1
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
}
}