我是RestAPI的初学者,在这种情况下,我应该把我内置到这个RestAPI中的聊天机器人合并起来。chatbot是使用python完成的,并且有一个函数chat(),当调用此函数时,程序将进入一个与chatbot的无限循环聊天,直到我编写“退出”程序终止为止。
RestAPI程序在这里
from flask import Flask
from flask_restful import Api, Resource
import chatting
app = Flask(__name__)
api = Api(app)
class chatbot(Resource):
def get(self):
return{"data": chatting.chat()}
def post(self):
return{"data": "posted"}
api.add_resource(chatbot, '/hello')
if __name__ == "__main__":
app.run(debug= True)我导入了聊天脚本,这个脚本有聊天功能,可以和聊天机器人聊天,这个程序不能正常工作。
这是API测试程序:
import requests
Base = "http://127.0.0.1:5000/"
response = requests.get(Base + "/hello")
print(response.json())发布于 2021-04-14 10:15:46
调用此函数时,程序进入无限循环。
如果没有看到您的代码,我不能肯定地说,但这是一个问题,我的朋友。rest请求不应该存在那么长时间。你应该改变你的程序逻辑。每个rest调用都应该发送一个文本并得到一个响应。您的无限时间循环现在实际上是您的api服务器,它将永远运行并等待请求。
您的聊天功能应该如下所示:
def chat(text):
response = do_your_thing(text)
return response发布于 2021-04-30 21:55:25
post函数可以用来发送音频并返回音频文件。
https://stackoverflow.com/questions/67089681
复制相似问题