Skip to content

Commit

Permalink
Merge pull request #12 from StrayDragon/feat-env
Browse files Browse the repository at this point in the history
feat: support OPENAI_MODEL env config
  • Loading branch information
Leizhenpeng authored May 13, 2023
2 parents ba2ba43 + 333d2d1 commit 59f6bd7
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 6 deletions.
2 changes: 2 additions & 0 deletions code/config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ API_URL: https://api.openai.com
HTTP_PROXY: ""
# 访问OpenAi的 普通 Http请求的超时时间,单位秒,不配置的话默认为 550 秒
OPENAI_HTTP_CLIENT_TIMEOUT:
# openai 指定模型, 更多见 https://platform.openai.com/docs/models/model-endpoint-compatibility 中 /v1/chat/completions
OPENAI_MODEL: gpt-3.5-turbo

# AZURE OPENAI
AZURE_ON: true # set to true to use Azure rather than OpenAI
Expand Down
1 change: 0 additions & 1 deletion code/handlers/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ func (m MessageHandler) msgReceivedHandler(ctx context.Context, event *larkim.P2
&MessageAction{
chatgpt: chatgpt.NewGpt3(&m.config),
}, //消息处理

}

chain(data, actions...)
Expand Down
7 changes: 5 additions & 2 deletions code/handlers/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,8 +675,11 @@ func updateTextCard(ctx context.Context, msg string,
}
return nil
}
func updateFinalCard(ctx context.Context, msg string,
msgId *string) error {
func updateFinalCard(
ctx context.Context,
msg string,
msgId *string,
) error {
newCard, _ := newSendCardWithOutHeader(
withMainText(msg))
err := PatchCard(ctx, msgId, newCard)
Expand Down
2 changes: 2 additions & 0 deletions code/initialization/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Config struct {
AccessControlEnable bool
AccessControlMaxCountPerUserPerDay int
OpenAIHttpClientTimeOut int
OpenaiModel string
}

var (
Expand Down Expand Up @@ -94,6 +95,7 @@ func LoadConfig(cfg string) *Config {
AccessControlEnable: getViperBoolValue("ACCESS_CONTROL_ENABLE", false),
AccessControlMaxCountPerUserPerDay: getViperIntValue("ACCESS_CONTROL_MAX_COUNT_PER_USER_PER_DAY", 0),
OpenAIHttpClientTimeOut: getViperIntValue("OPENAI_HTTP_CLIENT_TIMEOUT", 550),
OpenaiModel: getViperStringValue("OPENAI_MODEL", "gpt-3.5-turbo"),
}

return config
Expand Down
2 changes: 1 addition & 1 deletion code/services/chatgpt/gpt3.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (c *ChatGPT) StreamChatWithHistory(ctx context.Context, msg []openai.ChatCo
client := openai.NewClientWithConfig(config)
//pp.Printf("client: %v", client)
req := openai.ChatCompletionRequest{
Model: openai.GPT3Dot5Turbo,
Model: c.config.OpenaiModel,
Messages: msg,
N: 1,
Temperature: 0.7,
Expand Down
2 changes: 2 additions & 0 deletions code/services/openai/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type ChatGPT struct {
Lb *loadbalancer.LoadBalancer
ApiKey []string
ApiUrl string
ApiModel string
HttpProxy string
Platform PlatForm
AzureConfig AzureConfig
Expand Down Expand Up @@ -224,6 +225,7 @@ func NewChatGPT(config initialization.Config) *ChatGPT {
ApiUrl: config.OpenaiApiUrl,
HttpProxy: config.HttpProxy,
Platform: platform,
ApiModel: config.OpenaiModel,
AzureConfig: AzureConfig{
BaseURL: AzureApiUrlV1,
ResourceName: config.AzureResourceName,
Expand Down
3 changes: 1 addition & 2 deletions code/services/openai/gpt3.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
const (
maxTokens = 2000
temperature = 0.7
engine = "gpt-3.5-turbo"
)

type Messages struct {
Expand Down Expand Up @@ -45,7 +44,7 @@ type ChatGPTRequestBody struct {
func (gpt *ChatGPT) Completions(msg []Messages) (resp Messages,
err error) {
requestBody := ChatGPTRequestBody{
Model: engine,
Model: gpt.ApiModel,
Messages: msg,
MaxTokens: maxTokens,
Temperature: temperature,
Expand Down

0 comments on commit 59f6bd7

Please sign in to comment.