Skip to content

Commit b96732d

Browse files
committed
Proxy kernel manager and make ports configurable.
1 parent 97e9f58 commit b96732d

File tree

5 files changed

+41
-7
lines changed

5 files changed

+41
-7
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,10 @@ $ gptcode
3030
### Using .env for OpenAI key
3131
You can put a .env in the working directory to load the `OPENAI_API_KEY` environment variable.
3232

33+
### Configurables
34+
Set the `API_PORT` and `WEB_PORT` variables to override the defaults.
35+
36+
Set `OPENAI_BASE_URL` to change the OpenAI API endpoint that's being used (note this environment variable includes the protocol `https://...`).
37+
3338
## Contributing
3439
Please do and have a look at the [contributions guide](.github/CONTRIBUTING.md)! This should be a community initiative. I'll try my best to be responsive.

frontend/src/config.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
let resolvedWebAddress = import.meta.env.VITE_WEB_ADDRESS ? import.meta.env.VITE_WEB_ADDRESS : "";
2+
13
const Config = {
2-
WEB_ADDRESS: "http://localhost:8080",
3-
API_ADDRESS: "http://localhost:5010"
4+
WEB_ADDRESS: resolvedWebAddress,
5+
API_ADDRESS: resolvedWebAddress + "/api"
46
}
57

68
export default Config;

gpt_code_ui/kernel_program/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import gpt_code_ui.kernel_program.config as config
2121
import gpt_code_ui.kernel_program.utils as utils
2222

23-
APP_PORT = 5010
23+
APP_PORT = int(os.environ.get("API_PORT", 5010))
2424

2525
# Get global logger
2626
logger = config.get_logger()

gpt_code_ui/webapp/main.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
from collections import deque
1111

1212
from flask_cors import CORS
13-
from flask import Flask, request, jsonify, send_from_directory
13+
from flask import Flask, request, jsonify, send_from_directory, Response
1414
from dotenv import load_dotenv
1515

16+
from gpt_code_ui.kernel_program.main import APP_PORT as KERNEL_APP_PORT
17+
1618
load_dotenv('.env')
1719

1820
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY", "")
@@ -21,7 +23,9 @@
2123
UPLOAD_FOLDER = 'workspace/'
2224
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
2325

24-
APP_PORT = 8080
26+
27+
APP_PORT = int(os.environ.get("WEB_PORT", 8080))
28+
2529

2630
class LimitedLengthString:
2731
def __init__(self, maxlen=2000):
@@ -43,6 +47,7 @@ def get_string(self):
4347

4448
message_buffer = LimitedLengthString()
4549

50+
4651
def allowed_file(filename):
4752
return True
4853

@@ -70,7 +75,7 @@ async def get_code(user_prompt, user_openai_key=None, model="gpt-3.5-turbo"):
7075
"Content-Type": "application/json",
7176
"Authorization": f"Bearer {final_openai_key}",
7277
}
73-
78+
7479
response = requests.post(
7580
f"{OPENAI_BASE_URL}/v1/chat/completions",
7681
data=json.dumps(data),
@@ -110,9 +115,31 @@ def extract_code(text):
110115

111116
@app.route('/')
112117
def index():
118+
119+
# Check if index.html exists in the static folder
120+
if not os.path.exists(os.path.join(app.root_path, 'static/index.html')):
121+
print("index.html not found in static folder. Exiting. Did you forget to run `make compile_frontend` before installing the local package?")
122+
113123
return send_from_directory('static', 'index.html')
114124

115125

126+
@app.route('/api/<path:path>', methods=["GET", "POST"])
127+
def proxy_kernel_manager(path):
128+
if request.method == "POST":
129+
resp = requests.post(
130+
f'http://localhost:{KERNEL_APP_PORT}/{path}', json=request.get_json())
131+
else:
132+
resp = requests.get(f'http://localhost:{KERNEL_APP_PORT}/{path}')
133+
134+
excluded_headers = ['content-encoding',
135+
'content-length', 'transfer-encoding', 'connection']
136+
headers = [(name, value) for (name, value) in resp.raw.headers.items()
137+
if name.lower() not in excluded_headers]
138+
139+
response = Response(resp.content, resp.status_code, headers)
140+
return response
141+
142+
116143
@app.route('/assets/<path:path>')
117144
def serve_static(path):
118145
return send_from_directory('static/assets/', path)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
setup(
99
name='gpt_code_ui',
10-
version='0.42.17',
10+
version='0.42.18',
1111
description="An Open Source version of ChatGPT Code Interpreter",
1212
long_description=long_description,
1313
long_description_content_type='text/markdown', # This field specifies the format of the `long_description`.

0 commit comments

Comments
 (0)