From 81ea80837709eb01a163115a1a95ff104ceac8b9 Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Fri, 10 Apr 2026 23:51:44 +0330 Subject: [PATCH] last ... --- AI/Services/XAIService.cs | 2 +- Controllers/V1/AIController.cs | 31 ++++++++++++++++++++++++++----- nuget.config | 15 +-------------- 3 files changed, 28 insertions(+), 20 deletions(-) diff --git a/AI/Services/XAIService.cs b/AI/Services/XAIService.cs index 42fe3db..8559eb1 100644 --- a/AI/Services/XAIService.cs +++ b/AI/Services/XAIService.cs @@ -7,8 +7,8 @@ using xCommons.Extensions; using xExceptions.Constants; using xAiApi.AI.Configuration; using System.Collections.Generic; -using Microsoft.SemanticKernel.ChatCompletion; using System.Threading; +using Microsoft.SemanticKernel.ChatCompletion; namespace xAiApi.AI.Services { diff --git a/Controllers/V1/AIController.cs b/Controllers/V1/AIController.cs index fd0bc21..2d7f480 100644 --- a/Controllers/V1/AIController.cs +++ b/Controllers/V1/AIController.cs @@ -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> 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 diff --git a/nuget.config b/nuget.config index fec1fa5..ddee910 100644 --- a/nuget.config +++ b/nuget.config @@ -2,20 +2,7 @@ + - - - - - - - - - \ No newline at end of file