Skip to content

Commit e7dfe02

Browse files
authored
Merge pull request #76 from Leizhenpeng/feat_custom_domain_name
feat: support custom api domain for reverse proxy
2 parents e13b10a + ec0467d commit e7dfe02

File tree

7 files changed

+20
-7
lines changed

7 files changed

+20
-7
lines changed

code/config.example.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ HTTPS_PORT: 9001
1313
USE_HTTPS: false
1414
CERT_FILE: cert.pem
1515
KEY_FILE: key.pem
16+
# openai 地址, 一般不需要修改, 除非你有自己的反向代理
17+
API_URL: https://api.openai.com
1618

code/initialization/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type Config struct {
2121
UseHttps bool
2222
CertFile string
2323
KeyFile string
24+
OpenaiApiUrl string
2425
}
2526

2627
func LoadConfig(cfg string) *Config {
@@ -45,6 +46,7 @@ func LoadConfig(cfg string) *Config {
4546
UseHttps: getViperBoolValue("USE_HTTPS", false),
4647
CertFile: getViperStringValue("CERT_FILE", "cert.pem"),
4748
KeyFile: getViperStringValue("KEY_FILE", "key.pem"),
49+
OpenaiApiUrl: getViperStringValue("API_URL", "https://api.openai.com"),
4850
}
4951

5052
return config

code/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func main() {
2626
pflag.Parse()
2727
config := initialization.LoadConfig(*cfg)
2828
initialization.LoadLarkClient(*config)
29-
gpt := services.NewChatGPT(config.OpenaiApiKeys)
29+
gpt := services.NewChatGPT(config.OpenaiApiKeys, config.OpenaiApiUrl)
3030
handlers.InitHandlers(gpt, *config)
3131

3232
eventHandler := dispatcher.NewEventDispatcher(

code/services/gpt3.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
)
1414

1515
const (
16-
BASEURL = "https://api.openai.com/v1/"
1716
maxTokens = 2000
1817
temperature = 0.7
1918
engine = "gpt-3.5-turbo"
@@ -52,6 +51,7 @@ type ChatGPTRequestBody struct {
5251
type ChatGPT struct {
5352
Lb *loadbalancer.LoadBalancer
5453
ApiKey []string
54+
ApiUrl string
5555
}
5656

5757
type ImageGenerationRequestBody struct {
@@ -125,7 +125,7 @@ func (gpt ChatGPT) Completions(msg []Messages) (resp Messages, err error) {
125125
PresencePenalty: 0,
126126
}
127127
gptResponseBody := &ChatGPTResponseBody{}
128-
err = gpt.sendRequest(BASEURL+"chat/completions", "POST",
128+
err = gpt.sendRequest(gpt.ApiUrl+"/v1/chat/completions", "POST",
129129
requestBody, gptResponseBody)
130130

131131
if err == nil {
@@ -143,7 +143,8 @@ func (gpt ChatGPT) GenerateImage(prompt string, size string, n int) ([]string, e
143143
}
144144

145145
imageResponseBody := &ImageGenerationResponseBody{}
146-
err := gpt.sendRequest(BASEURL+"images/generations", "POST", requestBody, imageResponseBody)
146+
err := gpt.sendRequest(gpt.ApiUrl+"/v1/images/generations",
147+
"POST", requestBody, imageResponseBody)
147148

148149
if err != nil {
149150
return nil, err
@@ -164,10 +165,11 @@ func (gpt ChatGPT) GenerateOneImage(prompt string, size string) (string, error)
164165
return b64s[0], nil
165166
}
166167

167-
func NewChatGPT(apiKeys []string) *ChatGPT {
168+
func NewChatGPT(apiKeys []string, apiUrl string) *ChatGPT {
168169
lb := loadbalancer.NewLoadBalancer(apiKeys)
169170
return &ChatGPT{
170171
Lb: lb,
171172
ApiKey: apiKeys,
173+
ApiUrl: apiUrl,
172174
}
173175
}

code/services/gpt3_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func TestCompletions(t *testing.T) {
1414
{Role: "user", Content: "翻译这段话: The assistant messages help store prior responses. They can also be written by a developer to help give examples of desired behavior."},
1515
}
1616

17-
gpt := NewChatGPT(config.OpenaiApiKeys)
17+
gpt := NewChatGPT(config.OpenaiApiKeys, config.OpenaiApiUrl)
1818

1919
resp, err := gpt.Completions(msgs)
2020
if err != nil {
@@ -27,7 +27,7 @@ func TestCompletions(t *testing.T) {
2727
func TestGenerateOneImage(t *testing.T) {
2828
config := initialization.LoadConfig("../config.yaml")
2929

30-
gpt := NewChatGPT(config.OpenaiApiKeys)
30+
gpt := NewChatGPT(config.OpenaiApiKeys, config.OpenaiApiUrl)
3131
prompt := "a red apple"
3232
size := "256x256"
3333

entrypoint.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ HTTPS_PORT=${HTTPS_PORT:-""}
1616
USE_HTTPS=${USE_HTTPS:-""}
1717
CERT_FILE=${CERT_FILE:-""}
1818
KEY_FILE=${KEY_FILE:-""}
19+
API_URL=${API_URL:-""}
1920
CONFIG_PATH=${CONFIG_PATH:-"config.yaml"}
2021

2122

@@ -79,6 +80,10 @@ if [ "$KEY_FILE" != "" ] ; then
7980
sed -i "15c KEY_FILE: $KEY_FILE" $CONFIG_PATH
8081
fi
8182

83+
if [ "$API_URL" != "" ] ; then
84+
sed -i "17c API_URL: $API_URL" $CONFIG_PATH
85+
fi
86+
8287
echo -e "\033[32m[Success] Configuration file has been generated!\033[0m"
8388

8489
/dist/feishu_chatgpt

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ docker run -d --name feishu-chatgpt -p 9000:9000 \
207207
--env APP_VERIFICATION_TOKEN=xxx \
208208
--env BOT_NAME=chatGpt \
209209
--env OPENAI_KEY="sk-xxx1,sk-xxx2,sk-xxx3" \
210+
--env API_URL=https://api.openai.com \
210211
feishu-chatgpt:latest
211212
```
212213

@@ -224,6 +225,7 @@ docker run -d --restart=always --name feishu-chatgpt2 -p 9000:9000 -v /etc/local
224225
--env APP_VERIFICATION_TOKEN=xxx \
225226
--env BOT_NAME=chatGpt \
226227
--env OPENAI_KEY="sk-xxx1,sk-xxx2,sk-xxx3" \
228+
--env API_URL=https://api.openai.com \
227229
dockerproxy.com/leizhenpeng/feishu-chatgpt:latest
228230
```
229231

0 commit comments

Comments
 (0)