138 lines
4.0 KiB
C#
138 lines
4.0 KiB
C#
using System.Collections.Generic;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Extensions.AI;
|
|
using Microsoft.Extensions.Logging;
|
|
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
|
|
{
|
|
public class XAiService : XBaseIdentityHttpProvider, IXAiService
|
|
{
|
|
/// <summary>
|
|
/// XAiService Configurations ...
|
|
/// </summary>
|
|
/// <value></value>
|
|
public XAIServiceConfiguration Configuration { get; }
|
|
|
|
public XAiService(
|
|
ILogger<XAiService> logger,
|
|
IXTokenProvider tokenProvider,
|
|
IXIdentityProvider identityProvider,
|
|
XAIServiceConfiguration configuration,
|
|
XValidationProvider validationProvider
|
|
) : base(
|
|
configuration.Url,
|
|
logger,
|
|
configuration.XPoweredValue,
|
|
configuration.DefaultClientTimeout,
|
|
tokenProvider,
|
|
identityProvider,
|
|
validationProvider
|
|
)
|
|
{ }
|
|
|
|
//
|
|
#region Text Actions ...
|
|
/// <summary>
|
|
/// Generated Response ...
|
|
/// </summary>
|
|
/// <param name="prompt"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public Task<ChatResponse> GetResponseAsync(
|
|
string prompt,
|
|
XUserClaimsInfoDto userInfo,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
throw new System.NotImplementedException();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Generated Response ...
|
|
/// </summary>
|
|
/// <param name="prompt"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public async Task<string> GetTextResponseAsync(
|
|
string prompt,
|
|
XUserClaimsInfoDto userInfo,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
// Validate Args ...
|
|
ValidationProvider.NotEmpty(prompt);
|
|
|
|
//
|
|
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,
|
|
queryStrings: new Dictionary<string, string>
|
|
{
|
|
{ XAiApiQueryParams.Prompt, prompt }
|
|
}
|
|
);
|
|
|
|
//
|
|
var response = await RunRestRequest<string>(
|
|
client,
|
|
request,
|
|
XHttpMethod.GET,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
return response;
|
|
}
|
|
#endregion
|
|
}
|
|
} |