flask-brz9-backend/main.py

73 lines
1.8 KiB
Python

from flask import Flask, request, jsonify
from dotenv import load_dotenv
load_dotenv()
from langchain.chat_models import ChatOpenAI
from langchain.schema import HumanMessage
app = Flask(__name__)
# set fixed port for app
app.config['SERVER_NAME'] = 'localhost:5000'
@app.route('/openai', methods=['POST'])
def openai():
content_type = request.headers.get('Content-Type')
prompt = None
temperature = 0.9
model_name = 'gpt-3.5-turbo'
if content_type == 'application/json':
json_payload = request.json
prompt = json_payload['prompt']
else:
return jsonify({'error': 'Invalid content type'}), 400
llm = ChatOpenAI(temperature = temperature, model_name = model_name)
resp = llm([HumanMessage(content=prompt)])
return {
'statusCode': 200,
'body': resp.content
}
"""
curl test:
curl -XPOST --header "Content-Type: application/json" -d "{\"prompt\":\"What is the best way to learn a language?\"}" http://localhost:5000/openai
curl -XPOST --header "Content-Type: application/json" -d "{\"prompt\":\"What is a good name for a company that makes funny socks?\"}" http://localhost:5000/openai
"""
@app.route('/')
def home():
return """
<!DOCTYPE html>
<html>
<head>
<title>This is not for human, gtfo</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<style>
body {
max-width: 100vw;
max-height: 100vh;
font-family: monospace;
text-align: center;
background-color: #000;
color: #fff;
}
p {
margin-top: 30vh;
}
</style>
<body>
<p>You have no business being here.</p>
</body>
</html>
"""
if __name__ == '__main__':
app.run(debug=True)