Skip to content

Commit e4ace46

Browse files
committed
make available models/deployments configurable via environment variables
1 parent 51fba5a commit e4ace46

File tree

4 files changed

+17
-8
lines changed

4 files changed

+17
-8
lines changed

.env.azure-example

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ OPENAI_API_TYPE=azure
33
OPENAI_BASE_URL=https://your-resource-name.openai.azure.com
44
OPENAI_API_VERSION=2023-03-15-preview
55
# OPENAI_EXTRA_HEADERS={"key": "value"}
6-
AZURE_OPENAI_DEPLOYMENT=your-model-deployment-name
6+
AZURE_OPENAI_DEPLOYMENTS=[{"displayName": "GPT-3.5", "name": "your-gpt-3.5-deployment"}, {"displayName": "GPT-4", "name": "your-gpt-4-deployment"}]
7+
# OPENAI_API_LOGLEVEL=debug
78
API_PORT=5010
89
WEB_PORT=8080
910
SNAKEMQ_PORT=8765

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ OPENAI_API_TYPE=openai
33
OPENAI_BASE_URL=https://api.openai.com
44
OPENAI_API_VERSION=2023-03-15-preview
55
# OPENAI_EXTRA_HEADERS={"key": "value"}
6+
OPENAI_MODELS=[{"displayName": "GPT-3.5", "name": "gpt-3.5-turbo"}, {"displayName": "GPT-4", "name": "gpt-4"}]
7+
# OPENAI_API_LOGLEVEL=debug
68
API_PORT=5010
79
WEB_PORT=8080
810
SNAKEMQ_PORT=8765

frontend/src/App.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ function App() {
2222
try {
2323
const response = await fetch(`${Config.WEB_ADDRESS}/models`);
2424
const json = await response.json();
25-
console.log(json);
2625
setModels(json);
2726
} catch (e) {
2827
console.error(e);

gpt_code_ui/webapp/main.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
import requests
44
import asyncio
5+
import json
56
import re
67
import logging
78
import sys
@@ -23,9 +24,18 @@
2324
openai.api_version = os.environ.get("OPENAI_API_VERSION", "2023-03-15-preview")
2425
openai.api_key = os.environ.get("OPENAI_API_KEY", "")
2526
openai.log = os.getenv("OPENAI_API_LOGLEVEL", "")
26-
AZURE_OPENAI_DEPLOYMENT = os.environ.get("AZURE_OPENAI_DEPLOYMENT", "")
2727
OPENAI_EXTRA_HEADERS = json.loads(os.environ.get("OPENAI_EXTRA_HEADERS", "{}"))
2828

29+
if openai.api_type == "openai":
30+
AVAILABLE_MODELS = json.loads(os.environ.get("OPENAI_MODELS", '''[{"displayName": "GPT-3.5", "name": "gpt-3.5-turbo"}, {"displayName": "GPT-4", "name": "gpt-4"}]'''))
31+
elif openai.api_type == "azure":
32+
try:
33+
AVAILABLE_MODELS = json.loads(os.environ["AZURE_OPENAI_DEPLOYMENTS"])
34+
except KeyError as e:
35+
raise RuntimeError('AZURE_OPENAI_DEPLOYMENTS environment variable not set') from e
36+
else:
37+
raise ValueError(f'Invalid OPENAI_API_TYPE: {openai.api_type}')
38+
2939
UPLOAD_FOLDER = 'workspace/'
3040
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
3141

@@ -131,7 +141,7 @@ async def get_code(user_prompt, user_openai_key=None, model="gpt-3.5-turbo"):
131141
if openai.api_type == 'openai':
132142
arguments["model"] = model
133143
elif openai.api_type == 'azure':
134-
arguments["deployment_id"] = AZURE_OPENAI_DEPLOYMENT
144+
arguments["deployment_id"] = model
135145
else:
136146
return None, f"Error: Invalid OPENAI_PROVIDER: {openai.api_type}", 500
137147

@@ -198,10 +208,7 @@ def index():
198208

199209
@app.route("/models")
200210
def models():
201-
return jsonify([
202-
{"displayName": "GPT-3.5", "name": "gpt-3.5-turbo"},
203-
{"displayName": "GPT-4", "name": "gpt-4"},
204-
])
211+
return jsonify(AVAILABLE_MODELS)
205212

206213

207214
@app.route('/api/<path:path>', methods=["GET", "POST"])

0 commit comments

Comments
 (0)