using System; using System.Linq; using System.Threading; using xAiApi.Interfaces; using xCommons.Extensions; using xAiModels.Interfaces; using xExceptions.Constants; using System.Threading.Tasks; using System.Collections.Generic; using Microsoft.Extensions.Logging; using System.Runtime.CompilerServices; using Microsoft.Extensions.AI; using xAiApi.Models; using xAiApi.Configurations; using xAiApi.Extensions; namespace xAiApi.Providers { public abstract class XAIServiceBase : IXAIServiceBase { private readonly IXAiDataProvider dataProvider; private readonly ILogger logger; private readonly XAiApiConfiguration configuration; protected XAIServiceBase( IXAiDataProvider dataProvider, ILogger logger, XAiApiConfiguration configuration ) { this.logger = logger; this.dataProvider = dataProvider; this.configuration = configuration; } // #region Actions ... /// /// Base Method for Communicate with LLM ... /// /// /// /// public virtual async Task AskAsync( string prompt, CancellationToken cancellationToken = default ) { // // Check Prompt Exists ... if (prompt.IsNullOrEmpty()) { XException.InvalidArgs.Throw(); } // using var client = GetClient(); var history = GetHistory(prompt); var response = await client.GetResponseAsync( messages: history, cancellationToken: cancellationToken ); // // Validate Response ... if (!response.IsValid()) { // // Dispose Client ... client.Dispose(); XException.ActionFailed.Throw(); } // // Retrieve Response Text ... var result = response.Text; // // Dispose Client ... client.Dispose(); // return result; } /// /// Base Method for Communicate with LLM ... /// /// /// /// public virtual async IAsyncEnumerable AskAsEnumerable( string prompt, [EnumeratorCancellation] CancellationToken cancellationToken = default ) { // // Check Prompt Exists ... if (prompt.IsNullOrEmpty()) { XException.InvalidArgs.Throw(); } // using var client = GetClient(); var history = GetHistory(prompt); var enumerable = client.GetStreamingResponseAsync( options: null, messages: history ); // var result = ""; await foreach (var res in enumerable) { // // Cancellation Token ... if (cancellationToken.IsCancellationRequested) { yield break; } // result += res.Text; } // // Dispose Client ... client.Dispose(); // yield return result; } /// /// Get LLM Client instance for Communicating with LLM ... /// /// /// public virtual IChatClient GetClient(string llm = null) { // // Retrieve Model Descriptor ... var modelDescriptor = configuration.GetModel(llm); if (modelDescriptor.IsNullOrDefault()) { XException.NotFound.Throw(); } // bool isOllamaBased = modelDescriptor.Provider == Constants.XAiProvider.Ollama; bool isDeepSeekBased = modelDescriptor.Provider == Constants.XAiProvider.DeepSeek; // // Try to Initialize LLm ... var model = modelDescriptor.LLM; var apiKey = modelDescriptor.ApiKey; var url = new Uri(modelDescriptor.Url); var httpClient = GetHttpClient(modelDescriptor.Url); // IChatClient result = null; if (isOllamaBased) { // // Instance Ollama Client using Custom HttpCLient ... var ollamaClient = new OllamaApiClient(httpClient, model); result = new ChatClientBuilder(ollamaClient) .Build(); } else if (isDeepSeekBased) { // if (apiKey is null || string.IsNullOrWhiteSpace(apiKey)) { throw new Exception("Inavlid Args ..."); } // var deepseekClient = new DeepSeekChatClient(apiKey: apiKey); result = deepseekClient; } else { // // TODO: Implement this ... } // return result ?? throw new NotSupportedException("Chat Client not found ..."); } /// /// Create Custom HttpClient for Communicating with LLM API ... /// /// /// public virtual HttpClient GetHttpClient(string? url = null) { // var result = new HttpClient { // // Disable timeout completely (not recommended for production) Timeout = Timeout.InfiniteTimeSpan, // BaseAddress = url is null ? null : new Uri(url!), // // OR set a very long timeout (e.g., 30 minutes for large file uploads) // Timeout = TimeSpan.FromMinutes(30) }; // return result; } /// /// Prepare a Message History List for LLM Communication based on Prompt ... /// /// /// /// public virtual IList GetHistory( string? prompt = null, bool forcePrompt = true ) { // // User Prompt ... var promptMessage = new ChatMessage( ChatRole.User, prompt ); // // Preparing Introduction Prompt ... var introductionPrompt = new ChatMessage( ChatRole.System, PromptHelper.GetIntroductionPrompt().Prompt ); // List result = [ introductionPrompt, ]; if (forcePrompt && prompt is not null && !string.IsNullOrWhiteSpace(prompt)) { result.Add(promptMessage); } // return result; } /// /// Prepare a Message History List for LLM Communication based on Prompt ... /// /// /// /// public virtual IList GetHistory( ChatMessage? message = null, bool forceMessage = true ) { // // Preparing Introduction Prompt ... var introductionPrompt = new ChatMessage( ChatRole.System, PromptHelper.GetIntroductionPrompt().Prompt ); // List result = [ introductionPrompt, ]; if (forceMessage && message is not null) { result.Add(message); } // return result; } #endregion /// /// Base Dispose Action ... /// /// public virtual void Dispose() { } } }