Skip to content

Commit

Permalink
Proxy kernel manager and make ports configurable.
Browse files Browse the repository at this point in the history
  • Loading branch information
ricklamers committed May 21, 2023
1 parent 97e9f58 commit b96732d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 7 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,10 @@ $ gptcode
### Using .env for OpenAI key
You can put a .env in the working directory to load the `OPENAI_API_KEY` environment variable.

### Configurables
Set the `API_PORT` and `WEB_PORT` variables to override the defaults.

Set `OPENAI_BASE_URL` to change the OpenAI API endpoint that's being used (note this environment variable includes the protocol `https://...`).

## Contributing
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.
6 changes: 4 additions & 2 deletions frontend/src/config.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
let resolvedWebAddress = import.meta.env.VITE_WEB_ADDRESS ? import.meta.env.VITE_WEB_ADDRESS : "";

const Config = {
WEB_ADDRESS: "http://localhost:8080",
API_ADDRESS: "http://localhost:5010"
WEB_ADDRESS: resolvedWebAddress,
API_ADDRESS: resolvedWebAddress + "/api"
}

export default Config;
2 changes: 1 addition & 1 deletion gpt_code_ui/kernel_program/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import gpt_code_ui.kernel_program.config as config
import gpt_code_ui.kernel_program.utils as utils

APP_PORT = 5010
APP_PORT = int(os.environ.get("API_PORT", 5010))

# Get global logger
logger = config.get_logger()
Expand Down
33 changes: 30 additions & 3 deletions gpt_code_ui/webapp/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
from collections import deque

from flask_cors import CORS
from flask import Flask, request, jsonify, send_from_directory
from flask import Flask, request, jsonify, send_from_directory, Response
from dotenv import load_dotenv

from gpt_code_ui.kernel_program.main import APP_PORT as KERNEL_APP_PORT

load_dotenv('.env')

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

APP_PORT = 8080

APP_PORT = int(os.environ.get("WEB_PORT", 8080))


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

message_buffer = LimitedLengthString()


def allowed_file(filename):
return True

Expand Down Expand Up @@ -70,7 +75,7 @@ async def get_code(user_prompt, user_openai_key=None, model="gpt-3.5-turbo"):
"Content-Type": "application/json",
"Authorization": f"Bearer {final_openai_key}",
}

response = requests.post(
f"{OPENAI_BASE_URL}/v1/chat/completions",
data=json.dumps(data),
Expand Down Expand Up @@ -110,9 +115,31 @@ def extract_code(text):

@app.route('/')
def index():

# Check if index.html exists in the static folder
if not os.path.exists(os.path.join(app.root_path, 'static/index.html')):
print("index.html not found in static folder. Exiting. Did you forget to run `make compile_frontend` before installing the local package?")

return send_from_directory('static', 'index.html')


@app.route('/api/<path:path>', methods=["GET", "POST"])
def proxy_kernel_manager(path):
if request.method == "POST":
resp = requests.post(
f'http://localhost:{KERNEL_APP_PORT}/{path}', json=request.get_json())
else:
resp = requests.get(f'http://localhost:{KERNEL_APP_PORT}/{path}')

excluded_headers = ['content-encoding',
'content-length', 'transfer-encoding', 'connection']
headers = [(name, value) for (name, value) in resp.raw.headers.items()
if name.lower() not in excluded_headers]

response = Response(resp.content, resp.status_code, headers)
return response


@app.route('/assets/<path:path>')
def serve_static(path):
return send_from_directory('static/assets/', path)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setup(
name='gpt_code_ui',
version='0.42.17',
version='0.42.18',
description="An Open Source version of ChatGPT Code Interpreter",
long_description=long_description,
long_description_content_type='text/markdown', # This field specifies the format of the `long_description`.
Expand Down

0 comments on commit b96732d

Please sign in to comment.