OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Problem when testing webframe created using flask

  • Thread starter Thread starter Jason Pitt
  • Start date Start date
J

Jason Pitt

Guest
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.

Code:
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.

Code:
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.
<p>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.</p>
<pre><code>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"}'
</code></pre>
<p>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.</p>
<pre><code>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)
</code></pre>
<p>I upgraded openai package but it still caused same issue.</p>
 

Latest posts

H
Replies
0
Views
1
hohohohoho
H
Top