Skip to content

Commit 422da58

Browse files
committed
restruct: restruct channel factories
1 parent 3335e81 commit 422da58

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+283
-515
lines changed

adapter/adapter.go

+24-161
Original file line numberDiff line numberDiff line change
@@ -4,183 +4,46 @@ import (
44
"chat/adapter/azure"
55
"chat/adapter/baichuan"
66
"chat/adapter/bing"
7-
"chat/adapter/chatgpt"
87
"chat/adapter/claude"
8+
"chat/adapter/common"
99
"chat/adapter/dashscope"
1010
"chat/adapter/hunyuan"
1111
"chat/adapter/midjourney"
12+
"chat/adapter/openai"
1213
"chat/adapter/palm2"
1314
"chat/adapter/skylark"
1415
"chat/adapter/slack"
1516
"chat/adapter/sparkdesk"
1617
"chat/adapter/zhinao"
1718
"chat/adapter/zhipuai"
1819
"chat/globals"
19-
"chat/utils"
2020
"fmt"
2121
)
2222

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,
2738
}
2839

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)
3142

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)
18546
}
47+
48+
return fmt.Errorf("unknown channel type %s (channel #%d)", conf.GetType(), conf.GetId())
18649
}

adapter/azure/chat.go

+13-25
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,15 @@
11
package azure
22

33
import (
4+
adaptercommon "chat/adapter/common"
45
"chat/globals"
56
"chat/utils"
67
"errors"
78
"fmt"
89
"strings"
910
)
1011

