在我的 Twilio WhatsApp 聊天机器人中,我成功地传递了输入并获得了一个字符串对象,它最终没有传递到 MessagingResponse 中。
使用 Flask 在 AWS EC2 实例上呈现此聊天机器人。
这是我的聊天机器人代码:
# Define a route to handle incoming requests
@app.route('/chatgpt', methods=['POST'])
def chatgpt():
incoming_que = request.values.get('Body', '').lower()
print("Question: ", incoming_que)
answer = generate_answer(incoming_que)
print("BOT Answer: ", answer)
bot_resp = MessagingResponse()
message = bot_resp.message()
message.body(answer)
# Return the response as a string
return str(bot_resp)
# Run the Flask app
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=False, port=5000)
我得到的
answer
如下:
标题:西班牙之旅:探索马拉加和巴塞罗那
第一天
总结:参观马拉加的毕加索博物馆和阿尔卡萨瓦堡垒
地点:西班牙马拉加
地点:
1。毕加索博物馆
2。阿卡萨巴堡垒
然而,没有任何东西进入身体,因此,我的
ngrok
上没有状态 200。
我试过使用另一个聊天机器人,例如:
def generate_answer(question):
model_engine = "text-davinci-002"
prompt = (f"Q: {question}\n"
"A:")
response = openai.Completion.create(
engine=model_engine,
prompt=prompt,
max_tokens=1024,
n=1,
stop=None,
temperature=0.7,
)
answer = response.choices[0].text.strip()
return answer
# Define a route to handle incoming requests
@app.route('/chatgpt', methods=['POST'])
def chatgpt():
incoming_que = request.values.get('Body', '').lower()
print("Question: ", incoming_que)
# Generate the answer using GPT-3
answer = generate_answer(incoming_que)
print("BOT Answer: ", answer)
bot_resp = MessagingResponse()
msg = bot_resp.message()
msg.body(answer)
return str(bot_resp)
# Run the Flask app
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=False, port=5000)
效果很好,我得到了状态 200 响应。
我认为我的输出可能与 XML 不兼容。我尝试将一个简单的“嗨”传递到 msg.body 中,但这也没有用。不知道为什么我不能将任何东西传递到我的 MessagingResponse() 中。