Skip to content

Commit

Permalink
Merge pull request xtekky#53 from jackmingo/feature/enrich-api-configs
Browse files Browse the repository at this point in the history
Add ability to override the endpoint for the openai api in the event …
  • Loading branch information
xtekky committed Apr 27, 2023
2 parents 6286f77 + 44b4faf commit 8c93c23
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ venv\Scripts\activate
pip install -r requirements.txt
```

### Configure the Application
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.

| Field | Env Variable | config.json | examples |
|---------------------|-----------------|----------------|----------------------------------------------------|
| The OpenAI Api Key | OPENAI_API_KEY | openai_key | sk-...
| The OpenAI Base URL | OPENAI_API_BASE | openai_api_key | https://api.openai.com <br> http:https://my-reverse-proxy/

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 )


### Running the Application
To run the application, make sure the virtual environment is active and run the following command:
```
Expand All @@ -64,3 +75,4 @@ The easiest way to run ChatGPT Clone is by using docker
```
docker-compose up
```

4 changes: 3 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@
"port" : 1338,
"debug": false
},
"openai_key": "sk-..."
"openai_key": "sk-...",

"openai_api_base": "https://api.openai.com"
}
7 changes: 5 additions & 2 deletions server/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
from requests import get
from requests import post
from json import loads
import os

from server.config import special_instructions


class Backend_Api:
def __init__(self, app, config: dict) -> None:
self.app = app
self.openai_key = config['openai_key']
self.openai_key = os.environ["OPENAI_API_KEY"] or config['openai_key']
self.openai_api_base = os.environ["OPENAI_API_BASE"] or config['openai_api_base']
self.routes = {
'/backend-api/v2/conversation': {
'function': self._conversation,
Expand Down Expand Up @@ -52,7 +54,8 @@ def _conversation(self):
extra + special_instructions[jailbreak] + \
_conversation + [prompt]

gpt_resp = post('https://api.openai.com/v1/chat/completions',
url = f"{self.openai_api_base}/v1/chat/completions"
gpt_resp = post(url,
headers = {'Authorization': 'Bearer %s' % self.openai_key},
json = {
'model' : request.json['model'],
Expand Down

0 comments on commit 8c93c23

Please sign in to comment.