44using HuaJiBot . NET . Logger ;
55using Microsoft . Extensions . Logging ;
66using Newtonsoft . Json ;
7+ using Newtonsoft . Json . Converters ;
78using OpenAI ;
89using OpenAI . Chat ;
910using OpenAI . Models ;
@@ -13,11 +14,25 @@ namespace HuaJiBot.NET.Plugin.AIChat;
1314
1415public class PluginConfig : ConfigBase
1516{
17+ public string SystemPrompt = "你是一个有用的AI助手" ;
18+ public ModelConfig Model = new ( ) ;
19+ }
20+
21+ [ JsonConverter ( typeof ( StringEnumConverter ) ) ]
22+ public enum ModelProvider
23+ {
24+ // ReSharper disable once InconsistentNaming
25+ OpenAI ,
26+ Google ,
27+ }
28+
29+ public class ModelConfig
30+ {
31+ public ModelProvider Provider = ModelProvider . OpenAI ;
1632 public string Endpoint = "" ;
33+ public string Id = "" ;
1734 public string ApiKey = "" ;
18- public string Model = "huihui_ai/qwen2.5-1m-abliterated:14b" ;
19- public string SystemPrompt = "你是一个有用的AI助手" ;
20- public bool OpenAILogging = false ;
35+ public bool Logging = false ;
2136}
2237
2338public class PluginMain : PluginBase , IPluginWithConfig < PluginConfig >
@@ -32,34 +47,34 @@ private OpenAIClient Client
3247 {
3348 if (
3449 _client is null //首次获取
35- || _clientApiKey != Config . ApiKey
36- || _clientModel != Config . Model //模型设置有变动
50+ || _clientApiKey != Config . Model . ApiKey
51+ || _clientModel != Config . Model . Id //模型设置有变动
3752 )
3853 {
3954 _client = new OpenAIClient (
4055 new ApiKeyCredential (
41- string . IsNullOrEmpty ( Config . ApiKey ) ? "null" : Config . ApiKey
56+ string . IsNullOrEmpty ( Config . Model . ApiKey ) ? "null" : Config . Model . ApiKey
4257 ) ,
4358 new OpenAIClientOptions
4459 {
45- Endpoint = new Uri ( Config . Endpoint ) ,
60+ Endpoint = new Uri ( Config . Model . Endpoint ) ,
4661 ClientLoggingOptions = new ( )
4762 {
48- EnableLogging = Config . OpenAILogging ,
63+ EnableLogging = Config . Model . Logging ,
4964 LoggerFactory = LoggerFactory . Create ( logger =>
5065 {
5166 logger . AddProvider ( new PluginLoggerProvider ( this ) ) ;
5267 } ) ,
5368 } ,
5469 }
5570 ) ;
56- _clientApiKey = Config . ApiKey ;
57- _clientModel = Config . Model ;
71+ _clientApiKey = Config . Model . ApiKey ;
72+ _clientModel = Config . Model . Id ;
5873 }
5974 return _client ;
6075 }
6176 }
62- private ChatClient ChatClient => Client . GetChatClient ( Config . Model ) ;
77+ private ChatClient ChatClient => Client . GetChatClient ( Config . Model . Id ) ;
6378 private OpenAIModelClient ModelClient => Client . GetOpenAIModelClient ( ) ;
6479
6580 private MessageHistory _history = null ! ;
@@ -72,8 +87,7 @@ protected override void Initialize()
7287 Task . Run ( async ( ) =>
7388 {
7489 var models = await ModelClient . GetModelsAsync ( ) ;
75- var s = new StringBuilder ( "模型列表:" ) ;
76- foreach ( var model in models . Value ) { }
90+ Info ( "模型列表:" + string . Join ( ", " , models . Value . Select ( x => x . Id ) ) ) ;
7791 } ) ;
7892 }
7993
@@ -168,7 +182,7 @@ await InvokeLlmMessage(
168182 reader = e . CommandReader ;
169183 if ( reader . Reply ( out var data ) )
170184 {
171- _ = reader . Input ( out var text ) ;
185+ _ = reader . Input ( out var text , true ) ;
172186 text ??= "" ;
173187 SortedList < DateTime , GroupMessage > messageList = [ ] ;
174188 void PrependMessage ( GroupMessage message )
0 commit comments