在我的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天
简介:参观毕加索博物馆和马拉加的阿尔卡扎巴要塞。
地点:西班牙马拉加
地点:
1.毕加索博物馆
2.阿尔卡扎巴要塞
然而,没有任何东西进入身体,因此,我的状态不会达到200
ngrok
.
我尝试过使用另一个聊天机器人,例如:
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不兼容。我试着在短信正文中只传一个简单的“嗨”,但也没用。不知道为什么我不能将任何东西传递到我的MessagingResponse()中。