I am trying to test out how openai api works by creating to-do list maker. After writing the code and sending some test using curl. When I tested on terminal it return 200.
curl -X POST http://127.0.0.1:5000/chatbot -H "Content-Type: application/json" -d '{"message": "Buy groceries, clean the house, sleep for 8 hours"}'
Assuming I would have the result on 127.0.0.1.5000/chatbot I typed localhost:5000/chatbot on chrome then it returns 415 error. Never used flask before so not sure if I am testing it right.
import openai
from flask import Flask, request, jsonify
app = Flask(__name__)
openai.api_key = 'api_code'
def generate_response(prompt):
response = openai.completions.create(
model="gpt-3.5-turbo-instruct",
prompt=prompt,
max_tokens=150
)
return response.choices[0].text.strip()
@app.route('/chatbot', methods=['GET', 'POST'])
def chatbot():
try:
user_input = request.json['message']
print(f"Received message: {user_input}")
prompt = f"Create a to-do list based on the following tasks: {user_input}"
response_text = generate_response(prompt)
return jsonify({'response': response_text})
except Exception as e:
print(f"Error: {e}")
return jsonify({'error': 'Internal Server Error'}), 500
if __name__ == '__main__':
app.run(port=5000, debug=True)
I upgraded openai package but it still caused same issue.
New contributor
Jason Pitt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.