This commit is contained in:
2026-08-14 21:33:36 +03:30
parent 632dd04d98
commit aaa1ae2a2b
7 changed files with 466 additions and 1 deletions
+12
View File
@@ -0,0 +1,12 @@
using xAiApi.Models;
namespace xAiApi.Configurations
{
public class XAiApiConfiguration
{
/// <summary>
/// Allowed Models to Use ...
/// </summary>
public XAiModelDescriptor[] Models { get; set; } = [];
}
}
+10
View File
@@ -0,0 +1,10 @@
namespace xAiApi.Constants
{
public enum XAiProvider
{
None,
Ollama,
DeepSeek,
HuggingFace
}
}
+101
View File
@@ -0,0 +1,101 @@
using System.Linq;
using xAiApi.Configurations;
using xAiApi.Models;
using xCommons.Extensions;
namespace xAiApi.Extensions
{
public static class XModelsExtensions
{
/// <summary>
/// Validate a Model Descriptor ...
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static bool IsValid(this XAiModelDescriptor source)
{
//
var result =
!source.IsNullOrDefault() &&
!source.Name.IsNullOrEmpty() &&
!source.Url.IsNullOrEmpty() &&
source.Url.IsValidUrl() &&
!source.LLM.IsNullOrEmpty() &&
source.Provider != Constants.XAiProvider.None;
//
return result;
}
/// <summary>
/// Validate Configurations ...
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static bool IsValid(this XAiApiConfiguration source)
{
//
var result =
!source.IsNullOrDefault() &&
source.Models.HasChild() &&
source.Models.All(m => m.IsValid());
//
return result;
}
/// <summary>
/// Extract Default Model Descriptor ...
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static XAiModelDescriptor GetDefaultModel(this XAiApiConfiguration source)
{
//
XAiModelDescriptor result = null;
//
if (source.IsValid())
{
result = source.Models[0];
}
//
return result;
}
/// <summary>
/// Retrieve Specified Model from Config ...
/// </summary>
/// <param name="source"></param>
/// <param name="name"></param>
/// <returns></returns>
public static XAiModelDescriptor GetModel(
this XAiApiConfiguration source,
string name
)
{
//
XAiModelDescriptor result = null;
//
if (source.IsValid())
{
//
if (name.IsNullOrEmpty())
{
result = source.GetDefaultModel();
}
else
{
result = source.Models
.FirstOrDefault(m => m.Name
.Equals(name, System.StringComparison.InvariantCultureIgnoreCase));
}
}
//
return result;
}
}
}
+1 -1
View File
@@ -10,7 +10,7 @@ namespace xAiApi.Interfaces
/// <summary> /// <summary>
/// Describe a Base Service Actions of any Ai Based Services ... /// Describe a Base Service Actions of any Ai Based Services ...
/// </summary> /// </summary>
public interface IAIServiceBase : IDisposable public interface IXAIServiceBase : IDisposable
{ {
/// <summary> /// <summary>
/// Base Method for Communicate with LLM ... /// Base Method for Communicate with LLM ...
+35
View File
@@ -0,0 +1,35 @@
using xAiApi.Constants;
namespace xAiApi.Models
{
/// <summary>
/// Describe an Ai Model ...
/// </summary>
public class XAiModelDescriptor
{
/// <summary>
/// Provider Name ...
/// </summary>
public string Name { get; set; }
/// <summary>
/// LLM Provider Url ...
/// </summary>
public string Url { get; set; }
/// <summary>
/// Model Name ...
/// </summary>
public string LLM { get; set; }
/// <summary>
/// Api Key for Connection ...
/// </summary>
public string ApiKey { get; set; } = null;
/// <summary>
/// Model Provider ...
/// </summary>
public XAiProvider Provider { get; set; } = XAiProvider.Ollama;
}
}
+306
View File
@@ -0,0 +1,306 @@
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<XAIServiceBase> logger;
private readonly XAiApiConfiguration configuration;
protected XAIServiceBase(
IXAiDataProvider dataProvider,
ILogger<XAIServiceBase> logger,
XAiApiConfiguration configuration
)
{
this.logger = logger;
this.dataProvider = dataProvider;
this.configuration = configuration;
}
//
#region Actions ...
/// <summary>
/// Base Method for Communicate with LLM ...
/// </summary>
/// <param name="prompt"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual async Task<string> 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;
}
/// <summary>
/// Base Method for Communicate with LLM ...
/// </summary>
/// <param name="prompt"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public virtual async IAsyncEnumerable<string> 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;
}
/// <summary>
/// Get LLM Client instance for Communicating with LLM ...
/// </summary>
/// <param name="llm"></param>
/// <returns></returns>
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 ...");
}
/// <summary>
/// Create Custom HttpClient for Communicating with LLM API ...
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
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;
}
/// <summary>
/// Prepare a Message History List for LLM Communication based on Prompt ...
/// </summary>
/// <param name="prompt"></param>
/// <param name="forcePrompt"></param>
/// <returns></returns>
public virtual IList<ChatMessage> 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<ChatMessage> result =
[
introductionPrompt,
];
if (forcePrompt &&
prompt is not null &&
!string.IsNullOrWhiteSpace(prompt))
{
result.Add(promptMessage);
}
//
return result;
}
/// <summary>
/// Prepare a Message History List for LLM Communication based on Prompt ...
/// </summary>
/// <param name="message"></param>
/// <param name="forceMessage"></param>
/// <returns></returns>
public virtual IList<ChatMessage> GetHistory(
ChatMessage? message = null,
bool forceMessage = true
)
{
//
// Preparing Introduction Prompt ...
var introductionPrompt = new ChatMessage(
ChatRole.System,
PromptHelper.GetIntroductionPrompt().Prompt
);
//
List<ChatMessage> result =
[
introductionPrompt,
];
if (forceMessage &&
message is not null)
{
result.Add(message);
}
//
return result;
}
#endregion
/// <summary>
/// Base Dispose Action ...
/// </summary>
/// <exception cref="NotImplementedException"></exception>
public virtual void Dispose()
{ }
}
}
+1
View File
@@ -47,6 +47,7 @@
<!-- Dependencies --> <!-- Dependencies -->
<ItemGroup> <ItemGroup>
<PackageReference Include="OllamaSharp" Version="5.4.25" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.11" /> <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.11" />
<PackageReference Include="IdentityModel.AspNetCore.OAuth2Introspection" Version="5.0.0" /> <PackageReference Include="IdentityModel.AspNetCore.OAuth2Introspection" Version="5.0.0" />
</ItemGroup> </ItemGroup>