Skip to content

Commit

Permalink
Add cors
Browse files Browse the repository at this point in the history
  • Loading branch information
hhhizzz committed Aug 5, 2024
1 parent f00bdeb commit 10bd63c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// "forwardPorts": [],

// Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": "pip3 install --no-cache-dir flask requests openai opentelemetry-instrumentation-flask gunicorn"
"postCreateCommand": "pip3 install -r requirements.txt"

// Configure tool-specific properties.
// "customizations": {},
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ WORKDIR /app
COPY . /app

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir flask requests openai opentelemetry-instrumentation-flask gunicorn
RUN pip install -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 8000
Expand Down
9 changes: 7 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os

from flask import Flask, request, Response, jsonify
from flask_cors import CORS
import requests
import json

Expand All @@ -10,6 +11,8 @@

FlaskInstrumentor().instrument_app(app)

CORS(app)

default_resource = "default_resource"

# format of the resource_mapper is {"deployment name": "openai resource name"}, e.g. {"gpt-4": "azureopenai1"}
Expand Down Expand Up @@ -82,7 +85,8 @@ def handler(path):
return 'Not Found', 404

resource = resource_mapper[deployment]
request_url = f"https://{resource}.openai.azure.com/openai/deployments/{deployment}/{path}?api-version={api_version}"
request_url = f"https://{resource}.openai.azure.com/openai/deployments/{
deployment}/{path}?api-version={api_version}"

headers = {'api-key': resource_keys[resource]}
for key, value in request.headers.items():
Expand Down Expand Up @@ -139,7 +143,8 @@ def get_models():

if __name__ == '__main__':
if os.getenv('MODEL_MAPPER') is None or os.getenv('RESOURCE_MAPPER') is None:
raise ValueError("MODEL_MAPPER and RESOURCE_MAPPER environment variables must be set")
raise ValueError(
"MODEL_MAPPER and RESOURCE_MAPPER environment variables must be set")
if os.getenv('KEYS_MAPPER') is None:
raise ValueError("KEYS_MAPPER environment variable must be set")
app.run(host='0.0.0.0', port=8000, debug=True)
6 changes: 6 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
flask
flask_cors
requests
openai
opentelemetry-instrumentation-flask
gunicorn

0 comments on commit 10bd63c

Please sign in to comment.