292 lines
8.6 KiB
C#
292 lines
8.6 KiB
C#
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
|
|
{
|
|
public static class XModelsExtensions
|
|
{
|
|
/// <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()) &&
|
|
source.Prompts.All(m => m.IsValid()) &&
|
|
source.Prompts.Any(p => p.Name
|
|
.Equals(
|
|
XAiApiConstants.XAiApiIntroductionPromptName,
|
|
System.StringComparison.InvariantCultureIgnoreCase));
|
|
|
|
//
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieve Introduction Prompt ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static XAiPromptDescriptor GetIntroductionPrompt(this XAiApiConfiguration source)
|
|
{
|
|
//
|
|
XAiPromptDescriptor result = null;
|
|
|
|
//
|
|
if (source.IsValid())
|
|
{
|
|
//
|
|
result = source.Prompts
|
|
.FirstOrDefault(p => p.Name
|
|
.Equals(
|
|
XAiApiConstants.XAiApiIntroductionPromptName,
|
|
System.StringComparison.InvariantCultureIgnoreCase));
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Select Specified Prompts and Parse it's Template
|
|
/// and Generate Message ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="name"></param>
|
|
/// <param name="params"></param>
|
|
/// <returns></returns>
|
|
public static string GetPrompt(
|
|
this XAiApiConfiguration source,
|
|
string name = null,
|
|
IDictionary<string, string> @params = null
|
|
)
|
|
{
|
|
//
|
|
var result = string.Empty;
|
|
|
|
//
|
|
// Normalize ...
|
|
if (name.IsNullOrEmpty())
|
|
{
|
|
name = XAiApiConstants.XAiApiIntroductionPromptName;
|
|
}
|
|
|
|
//
|
|
if (source.IsValid())
|
|
{
|
|
//
|
|
var prompt = source.Prompts
|
|
.FirstOrDefault(p => p.Name
|
|
.Equals(
|
|
name,
|
|
System.StringComparison.InvariantCultureIgnoreCase
|
|
)
|
|
);
|
|
if (!prompt.IsNullOrDefault())
|
|
{
|
|
//
|
|
result = prompt.Template;
|
|
|
|
//
|
|
// Handle Params ...
|
|
if (!@params.IsNullOrDefault() && @params.HasChild())
|
|
{
|
|
//
|
|
// Select Params ...
|
|
var existsParams = prompt.Params
|
|
.Where(p => @params.Any(pr => pr.Key == p.Name))
|
|
.Distinct()
|
|
.ToList();
|
|
|
|
//
|
|
// Replace Selected Params ...
|
|
existsParams
|
|
.ForEach(pk =>
|
|
{
|
|
result = result.Replace(pk.Param, @params[pk.Name]);
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
//
|
|
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;
|
|
}
|
|
|
|
}
|
|
} |