This commit is contained in:
2026-04-10 23:51:44 +03:30
parent e4ddceafa0
commit 81ea808377
3 changed files with 28 additions and 20 deletions
+26 -5
View File
@@ -1,6 +1,8 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using xAiApi.AI.Interfaces;
@@ -37,7 +39,8 @@ namespace xAiApi.Controllers.V1
[AllowAnonymous]
[HttpGet("AskAI")]
public async Task<ActionResult<string>> AskAI(
[FromQuery] string prompt
[FromQuery] string prompt,
CancellationToken cancellationToken = default
)
{
//
@@ -46,7 +49,10 @@ namespace xAiApi.Controllers.V1
{
//
var result = await aiService
.GetTextResponseAsync(prompt);
.GetTextResponseAsync(
prompt: prompt,
cancellationToken: cancellationToken
);
//
return Ok(result);
@@ -62,7 +68,8 @@ namespace xAiApi.Controllers.V1
[AllowAnonymous]
[HttpGet("AskAIStream")]
public async Task AskAIStream(
[FromQuery] string prompt
[FromQuery] string prompt,
CancellationToken cancellationToken = default
)
{
//
@@ -70,8 +77,17 @@ namespace xAiApi.Controllers.V1
try
{
//
// Adding Content Type ...
Response.Headers
.Append("Content-Type", "text/event-stream");
//
// Retrieving Stream ...
var stream = aiService
.GetTextReponseStreamAsync(prompt);
.GetTextReponseStreamAsync(
prompt: prompt,
cancellationToken: cancellationToken
);
await foreach (var message in stream)
{
//
@@ -79,11 +95,16 @@ namespace xAiApi.Controllers.V1
//
var msgBytes = message.Text.ToBytes();
await Response.Body.WriteAsync(msgBytes, 0, msgBytes.Length);
await Response.Body.WriteAsync(
msgBytes,
0,
msgBytes.Length
);
await Response.Body.FlushAsync();
}
//
// Closing Connection ...
await Response.Body.FlushAsync();
}
catch