This commit is contained in:
2026-08-14 23:03:14 +03:30
parent aaa1ae2a2b
commit 6723454690
+59 -29
View File
@@ -1,5 +1,4 @@
using System; using System;
using System.Linq;
using System.Threading; using System.Threading;
using xAiApi.Interfaces; using xAiApi.Interfaces;
using xCommons.Extensions; using xCommons.Extensions;
@@ -10,9 +9,11 @@ using System.Collections.Generic;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using Microsoft.Extensions.AI; using Microsoft.Extensions.AI;
using xAiApi.Models; using xAiModels.Extensions;
using xAiApi.Configurations; using xAiApi.Configurations;
using xAiApi.Extensions; using xAiApi.Extensions;
using System.Net.Http;
using OllamaSharp;
namespace xAiApi.Providers namespace xAiApi.Providers
{ {
@@ -148,10 +149,6 @@ namespace xAiApi.Providers
XException.NotFound.Throw(); XException.NotFound.Throw();
} }
//
bool isOllamaBased = modelDescriptor.Provider == Constants.XAiProvider.Ollama;
bool isDeepSeekBased = modelDescriptor.Provider == Constants.XAiProvider.DeepSeek;
// //
// Try to Initialize LLm ... // Try to Initialize LLm ...
var model = modelDescriptor.LLM; var model = modelDescriptor.LLM;
@@ -161,36 +158,62 @@ namespace xAiApi.Providers
// //
IChatClient result = null; IChatClient result = null;
if (isOllamaBased) switch (modelDescriptor.Provider)
{ {
//
case Constants.XAiProvider.Ollama:
// //
// Instance Ollama Client using Custom HttpCLient ... // Instance Ollama Client using Custom HttpCLient ...
var ollamaClient = new OllamaApiClient(httpClient, model); var ollamaClient = new OllamaApiClient(httpClient, model);
result = result =
new ChatClientBuilder(ollamaClient) new ChatClientBuilder(ollamaClient)
.Build(); .Build();
} break;
else if (isDeepSeekBased)
{
// //
if (apiKey is null || case Constants.XAiProvider.DeepSeek:
string.IsNullOrWhiteSpace(apiKey)) break;
//
case Constants.XAiProvider.HuggingFace:
break;
//
default:
break;
}
// //
// if (isOllamaBased)
// {
// }
// 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 ...
// }
//
if (result.IsNullOrDefault())
{ {
throw new Exception("Inavlid Args ..."); XException.InvalidData.Throw();
} }
// //
var deepseekClient = new DeepSeekChatClient(apiKey: apiKey); return result;
result = deepseekClient;
}
else
{
//
// TODO: Implement this ...
}
//
return result ?? throw new NotSupportedException("Chat Client not found ...");
} }
/// <summary> /// <summary>
@@ -198,7 +221,7 @@ namespace xAiApi.Providers
/// </summary> /// </summary>
/// <param name="url"></param> /// <param name="url"></param>
/// <returns></returns> /// <returns></returns>
public virtual HttpClient GetHttpClient(string? url = null) public virtual HttpClient GetHttpClient(string url = null)
{ {
// //
var result = new HttpClient var result = new HttpClient
@@ -208,9 +231,9 @@ namespace xAiApi.Providers
Timeout = Timeout.InfiniteTimeSpan, Timeout = Timeout.InfiniteTimeSpan,
// //
BaseAddress = url is null BaseAddress = url.IsNullOrEmpty()
? null ? null
: new Uri(url!), : new Uri(url),
// //
// OR set a very long timeout (e.g., 30 minutes for large file uploads) // OR set a very long timeout (e.g., 30 minutes for large file uploads)
@@ -228,10 +251,17 @@ namespace xAiApi.Providers
/// <param name="forcePrompt"></param> /// <param name="forcePrompt"></param>
/// <returns></returns> /// <returns></returns>
public virtual IList<ChatMessage> GetHistory( public virtual IList<ChatMessage> GetHistory(
string? prompt = null, string prompt = null,
bool forcePrompt = true bool forcePrompt = true
) )
{ {
//
// Validate Args ...
if (prompt.IsNullOrEmpty())
{
XException.InvalidArgs.Throw();
}
// //
// User Prompt ... // User Prompt ...
var promptMessage = new ChatMessage( var promptMessage = new ChatMessage(
@@ -269,7 +299,7 @@ namespace xAiApi.Providers
/// <param name="forceMessage"></param> /// <param name="forceMessage"></param>
/// <returns></returns> /// <returns></returns>
public virtual IList<ChatMessage> GetHistory( public virtual IList<ChatMessage> GetHistory(
ChatMessage? message = null, ChatMessage message = null,
bool forceMessage = true bool forceMessage = true
) )
{ {