Rename app/proxy.py to app.py
Browse files- app.py +33 -0
- app/proxy.py +0 -71
app.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
import os
|
| 3 |
+
import requests
|
| 4 |
+
|
| 5 |
+
app = Flask(__name__)
|
| 6 |
+
|
| 7 |
+
# Load the OpenAI API key from environment variables
|
| 8 |
+
API_KEY = os.getenv("OPENAI_API_KEY")
|
| 9 |
+
BASE_URL = "https://api.typegpt.net/v1"
|
| 10 |
+
|
| 11 |
+
# A simple proxy to OpenAI's endpoints
|
| 12 |
+
@app.route("/<path:endpoint>", methods=["POST"])
|
| 13 |
+
def proxy(endpoint):
|
| 14 |
+
# Check for a fake API key from the client
|
| 15 |
+
client_key = request.headers.get("Authorization")
|
| 16 |
+
if client_key != "Bearer fake-key":
|
| 17 |
+
return jsonify({"error": "Unauthorized"}), 401
|
| 18 |
+
|
| 19 |
+
# Forward the request to OpenAI with the actual API key
|
| 20 |
+
headers = {
|
| 21 |
+
"Authorization": f"Bearer {API_KEY}",
|
| 22 |
+
"Content-Type": "application/json"
|
| 23 |
+
}
|
| 24 |
+
data = request.get_json()
|
| 25 |
+
|
| 26 |
+
# Make the request to OpenAI API
|
| 27 |
+
response = requests.post(f"{BASE_URL}/{endpoint}", headers=headers, json=data)
|
| 28 |
+
|
| 29 |
+
# Forward OpenAI's response back to the client
|
| 30 |
+
return jsonify(response.json()), response.status_code
|
| 31 |
+
|
| 32 |
+
if __name__ == "__main__":
|
| 33 |
+
app.run(host="0.0.0.0", port=5000)
|
app/proxy.py
DELETED
|
@@ -1,71 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import requests
|
| 3 |
-
from dotenv import load_dotenv
|
| 4 |
-
from flask import Flask, request, jsonify, Response
|
| 5 |
-
|
| 6 |
-
# Load environment variables from .env file
|
| 7 |
-
load_dotenv()
|
| 8 |
-
|
| 9 |
-
app = Flask(__name__)
|
| 10 |
-
|
| 11 |
-
# OpenAI API base URL
|
| 12 |
-
OPENAI_API_BASE_URL = "https://api.typegpt.net/v1"
|
| 13 |
-
|
| 14 |
-
# Retrieve API keys from environment variables
|
| 15 |
-
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
|
| 16 |
-
APP_API_KEY = os.getenv('APP_API_KEY')
|
| 17 |
-
|
| 18 |
-
if not OPENAI_API_KEY:
|
| 19 |
-
raise ValueError("OpenAI API key not found. Please set OPENAI_API_KEY in the .env file.")
|
| 20 |
-
|
| 21 |
-
if not APP_API_KEY:
|
| 22 |
-
raise ValueError("App API key not found. Please set APP_API_KEY in the .env file.")
|
| 23 |
-
|
| 24 |
-
@app.route('/v1/chat/completions', methods=['POST'])
|
| 25 |
-
def chat_completions():
|
| 26 |
-
"""
|
| 27 |
-
Proxy endpoint for OpenAI's /v1/chat/completions.
|
| 28 |
-
Requires APP_API_KEY for authentication.
|
| 29 |
-
"""
|
| 30 |
-
# Authenticate the request
|
| 31 |
-
client_api_key = request.headers.get('Authorization')
|
| 32 |
-
if not client_api_key:
|
| 33 |
-
return jsonify({'error': 'Authorization header missing.'}), 401
|
| 34 |
-
|
| 35 |
-
if client_api_key != f"Bearer {APP_API_KEY}":
|
| 36 |
-
return jsonify({'error': 'Invalid API key.'}), 403
|
| 37 |
-
|
| 38 |
-
# Forward the request to OpenAI
|
| 39 |
-
openai_url = f"{OPENAI_API_BASE_URL}/chat/completions"
|
| 40 |
-
headers = {
|
| 41 |
-
'Content-Type': 'application/json',
|
| 42 |
-
'Authorization': f'Bearer {OPENAI_API_KEY}'
|
| 43 |
-
}
|
| 44 |
-
|
| 45 |
-
try:
|
| 46 |
-
# Forward the JSON payload
|
| 47 |
-
payload = request.get_json()
|
| 48 |
-
|
| 49 |
-
if not payload:
|
| 50 |
-
return jsonify({'error': 'Invalid JSON payload.'}), 400
|
| 51 |
-
|
| 52 |
-
# Make the POST request to OpenAI
|
| 53 |
-
response = requests.post(openai_url, headers=headers, json=payload)
|
| 54 |
-
|
| 55 |
-
# Forward OpenAI's response back to the client
|
| 56 |
-
return Response(
|
| 57 |
-
response.content,
|
| 58 |
-
status=response.status_code,
|
| 59 |
-
content_type=response.headers.get('Content-Type', 'application/json')
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
-
except requests.exceptions.RequestException as e:
|
| 63 |
-
return jsonify({'error': str(e)}), 500
|
| 64 |
-
|
| 65 |
-
@app.route('/')
|
| 66 |
-
def index():
|
| 67 |
-
return "Secure OpenAI API Proxy - /v1/chat/completions Only"
|
| 68 |
-
|
| 69 |
-
if __name__ == '__main__':
|
| 70 |
-
# It's recommended to use a production-ready server like Gunicorn for deployment
|
| 71 |
-
app.run(host='0.0.0.0', port=5000)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|