last ...
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
bin
|
||||
obj
|
||||
@@ -6,5 +6,7 @@ namespace xAiService.Constants
|
||||
{
|
||||
[StringValue("AskAI")]
|
||||
Ask,
|
||||
[StringValue("AskAIStream")]
|
||||
Stream,
|
||||
}
|
||||
}
|
||||
@@ -47,11 +47,11 @@ namespace xAiService.Controllers
|
||||
try
|
||||
{
|
||||
//
|
||||
var tokens = Ex();
|
||||
var userInfo = await GetUserInfo();
|
||||
var result = await aiService
|
||||
.GetTextResponseAsync(
|
||||
prompt: prompt,
|
||||
tokens: tokens,
|
||||
userInfo: userInfo,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
@@ -67,12 +67,33 @@ namespace xAiService.Controllers
|
||||
}
|
||||
|
||||
[HttpGet("AskStream")]
|
||||
public async Task AskAIStream(
|
||||
public async Task<ActionResult> AskAIStream(
|
||||
[FromQuery] string prompt,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
|
||||
//
|
||||
// Do ...
|
||||
try
|
||||
{
|
||||
//
|
||||
var userInfo = await GetUserInfo();
|
||||
var result = aiService
|
||||
.GetTextReponseStreamAsync(
|
||||
prompt: prompt,
|
||||
userInfo: userInfo,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.AI;
|
||||
@@ -21,6 +22,7 @@ namespace xAiService.Interfaces
|
||||
/// Generated Response ...
|
||||
/// </summary>
|
||||
/// <param name="prompt"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public Task<ChatResponse> GetResponseAsync(
|
||||
@@ -33,6 +35,7 @@ namespace xAiService.Interfaces
|
||||
/// Generated Response ...
|
||||
/// </summary>
|
||||
/// <param name="prompt"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public Task<string> GetTextResponseAsync(
|
||||
@@ -45,9 +48,10 @@ namespace xAiService.Interfaces
|
||||
/// Generate Text Response Stream ...
|
||||
/// </summary>
|
||||
/// <param name="prompt"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task GetTextReponseStreamAsync(
|
||||
IAsyncEnumerable<string> GetTextReponseStreamAsync(
|
||||
string prompt,
|
||||
XUserClaimsInfoDto userInfo,
|
||||
CancellationToken cancellationToken = default
|
||||
|
||||
@@ -10,5 +10,10 @@ namespace xAiService.Interfaces
|
||||
[FromQuery] string prompt,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
|
||||
Task<ActionResult> AskAIStream(
|
||||
[FromQuery] string prompt,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
# xSaherElm.xAiService
|
||||
|
||||
it is a Part of xSaherElm Project which integrate and provides all xAiApi features.
|
||||
|
||||
## Maintainer
|
||||
|
||||
Hadi Khazaee asl
|
||||
|
||||
[https://www.saherelm.ir](https://www.saherelm.ir)
|
||||
|
||||
[hadi_khazaee_asl@yahoo.com](mailto:hadi_khazaee_asl@yahoo.com)
|
||||
+18
-14
@@ -45,6 +45,7 @@ namespace xAiService.Services
|
||||
/// Generated Response ...
|
||||
/// </summary>
|
||||
/// <param name="prompt"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public Task<ChatResponse> GetResponseAsync(
|
||||
@@ -60,6 +61,7 @@ namespace xAiService.Services
|
||||
/// Generated Response ...
|
||||
/// </summary>
|
||||
/// <param name="prompt"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<string> GetTextResponseAsync(
|
||||
@@ -99,9 +101,10 @@ namespace xAiService.Services
|
||||
/// Generate Text Response Stream ...
|
||||
/// </summary>
|
||||
/// <param name="prompt"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task GetTextReponseStreamAsync(
|
||||
public IAsyncEnumerable<string> GetTextReponseStreamAsync(
|
||||
string prompt,
|
||||
XUserClaimsInfoDto userInfo,
|
||||
CancellationToken cancellationToken = default
|
||||
@@ -109,13 +112,22 @@ namespace xAiService.Services
|
||||
{
|
||||
//
|
||||
// Validate Args ...
|
||||
ValidationProvider.NotEmpty(prompt);
|
||||
ValidationProvider
|
||||
.GroupValidationBuilder()
|
||||
.AddNotEmpty(prompt)
|
||||
.AddNotNull(userInfo)
|
||||
.AddNotZero(userInfo.ExpiresAt)
|
||||
.AddNotEmpty(userInfo.AccessToken)
|
||||
.AddNotEmpty(userInfo.RefreshToken)
|
||||
.ValidateGroup();
|
||||
|
||||
//
|
||||
var client = GetRestClient(userInfo);
|
||||
var request = GetRestRequet(
|
||||
endpoint: XAiApiEndpoint.Ask,
|
||||
var tokens = ToTokenResponse(userInfo);
|
||||
var result = StreamData<string>(
|
||||
tokens: tokens,
|
||||
addXPoweredValue: true,
|
||||
endpoint: XAiApiEndpoint.Stream,
|
||||
cancellationToken: cancellationToken,
|
||||
queryStrings: new Dictionary<string, string>
|
||||
{
|
||||
{ XAiApiQueryParams.Prompt, prompt }
|
||||
@@ -123,15 +135,7 @@ namespace xAiService.Services
|
||||
);
|
||||
|
||||
//
|
||||
var response = await RunRestRequest<string>(
|
||||
client,
|
||||
request,
|
||||
XHttpMethod.GET,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return response;
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user