diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8d4a6c0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +bin +obj \ No newline at end of file diff --git a/Constants/XAiApiEndpoint.cs b/Constants/XAiApiEndpoint.cs index c392813..e5c9634 100644 --- a/Constants/XAiApiEndpoint.cs +++ b/Constants/XAiApiEndpoint.cs @@ -6,5 +6,7 @@ namespace xAiService.Constants { [StringValue("AskAI")] Ask, + [StringValue("AskAIStream")] + Stream, } } \ No newline at end of file diff --git a/Controllers/XAiServiceControllerBase.cs b/Controllers/XAiServiceControllerBase.cs index 0ddb6a1..e6acfc3 100644 --- a/Controllers/XAiServiceControllerBase.cs +++ b/Controllers/XAiServiceControllerBase.cs @@ -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 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 } diff --git a/Interfaces/IXAiService.cs b/Interfaces/IXAiService.cs index f393f4c..3e4b314 100644 --- a/Interfaces/IXAiService.cs +++ b/Interfaces/IXAiService.cs @@ -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 ... /// /// + /// /// /// public Task GetResponseAsync( @@ -33,6 +35,7 @@ namespace xAiService.Interfaces /// Generated Response ... /// /// + /// /// /// public Task GetTextResponseAsync( @@ -45,9 +48,10 @@ namespace xAiService.Interfaces /// Generate Text Response Stream ... /// /// + /// /// /// - Task GetTextReponseStreamAsync( + IAsyncEnumerable GetTextReponseStreamAsync( string prompt, XUserClaimsInfoDto userInfo, CancellationToken cancellationToken = default diff --git a/Interfaces/IXAiServiceControllerBase.cs b/Interfaces/IXAiServiceControllerBase.cs index 49239da..bfe126c 100644 --- a/Interfaces/IXAiServiceControllerBase.cs +++ b/Interfaces/IXAiServiceControllerBase.cs @@ -10,5 +10,10 @@ namespace xAiService.Interfaces [FromQuery] string prompt, CancellationToken cancellationToken = default ); + + Task AskAIStream( + [FromQuery] string prompt, + CancellationToken cancellationToken = default + ); } } \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..b029642 --- /dev/null +++ b/README.md @@ -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) diff --git a/Services/XAiService.cs b/Services/XAiService.cs index eb30a37..79728f1 100644 --- a/Services/XAiService.cs +++ b/Services/XAiService.cs @@ -45,6 +45,7 @@ namespace xAiService.Services /// Generated Response ... /// /// + /// /// /// public Task GetResponseAsync( @@ -60,6 +61,7 @@ namespace xAiService.Services /// Generated Response ... /// /// + /// /// /// public async Task GetTextResponseAsync( @@ -99,9 +101,10 @@ namespace xAiService.Services /// Generate Text Response Stream ... /// /// + /// /// /// - public async Task GetTextReponseStreamAsync( + public IAsyncEnumerable 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( + tokens: tokens, addXPoweredValue: true, + endpoint: XAiApiEndpoint.Stream, + cancellationToken: cancellationToken, queryStrings: new Dictionary { { XAiApiQueryParams.Prompt, prompt } @@ -123,15 +135,7 @@ namespace xAiService.Services ); // - var response = await RunRestRequest( - client, - request, - XHttpMethod.GET, - cancellationToken: cancellationToken - ); - - // - return response; + return result; } #endregion }