last ...
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user