TestPY0 / app.py
Niansuh's picture
Rename app/proxy.py to app.py
d3e86e7 verified
raw
history blame
1.03 kB
from flask import Flask, request, jsonify
import os
import requests
app = Flask(__name__)
# Load the OpenAI API key from environment variables
API_KEY = os.getenv("OPENAI_API_KEY")
BASE_URL = "https://api.typegpt.net/v1"
# A simple proxy to OpenAI's endpoints
@app.route("/<path:endpoint>", methods=["POST"])
def proxy(endpoint):
# Check for a fake API key from the client
client_key = request.headers.get("Authorization")
if client_key != "Bearer fake-key":
return jsonify({"error": "Unauthorized"}), 401
# Forward the request to OpenAI with the actual API key
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
}
data = request.get_json()
# Make the request to OpenAI API
response = requests.post(f"{BASE_URL}/{endpoint}", headers=headers, json=data)
# Forward OpenAI's response back to the client
return jsonify(response.json()), response.status_code
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)