using System;
using OllamaSharp;
using System.Threading.Tasks;
using Microsoft.Extensions.AI;
using xAiApi.AI.Interfaces;
using xCommons.Extensions;
using xExceptions.Constants;
using xAiApi.AI.Configuration;
using System.Collections.Generic;
using Microsoft.SemanticKernel.ChatCompletion;
using System.Threading;
namespace xAiApi.AI.Services
{
public class XAIService : IXAIService
{
///
/// Provides Options for Chat ...
///
///
public ChatOptions ChatOptions { get; }
///
/// Provides History for Chat ...
///
///
public ChatHistory ChatHistory { get; }
///
/// a Client for Text Chat ...
///
///
public IChatClient TextGenreatorClient { get; }
///
/// a Client for Code Chat ...
///
///
public IChatClient CodeGenreatorClient { get; }
///
/// a Client for Image Chats ...
///
///
public IChatClient ImageGenreatorClient { get; }
///
/// Configuration ...
///
///
public XAiApiConfiguration Configuration { get; }
///
/// Constructing Service ...
///
/// configuration ...
public XAIService(
XAiApiConfiguration configuration
)
{
//
Configuration = configuration;
//
// here we are Initialize Text Generator Model ...
TextGenreatorClient = new ChatClientBuilder(new OllamaApiClient(
new Uri(configuration.Url),
configuration.TextModel
))
// .UseFunctionInvocation()
.Build();
//
// Code Generator Client ...
if (!configuration.CodeModel.IsNullOrEmpty())
{
//
CodeGenreatorClient = new ChatClientBuilder(new OllamaApiClient(
new Uri(configuration.Url),
configuration.CodeModel
))
// .UseFunctionInvocation()
.Build();
}
//
// Image Generator Client ...
if (!configuration.ImageModel.IsNullOrEmpty())
{
//
ImageGenreatorClient = new ChatClientBuilder(new OllamaApiClient(
new Uri(configuration.Url),
configuration.ImageModel
))
// .UseFunctionInvocation()
.Build();
}
//
// Configuring Chant Options ...
ChatOptions = new ChatOptions()
{
//
// Preparing Tools ...
// Tools = [
// //
// // Tempreature Tool ...
// AIFunctionFactory.Create((string location, string unit) => {
// //
// var temp = Random.Shared.Next(5, 20);
// var cond = Random.Shared.Next(0, 1) == 0 ? "sunny" : "rainy";
// //
// var result = $"The weather is {temp} degrees C and {cond}.";
// //
// return result;
// },
// "get_current_weather",
// "Get the current weather in given location"
// )
// ],
};
}
//
#region Text Actions ...
///
/// Generated Response ...
///
///
///
///
public async Task GetResponseAsync(
string prompt,
CancellationToken cancellationToken = default
)
{
//
// Validate ...
var isValid = !prompt.IsNullOrEmpty();
if (!isValid)
{
XException.InvalidArgs.Throw();
}
//
// Preparing Prompt ...
prompt = await PreparePrompt(
prompt: prompt,
cancellationToken: cancellationToken
);
//
// Generate Response ...
var result = await TextGenreatorClient
.GetResponseAsync(
chatMessage: prompt,
options: ChatOptions,
cancellationToken: cancellationToken
);
//
return result;
}
///
/// Generated Text ...
///
///
///
///
public async Task GetTextResponseAsync(
string prompt,
CancellationToken cancellationToken = default
)
{
//
var response = await GetResponseAsync(
prompt: prompt,
cancellationToken: cancellationToken
);
//
return response.Text;
}
///
/// Generate Text Response Stream ...
///
///
///
///
public IAsyncEnumerable GetTextReponseStreamAsync(
string prompt,
CancellationToken cancellationToken = default
)
{
//
// Validate ...
var isValid = !prompt.IsNullOrEmpty();
if (!isValid)
{
XException.InvalidArgs.Throw();
}
//
// Preparing Prompt ...
prompt = PreparePrompt(
prompt: prompt,
cancellationToken: cancellationToken
)
.RunTask();
//
// Generate Response ...
var result = TextGenreatorClient
.GetStreamingResponseAsync(
chatMessage: prompt,
options: ChatOptions,
cancellationToken: cancellationToken
);
//
return result;
}
#endregion
//
#region Code Actions ...
#endregion
//
#region Image Actions ...
#endregion
//
#region Private ...
///
/// Preparing Prompt for AI ...
///
///
///
///
private async Task PreparePrompt(
string prompt,
CancellationToken cancellationToken = default
)
{
//
return await Task.FromResult(prompt);
}
#endregion
}
}