From b596afbe5794d8111c252074a391823907d6820a Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Sat, 26 Sep 2026 16:46:30 +0330 Subject: [PATCH] last ... --- Constants/XAiApiConstants.cs | 2 + Controllers/DefaultThinkingAiController.cs | 18 ++++++++ Interfaces/IXAiServiceBase.cs | 16 +++++-- Interfaces/IXDefaultThinkingAiService.cs | 5 +++ Providers/XAIServiceBase.cs | 49 ++++++++++++++-------- Providers/XAiEmbeddingServiceBase.cs | 11 +++-- Providers/XDefaultAiService.cs | 8 ++-- Providers/XDefaultEmbeddingService.cs | 8 ++-- Providers/XDefaultThinkingAiService.cs | 35 ++++++++++++++++ Startup.cs | 11 ++--- 10 files changed, 126 insertions(+), 37 deletions(-) create mode 100644 Controllers/DefaultThinkingAiController.cs create mode 100644 Interfaces/IXDefaultThinkingAiService.cs create mode 100644 Providers/XDefaultThinkingAiService.cs diff --git a/Constants/XAiApiConstants.cs b/Constants/XAiApiConstants.cs index dd30996..d9e53f5 100644 --- a/Constants/XAiApiConstants.cs +++ b/Constants/XAiApiConstants.cs @@ -12,7 +12,9 @@ namespace xAiApi.Constants public const string XAiApiIntroductionPromptName = "Introduction"; // + public const string XAiDefaultModelName = "Gemma"; public const string XAiEmbeddingModelName = "Embed"; + public const string XAiDefaultThinkingModelName = "Qwen"; public const string XAiEmbeddingPromptName = "Embedding"; // diff --git a/Controllers/DefaultThinkingAiController.cs b/Controllers/DefaultThinkingAiController.cs new file mode 100644 index 0000000..5da6308 --- /dev/null +++ b/Controllers/DefaultThinkingAiController.cs @@ -0,0 +1,18 @@ +using Microsoft.Extensions.Logging; +using xAiApi.Interfaces; +using xCommons.Configurations; +using xCommons.Providers; + +namespace xAiApi.Controllers +{ + public class DefaultThinkingAiController : XAiServiceControllerBase + { + public DefaultThinkingAiController( + ILogger logger, + IXDefaultThinkingAiService aiService, + XAppConfiguration appConfiguration, + XValidationProvider validationProvider + ) : base(logger, aiService, appConfiguration, validationProvider) + { } + } +} \ No newline at end of file diff --git a/Interfaces/IXAiServiceBase.cs b/Interfaces/IXAiServiceBase.cs index 82f0545..578df25 100644 --- a/Interfaces/IXAiServiceBase.cs +++ b/Interfaces/IXAiServiceBase.cs @@ -1,9 +1,10 @@ using System; -using System.Collections.Generic; using System.Net.Http; using System.Threading; +using xAiModels.Models; using System.Threading.Tasks; using Microsoft.Extensions.AI; +using System.Collections.Generic; namespace xAiApi.Interfaces { @@ -12,6 +13,16 @@ namespace xAiApi.Interfaces /// public interface IXAiServiceBase : IDisposable { + /// + /// Chat Options ... + /// + ChatOptions Options { get; } + + /// + /// Descriptor of Models which used in Service ... + /// + XAiModelDescriptor Descriptor { get; } + /// /// Base Method for Communicate with LLM ... /// @@ -37,9 +48,8 @@ namespace xAiApi.Interfaces /// /// Get LLM Client instance for Communicating with LLM ... /// - /// /// - IChatClient GetClient(string llm = null); + IChatClient GetClient(); /// /// Create Custom HttpClient for Communicating with LLM API ... diff --git a/Interfaces/IXDefaultThinkingAiService.cs b/Interfaces/IXDefaultThinkingAiService.cs new file mode 100644 index 0000000..cbe6d59 --- /dev/null +++ b/Interfaces/IXDefaultThinkingAiService.cs @@ -0,0 +1,5 @@ +namespace xAiApi.Interfaces +{ + public interface IXDefaultThinkingAiService: IXAiServiceBase + { } +} \ No newline at end of file diff --git a/Providers/XAIServiceBase.cs b/Providers/XAIServiceBase.cs index afb5302..0389a9d 100644 --- a/Providers/XAIServiceBase.cs +++ b/Providers/XAIServiceBase.cs @@ -4,6 +4,7 @@ using OllamaSharp; using System.Net.Http; using System.Threading; using xAiApi.Constants; +using xAiModels.Models; using xAiApi.Interfaces; using xAiApi.Extensions; using System.ClientModel; @@ -24,6 +25,16 @@ namespace xAiApi.Providers { public abstract class XAIServiceBase : IXAiServiceBase { + /// + /// Chat Options ... + /// + public ChatOptions Options { get; } + + /// + /// Descriptor of Models which used in Service ... + /// + public XAiModelDescriptor Descriptor { get; } + private readonly IXAiDataProvider dataProvider; private readonly ILogger logger; private readonly XAiApiConfiguration configuration; @@ -31,12 +42,25 @@ namespace xAiApi.Providers protected XAIServiceBase( IXAiDataProvider dataProvider, ILogger logger, - XAiApiConfiguration configuration + XAiApiConfiguration configuration, + string model = null ) { this.logger = logger; this.dataProvider = dataProvider; this.configuration = configuration; + + // + // Prepare Embedding Model Descriptor ... + Descriptor = configuration.GetModel(model); + if (!Descriptor.IsValid()) + { + XException.InvalidConfiguration.Throw(); + } + + // + // Prepare Chat Options based On Tools ... + Options = new ChatOptions(); } // @@ -130,28 +154,19 @@ namespace xAiApi.Providers /// /// Get LLM Client instance for Communicating with LLM ... /// - /// /// - public virtual IChatClient GetClient(string llm = null) + public virtual IChatClient GetClient() { - // - // Retrieve Model Descriptor ... - var modelDescriptor = configuration.GetModel(llm); - if (modelDescriptor.IsNullOrDefault()) - { - XException.NotFound.Throw(); - } - // // Try to Initialize LLm ... - var model = modelDescriptor.LLM; - var apiKey = modelDescriptor.ApiKey; - var url = new Uri(modelDescriptor.Url); - var httpClient = GetHttpClient(modelDescriptor.Url); + var model = Descriptor.LLM; + var apiKey = Descriptor.ApiKey; + var url = new Uri(Descriptor.Url); + var httpClient = GetHttpClient(Descriptor.Url); // IChatClient result = null; - switch (modelDescriptor.Provider) + switch (Descriptor.Provider) { // case XAiModelProviderType.Ollama: @@ -163,7 +178,7 @@ namespace xAiApi.Providers .UseFunctionInvocation() .Build(); break; - + // case XAiModelProviderType.OpenAI: // diff --git a/Providers/XAiEmbeddingServiceBase.cs b/Providers/XAiEmbeddingServiceBase.cs index 62a63d9..0a1cda3 100644 --- a/Providers/XAiEmbeddingServiceBase.cs +++ b/Providers/XAiEmbeddingServiceBase.cs @@ -1,4 +1,5 @@ using System; +using OpenAI; using System.Linq; using OllamaSharp; using System.Threading; @@ -6,20 +7,18 @@ using xAiApi.Constants; using xAiModels.Models; using xAiApi.Extensions; using xAiApi.Interfaces; -using OllamaSharp.Models; using xAiService.Helpers; +using System.ClientModel; using xCommons.Extensions; +using xAiModels.Constants; using xAiModels.Extensions; using xAiModels.Interfaces; using xAiApi.Configurations; using xExceptions.Constants; using System.Threading.Tasks; +using Microsoft.Extensions.AI; using System.Collections.Generic; using Microsoft.Extensions.Logging; -using Microsoft.Extensions.AI; -using xAiModels.Constants; -using OpenAI; -using System.ClientModel; using System.ClientModel.Primitives; namespace xAiApi.Providers @@ -37,7 +36,7 @@ namespace xAiApi.Providers XAiApiConfiguration configuration, ILogger logger, string embeddingModelName = XAiApiConstants.XAiEmbeddingModelName - ) : base(dataProvider, logger, configuration) + ) : base(dataProvider, logger, configuration, null) { // // Prepare Embedding Model Descriptor ... diff --git a/Providers/XDefaultAiService.cs b/Providers/XDefaultAiService.cs index ad086cc..aa74eab 100644 --- a/Providers/XDefaultAiService.cs +++ b/Providers/XDefaultAiService.cs @@ -1,7 +1,8 @@ -using Microsoft.Extensions.Logging; -using xAiApi.Configurations; +using xAiApi.Constants; using xAiApi.Interfaces; using xAiModels.Interfaces; +using xAiApi.Configurations; +using Microsoft.Extensions.Logging; namespace xAiApi.Providers { @@ -14,7 +15,8 @@ namespace xAiApi.Providers ) : base( dataProvider, logger, - configuration + configuration, + XAiApiConstants.XAiDefaultModelName ) { } } diff --git a/Providers/XDefaultEmbeddingService.cs b/Providers/XDefaultEmbeddingService.cs index 66e609a..ced9f0d 100644 --- a/Providers/XDefaultEmbeddingService.cs +++ b/Providers/XDefaultEmbeddingService.cs @@ -1,7 +1,8 @@ -using Microsoft.Extensions.Logging; -using xAiApi.Configurations; +using xAiApi.Constants; using xAiApi.Interfaces; using xAiModels.Interfaces; +using xAiApi.Configurations; +using Microsoft.Extensions.Logging; namespace xAiApi.Providers { @@ -14,7 +15,8 @@ namespace xAiApi.Providers ) : base( dataProvider, configuration, - logger + logger, + XAiApiConstants.XAiEmbeddingModelName ) { } } diff --git a/Providers/XDefaultThinkingAiService.cs b/Providers/XDefaultThinkingAiService.cs new file mode 100644 index 0000000..870940a --- /dev/null +++ b/Providers/XDefaultThinkingAiService.cs @@ -0,0 +1,35 @@ +using xAiApi.Constants; +using xAiApi.Interfaces; +using xAiModels.Interfaces; +using xAiApi.Configurations; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.AI; +using xCommons.Extensions; + +namespace xAiApi.Providers +{ + public class XDefaultThinkingAiService : XAIServiceBase, IXDefaultThinkingAiService + { + public XDefaultThinkingAiService( + IXAiDataProvider dataProvider, + ILogger logger, + XAiApiConfiguration configuration, + ReasoningEffort effort = ReasoningEffort.Medium, + ReasoningOutput output = ReasoningOutput.Full + ) : base( + dataProvider, + logger, + configuration, + XAiApiConstants.XAiDefaultThinkingModelName + ) + { + // + // Preparing Reasoning ... + Options.Reasoning = new ReasoningOptions + { + Output = output, + Effort = effort, + }; + } + } +} \ No newline at end of file diff --git a/Startup.cs b/Startup.cs index e58b133..21ea129 100644 --- a/Startup.cs +++ b/Startup.cs @@ -1,6 +1,8 @@ using System; using System.IO; +using xAiApi.DI; using System.Linq; +using xAiModels.DI; using xTagService.DI; using System.Net.Http; using xAiApi.Database; @@ -9,13 +11,16 @@ using xPushService.DI; using xFileService.DI; using xDataService.DI; using Newtonsoft.Json; +using xAiApi.Providers; using System.Reflection; using xAiApi.Extensions; using xStringService.DI; +using xAiApi.Interfaces; using xStorageService.DI; using xIdentityService.DI; using xDataService.Models; using xCommons.Extensions; +using xPushService.Helpers; using xCommons.Configurations; using System.Collections.Generic; using Microsoft.AspNetCore.Builder; @@ -25,11 +30,6 @@ using Newtonsoft.Json.Serialization; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using IdentityModel.AspNetCore.OAuth2Introspection; -using xPushService.Helpers; -using xAiApi.DI; -using xAiApi.Interfaces; -using xAiApi.Providers; -using xAiModels.DI; namespace xAiApi { @@ -189,6 +189,7 @@ namespace xAiApi // Register Default Ai Service ... services.AddScoped(); services.AddScoped(); + services.AddScoped(); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env)