last ...
This commit is contained in:
@@ -1,653 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using RestSharp;
|
||||
using RestSharp.Authenticators;
|
||||
using xCommons.Constants;
|
||||
using xCommons.Extensions;
|
||||
using xCommons.Providers;
|
||||
using xExceptions.Constants;
|
||||
using xHttpService.Constants;
|
||||
using xIdentityModels.Constants;
|
||||
using xIdentityModels.Extensions;
|
||||
using xIdentityModels.Models;
|
||||
using xIdentityService.Constants;
|
||||
using xIdentityService.Interfaces;
|
||||
using xIdentityService.Models;
|
||||
|
||||
namespace xAiService.Base
|
||||
{
|
||||
public interface IXBaseIdentityHttpProvider
|
||||
{
|
||||
string BaseUrl { get; }
|
||||
ILogger Logger { get; }
|
||||
string XPoweredValue { get; }
|
||||
int DefaultClientTimeout { get; }
|
||||
XValidationProvider ValidationProvider { get; }
|
||||
|
||||
HttpClient GetHttpClient();
|
||||
RestClient GetRestClient(XTokenResponse tokens = null);
|
||||
RestRequest GetRestRequet(
|
||||
Enum endpoint,
|
||||
bool addXPoweredValue = true,
|
||||
IDictionary<string, string> @params = null,
|
||||
IDictionary<string, string> headers = null,
|
||||
IDictionary<string, string> queryStrings = null
|
||||
);
|
||||
|
||||
Task<T> RunRestRequest<T>(
|
||||
RestClient client,
|
||||
RestRequest request,
|
||||
XHttpMethod method,
|
||||
bool supportRefreshingTokens = true,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
|
||||
Task<XBaseRestResponse<T>> RunRestRequestByResponse<T>(
|
||||
RestClient client,
|
||||
RestRequest request,
|
||||
XHttpMethod method,
|
||||
bool supportRefreshingTokens = true,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
|
||||
string GetUserSelectByParam(
|
||||
XActionRequest model,
|
||||
bool forceNotNull = true,
|
||||
ICollection<XUserSelectBy> excludes = null
|
||||
);
|
||||
}
|
||||
|
||||
public abstract class XBaseIdentityHttpProvider : IXBaseIdentityHttpProvider
|
||||
{
|
||||
public string BaseUrl { get; }
|
||||
public ILogger Logger { get; }
|
||||
public string XPoweredValue { get; }
|
||||
public int DefaultClientTimeout { get; }
|
||||
public XValidationProvider ValidationProvider { get; }
|
||||
|
||||
private readonly IXTokenProvider tokenProvider;
|
||||
private readonly IXIdentityProvider identityProvider;
|
||||
|
||||
public XBaseIdentityHttpProvider(
|
||||
string baseUrl,
|
||||
ILogger logger,
|
||||
string poweredValue,
|
||||
int defaultClientTimeout,
|
||||
IXTokenProvider tokenProvider,
|
||||
IXIdentityProvider identityProvider,
|
||||
XValidationProvider validationProvider
|
||||
)
|
||||
{
|
||||
//
|
||||
Logger = logger;
|
||||
BaseUrl = baseUrl;
|
||||
XPoweredValue = poweredValue;
|
||||
this.tokenProvider = tokenProvider;
|
||||
ValidationProvider = validationProvider;
|
||||
this.identityProvider = identityProvider;
|
||||
DefaultClientTimeout = defaultClientTimeout;
|
||||
}
|
||||
|
||||
//
|
||||
#region Tools ...
|
||||
/// <summary>
|
||||
/// Get an Instance of Http Client
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public HttpClient GetHttpClient()
|
||||
{
|
||||
//
|
||||
HttpClient httpClient = null;
|
||||
|
||||
//
|
||||
// httpClient = new HttpClient ();
|
||||
var httpClientHandler = new HttpClientHandler()
|
||||
{
|
||||
ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) =>
|
||||
{
|
||||
//
|
||||
Logger.LogInformation(
|
||||
$"XSSL Handler: {Environment.NewLine}, sender: {sender}, {Environment.NewLine} cert: {cert}, {Environment.NewLine} chain: {chain}, {Environment.NewLine} sslPolicyErrors: {sslPolicyErrors}"
|
||||
);
|
||||
|
||||
//
|
||||
return true;
|
||||
},
|
||||
ClientCertificateOptions = ClientCertificateOption.Manual,
|
||||
};
|
||||
httpClient = new HttpClient(httpClientHandler);
|
||||
|
||||
//
|
||||
// Set Timeout ...
|
||||
// TODO: Fix this ...
|
||||
// httpClient.Timeout = TimeSpan
|
||||
// .FromSeconds (identityConfiguration.DefaultClientTimeout);
|
||||
|
||||
//
|
||||
return httpClient;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get an Instance of RestClient
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public RestClient GetRestClient(XTokenResponse tokens = null)
|
||||
{
|
||||
//
|
||||
var client = new RestClient(BaseUrl);
|
||||
|
||||
//
|
||||
// Set Authenticator ...
|
||||
if (!tokens.IsNull() &&
|
||||
!tokens.AccessToken.IsNullOrEmpty())
|
||||
{
|
||||
client.Authenticator = new JwtAuthenticator(tokens.AccessToken);
|
||||
}
|
||||
|
||||
//
|
||||
// Refresh Token ...
|
||||
if (!tokens.IsNull() &&
|
||||
!tokens.RefreshToken.IsNullOrEmpty())
|
||||
{
|
||||
client.AddDefaultHeader(XAuthorization.RefreshToken, tokens.RefreshToken);
|
||||
}
|
||||
|
||||
//
|
||||
// ExpiresAt ...
|
||||
if (!tokens.IsNull() &&
|
||||
tokens.ExpiresAt > 0)
|
||||
{
|
||||
client.AddDefaultHeader(XAuthorization.ExpiresAt, tokens.ExpiresAt.AsHttpParamString());
|
||||
}
|
||||
|
||||
//
|
||||
// Set Default Timeout ...
|
||||
client.Timeout = DefaultClientTimeout;
|
||||
|
||||
//
|
||||
// SSL Validation Handler ...
|
||||
client.RemoteCertificateValidationCallback = (sender, cert, chain, sslPolicyErrors) =>
|
||||
{
|
||||
//
|
||||
Logger.LogInformation(
|
||||
$"XSSL Handler: {Environment.NewLine}, sender: {sender}, {Environment.NewLine} cert: {cert}, {Environment.NewLine} chain: {chain}, {Environment.NewLine} sslPolicyErrors: {sslPolicyErrors}"
|
||||
);
|
||||
|
||||
//
|
||||
return true;
|
||||
};
|
||||
|
||||
//
|
||||
return client;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get an Instance of RestRequest
|
||||
/// </summary>
|
||||
public RestRequest GetRestRequet(
|
||||
Enum endpoint,
|
||||
bool addXPoweredValue = true,
|
||||
IDictionary<string, string> @params = null,
|
||||
IDictionary<string, string> headers = null,
|
||||
IDictionary<string, string> queryStrings = null
|
||||
)
|
||||
{
|
||||
//
|
||||
var url = endpoint.GetStringValue();
|
||||
|
||||
//
|
||||
// Add Route Payloads ...
|
||||
if (!@params.IsNull() && @params.Count > 0)
|
||||
{
|
||||
//
|
||||
var enumerator = @params.GetEnumerator();
|
||||
while (enumerator.MoveNext())
|
||||
{
|
||||
//
|
||||
var item = enumerator.Current;
|
||||
|
||||
//
|
||||
url = url.Replace(item.Key, item.Value.ToUrlEncoded());
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Add Query Strings ...
|
||||
if (!queryStrings.IsNull() && queryStrings.Count > 0)
|
||||
{
|
||||
//
|
||||
url = $"{url}?";
|
||||
var enumerator = queryStrings.GetEnumerator();
|
||||
while (enumerator.MoveNext())
|
||||
{
|
||||
//
|
||||
var item = enumerator.Current;
|
||||
|
||||
//
|
||||
url += $"{item.Key}={item.Value.ToUrlEncoded()}&";
|
||||
}
|
||||
|
||||
//
|
||||
// remove lates & ...
|
||||
if (url.EndsWith("&"))
|
||||
{
|
||||
url = url.Substring(0, url.Length - 1);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Create request ...
|
||||
var request = new RestRequest(url, DataFormat.Json);
|
||||
|
||||
//
|
||||
if (addXPoweredValue)
|
||||
{
|
||||
request.AddHeader(XAuthorization.XPoweredBy, XPoweredValue);
|
||||
}
|
||||
|
||||
//
|
||||
// Add Headers to request ...
|
||||
if (!headers.IsNull() && headers.Count > 0)
|
||||
{
|
||||
//
|
||||
var enumerator = headers.GetEnumerator();
|
||||
while (enumerator.MoveNext())
|
||||
{
|
||||
//
|
||||
var item = enumerator.Current;
|
||||
request.AddHeader(item.Key, item.Value);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return request;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handle Call Specific Request and retrieve Response
|
||||
/// </summary>
|
||||
/// <param name="client"></param>
|
||||
/// <param name="request"></param>
|
||||
/// <param name="method"></param>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
public async Task<T> RunRestRequest<T>(
|
||||
RestClient client,
|
||||
RestRequest request,
|
||||
XHttpMethod method,
|
||||
bool supportRefreshingTokens = true,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
var response = await Task.Run(async () =>
|
||||
{
|
||||
//
|
||||
IRestResponse resp = null;
|
||||
|
||||
//
|
||||
// Call Endpoint Service using Specified Method ...
|
||||
switch (method)
|
||||
{
|
||||
//
|
||||
// Get ...
|
||||
case XHttpMethod.GET:
|
||||
resp = client.Get(request);
|
||||
break;
|
||||
|
||||
//
|
||||
// Put ...
|
||||
case XHttpMethod.PUT:
|
||||
resp = client.Put(request);
|
||||
break;
|
||||
|
||||
//
|
||||
// Post ...
|
||||
case XHttpMethod.POST:
|
||||
resp = client.Post(request);
|
||||
break;
|
||||
|
||||
//
|
||||
// Delete ...
|
||||
case XHttpMethod.DELETE:
|
||||
resp = client.Delete(request);
|
||||
break;
|
||||
}
|
||||
|
||||
//
|
||||
if (!resp.IsSuccessful)
|
||||
{
|
||||
//
|
||||
// Log Exception ...
|
||||
Logger.LogError($"Error: {resp.ErrorMessage}");
|
||||
Logger.LogError($"Exception: {resp.ErrorException.ToJSON()}");
|
||||
if (!resp.ErrorMessage.IsNullOrEmpty() &&
|
||||
resp.ErrorMessage.Contains("timed out"))
|
||||
{
|
||||
XException.Timeout.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// TODO: Check UnAuthorized Status here for
|
||||
// Refresh Tokens or Issue Correct Exception ...
|
||||
if (supportRefreshingTokens &&
|
||||
resp.StatusCode == System.Net.HttpStatusCode.Unauthorized)
|
||||
{
|
||||
//
|
||||
var isRefreshed = request.Parameters.Any(rp =>
|
||||
rp.Name == XParam.XIsRefreshed.GetStringValue() &&
|
||||
rp.Value.ToString().ToNormalString() == true.ToJSON());
|
||||
|
||||
//
|
||||
Logger.LogInformation($"isRefreshed: {isRefreshed}");
|
||||
|
||||
//
|
||||
// Get Access Token ...
|
||||
XTokenResponse tokens = null;
|
||||
var accessToken = request.Parameters.FirstOrDefault(rp =>
|
||||
rp.Name.ToNormalString() == XAuthorization.Header.ToNormalString()).Value.ToString();
|
||||
if (!accessToken.IsNullOrEmpty())
|
||||
{
|
||||
//
|
||||
accessToken = accessToken.Replace(XAuthorization.TokenIdentifier, "");
|
||||
|
||||
//
|
||||
if (!accessToken.IsNullOrEmpty())
|
||||
{
|
||||
tokens = await tokenProvider.RetrieveToken(accessToken);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Refresh Token and then re do request ...
|
||||
if (!isRefreshed &&
|
||||
!accessToken.IsNullOrEmpty() &&
|
||||
!tokens.IsNull() &&
|
||||
tokens.IsRefreshable())
|
||||
{
|
||||
//
|
||||
request.AddHeader(XParam.XIsRefreshed.GetStringValue(), true.ToJSON());
|
||||
|
||||
//
|
||||
// Refreshe Tokens ...
|
||||
var refreshedTokens = await identityProvider
|
||||
.RefreshTokens(tokens);
|
||||
|
||||
//
|
||||
// Renew Rest Client with Refreshed Tokens ...
|
||||
var timeout = client.Timeout;
|
||||
client = GetRestClient(refreshedTokens);
|
||||
client.Timeout = Timeout.Infinite;
|
||||
|
||||
//
|
||||
// Do Request Again using renew Client ...
|
||||
return await RunRestRequest<T>(
|
||||
client: client,
|
||||
request: request,
|
||||
method: method);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
var ex = resp.Content.ToXError();
|
||||
if (!ex.IsNull())
|
||||
{
|
||||
throw ex.ToException();
|
||||
}
|
||||
|
||||
//
|
||||
// UnAuthorized ...
|
||||
if (resp.StatusCode == System.Net.HttpStatusCode.Unauthorized)
|
||||
{
|
||||
XException.NotAuthorized.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// NotFound ...
|
||||
if (resp.StatusCode == System.Net.HttpStatusCode.NotFound)
|
||||
{
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
XException.ActionFailed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var result = resp.Content.FromJSON<T>();
|
||||
return result;
|
||||
},
|
||||
cancellationToken: cancellationToken);
|
||||
|
||||
//
|
||||
return response;
|
||||
}
|
||||
public async Task<XBaseRestResponse<T>> RunRestRequestByResponse<T>(
|
||||
RestClient client,
|
||||
RestRequest request,
|
||||
XHttpMethod method,
|
||||
bool supportRefreshingTokens = true,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
var response = await Task.Run(async () =>
|
||||
{
|
||||
//
|
||||
IRestResponse resp = null;
|
||||
|
||||
//
|
||||
// Call Endpoint Service using Specified Method ...
|
||||
switch (method)
|
||||
{
|
||||
//
|
||||
// Get ...
|
||||
case XHttpMethod.GET:
|
||||
resp = client.Get(request);
|
||||
break;
|
||||
|
||||
//
|
||||
// Put ...
|
||||
case XHttpMethod.PUT:
|
||||
resp = client.Put(request);
|
||||
break;
|
||||
|
||||
//
|
||||
// Post ...
|
||||
case XHttpMethod.POST:
|
||||
resp = client.Post(request);
|
||||
break;
|
||||
|
||||
//
|
||||
// Delete ...
|
||||
case XHttpMethod.DELETE:
|
||||
resp = client.Delete(request);
|
||||
break;
|
||||
}
|
||||
|
||||
//
|
||||
if (!resp.IsSuccessful)
|
||||
{
|
||||
//
|
||||
// Log Exception ...
|
||||
Logger.LogError($"Error: {resp.ErrorMessage}");
|
||||
Logger.LogError($"Exception: {resp.ErrorException.ToJSON()}");
|
||||
if (!resp.ErrorMessage.IsNullOrEmpty() &&
|
||||
resp.ErrorMessage.Contains("timed out"))
|
||||
{
|
||||
XException.Timeout.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// TODO: Check UnAuthorized Status here for
|
||||
// Refresh Tokens or Issue Correct Exception ...
|
||||
if (supportRefreshingTokens &&
|
||||
resp.StatusCode == System.Net.HttpStatusCode.Unauthorized)
|
||||
{
|
||||
//
|
||||
var isRefreshed = request.Parameters.Any(rp =>
|
||||
rp.Name == XParam.XIsRefreshed.GetStringValue() &&
|
||||
rp.Value.ToString().ToNormalString() == true.ToJSON());
|
||||
|
||||
//
|
||||
Logger.LogInformation($"isRefreshed: {isRefreshed}");
|
||||
|
||||
//
|
||||
// Get Access Token ...
|
||||
XTokenResponse tokens = null;
|
||||
var accessToken = request.Parameters.FirstOrDefault(rp =>
|
||||
rp.Name.ToNormalString() == XAuthorization.Header.ToNormalString()).Value.ToString();
|
||||
if (!accessToken.IsNullOrEmpty())
|
||||
{
|
||||
//
|
||||
accessToken = accessToken.Replace(XAuthorization.TokenIdentifier, "");
|
||||
|
||||
//
|
||||
if (!accessToken.IsNullOrEmpty())
|
||||
{
|
||||
tokens = await tokenProvider.RetrieveToken(accessToken);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Refresh Token and then re do request ...
|
||||
if (!isRefreshed &&
|
||||
!accessToken.IsNullOrEmpty() &&
|
||||
!tokens.IsNull() &&
|
||||
tokens.IsRefreshable())
|
||||
{
|
||||
//
|
||||
request.AddHeader(XParam.XIsRefreshed.GetStringValue(), true.ToJSON());
|
||||
|
||||
//
|
||||
// Refreshe Tokens ...
|
||||
var refreshedTokens = await identityProvider
|
||||
.RefreshTokens(tokens);
|
||||
|
||||
//
|
||||
// Renew Rest Client with Refreshed Tokens ...
|
||||
var timeout = client.Timeout;
|
||||
client = GetRestClient(refreshedTokens);
|
||||
client.Timeout = Timeout.Infinite;
|
||||
|
||||
//
|
||||
// Do Request Again using renew Client ...
|
||||
return await RunRestRequestByResponse<T>(
|
||||
client: client,
|
||||
request: request,
|
||||
method: method
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
var ex = resp.Content.ToXError();
|
||||
if (!ex.IsNull())
|
||||
{
|
||||
throw ex.ToException();
|
||||
}
|
||||
|
||||
//
|
||||
// UnAuthorized ...
|
||||
if (resp.StatusCode == System.Net.HttpStatusCode.Unauthorized)
|
||||
{
|
||||
XException.NotAuthorized.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// NotFound ...
|
||||
if (resp.StatusCode == System.Net.HttpStatusCode.NotFound)
|
||||
{
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
XException.ActionFailed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var result = new XBaseRestResponse<T>
|
||||
{
|
||||
Response = resp,
|
||||
Model = resp.Content.FromJSON<T>()
|
||||
};
|
||||
|
||||
//
|
||||
return result;
|
||||
},
|
||||
cancellationToken: cancellationToken);
|
||||
|
||||
//
|
||||
return response;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get User SelectBy Param
|
||||
/// </summary>
|
||||
/// <param name="model"></param>
|
||||
/// <param name="forceNotNull"></param>
|
||||
/// <param name="excludes"></param>
|
||||
/// <returns></returns>
|
||||
public string GetUserSelectByParam(
|
||||
XActionRequest model,
|
||||
bool forceNotNull = true,
|
||||
ICollection<XUserSelectBy> excludes = null
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate Args ...
|
||||
ValidationProvider.NotNull(model);
|
||||
|
||||
//
|
||||
var id = model.UserId;
|
||||
var userName = model.UserName;
|
||||
var email = model.Email;
|
||||
var phoneNumber = model.MobileNumber;
|
||||
|
||||
//
|
||||
if (excludes != null)
|
||||
{
|
||||
//
|
||||
if (excludes.Contains(XUserSelectBy.ID))
|
||||
{
|
||||
id = null;
|
||||
}
|
||||
if (excludes.Contains(XUserSelectBy.Email))
|
||||
{
|
||||
email = null;
|
||||
}
|
||||
if (excludes.Contains(XUserSelectBy.MobileNumber))
|
||||
{
|
||||
phoneNumber = null;
|
||||
}
|
||||
if (excludes.Contains(XUserSelectBy.Username))
|
||||
{
|
||||
userName = null;
|
||||
}
|
||||
}
|
||||
|
||||
var selectByParam =
|
||||
id.IsNullOrEmpty() ?
|
||||
userName.IsNullOrEmpty() ?
|
||||
email.IsNullOrEmpty() ?
|
||||
phoneNumber.IsNullOrEmpty() ? null : phoneNumber : email : userName : id;
|
||||
|
||||
//
|
||||
// Check result ...
|
||||
if (forceNotNull &&
|
||||
selectByParam.IsNullOrEmpty())
|
||||
{
|
||||
XException.InvalidData.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
return selectByParam;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -47,9 +47,11 @@ namespace xAiService.Controllers
|
||||
try
|
||||
{
|
||||
//
|
||||
var tokens = Ex();
|
||||
var result = await aiService
|
||||
.GetTextResponseAsync(
|
||||
prompt: prompt,
|
||||
tokens: tokens,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
@@ -63,6 +65,15 @@ namespace xAiService.Controllers
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("AskStream")]
|
||||
public async Task AskAIStream(
|
||||
[FromQuery] string prompt,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,9 @@
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.AI;
|
||||
using xAiService.Base;
|
||||
using xAiService.Configurations;
|
||||
using xIdentityModels.Models;
|
||||
using xIdentityService.Interfaces;
|
||||
|
||||
namespace xAiService.Interfaces
|
||||
{
|
||||
@@ -24,6 +25,7 @@ namespace xAiService.Interfaces
|
||||
/// <returns></returns>
|
||||
public Task<ChatResponse> GetResponseAsync(
|
||||
string prompt,
|
||||
XUserClaimsInfoDto userInfo,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
|
||||
@@ -35,6 +37,19 @@ namespace xAiService.Interfaces
|
||||
/// <returns></returns>
|
||||
public Task<string> GetTextResponseAsync(
|
||||
string prompt,
|
||||
XUserClaimsInfoDto userInfo,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Generate Text Response Stream ...
|
||||
/// </summary>
|
||||
/// <param name="prompt"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task GetTextReponseStreamAsync(
|
||||
string prompt,
|
||||
XUserClaimsInfoDto userInfo,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
#endregion
|
||||
|
||||
+44
-2
@@ -3,13 +3,14 @@ using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.AI;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using xAiService.Base;
|
||||
using xAiService.Configurations;
|
||||
using xAiService.Constants;
|
||||
using xAiService.Interfaces;
|
||||
using xCommons.Providers;
|
||||
using xHttpService.Constants;
|
||||
using xIdentityModels.Models;
|
||||
using xIdentityService.Interfaces;
|
||||
using xIdentityService.Providers;
|
||||
|
||||
namespace xAiService.Services
|
||||
{
|
||||
@@ -48,6 +49,7 @@ namespace xAiService.Services
|
||||
/// <returns></returns>
|
||||
public Task<ChatResponse> GetResponseAsync(
|
||||
string prompt,
|
||||
XUserClaimsInfoDto userInfo,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
@@ -62,6 +64,7 @@ namespace xAiService.Services
|
||||
/// <returns></returns>
|
||||
public async Task<string> GetTextResponseAsync(
|
||||
string prompt,
|
||||
XUserClaimsInfoDto userInfo,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
@@ -70,7 +73,46 @@ namespace xAiService.Services
|
||||
ValidationProvider.NotEmpty(prompt);
|
||||
|
||||
//
|
||||
var client = GetRestClient();
|
||||
var client = GetRestClient(userInfo);
|
||||
var request = GetRestRequet(
|
||||
endpoint: XAiApiEndpoint.Ask,
|
||||
addXPoweredValue: true,
|
||||
queryStrings: new Dictionary<string, string>
|
||||
{
|
||||
{ XAiApiQueryParams.Prompt, prompt }
|
||||
}
|
||||
);
|
||||
|
||||
//
|
||||
var response = await RunRestRequest<string>(
|
||||
client,
|
||||
request,
|
||||
XHttpMethod.GET,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return response;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generate Text Response Stream ...
|
||||
/// </summary>
|
||||
/// <param name="prompt"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task GetTextReponseStreamAsync(
|
||||
string prompt,
|
||||
XUserClaimsInfoDto userInfo,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate Args ...
|
||||
ValidationProvider.NotEmpty(prompt);
|
||||
|
||||
//
|
||||
var client = GetRestClient(userInfo);
|
||||
var request = GetRestRequet(
|
||||
endpoint: XAiApiEndpoint.Ask,
|
||||
addXPoweredValue: true,
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -15,9 +15,9 @@ using System.Reflection;
|
||||
[assembly: System.Reflection.AssemblyDescriptionAttribute(("\r\n it is a Part of xSaherElm project which Provides Services to use xSaherEl" +
|
||||
"m AI Features ...\r\n "))]
|
||||
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+9b271d15725deb547d40f0de46749880ce499e26")]
|
||||
[assembly: System.Reflection.AssemblyProductAttribute("XAiService")]
|
||||
[assembly: System.Reflection.AssemblyTitleAttribute("XAiService")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+dc96e210e7719c49b4fd7607d203affbc2216744")]
|
||||
[assembly: System.Reflection.AssemblyProductAttribute("xAiService")]
|
||||
[assembly: System.Reflection.AssemblyTitleAttribute("xAiService")]
|
||||
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
|
||||
|
||||
// Generated by the MSBuild WriteCodeFragment class.
|
||||
|
||||
@@ -1 +1 @@
|
||||
3751af62e393104aa7cc93fb29dadb0ce87a2742a42c91243d02c956bbb0a2bd
|
||||
c97880c66ea21e3114c5243ba3dd31f0b255900adcae32427b9298cee8f74054
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
is_global = true
|
||||
build_property.RootNamespace = XAiService
|
||||
build_property.ProjectDir = C:\Users\saherelm\Documents\Projects\xSaherElmAIWorkspace\Modules\XAiService\
|
||||
build_property.RootNamespace = xAiService
|
||||
build_property.ProjectDir = C:\Users\saherelm\Documents\Projects\xSaherElmAIWorkspace\Modules\xAiService\
|
||||
build_property.EnableComHosting =
|
||||
build_property.EnableGeneratedComInterfaceComImportInterop =
|
||||
build_property.CsWinRTUseWindowsUIXamlProjections = false
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+11
-11
@@ -10906,7 +10906,7 @@
|
||||
]
|
||||
},
|
||||
"xDashboard.xCommons/1.0.0": {
|
||||
"sha512": "GmvTlUVjad44Uyf3bc2yS4DZrHoO9hPzwgx9ZPe+GcDhEi1OX0H2Nrs2eEQeskL7MKEhNybd+13jEYDCqTuNlw==",
|
||||
"sha512": "hl7aVuOuSWX3v8ZkjxlKZlQnfWvZaR3MpKEFG7jL4NiJnuBqg3hSUoWlm3/ECfacxL7KSclKO1h1/BDi7/3qIA==",
|
||||
"type": "package",
|
||||
"path": "xdashboard.xcommons/1.0.0",
|
||||
"files": [
|
||||
@@ -10919,7 +10919,7 @@
|
||||
]
|
||||
},
|
||||
"xDashboard.xDataService/1.0.0": {
|
||||
"sha512": "cKdH1fqyEkH0sIzAY4Y5KbsyORDTI1rQ/SL71c4GXY3ruY7vA6CebqEPBDsQQxGflJwI3RiIsRl3Ms5eULejzA==",
|
||||
"sha512": "wPrHKUpUCbaAYcfZryWs8HphLrcrIdhaba2sphFBRIGYzVQjH2YDPM1aGn4rItSlpFFgw44hmI124CM5kjtecg==",
|
||||
"type": "package",
|
||||
"path": "xdashboard.xdataservice/1.0.0",
|
||||
"files": [
|
||||
@@ -10931,7 +10931,7 @@
|
||||
]
|
||||
},
|
||||
"xDashboard.xExceptions/1.0.0": {
|
||||
"sha512": "VQzY7lVQLimslT4DUPvtHskX4lb6N+lBbWlySzzohOWcU+w1zFpz6pEcsEFZwnAsADbCiGebubMR2nFexiWbYw==",
|
||||
"sha512": "pOVIBggehSUk/Z4PSugUSBXRjufK1+ho/g90zgVjF/Av6xmpRk1FYeRB76FJ/JHRYWJibJicleoF9qv+PgyRAw==",
|
||||
"type": "package",
|
||||
"path": "xdashboard.xexceptions/1.0.0",
|
||||
"files": [
|
||||
@@ -10943,7 +10943,7 @@
|
||||
]
|
||||
},
|
||||
"xDashboard.xHttpService/1.0.0": {
|
||||
"sha512": "f9akpO5K8VioWTHkKCC0+zuDz1BQarwRcBWtDx0ESEqsJHKr/Ad4iSNJYYQ/5I1gU6R8vULm523ddeebn36zwQ==",
|
||||
"sha512": "BacM5u8P9p53pFVCtQ7NNdVPOhak6Sdf3pIrC/UYMjPFnOcrJgTh79pQAVmV3B489pSJ3DAJoDNtpauqNUKrKw==",
|
||||
"type": "package",
|
||||
"path": "xdashboard.xhttpservice/1.0.0",
|
||||
"files": [
|
||||
@@ -10955,7 +10955,7 @@
|
||||
]
|
||||
},
|
||||
"xDashboard.xIdentityModels/1.0.0": {
|
||||
"sha512": "VbTXDZWcy/1iDCsW3QFM7lKO+Q+9TAPYcpXzNCrOQethdUYMQlPY9s/pXRg3+bQVIc8FH/TxJQJt6h1M2UFKFw==",
|
||||
"sha512": "UYGX9yei5aNbrX41rSySHVMAh3coQR7bTg81R/GjpJ8Yh+/QeZoM7s4vy8NDYmg4TJo251N2Fn9kaybasFCS9A==",
|
||||
"type": "package",
|
||||
"path": "xdashboard.xidentitymodels/1.0.0",
|
||||
"files": [
|
||||
@@ -10967,7 +10967,7 @@
|
||||
]
|
||||
},
|
||||
"xDashboard.xIdentityService/1.0.0": {
|
||||
"sha512": "/MEQ6UoBFwQnQADarkT2EAOmKyKziMYFw5pcboU0xJQARQhz2sLoDVXYrziCufNilImlG6zNGt2a2GuCc461Jg==",
|
||||
"sha512": "NYSnh9B73XAj/m+jKQR0szd5fT172bxAmyhcuUH/ZQOsAhE3BLymSdtvsWxeXFCmr3rhq6D/v4aanjhhZf/xCw==",
|
||||
"type": "package",
|
||||
"path": "xdashboard.xidentityservice/1.0.0",
|
||||
"files": [
|
||||
@@ -10979,7 +10979,7 @@
|
||||
]
|
||||
},
|
||||
"xDashboard.xModels/1.0.0": {
|
||||
"sha512": "w4lcmCD2B2VugvC2w1NT8XZKsF1fguSQGlQyvi+BlQh0UhHKhdR6oCqesLEiBB0guvgFQAmHHXVT2NS0K67yYA==",
|
||||
"sha512": "HUIX3KkUvl1Pr66eoXzbtpSKX/PEWgWi/Q9/2y03XP+fCkDbUHQUrGre82eouGrbw1Jmz/EjJpIJ5xsaZP50CQ==",
|
||||
"type": "package",
|
||||
"path": "xdashboard.xmodels/1.0.0",
|
||||
"files": [
|
||||
@@ -11005,14 +11005,14 @@
|
||||
"project": {
|
||||
"version": "1.0.0",
|
||||
"restore": {
|
||||
"projectUniqueName": "C:\\Users\\saherelm\\Documents\\Projects\\xSaherElmAIWorkspace\\Modules\\XAiService\\XAiService.csproj",
|
||||
"projectUniqueName": "C:\\Users\\saherelm\\Documents\\Projects\\xSaherElmAIWorkspace\\Modules\\xAiService\\xAiService.csproj",
|
||||
"projectName": "xSaherelm.xAiService",
|
||||
"projectPath": "C:\\Users\\saherelm\\Documents\\Projects\\xSaherElmAIWorkspace\\Modules\\XAiService\\XAiService.csproj",
|
||||
"projectPath": "C:\\Users\\saherelm\\Documents\\Projects\\xSaherElmAIWorkspace\\Modules\\xAiService\\xAiService.csproj",
|
||||
"packagesPath": "C:\\Users\\saherelm\\.nuget\\packages\\",
|
||||
"outputPath": "C:\\Users\\saherelm\\Documents\\Projects\\xSaherElmAIWorkspace\\Modules\\XAiService\\obj\\",
|
||||
"outputPath": "C:\\Users\\saherelm\\Documents\\Projects\\xSaherElmAIWorkspace\\Modules\\xAiService\\obj\\",
|
||||
"projectStyle": "PackageReference",
|
||||
"configFilePaths": [
|
||||
"C:\\Users\\saherelm\\Documents\\Projects\\xSaherElmAIWorkspace\\Modules\\XAiService\\NuGet.Config",
|
||||
"C:\\Users\\saherelm\\Documents\\Projects\\xSaherElmAIWorkspace\\Modules\\xAiService\\NuGet.Config",
|
||||
"C:\\Users\\saherelm\\Documents\\Projects\\xSaherElmAIWorkspace\\NuGet.Config",
|
||||
"C:\\Users\\saherelm\\AppData\\Roaming\\NuGet\\NuGet.Config"
|
||||
],
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
{
|
||||
"version": 2,
|
||||
"dgSpecHash": "4NMvJuwPaPw=",
|
||||
"dgSpecHash": "YHkhfi+a77s=",
|
||||
"success": true,
|
||||
"projectFilePath": "C:\\Users\\saherelm\\Documents\\Projects\\xSaherElmAIWorkspace\\Modules\\XAiService\\XAiService.csproj",
|
||||
"projectFilePath": "C:\\Users\\saherelm\\Documents\\Projects\\xSaherElmAIWorkspace\\Modules\\xAiService\\xAiService.csproj",
|
||||
"expectedPackageFiles": [
|
||||
"C:\\Users\\saherelm\\.nuget\\packages\\automapper\\10.1.1\\automapper.10.1.1.nupkg.sha512",
|
||||
"C:\\Users\\saherelm\\.nuget\\packages\\automapper.extensions.microsoft.dependencyinjection\\8.1.0\\automapper.extensions.microsoft.dependencyinjection.8.1.0.nupkg.sha512",
|
||||
@@ -239,18 +239,18 @@
|
||||
"code": "NU1900",
|
||||
"level": "Warning",
|
||||
"message": "Error occurred while getting package vulnerability data: A socket operation was attempted to an unreachable network. (api.nuget.org:443)",
|
||||
"projectPath": "C:\\Users\\saherelm\\Documents\\Projects\\xSaherElmAIWorkspace\\Modules\\XAiService\\XAiService.csproj",
|
||||
"projectPath": "C:\\Users\\saherelm\\Documents\\Projects\\xSaherElmAIWorkspace\\Modules\\xAiService\\xAiService.csproj",
|
||||
"warningLevel": 1,
|
||||
"filePath": "C:\\Users\\saherelm\\Documents\\Projects\\xSaherElmAIWorkspace\\Modules\\XAiService\\XAiService.csproj",
|
||||
"filePath": "C:\\Users\\saherelm\\Documents\\Projects\\xSaherElmAIWorkspace\\Modules\\xAiService\\xAiService.csproj",
|
||||
"targetGraphs": []
|
||||
},
|
||||
{
|
||||
"code": "NU1900",
|
||||
"level": "Warning",
|
||||
"message": "Error occurred while getting package vulnerability data: Unable to load the service index for source https://api.nuget.org/v3/index.json.",
|
||||
"projectPath": "C:\\Users\\saherelm\\Documents\\Projects\\xSaherElmAIWorkspace\\Modules\\XAiService\\XAiService.csproj",
|
||||
"projectPath": "C:\\Users\\saherelm\\Documents\\Projects\\xSaherElmAIWorkspace\\Modules\\xAiService\\xAiService.csproj",
|
||||
"warningLevel": 1,
|
||||
"filePath": "C:\\Users\\saherelm\\Documents\\Projects\\xSaherElmAIWorkspace\\Modules\\XAiService\\XAiService.csproj",
|
||||
"filePath": "C:\\Users\\saherelm\\Documents\\Projects\\xSaherElmAIWorkspace\\Modules\\xAiService\\xAiService.csproj",
|
||||
"targetGraphs": []
|
||||
}
|
||||
]
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
{
|
||||
"format": 1,
|
||||
"restore": {
|
||||
"C:\\Users\\saherelm\\Documents\\Projects\\xSaherElmAIWorkspace\\Modules\\XAiService\\XAiService.csproj": {}
|
||||
"C:\\Users\\saherelm\\Documents\\Projects\\xSaherElmAIWorkspace\\Modules\\xAiService\\xAiService.csproj": {}
|
||||
},
|
||||
"projects": {
|
||||
"C:\\Users\\saherelm\\Documents\\Projects\\xSaherElmAIWorkspace\\Modules\\XAiService\\XAiService.csproj": {
|
||||
"C:\\Users\\saherelm\\Documents\\Projects\\xSaherElmAIWorkspace\\Modules\\xAiService\\xAiService.csproj": {
|
||||
"version": "1.0.0",
|
||||
"restore": {
|
||||
"projectUniqueName": "C:\\Users\\saherelm\\Documents\\Projects\\xSaherElmAIWorkspace\\Modules\\XAiService\\XAiService.csproj",
|
||||
"projectUniqueName": "C:\\Users\\saherelm\\Documents\\Projects\\xSaherElmAIWorkspace\\Modules\\xAiService\\xAiService.csproj",
|
||||
"projectName": "xSaherelm.xAiService",
|
||||
"projectPath": "C:\\Users\\saherelm\\Documents\\Projects\\xSaherElmAIWorkspace\\Modules\\XAiService\\XAiService.csproj",
|
||||
"projectPath": "C:\\Users\\saherelm\\Documents\\Projects\\xSaherElmAIWorkspace\\Modules\\xAiService\\xAiService.csproj",
|
||||
"packagesPath": "C:\\Users\\saherelm\\.nuget\\packages\\",
|
||||
"outputPath": "C:\\Users\\saherelm\\Documents\\Projects\\xSaherElmAIWorkspace\\Modules\\XAiService\\obj\\",
|
||||
"outputPath": "C:\\Users\\saherelm\\Documents\\Projects\\xSaherElmAIWorkspace\\Modules\\xAiService\\obj\\",
|
||||
"projectStyle": "PackageReference",
|
||||
"configFilePaths": [
|
||||
"C:\\Users\\saherelm\\Documents\\Projects\\xSaherElmAIWorkspace\\Modules\\XAiService\\NuGet.Config",
|
||||
"C:\\Users\\saherelm\\Documents\\Projects\\xSaherElmAIWorkspace\\Modules\\xAiService\\NuGet.Config",
|
||||
"C:\\Users\\saherelm\\Documents\\Projects\\xSaherElmAIWorkspace\\NuGet.Config",
|
||||
"C:\\Users\\saherelm\\AppData\\Roaming\\NuGet\\NuGet.Config"
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user