11-
type ChatProps struct {
12-
Model string
13-
Message []globals.Message
14-
Token *int
15-
PresencePenalty *float32
16-
FrequencyPenalty *float32
17-
Temperature *float32
18-
TopP *float32
19-
Tools *globals.FunctionTools
20-
ToolChoice *interface{}
21-
Buffer utils.Buffer
22-
}
23-
24-
func (c *ChatInstance) GetChatEndpoint(props *ChatProps) string {
12+
func (c *ChatInstance) GetChatEndpoint(props *adaptercommon.ChatProps) string {
2513
model := strings.ReplaceAll(props.Model, ".", "")
2614
if props.Model == globals.GPT3TurboInstruct {
2715
return fmt.Sprintf("%s/openai/deployments/%s/completions?api-version=%s", c.GetResource(), model, c.GetEndpoint())
@@ -37,27 +25,27 @@ func (c *ChatInstance) GetCompletionPrompt(messages []globals.Message) string {
3725
return result
3826
}
3927

40-
func (c *ChatInstance) GetLatestPrompt(props *ChatProps) string {
28+
func (c *ChatInstance) GetLatestPrompt(props *adaptercommon.ChatProps) string {
4129
if len(props.Message) == 0 {
4230
return ""
4331
}
4432

4533
return props.Message[len(props.Message)-1].Content
4634
}
4735

48-
func (c *ChatInstance) GetChatBody(props *ChatProps, stream bool) interface{} {
36+
func (c *ChatInstance) GetChatBody(props *adaptercommon.ChatProps, stream bool) interface{} {
4937
if props.Model == globals.GPT3TurboInstruct {
5038
// for completions
5139
return CompletionRequest{
5240
Prompt: c.GetCompletionPrompt(props.Message),
53-
MaxToken: props.Token,
41+
MaxToken: props.MaxTokens,
5442
Stream: stream,
5543
}
5644
}
5745

5846
return ChatRequest{
5947
Messages: formatMessages(props),
60-
MaxToken: props.Token,
48+
MaxToken: props.MaxTokens,
6149
Stream: stream,
6250
PresencePenalty: props.PresencePenalty,
6351
FrequencyPenalty: props.FrequencyPenalty,
@@ -68,8 +56,8 @@ func (c *ChatInstance) GetChatBody(props *ChatProps, stream bool) interface{} {
6856
}
6957
}
7058

71-
// CreateChatRequest is the native http request body for chatgpt
72-
func (c *ChatInstance) CreateChatRequest(props *ChatProps) (string, error) {
59+
// CreateChatRequest is the native http request body for openai
60+
func (c *ChatInstance) CreateChatRequest(props *adaptercommon.ChatProps) (string, error) {
7361
if globals.IsOpenAIDalleModel(props.Model) {
7462
return c.CreateImage(props)
7563
}
@@ -81,20 +69,20 @@ func (c *ChatInstance) CreateChatRequest(props *ChatProps) (string, error) {
8169
)
8270

8371
if err != nil || res == nil {
84-
return "", fmt.Errorf("chatgpt error: %s", err.Error())
72+
return "", fmt.Errorf("openai error: %s", err.Error())
8573
}
8674

8775
data := utils.MapToStruct[ChatResponse](res)
8876
if data == nil {
89-
return "", fmt.Errorf("chatgpt error: cannot parse response")
77+
return "", fmt.Errorf("openai error: cannot parse response")
9078
} else if data.Error.Message != "" {
91-
return "", fmt.Errorf("chatgpt error: %s", data.Error.Message)
79+
return "", fmt.Errorf("openai error: %s", data.Error.Message)
9280
}
9381
return data.Choices[0].Message.Content, nil
9482
}
9583

96-
// CreateStreamChatRequest is the stream response body for chatgpt
97-
func (c *ChatInstance) CreateStreamChatRequest(props *ChatProps, callback globals.Hook) error {
84+
// CreateStreamChatRequest is the stream response body for openai
85+
func (c *ChatInstance) CreateStreamChatRequest(props *adaptercommon.ChatProps, callback globals.Hook) error {
9886
if globals.IsOpenAIDalleModel(props.Model) {
9987
if url, err := c.CreateImage(props); err != nil {
10088
return err

adapter/azure/image.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package azure
22

33
import (
4+
adaptercommon "chat/adapter/common"
45
"chat/globals"
56
"chat/utils"
67
"fmt"
@@ -32,21 +33,21 @@ func (c *ChatInstance) CreateImageRequest(props ImageProps) (string, error) {
3233
N: 1,
3334
})
3435
if err != nil || res == nil {
35-
return "", fmt.Errorf("chatgpt error: %s", err.Error())
36+
return "", fmt.Errorf("openai error: %s", err.Error())
3637
}
3738

3839
data := utils.MapToStruct[ImageResponse](res)
3940
if data == nil {
40-
return "", fmt.Errorf("chatgpt error: cannot parse response")
41+
return "", fmt.Errorf("openai error: cannot parse response")
4142
} else if data.Error.Message != "" {
42-
return "", fmt.Errorf("chatgpt error: %s", data.Error.Message)
43+
return "", fmt.Errorf("openai error: %s", data.Error.Message)
4344
}
4445

4546
return data.Data[0].Url, nil
4647
}
4748

4849
// CreateImage will create a dalle image from prompt, return markdown of image
49-
func (c *ChatInstance) CreateImage(props *ChatProps) (string, error) {
50+
func (c *ChatInstance) CreateImage(props *adaptercommon.ChatProps) (string, error) {
5051
url, err := c.CreateImageRequest(ImageProps{
5152
Model: props.Model,
5253
Prompt: c.GetLatestPrompt(props),

adapter/azure/processor.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package azure
22

33
import (
4+
adaptercommon "chat/adapter/common"
45
"chat/globals"
56
"chat/utils"
67
"errors"
78
"fmt"
89
"regexp"
910
)
1011

11-
func formatMessages(props *ChatProps) interface{} {
12+
func formatMessages(props *adaptercommon.ChatProps) interface{} {
1213
if globals.IsVisionModel(props.Model) {
1314
return utils.Each[globals.Message, Message](props.Message, func(message globals.Message) Message {
1415
if message.Role == globals.User {
@@ -120,7 +121,7 @@ func (c *ChatInstance) ProcessLine(data string, isCompletionType bool) (*globals
120121
}, nil
121122
}
122123

123-
globals.Warn(fmt.Sprintf("chatgpt error: cannot parse completion response: %s", data))
124+
globals.Warn(fmt.Sprintf("openai error: cannot parse completion response: %s", data))
124125
return &globals.Chunk{Content: ""}, errors.New("parser error: cannot parse completion response")
125126
}
126127

@@ -129,9 +130,9 @@ func (c *ChatInstance) ProcessLine(data string, isCompletionType bool) (*globals
129130
}
130131

131132
if form := processChatErrorResponse(data); form != nil {
132-
return &globals.Chunk{Content: ""}, errors.New(fmt.Sprintf("chatgpt error: %s (type: %s)", form.Error.Message, form.Error.Type))
133+
return &globals.Chunk{Content: ""}, errors.New(fmt.Sprintf("openai error: %s (type: %s)", form.Error.Message, form.Error.Type))
133134
}
134135

135-
globals.Warn(fmt.Sprintf("chatgpt error: cannot parse chat completion response: %s", data))
136+
globals.Warn(fmt.Sprintf("openai error: cannot parse chat completion response: %s", data))
136137
return &globals.Chunk{Content: ""}, errors.New("parser error: cannot parse chat completion response")
137138
}

adapter/azure/struct.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package azure
22

33
import (
4+
factory "chat/adapter/common"
45
"chat/globals"
56
)
67

@@ -42,7 +43,7 @@ func NewChatInstance(endpoint, apiKey string, resource string) *ChatInstance {
4243
}
4344
}
4445

45-
func NewChatInstanceFromConfig(conf globals.ChannelConfig) *ChatInstance {
46+
func NewChatInstanceFromConfig(conf globals.ChannelConfig) factory.Factory {
4647
param := conf.SplitRandomSecret(2)
4748
return NewChatInstance(
4849
conf.GetEndpoint(),

0 commit comments

Comments
 (0)