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 """
You have no business being here...