first commit
This commit is contained in:
commit
9091ab0610
2 changed files with 62 additions and 0 deletions
48
main.py
Normal file
48
main.py
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
# pip install Flask requests
|
||||
# pip install openai
|
||||
|
||||
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.chat.completions.create(
|
||||
messages=[
|
||||
{
|
||||
"role": "user",
|
||||
"content": prompt,
|
||||
}
|
||||
],
|
||||
model=model,
|
||||
)
|
||||
return jsonify({'response': response.choices[0].message.content.strip()})
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='localhost', port=6000)
|
||||
|
||||
|
||||
14
test.py
Normal file
14
test.py
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import requests
|
||||
|
||||
def test_gateway(prompttext):
|
||||
url = 'http://localhost:6000/gpt'
|
||||
payload = {
|
||||
'prompt': prompttext,
|
||||
}
|
||||
response = requests.post(url, json=payload)
|
||||
if response.status_code == 200:
|
||||
print('Response from GPT:', response.json()['response'])
|
||||
else:
|
||||
print('Failed to get response:', response.status_code, response.text)
|
||||
|
||||
test_gateway("Please give me a python script to summarize cells in excel")
|
||||
Loading…
Reference in a new issue