Datastreaming with flask.
I posted this earlier but it was closed so I apologize for reopening.
https://github.com/ading2210/poe-api/issues/29#issue-1659761715
I have tried following the flask documentation to stream the generator but it's not working. I am not sure if it has something to do with the addon itself or if this is not possible but if the code contains any mention of "poe_client.send_message" I instantly receive "WARNING:root:SendMessageMutation returned an error: Server Error | Retrying (1/20)". I've tried streaming with context, just printing to the console to see if it will work, literally everything and I have not found a working way to do it. If someone could help me figure out a working way to stream the output to a flask page I would appreciate it. Thanks!
Please let me test to make sure the code works before closing the issue though. I've got this code but it's still not working.
def chatbot2_response(query):
max_retries = 20
for attempt in range(1, max_retries + 1):
try:
for chunk in poe_client.send_message("capybara", query, with_chat_break=True):
yield chunk["text_new"]
break
except Exception as e:
logging.warning(f"SendMessageMutation returned an error: {e} | Retrying ({attempt}/{max_retries})")
time.sleep(1) # Add a sleep time to avoid overloading the server
else:
logging.error("SendMessageMutation failed after maximum retries")
@app.route("/chatbot2", methods=['GET', 'POST'])
def chatbot2():
chatbot_2_question = flask.request.form.get('question')
return Response(stream_with_context(chatbot2_response(chatbot_2_question)), mimetype='text/plain')
Output is just: WARNING:root:SendMessageMutation returned an error: Server Error | Retrying (1/20) WARNING:root:SendMessageMutation returned an error: Server Error | Retrying (2/20) WARNING:root:SendMessageMutation returned an error: Server Error | Retrying (3/20) and the page wont load
If I put it inside like this it does not work either.
@app.route("/chatbot2", methods=['GET', 'POST'])
def chatbot2():
chatbot_2_question = flask.request.form.get('question')
def chatbot2_response(query):
max_retries = 20
for attempt in range(1, max_retries + 1):
try:
for chunk in poe_client.send_message("capybara", query, with_chat_break=True):
yield chunk["text_new"]
break
except Exception as e:
logging.warning(f"SendMessageMutation returned an error: {e} | Retrying ({attempt}/{max_retries})")
time.sleep(1)
else:
logging.error("SendMessageMutation failed after maximum retries")
return Response(stream_with_context(chatbot2_response(chatbot_2_question)), mimetype='text/plain')
I apologize if my question seemed invalid I am just looking to get some help.
I've gotten this library to work with Flask just fine. client.send_message is just a normal python generator, so you'd just create a wrapper around it and return that.
Something like this should work (untested though):
@app.route("/send")
def send_message():
prompt = request.query.get("prompt")
def generator():
for chunk in client.send_message("capybara", prompt):
yield json.dumps(chunk)+"\n"
return Response(generator(), content_type="text/event-stream")
Nope :(
WARNING:root:SendMessageMutation returned an error: Server Error | Retrying (1/20) WARNING:root:SendMessageMutation returned an error: Server Error | Retrying (2/20) WARNING:root:SendMessageMutation returned an error: Server Error | Retrying (3/20)
def chatbot2():
chatbot_2_question = flask.request.form.get('question')
def generator():
for chunk in poe_client.send_message("capybara", chatbot_2_question):
yield json.dumps(chunk)+"\n"
return Response(generator(), content_type="text/event-stream")`````
Nope :(
Your issue is not poe-api related, but python/flask.