1 Star 0 Fork 0

mujie/chitchatassistant

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
server.py 2.05 KB
一键复制 编辑 原始数据 按行查看 历史
"""
server.py
~~~~~~~~~
web server,定义前端调用接口
:date: 2020-02-14 14:36:00
:author: by jiangdg
"""
from flask import Flask, jsonify
from flask import request
import requests
import json
import logging
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def test():
return"测试"
@app.route('/ai', methods=['GET', 'POST'])
def webToBot():
"""
前端调用接口
路径:/ai
请求方式:GET、POST
请求参数:content
:return: response rasa响应数据
"""
content = request.values.get('content')
if content is None:
return 'empty input'
response = requestRasabotServer('jiangdg', content)
return response.text.encode('utf-8').decode("unicode-escape")
def requestRasabotServer(userid, content):
"""
访问rasa服务
:param userid: 用户id
:param content: 自然语言文本
:return: json格式响应数据
"""
params = {'sender': userid, 'message': content}
botIp = '127.0.0.1'
botPort = '5005'
# rasa使用rest channel
# https://rasa.com/docs/rasa/user-guide/connectors/your-own-website/#rest-channels
# POST /webhooks/rest/webhook
rasaUrl = "http://{0}:{1}/webhooks/rest/webhook".format(botIp, botPort)
reponse = requests.post(
rasaUrl,
data=json.dumps(params),
headers={'Content-Type': 'application/json'}
)
return reponse
if __name__ == '__main__':
webIp = '127.0.0.1'
webPort = '8088'
print("##### webIp={}, webPort={}".format(webIp, webPort))
# 初始化日志引擎
fh = logging.FileHandler(encoding='utf-8', mode='a', filename='chitchat.log')
logging.basicConfig(
handlers=[fh],
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
)
# 启动服务,开启多线程、debug模式
# 浏览器访问http://127.0.0.1:8088/ai?content="你好"
app.run(
host=webIp,
port=int(webPort),
threaded=True,
debug=True
)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/bete/chitchatassistant.git
git@gitee.com:bete/chitchatassistant.git
bete
chitchatassistant
chitchatassistant
master

搜索帮助