last ...
This commit is contained in:
@@ -1,10 +1,20 @@
|
||||
using System;
|
||||
using OpenAI;
|
||||
using System.Linq;
|
||||
using OllamaSharp;
|
||||
using System.Net.Http;
|
||||
using xAiApi.Constants;
|
||||
using xAiModels.Models;
|
||||
using System.Threading;
|
||||
using System.ClientModel;
|
||||
using xCommons.Extensions;
|
||||
using xAiModels.Constants;
|
||||
using xAiModels.Extensions;
|
||||
using xAiApi.Configurations;
|
||||
using xExceptions.Constants;
|
||||
using Microsoft.Extensions.AI;
|
||||
using System.Collections.Generic;
|
||||
using System.ClientModel.Primitives;
|
||||
|
||||
namespace xAiApi.Extensions
|
||||
{
|
||||
@@ -176,5 +186,107 @@ namespace xAiApi.Extensions
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Provide HttpClient for Common Usages ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <returns></returns>
|
||||
public static HttpClient GetHttpClient(this string source)
|
||||
{
|
||||
//
|
||||
var result = new HttpClient
|
||||
{
|
||||
//
|
||||
// Disable timeout completely (not recommended for production)
|
||||
Timeout = Timeout.InfiniteTimeSpan,
|
||||
|
||||
//
|
||||
BaseAddress = source.IsNullOrEmpty()
|
||||
? null
|
||||
: new Uri(source),
|
||||
|
||||
//
|
||||
// OR set a very long timeout (e.g., 30 minutes for large file uploads)
|
||||
// Timeout = TimeSpan.FromMinutes(30)
|
||||
};
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Instance and Retrieve Chat Client ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <returns></returns>
|
||||
public static IChatClient GetClient(this XAiModelDescriptor source)
|
||||
{
|
||||
//
|
||||
// Try to Initialize LLm ...
|
||||
var model = source.LLM;
|
||||
var apiKey = source.ApiKey;
|
||||
var url = new Uri(source.Url);
|
||||
var httpClient = source.Url.GetHttpClient();
|
||||
|
||||
//
|
||||
IChatClient result = null;
|
||||
switch (source.Provider)
|
||||
{
|
||||
//
|
||||
case XAiModelProviderType.Ollama:
|
||||
//
|
||||
// Instance Ollama Client using Custom HttpCLient ...
|
||||
var ollamaClient = new OllamaApiClient(httpClient, model);
|
||||
result =
|
||||
new ChatClientBuilder(ollamaClient)
|
||||
.UseFunctionInvocation()
|
||||
.Build();
|
||||
break;
|
||||
|
||||
//
|
||||
case XAiModelProviderType.OpenAI:
|
||||
//
|
||||
// Instance Ollama Client using Custom HttpCLient ...
|
||||
var openAiClient = new OpenAIClient(
|
||||
new ApiKeyCredential(apiKey.IsNullOrEmpty() ? XAiApiConstants.XOpenAINoKey : apiKey),
|
||||
new OpenAIClientOptions
|
||||
{
|
||||
Endpoint = url,
|
||||
Transport = new HttpClientPipelineTransport(httpClient)
|
||||
}
|
||||
);
|
||||
result =
|
||||
new ChatClientBuilder(
|
||||
openAiClient
|
||||
.GetChatClient(model)
|
||||
.AsIChatClient())
|
||||
.UseFunctionInvocation()
|
||||
.Build();
|
||||
break;
|
||||
|
||||
//
|
||||
case XAiModelProviderType.DeepSeek:
|
||||
break;
|
||||
|
||||
//
|
||||
case XAiModelProviderType.HuggingFace:
|
||||
break;
|
||||
|
||||
//
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
//
|
||||
if (result.IsNullOrDefault())
|
||||
{
|
||||
XException.InvalidData.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user