@@ -4,183 +4,46 @@ import (
4
4
"chat/adapter/azure"
5
5
"chat/adapter/baichuan"
6
6
"chat/adapter/bing"
7
- "chat/adapter/chatgpt"
8
7
"chat/adapter/claude"
8
+ "chat/adapter/common"
9
9
"chat/adapter/dashscope"
10
10
"chat/adapter/hunyuan"
11
11
"chat/adapter/midjourney"
12
+ "chat/adapter/openai"
12
13
"chat/adapter/palm2"
13
14
"chat/adapter/skylark"
14
15
"chat/adapter/slack"
15
16
"chat/adapter/sparkdesk"
16
17
"chat/adapter/zhinao"
17
18
"chat/adapter/zhipuai"
18
19
"chat/globals"
19
- "chat/utils"
20
20
"fmt"
21
21
)
22
22
23
- type RequestProps struct {
24
- MaxRetries * int
25
- Current int
26
- Group string
23
+ var channelFactories = map [string ]adaptercommon.FactoryCreator {
24
+ globals .OpenAIChannelType : openai .NewChatInstanceFromConfig ,
25
+ globals .AzureOpenAIChannelType : azure .NewChatInstanceFromConfig ,
26
+ globals .ClaudeChannelType : claude .NewChatInstanceFromConfig ,
27
+ globals .SlackChannelType : slack .NewChatInstanceFromConfig ,
28
+ globals .BingChannelType : bing .NewChatInstanceFromConfig ,
29
+ globals .PalmChannelType : palm2 .NewChatInstanceFromConfig ,
30
+ globals .SparkdeskChannelType : sparkdesk .NewChatInstanceFromConfig ,
31
+ globals .ChatGLMChannelType : zhipuai .NewChatInstanceFromConfig ,
32
+ globals .QwenChannelType : dashscope .NewChatInstanceFromConfig ,
33
+ globals .HunyuanChannelType : hunyuan .NewChatInstanceFromConfig ,
34
+ globals .BaichuanChannelType : baichuan .NewChatInstanceFromConfig ,
35
+ globals .SkylarkChannelType : skylark .NewChatInstanceFromConfig ,
36
+ globals .ZhinaoChannelType : zhinao .NewChatInstanceFromConfig ,
37
+ globals .MidjourneyChannelType : midjourney .NewChatInstanceFromConfig ,
27
38
}
28
39
29
- type ChatProps struct {
30
- RequestProps
40
+ func createChatRequest ( conf globals. ChannelConfig , props * adaptercommon. ChatProps , hook globals. Hook ) error {
41
+ props . Model = conf . GetModelReflect ( props . OriginalModel )
31
42
32
- Model string
33
- Message []globals.Message
34
- MaxTokens * int
35
- PresencePenalty * float32
36
- FrequencyPenalty * float32
37
- RepetitionPenalty * float32
38
- Temperature * float32
39
- TopP * float32
40
- TopK * int
41
- Tools * globals.FunctionTools
42
- ToolChoice * interface {}
43
- Buffer utils.Buffer
44
- }
45
-
46
- func createChatRequest (conf globals.ChannelConfig , props * ChatProps , hook globals.Hook ) error {
47
- model := conf .GetModelReflect (props .Model )
48
-
49
- switch conf .GetType () {
50
- case globals .OpenAIChannelType :
51
- return chatgpt .NewChatInstanceFromConfig (conf ).CreateStreamChatRequest (& chatgpt.ChatProps {
52
- Model : model ,
53
- Message : props .Message ,
54
- Token : props .MaxTokens ,
55
- PresencePenalty : props .PresencePenalty ,
56
- FrequencyPenalty : props .FrequencyPenalty ,
57
- Temperature : props .Temperature ,
58
- TopP : props .TopP ,
59
- Tools : props .Tools ,
60
- ToolChoice : props .ToolChoice ,
61
- Buffer : props .Buffer ,
62
- }, hook )
63
-
64
- case globals .AzureOpenAIChannelType :
65
- return azure .NewChatInstanceFromConfig (conf ).CreateStreamChatRequest (& azure.ChatProps {
66
- Model : model ,
67
- Message : props .Message ,
68
- Token : props .MaxTokens ,
69
- PresencePenalty : props .PresencePenalty ,
70
- FrequencyPenalty : props .FrequencyPenalty ,
71
- Temperature : props .Temperature ,
72
- TopP : props .TopP ,
73
- Tools : props .Tools ,
74
- ToolChoice : props .ToolChoice ,
75
- Buffer : props .Buffer ,
76
- }, hook )
77
-
78
- case globals .ClaudeChannelType :
79
- return claude .NewChatInstanceFromConfig (conf ).CreateStreamChatRequest (& claude.ChatProps {
80
- Model : model ,
81
- Message : props .Message ,
82
- Token : props .MaxTokens ,
83
- TopP : props .TopP ,
84
- TopK : props .TopK ,
85
- Temperature : props .Temperature ,
86
- }, hook )
87
-
88
- case globals .SlackChannelType :
89
- return slack .NewChatInstanceFromConfig (conf ).CreateStreamChatRequest (& slack.ChatProps {
90
- Message : props .Message ,
91
- }, hook )
92
-
93
- case globals .BingChannelType :
94
- return bing .NewChatInstanceFromConfig (conf ).CreateStreamChatRequest (& bing.ChatProps {
95
- Model : model ,
96
- Message : props .Message ,
97
- }, hook )
98
-
99
- case globals .PalmChannelType :
100
- return palm2 .NewChatInstanceFromConfig (conf ).CreateStreamChatRequest (& palm2.ChatProps {
101
- Model : model ,
102
- Message : props .Message ,
103
- }, hook )
104
-
105
- case globals .SparkdeskChannelType :
106
- return sparkdesk .NewChatInstance (conf , model ).CreateStreamChatRequest (& sparkdesk.ChatProps {
107
- Model : model ,
108
- Message : props .Message ,
109
- Token : props .MaxTokens ,
110
- Temperature : props .Temperature ,
111
- TopK : props .TopK ,
112
- Tools : props .Tools ,
113
- Buffer : props .Buffer ,
114
- }, hook )
115
-
116
- case globals .ChatGLMChannelType :
117
- return zhipuai .NewChatInstanceFromConfig (conf ).CreateStreamChatRequest (& zhipuai.ChatProps {
118
- Model : model ,
119
- Message : props .Message ,
120
- Temperature : props .Temperature ,
121
- TopP : props .TopP ,
122
- }, hook )
123
-
124
- case globals .QwenChannelType :
125
- return dashscope .NewChatInstanceFromConfig (conf ).CreateStreamChatRequest (& dashscope.ChatProps {
126
- Model : model ,
127
- Message : props .Message ,
128
- Token : props .MaxTokens ,
129
- Temperature : props .Temperature ,
130
- TopP : props .TopP ,
131
- TopK : props .TopK ,
132
- RepetitionPenalty : props .RepetitionPenalty ,
133
- }, hook )
134
-
135
- case globals .HunyuanChannelType :
136
- return hunyuan .NewChatInstanceFromConfig (conf ).CreateStreamChatRequest (& hunyuan.ChatProps {
137
- Model : model ,
138
- Message : props .Message ,
139
- Temperature : props .Temperature ,
140
- TopP : props .TopP ,
141
- }, hook )
142
-
143
- case globals .BaichuanChannelType :
144
- return baichuan .NewChatInstanceFromConfig (conf ).CreateStreamChatRequest (& baichuan.ChatProps {
145
- Model : model ,
146
- Message : props .Message ,
147
- TopP : props .TopP ,
148
- TopK : props .TopK ,
149
- Temperature : props .Temperature ,
150
- }, hook )
151
-
152
- case globals .SkylarkChannelType :
153
- return skylark .NewChatInstanceFromConfig (conf ).CreateStreamChatRequest (& skylark.ChatProps {
154
- Model : model ,
155
- Message : props .Message ,
156
- Token : props .MaxTokens ,
157
- TopP : props .TopP ,
158
- TopK : props .TopK ,
159
- Temperature : props .Temperature ,
160
- FrequencyPenalty : props .FrequencyPenalty ,
161
- PresencePenalty : props .PresencePenalty ,
162
- RepeatPenalty : props .RepetitionPenalty ,
163
- Tools : props .Tools ,
164
- }, hook )
165
-
166
- case globals .ZhinaoChannelType :
167
- return zhinao .NewChatInstanceFromConfig (conf ).CreateStreamChatRequest (& zhinao.ChatProps {
168
- Model : model ,
169
- Message : props .Message ,
170
- Token : props .MaxTokens ,
171
- TopP : props .TopP ,
172
- TopK : props .TopK ,
173
- Temperature : props .Temperature ,
174
- RepetitionPenalty : props .RepetitionPenalty ,
175
- }, hook )
176
-
177
- case globals .MidjourneyChannelType :
178
- return midjourney .NewChatInstanceFromConfig (conf ).CreateStreamChatRequest (& midjourney.ChatProps {
179
- Model : model ,
180
- Messages : props .Message ,
181
- }, hook )
182
-
183
- default :
184
- return fmt .Errorf ("unknown channel type %s (model: %s)" , conf .GetType (), props .Model )
43
+ factoryType := conf .GetType ()
44
+ if factory , ok := channelFactories [factoryType ]; ok {
45
+ return factory (conf ).CreateStreamChatRequest (props , hook )
185
46
}
47
+
48
+ return fmt .Errorf ("unknown channel type %s (channel #%d)" , conf .GetType (), conf .GetId ())
186
49
}
0 commit comments