Skip to content

Commit

Permalink
Merge pull request #13 from bourne7/main
Browse files Browse the repository at this point in the history
feat: add http client timeout config
  • Loading branch information
Leizhenpeng authored May 10, 2023
2 parents 89d8708 + 04ce040 commit ba2ba43
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 2 deletions.
2 changes: 2 additions & 0 deletions code/config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ KEY_FILE: key.pem
API_URL: https://api.openai.com
# 代理设置, 例如 "http://127.0.0.1:7890", ""代表不使用代理
HTTP_PROXY: ""
# 访问OpenAi的 普通 Http请求的超时时间,单位秒,不配置的话默认为 550 秒
OPENAI_HTTP_CLIENT_TIMEOUT:

# AZURE OPENAI
AZURE_ON: true # set to true to use Azure rather than OpenAI
Expand Down
2 changes: 2 additions & 0 deletions code/initialization/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Config struct {
AzureOpenaiToken string
AccessControlEnable bool
AccessControlMaxCountPerUserPerDay int
OpenAIHttpClientTimeOut int
}

var (
Expand Down Expand Up @@ -92,6 +93,7 @@ func LoadConfig(cfg string) *Config {
AzureOpenaiToken: getViperStringValue("AZURE_OPENAI_TOKEN", ""),
AccessControlEnable: getViperBoolValue("ACCESS_CONTROL_ENABLE", false),
AccessControlMaxCountPerUserPerDay: getViperIntValue("ACCESS_CONTROL_MAX_COUNT_PER_USER_PER_DAY", 0),
OpenAIHttpClientTimeOut: getViperIntValue("OPENAI_HTTP_CLIENT_TIMEOUT", 550),
}

return config
Expand Down
6 changes: 6 additions & 0 deletions code/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"encoding/json"
"fmt"
larkcard "github.com/larksuite/oapi-sdk-go/v3/card"
larkim "github.com/larksuite/oapi-sdk-go/v3/service/im/v1"
Expand All @@ -26,6 +27,11 @@ func main() {
initialization.InitRoleList()
pflag.Parse()
globalConfig := initialization.GetConfig()

// 打印一下实际读取到的配置
globalConfigPrettyString, _ := json.MarshalIndent(globalConfig, "", " ")
log.Println(string(globalConfigPrettyString))

initialization.LoadLarkClient(*globalConfig)
gpt := openai.NewChatGPT(*globalConfig)
handlers.InitHandlers(gpt, *globalConfig)
Expand Down
5 changes: 3 additions & 2 deletions code/services/openai/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,9 @@ func (gpt *ChatGPT) sendRequestWithBodyType(link, method string,

func GetProxyClient(proxyString string) (*http.Client, error) {
var client *http.Client
timeOutDuration := time.Duration(initialization.GetConfig().OpenAIHttpClientTimeOut) * time.Second
if proxyString == "" {
client = &http.Client{Timeout: 110 * time.Second}
client = &http.Client{Timeout: timeOutDuration}
} else {
proxyUrl, err := url.Parse(proxyString)
if err != nil {
Expand All @@ -197,7 +198,7 @@ func GetProxyClient(proxyString string) (*http.Client, error) {
}
client = &http.Client{
Transport: transport,
Timeout: 110 * time.Second,
Timeout: timeOutDuration,
}
}
return client, nil
Expand Down
2 changes: 2 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,5 @@ services:
- ACCESS_CONTROL_ENABLE=false
# 每个用户每天最多问多少个问题。默认为0. 配置成为小于等于0表示不限制。
- ACCESS_CONTROL_MAX_COUNT_PER_USER_PER_DAY=0
# 访问OpenAi的 普通 Http请求的超时时间,单位秒,不配置的话默认为 550 秒
- OPENAI_HTTP_CLIENT_TIMEOUT

0 comments on commit ba2ba43

Please sign in to comment.