| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- using Azure.AI.OpenAI;
- using Microsoft.Extensions.AI;
- using OllamaSharp;
- using OpenAI;
- using System.ClientModel;
- public static class AIClientHelper
- {
- /// <summary>
- /// 获取默认的聊天客户端,默认使用 AzureOpenAI 和 gpt-4o 模型
- /// AzureOpenAI: gpt-4o
- /// DeepSeek: deepseek-chat, deepseek-reasoner
- /// Qwen: qwen-max, qwen-plus, qwen-flash
- /// TokenAI: gpt-5-pro, claude-sonnet-4-5-20250929
- /// ollama
- /// </summary>
- public static IChatClient GetDefaultChatClient(string provider = "AzureOpenAI", string model = "gpt-4o", bool enableLogging = false)
- {
- IChatClient chatClient;
- switch (provider) {
- case "AzureOpenAI":
- chatClient= GetAzureOpenAIClient(enableLogging).GetChatClient(model).AsIChatClient();
- break;
- case "DeepSeek":
- chatClient = GetDeepSeekClient(enableLogging).GetChatClient(model).AsIChatClient();
- break;
- case "Qwen":
- chatClient = GetQwenClient(enableLogging).GetChatClient(model).AsIChatClient();
- break;
- case "TokenAI":
- chatClient = GetTokenAIClient(enableLogging).GetChatClient(model).AsIChatClient();
- break;
- case "Ollama":
- chatClient = GetOllamaApiClient(model);
- break;
- default:
- chatClient = GetTokenAIClient(enableLogging).GetChatClient(model).AsIChatClient();
- break;
- };
- //var chatClient = provider switch
- //{
- // "AzureOpenAI" => GetAzureOpenAIClient(enableLogging).GetChatClient(model),
- // "DeepSeek" => GetDeepSeekClient(enableLogging).GetChatClient(model),
- // "Qwen" => GetQwenClient(enableLogging).GetChatClient(model),
- // "TokenAI" => GetTokenAIClient(enableLogging).GetChatClient(model),
- // "Ollama" => GetOllamaApiClient(model),
- // _ => GetAzureOpenAIClient(enableLogging).GetChatClient(model),
- //};
- return chatClient;
- }
- public static AzureOpenAIClient GetAzureOpenAIClient(bool enableLogging = false)
- {
- var endpoint = Keys.AzureOpenAIEndpoint;
- var apiKey = Keys.AzureOpenAIApiKey;
- var clientOptions = new AzureOpenAIClientOptions();
- if (enableLogging)
- {
- var clientLoggingOptions = NLogHelper.CreateClientLoggingOptions();
- clientOptions.ClientLoggingOptions = clientLoggingOptions;
- }
- var client = new AzureOpenAIClient(new Uri(endpoint), new ApiKeyCredential(apiKey), clientOptions);
- return client;
- }
- public static IEmbeddingGenerator<string, Embedding<float>> GetAzureOpenAIEmbeddingGenerator()
- {
- var client = GetAzureOpenAIClient();
- return client.GetEmbeddingClient("text-embedding-3-small").AsIEmbeddingGenerator();
- }
- public static OpenAIClient GetDeepSeekClient(bool enableLogging = false)
- => GetAIClient(Keys.DeepSeekEndpoint, Keys.DeepSeekApiKey, enableLogging);
- //public static OpenAIClient GetOllamaClient(bool enableLogging = false)
- // => GetAIClient(Keys.DeepSeekEndpoint, Keys.DeepSeekApiKey, enableLogging);
- public static OpenAIClient GetQwenClient(bool enableLogging = false) =>
- GetAIClient(Keys.QwenEndpoint, Keys.QwenApiKey, enableLogging);
- public static OpenAIClient GetTokenAIClient(bool enableLogging = false) =>
- GetAIClient(Keys.TokenAIEndpoint, Keys.TokenAIApiKey, enableLogging);
- private static OpenAIClient GetAIClient(string endpoint, string apiKey, bool enableLogging = false)
- {
- OpenAIClientOptions clientOptions = new OpenAIClientOptions();
- clientOptions.Endpoint = new Uri(endpoint);
- if (enableLogging)
- {
- var clientLoggingOptions = NLogHelper.CreateClientLoggingOptions();
- clientOptions.ClientLoggingOptions = clientLoggingOptions;
- }
- // 创建自定义的OpenAI客户端
- OpenAIClient aiClient = new(new ApiKeyCredential(apiKey), clientOptions);
- return aiClient;
- }
- public static IChatClient GetOllamaApiClient( string model) {
- return new OllamaApiClient(Keys.OllamaUri, model);
- }
-
- }
|