Skip to content

Commit 8c93c23

Browse files
authored
Merge pull request ricklamers#53 from jackmingo/feature/enrich-api-configs
Add ability to override the endpoint for the openai api in the event …
2 parents 6286f77 + 44b4faf commit 8c93c23

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ venv\Scripts\activate
5353
pip install -r requirements.txt
5454
```
5555

56+
### Configure the Application
57+
To configure the application, there are a few properties that can be set either via the environment or via config.json. The environment variable takes priority.
58+
59+
| Field | Env Variable | config.json | examples |
60+
|---------------------|-----------------|----------------|----------------------------------------------------|
61+
| The OpenAI Api Key | OPENAI_API_KEY | openai_key | sk-...
62+
| The OpenAI Base URL | OPENAI_API_BASE | openai_api_key | https://api.openai.com <br> http://my-reverse-proxy/
63+
64+
Use the Base URL if you need to run your queries through a reverse proxy (like [this one](https://github.com/stulzq/azure-openai-proxy) which will run your queries through Azure's OpenAI endpoints )
65+
66+
5667
### Running the Application
5768
To run the application, make sure the virtual environment is active and run the following command:
5869
```
@@ -64,3 +75,4 @@ The easiest way to run ChatGPT Clone is by using docker
6475
```
6576
docker-compose up
6677
```
78+

config.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@
44
"port" : 1338,
55
"debug": false
66
},
7-
"openai_key": "sk-..."
7+
"openai_key": "sk-...",
8+
9+
"openai_api_base": "https://api.openai.com"
810
}

server/backend.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@
66
from requests import get
77
from requests import post
88
from json import loads
9+
import os
910

1011
from server.config import special_instructions
1112

1213

1314
class Backend_Api:
1415
def __init__(self, app, config: dict) -> None:
1516
self.app = app
16-
self.openai_key = config['openai_key']
17+
self.openai_key = os.environ["OPENAI_API_KEY"] or config['openai_key']
18+
self.openai_api_base = os.environ["OPENAI_API_BASE"] or config['openai_api_base']
1719
self.routes = {
1820
'/backend-api/v2/conversation': {
1921
'function': self._conversation,
@@ -52,7 +54,8 @@ def _conversation(self):
5254
extra + special_instructions[jailbreak] + \
5355
_conversation + [prompt]
5456

55-
gpt_resp = post('https://api.openai.com/v1/chat/completions',
57+
url = f"{self.openai_api_base}/v1/chat/completions"
58+
gpt_resp = post(url,
5659
headers = {'Authorization': 'Bearer %s' % self.openai_key},
5760
json = {
5861
'model' : request.json['model'],

0 commit comments

Comments
 (0)