45 lines
1.2 KiB
Python
45 lines
1.2 KiB
Python
# pip install Flask requests gunicorn
|
|
|
|
from flask import Flask, request, jsonify
|
|
import requests
|
|
import openai
|
|
|
|
openai.api_key = "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"
|
|
|
|
app = Flask(__name__)
|
|
|
|
# Define the routes for the gateway
|
|
|
|
@app.route('/service1/<path:path>', methods=['GET', 'POST', 'PUT', 'DELETE'])
|
|
def service1_proxy(path):
|
|
url = f'http://service1/{path}'
|
|
response = requests.request(
|
|
method=request.method,
|
|
url=url,
|
|
headers=request.headers,
|
|
data=request.get_data(),
|
|
cookies=request.cookies,
|
|
allow_redirects=False
|
|
)
|
|
return (response.content, response.status_code, response.headers.items())
|
|
|
|
@app.route('/gpt', methods=['POST'])
|
|
def gpt4_proxy():
|
|
data = request.json
|
|
prompt = data.get('prompt', '')
|
|
model = 'gpt-4'
|
|
response = openai.ChatCompletion.create(
|
|
messages=[
|
|
{
|
|
"role": "user",
|
|
"content": prompt,
|
|
}
|
|
],
|
|
model=model,
|
|
)
|
|
return jsonify({'response': response.choices[0].message.content.strip()})
|
|
|
|
if __name__ == '__main__':
|
|
app.run(host='0.0.0.0', port=8000)
|
|
|
|
# start with: gunicorn -w 4 -b 0.0.0.0:8000 app:app
|