AIClientHelper.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using Azure.AI.OpenAI;
  2. using Microsoft.Extensions.AI;
  3. using OllamaSharp;
  4. using OpenAI;
  5. using System.ClientModel;
  6. public static class AIClientHelper
  7. {
  8. /// <summary>
  9. /// 获取默认的聊天客户端,默认使用 AzureOpenAI 和 gpt-4o 模型
  10. /// AzureOpenAI: gpt-4o
  11. /// DeepSeek: deepseek-chat, deepseek-reasoner
  12. /// Qwen: qwen-max, qwen-plus, qwen-flash
  13. /// TokenAI: gpt-5-pro, claude-sonnet-4-5-20250929
  14. /// ollama
  15. /// </summary>
  16. public static IChatClient GetDefaultChatClient(string provider = "AzureOpenAI", string model = "gpt-4o", bool enableLogging = false)
  17. {
  18. IChatClient chatClient;
  19. switch (provider) {
  20. case "AzureOpenAI":
  21. chatClient= GetAzureOpenAIClient(enableLogging).GetChatClient(model).AsIChatClient();
  22. break;
  23. case "DeepSeek":
  24. chatClient = GetDeepSeekClient(enableLogging).GetChatClient(model).AsIChatClient();
  25. break;
  26. case "Qwen":
  27. chatClient = GetQwenClient(enableLogging).GetChatClient(model).AsIChatClient();
  28. break;
  29. case "TokenAI":
  30. chatClient = GetTokenAIClient(enableLogging).GetChatClient(model).AsIChatClient();
  31. break;
  32. case "Ollama":
  33. chatClient = GetOllamaApiClient(model);
  34. break;
  35. default:
  36. chatClient = GetTokenAIClient(enableLogging).GetChatClient(model).AsIChatClient();
  37. break;
  38. };
  39. //var chatClient = provider switch
  40. //{
  41. // "AzureOpenAI" => GetAzureOpenAIClient(enableLogging).GetChatClient(model),
  42. // "DeepSeek" => GetDeepSeekClient(enableLogging).GetChatClient(model),
  43. // "Qwen" => GetQwenClient(enableLogging).GetChatClient(model),
  44. // "TokenAI" => GetTokenAIClient(enableLogging).GetChatClient(model),
  45. // "Ollama" => GetOllamaApiClient(model),
  46. // _ => GetAzureOpenAIClient(enableLogging).GetChatClient(model),
  47. //};
  48. return chatClient;
  49. }
  50. public static AzureOpenAIClient GetAzureOpenAIClient(bool enableLogging = false)
  51. {
  52. var endpoint = Keys.AzureOpenAIEndpoint;
  53. var apiKey = Keys.AzureOpenAIApiKey;
  54. var clientOptions = new AzureOpenAIClientOptions();
  55. if (enableLogging)
  56. {
  57. var clientLoggingOptions = NLogHelper.CreateClientLoggingOptions();
  58. clientOptions.ClientLoggingOptions = clientLoggingOptions;
  59. }
  60. var client = new AzureOpenAIClient(new Uri(endpoint), new ApiKeyCredential(apiKey), clientOptions);
  61. return client;
  62. }
  63. public static IEmbeddingGenerator<string, Embedding<float>> GetAzureOpenAIEmbeddingGenerator()
  64. {
  65. var client = GetAzureOpenAIClient();
  66. return client.GetEmbeddingClient("text-embedding-3-small").AsIEmbeddingGenerator();
  67. }
  68. public static OpenAIClient GetDeepSeekClient(bool enableLogging = false)
  69. => GetAIClient(Keys.DeepSeekEndpoint, Keys.DeepSeekApiKey, enableLogging);
  70. //public static OpenAIClient GetOllamaClient(bool enableLogging = false)
  71. // => GetAIClient(Keys.DeepSeekEndpoint, Keys.DeepSeekApiKey, enableLogging);
  72. public static OpenAIClient GetQwenClient(bool enableLogging = false) =>
  73. GetAIClient(Keys.QwenEndpoint, Keys.QwenApiKey, enableLogging);
  74. public static OpenAIClient GetTokenAIClient(bool enableLogging = false) =>
  75. GetAIClient(Keys.TokenAIEndpoint, Keys.TokenAIApiKey, enableLogging);
  76. private static OpenAIClient GetAIClient(string endpoint, string apiKey, bool enableLogging = false)
  77. {
  78. OpenAIClientOptions clientOptions = new OpenAIClientOptions();
  79. clientOptions.Endpoint = new Uri(endpoint);
  80. if (enableLogging)
  81. {
  82. var clientLoggingOptions = NLogHelper.CreateClientLoggingOptions();
  83. clientOptions.ClientLoggingOptions = clientLoggingOptions;
  84. }
  85. // 创建自定义的OpenAI客户端
  86. OpenAIClient aiClient = new(new ApiKeyCredential(apiKey), clientOptions);
  87. return aiClient;
  88. }
  89. public static IChatClient GetOllamaApiClient( string model) {
  90. return new OllamaApiClient(Keys.OllamaUri, model);
  91. }
  92. }