Skip to content

Commit e3cd102

Browse files
committed
feat: optimize read config file
1 parent 6a72aac commit e3cd102

File tree

4 files changed

+122
-13
lines changed

4 files changed

+122
-13
lines changed

README.md

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Verified support projects:
2020
| [chatgpt-web](https://github.com/Chanzhaoyu/chatgpt-web) ||
2121
| [chatbox](https://github.com/Bin-Huang/chatbox) ||
2222
| [langchain](https://python.langchain.com/en/latest/) ||
23+
| [ChatGPT-Next-Web](https://github.com/Yidadaa/ChatGPT-Next-Web) ||
2324

2425
## Get Start
2526

@@ -30,8 +31,8 @@ To successfully make a call against Azure OpenAI, you'll need the following:
3031
| Name | Desc | Default |
3132
| --------------------- | ------------------------------------------------------------ | ----------------------------- |
3233
| AZURE_OPENAI_ENDPOINT | This value can be found in the **Keys & Endpoint** section when examining your resource from the Azure portal. Alternatively, you can find the value in **Azure OpenAI Studio** > **Playground** > **Code View**. An example endpoint is: `https://docs-test-001.openai.azure.com/`. | N |
33-
| AZURE_OPENAI_API_VER | [See here](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/quickstart?tabs=command-line&pivots=rest-api) or Azure OpenAI Studio | 2023-03-15-preview |
34-
| AZURE_OPENAI_MODEL_MAPPER | This value will correspond to the custom name you chose for your deployment when you deployed a model. This value can be found under **Resource Management** > **Deployments** in the Azure portal or alternatively under **Management** > **Deployments** in Azure OpenAI Studio. | gpt-3.5-turbo=gpt-35-turbo |
34+
| AZURE_OPENAI_API_VER | [See here](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/quickstart?tabs=command-line&pivots=rest-api) or Azure OpenAI Studio | 2023-07-01-preview |
35+
| AZURE_OPENAI_MODEL_MAPPER | This value will correspond to the custom name you chose for your deployment when you deployed a model. This value can be found under **Resource Management** > **Deployments** in the Azure portal or alternatively under **Management** > **Deployments** in Azure OpenAI Studio. | N |
3536

3637
`AZURE_OPENAI_MODEL_MAPPER` is a mapping from Azure OpenAI deployed model names to official OpenAI model names. You can use commas to separate multiple mappings.
3738

@@ -46,7 +47,7 @@ Azure Deployment Names: **Resource Management** > **Deployments**
4647
**Example:**
4748

4849
````yaml
49-
AZURE_OPENAI_MODEL_MAPPER: gpt-3.5-turbo=azure-gpt-35
50+
AZURE_OPENAI_MODEL_MAPPER: gpt-3.5-turbo=gpt-35-turbo
5051
````
5152

5253
![Screenshot of the overview UI for an OpenAI Resource in the Azure portal with the endpoint & access keys location circled in red.](assets/images/endpoint.png)
@@ -135,7 +136,7 @@ services:
135136
environment:
136137
AZURE_OPENAI_ENDPOINT: <Azure OpenAI API Endpoint>
137138
AZURE_OPENAI_MODEL_MAPPER: <Azure OpenAI API Deployment Mapper>
138-
AZURE_OPENAI_API_VER: 2023-03-15-preview
139+
AZURE_OPENAI_API_VER: 2023-07-01-preview
139140
networks:
140141
- chatgpt-ns
141142

@@ -150,3 +151,77 @@ Run:
150151
docker compose up -d
151152
````
152153

154+
### Use ChatGPT-Next-Web
155+
156+
docker-compose.yml
157+
158+
````yaml
159+
version: '3'
160+
161+
services:
162+
chatgpt-web:
163+
image: yidadaa/chatgpt-next-web
164+
ports:
165+
- 3000:3000
166+
environment:
167+
OPENAI_API_KEY: <Azure OpenAI API Key>
168+
BASE_URL: http://azure-openai:8080
169+
CODE: ""
170+
HIDE_USER_API_KEY: 1
171+
HIDE_BALANCE_QUERY: 1
172+
depends_on:
173+
- azure-openai
174+
links:
175+
- azure-openai
176+
networks:
177+
- chatgpt-ns
178+
179+
azure-openai:
180+
image: stulzq/azure-openai-proxy
181+
ports:
182+
- 8080:8080
183+
environment:
184+
AZURE_OPENAI_ENDPOINT: <Azure OpenAI API Endpoint>
185+
AZURE_OPENAI_MODEL_MAPPER: <Azure OpenAI API Deployment Mapper>
186+
# AZURE_OPENAI_MODEL_MAPPER: gpt-4=gpt-4,gpt-3.5-turbo=gpt-35-turbo
187+
AZURE_OPENAI_API_VER: 2023-07-01-preview
188+
networks:
189+
- chatgpt-ns
190+
191+
networks:
192+
chatgpt-ns:
193+
driver: bridge
194+
````
195+
196+
### Use Config File
197+
198+
The configuration file supports different endpoints and API keys for each model.
199+
200+
201+
202+
config.yaml
203+
204+
````yaml
205+
api_base: "/v1"
206+
deployment_config:
207+
- deployment_name: "xxx"
208+
model_name: "text-davinci-003"
209+
endpoint: "https://xxx-east-us.openai.azure.com/"
210+
api_key: "11111111111"
211+
api_version: "2023-03-15-preview"
212+
- deployment_name: "yyy"
213+
model_name: "gpt-3.5-turbo"
214+
endpoint: "https://yyy.openai.azure.com/"
215+
api_key: "11111111111"
216+
api_version: "2023-03-15-preview"
217+
- deployment_name: "zzzz"
218+
model_name: "text-embedding-ada-002"
219+
endpoint: "https://zzzz.openai.azure.com/"
220+
api_key: "11111111111"
221+
api_version: "2023-03-15-preview"
222+
````
223+
224+
225+
226+
By default, it reads `<workdir>/config.yaml`, and you can pass the path through the parameter `-c config.yaml`.
227+

azure/init.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/stulzq/azure-openai-proxy/util"
88
"log"
99
"net/url"
10+
"path/filepath"
1011
"strings"
1112
)
1213

@@ -32,7 +33,7 @@ func Init() error {
3233
openaiModelMapper = viper.GetString(constant.ENV_AZURE_OPENAI_MODEL_MAPPER)
3334
if endpoint != "" && openaiModelMapper != "" {
3435
if apiVersion == "" {
35-
apiVersion = "2023-03-15-preview"
36+
apiVersion = "2023-07-01-preview"
3637
}
3738
InitFromEnvironmentVariables(apiVersion, endpoint, openaiModelMapper)
3839
} else {
@@ -92,10 +93,16 @@ func InitFromEnvironmentVariables(apiVersion, endpoint, openaiModelMapper string
9293

9394
func InitFromConfigFile() error {
9495
log.Println("Init from config file")
95-
workDir := util.GetWorkdir()
96-
viper.SetConfigName("config")
96+
97+
configFile := viper.GetString("configFile")
98+
if configFile == "" {
99+
configFile = filepath.Join(util.GetWorkdir(), "config.yaml")
100+
} else if !filepath.IsAbs(configFile) {
101+
configFile = filepath.Join(util.GetWorkdir(), configFile)
102+
}
103+
97104
viper.SetConfigType("yaml")
98-
viper.AddConfigPath(fmt.Sprintf("%s/config", workDir))
105+
viper.SetConfigFile(configFile)
99106
if err := viper.ReadInConfig(); err != nil {
100107
log.Printf("read config file error: %+v\n", err)
101108
return err
@@ -108,5 +115,7 @@ func InitFromConfigFile() error {
108115
for _, configItem := range C.DeploymentConfig {
109116
ModelDeploymentConfig[configItem.ModelName] = configItem
110117
}
118+
119+
log.Println("read config file success")
111120
return nil
112121
}

cmd/main.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package main
22

33
import (
44
"context"
5-
"flag"
65
"fmt"
6+
"github.com/spf13/pflag"
77
"github.com/spf13/viper"
88
"github.com/stulzq/azure-openai-proxy/azure"
99
"log"
@@ -26,7 +26,11 @@ func main() {
2626
viper.AutomaticEnv()
2727
parseFlag()
2828

29-
azure.Init()
29+
err := azure.Init()
30+
if err != nil {
31+
panic(err)
32+
}
33+
3034
gin.SetMode(gin.ReleaseMode)
3135
r := gin.Default()
3236
registerRoute(r)
@@ -59,9 +63,13 @@ func runServer(srv *http.Server) {
5963
}
6064

6165
func parseFlag() {
62-
ver := flag.Bool("v", false, "version")
63-
flag.Parse()
64-
if *ver {
66+
pflag.StringP("configFile", "c", "config.yaml", "config file")
67+
pflag.BoolP("version", "v", false, "version information")
68+
pflag.Parse()
69+
if err := viper.BindPFlags(pflag.CommandLine); err != nil {
70+
panic(err)
71+
}
72+
if viper.GetBool("v") {
6573
fmt.Println("version:", version)
6674
fmt.Println("buildDate:", buildDate)
6775
fmt.Println("gitCommit:", gitCommit)

config/config.example.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
api_base: "/v1"
2+
deployment_config:
3+
- deployment_name: "xxx"
4+
model_name: "text-davinci-003"
5+
endpoint: "https://xxx-east-us.openai.azure.com/"
6+
api_key: "11111111111"
7+
api_version: "2023-03-15-preview"
8+
- deployment_name: "yyy"
9+
model_name: "gpt-3.5-turbo"
10+
endpoint: "https://yyy.openai.azure.com/"
11+
api_key: "11111111111"
12+
api_version: "2023-03-15-preview"
13+
- deployment_name: "zzzz"
14+
model_name: "text-embedding-ada-002"
15+
endpoint: "https://zzzz.openai.azure.com/"
16+
api_key: "11111111111"
17+
api_version: "2023-03-15-preview"

0 commit comments

Comments
 (0)