File size: 1,029 Bytes
d3e86e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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)