Skip to content

Commit

Permalink
update: channel conf
Browse files Browse the repository at this point in the history
  • Loading branch information
zmh-program committed Dec 2, 2023
1 parent 8e3a424 commit 63f0314
Show file tree
Hide file tree
Showing 32 changed files with 231 additions and 294 deletions.
36 changes: 16 additions & 20 deletions adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,12 @@ func createChatRequest(conf globals.ChannelConfig, props *ChatProps, hook global

switch conf.GetType() {
case globals.OpenAIChannelType:
instance := chatgpt.NewChatInstanceFromModel(&chatgpt.InstanceProps{
Model: model,
Plan: props.Plan,
})
return instance.CreateStreamChatRequest(&chatgpt.ChatProps{
return chatgpt.NewChatInstanceFromConfig(conf).CreateStreamChatRequest(&chatgpt.ChatProps{
Model: model,
Message: props.Message,
Token: utils.Multi(
props.Token == 0,
utils.Multi(globals.IsGPT4Model(model) || props.Plan || props.Infinity, nil, utils.ToPtr(2500)),
utils.Multi(globals.IsFreeModel(model) && !props.Plan, utils.ToPtr(2500), nil),
&props.Token,
),
PresencePenalty: props.PresencePenalty,
Expand All @@ -71,7 +67,7 @@ func createChatRequest(conf globals.ChannelConfig, props *ChatProps, hook global
}, hook)

case globals.ClaudeChannelType:
return claude.NewChatInstanceFromConfig().CreateStreamChatRequest(&claude.ChatProps{
return claude.NewChatInstanceFromConfig(conf).CreateStreamChatRequest(&claude.ChatProps{
Model: model,
Message: props.Message,
Token: utils.Multi(props.Token == 0, 50000, props.Token),
Expand All @@ -81,24 +77,24 @@ func createChatRequest(conf globals.ChannelConfig, props *ChatProps, hook global
}, hook)

case globals.SlackChannelType:
return slack.NewChatInstanceFromConfig().CreateStreamChatRequest(&slack.ChatProps{
return slack.NewChatInstanceFromConfig(conf).CreateStreamChatRequest(&slack.ChatProps{
Message: props.Message,
}, hook)

case globals.BingChannelType:
return bing.NewChatInstanceFromConfig().CreateStreamChatRequest(&bing.ChatProps{
return bing.NewChatInstanceFromConfig(conf).CreateStreamChatRequest(&bing.ChatProps{
Model: model,
Message: props.Message,
}, hook)

case globals.PalmChannelType:
return palm2.NewChatInstanceFromConfig().CreateStreamChatRequest(&palm2.ChatProps{
return palm2.NewChatInstanceFromConfig(conf).CreateStreamChatRequest(&palm2.ChatProps{
Model: model,
Message: props.Message,
}, hook)

case globals.SparkdeskChannelType:
return sparkdesk.NewChatInstance(model).CreateStreamChatRequest(&sparkdesk.ChatProps{
return sparkdesk.NewChatInstance(conf, model).CreateStreamChatRequest(&sparkdesk.ChatProps{
Model: model,
Message: props.Message,
Token: utils.Multi(props.Token == 0, nil, utils.ToPtr(props.Token)),
Expand All @@ -109,15 +105,15 @@ func createChatRequest(conf globals.ChannelConfig, props *ChatProps, hook global
}, hook)

case globals.ChatGLMChannelType:
return zhipuai.NewChatInstanceFromConfig().CreateStreamChatRequest(&zhipuai.ChatProps{
return zhipuai.NewChatInstanceFromConfig(conf).CreateStreamChatRequest(&zhipuai.ChatProps{
Model: model,
Message: props.Message,
Temperature: props.Temperature,
TopP: props.TopP,
}, hook)

case globals.QwenChannelType:
return dashscope.NewChatInstanceFromConfig().CreateStreamChatRequest(&dashscope.ChatProps{
return dashscope.NewChatInstanceFromConfig(conf).CreateStreamChatRequest(&dashscope.ChatProps{
Model: model,
Message: props.Message,
Token: utils.Multi(props.Infinity || props.Plan, 2048, props.Token),
Expand All @@ -128,15 +124,15 @@ func createChatRequest(conf globals.ChannelConfig, props *ChatProps, hook global
}, hook)

case globals.HunyuanChannelType:
return hunyuan.NewChatInstanceFromConfig().CreateStreamChatRequest(&hunyuan.ChatProps{
return hunyuan.NewChatInstanceFromConfig(conf).CreateStreamChatRequest(&hunyuan.ChatProps{
Model: model,
Message: props.Message,
Temperature: props.Temperature,
TopP: props.TopP,
}, hook)

case globals.BaichuanChannelType:
return baichuan.NewChatInstanceFromConfig().CreateStreamChatRequest(&baichuan.ChatProps{
return baichuan.NewChatInstanceFromConfig(conf).CreateStreamChatRequest(&baichuan.ChatProps{
Model: model,
Message: props.Message,
TopP: props.TopP,
Expand All @@ -145,7 +141,7 @@ func createChatRequest(conf globals.ChannelConfig, props *ChatProps, hook global
}, hook)

case globals.SkylarkChannelType:
return skylark.NewChatInstanceFromConfig().CreateStreamChatRequest(&skylark.ChatProps{
return skylark.NewChatInstanceFromConfig(conf).CreateStreamChatRequest(&skylark.ChatProps{
Model: model,
Message: props.Message,
Token: utils.Multi(props.Token == 0, 4096, props.Token),
Expand All @@ -159,7 +155,7 @@ func createChatRequest(conf globals.ChannelConfig, props *ChatProps, hook global
}, hook)

case globals.ZhinaoChannelType:
return zhinao.NewChatInstanceFromConfig().CreateStreamChatRequest(&zhinao.ChatProps{
return zhinao.NewChatInstanceFromConfig(conf).CreateStreamChatRequest(&zhinao.ChatProps{
Model: model,
Message: props.Message,
Token: utils.Multi(props.Infinity || props.Plan, nil, utils.ToPtr(2048)),
Expand All @@ -170,18 +166,18 @@ func createChatRequest(conf globals.ChannelConfig, props *ChatProps, hook global
}, hook)

case globals.MidjourneyChannelType:
return midjourney.NewChatInstanceFromConfig().CreateStreamChatRequest(&midjourney.ChatProps{
return midjourney.NewChatInstanceFromConfig(conf).CreateStreamChatRequest(&midjourney.ChatProps{
Model: model,
Messages: props.Message,
}, hook)

case globals.OneAPIChannelType:
return oneapi.NewChatInstanceFromConfig().CreateStreamChatRequest(&oneapi.ChatProps{
return oneapi.NewChatInstanceFromConfig(conf).CreateStreamChatRequest(&oneapi.ChatProps{
Model: model,
Message: props.Message,
Token: utils.Multi(
props.Token == 0,
utils.Multi(globals.IsGPT4Model(model) || props.Plan || props.Infinity, nil, utils.ToPtr(2500)),
utils.Multi(props.Plan || props.Infinity, nil, utils.ToPtr(2500)),
&props.Token,
),
PresencePenalty: props.PresencePenalty,
Expand Down
8 changes: 4 additions & 4 deletions adapter/baichuan/struct.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package baichuan

import (
"chat/globals"
"fmt"
"github.com/spf13/viper"
)

type ChatInstance struct {
Expand Down Expand Up @@ -32,9 +32,9 @@ func NewChatInstance(endpoint, apiKey string) *ChatInstance {
}
}

func NewChatInstanceFromConfig() *ChatInstance {
func NewChatInstanceFromConfig(conf globals.ChannelConfig) *ChatInstance {
return NewChatInstance(
viper.GetString("baichuan.endpoint"),
viper.GetString("baichuan.apikey"),
conf.GetEndpoint(),
conf.GetRandomSecret(),
)
}
9 changes: 6 additions & 3 deletions adapter/bing/struct.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package bing

import (
"chat/globals"
"fmt"
"github.com/spf13/viper"
)

type ChatInstance struct {
Expand All @@ -21,6 +21,9 @@ func NewChatInstance(endpoint, secret string) *ChatInstance {
}
}

func NewChatInstanceFromConfig() *ChatInstance {
return NewChatInstance(viper.GetString("bing.endpoint"), viper.GetString("bing.secret"))
func NewChatInstanceFromConfig(conf globals.ChannelConfig) *ChatInstance {
return NewChatInstance(
conf.GetEndpoint(),
conf.GetRandomSecret(),
)
}
28 changes: 3 additions & 25 deletions adapter/chatgpt/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,31 +38,9 @@ func NewChatInstance(endpoint, apiKey string) *ChatInstance {
}
}

func NewChatInstanceFromConfig(v string) *ChatInstance {
func NewChatInstanceFromConfig(conf globals.ChannelConfig) *ChatInstance {
return NewChatInstance(
viper.GetString(fmt.Sprintf("openai.%s.endpoint", v)),
viper.GetString(fmt.Sprintf("openai.%s.apikey", v)),
viper.GetString(conf.GetEndpoint()),
viper.GetString(conf.GetRandomSecret()),
)
}

func NewChatInstanceFromModel(props *InstanceProps) *ChatInstance {
switch props.Model {
case globals.GPT4, globals.GPT40314, globals.GPT40613,
globals.GPT432k, globals.GPT432k0613, globals.GPT432k0314:
return NewChatInstanceFromConfig("gpt4")

case globals.GPT3Turbo1106, globals.GPT41106Preview, globals.GPT41106VisionPreview,
globals.GPT4Vision, globals.GPT4Dalle, globals.Dalle3, globals.GPT4All:
return NewChatInstanceFromConfig("reverse")

case globals.GPT3Turbo, globals.GPT3TurboInstruct, globals.GPT3Turbo0613, globals.GPT3Turbo0301,
globals.GPT3Turbo16k, globals.GPT3Turbo16k0301, globals.GPT3Turbo16k0613:
return NewChatInstanceFromConfig("gpt3")

case globals.Dalle2:
return NewChatInstanceFromConfig("image")

default:
return NewChatInstanceFromConfig("gpt3")
}
}
8 changes: 4 additions & 4 deletions adapter/claude/struct.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package claude

import (
"github.com/spf13/viper"
"chat/globals"
)

type ChatInstance struct {
Expand All @@ -16,10 +16,10 @@ func NewChatInstance(endpoint, apiKey string) *ChatInstance {
}
}

func NewChatInstanceFromConfig() *ChatInstance {
func NewChatInstanceFromConfig(conf globals.ChannelConfig) *ChatInstance {
return NewChatInstance(
viper.GetString("claude.endpoint"),
viper.GetString("claude.apikey"),
conf.GetEndpoint(),
conf.GetRandomSecret(),
)
}

Expand Down
8 changes: 4 additions & 4 deletions adapter/dashscope/struct.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dashscope

import (
"github.com/spf13/viper"
"chat/globals"
)

type ChatInstance struct {
Expand All @@ -24,9 +24,9 @@ func NewChatInstance(endpoint string, apiKey string) *ChatInstance {
}
}

func NewChatInstanceFromConfig() *ChatInstance {
func NewChatInstanceFromConfig(conf globals.ChannelConfig) *ChatInstance {
return NewChatInstance(
viper.GetString("dashscope.endpoint"),
viper.GetString("dashscope.apikey"),
conf.GetEndpoint(),
conf.GetRandomSecret(),
)
}
2 changes: 1 addition & 1 deletion adapter/hunyuan/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (c *ChatInstance) FormatMessages(messages []globals.Message) []globals.Mess

func (c *ChatInstance) CreateStreamChatRequest(props *ChatProps, callback globals.Hook) error {
credential := NewCredential(c.GetSecretId(), c.GetSecretKey())
client := NewInstance(c.GetAppId(), credential)
client := NewInstance(c.GetAppId(), c.GetEndpoint(), credential)
channel, err := client.Chat(context.Background(), NewRequest(Stream, c.FormatMessages(props.Message), props.Temperature, props.TopP))
if err != nil {
return fmt.Errorf("tencent hunyuan error: %+v", err)
Expand Down
Loading

0 comments on commit 63f0314

Please sign in to comment.