多个bot文本响应python/flask/jquery

2024-10-17 08:22:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我有我的聊天应用程序,上面有简单的询问/回复段落。如何编写多个bot返回?现在它只返回一行:“你好!我是维克”

任何帮助都将不胜感激

以下是我的python代码:

@app.route("/get")
def get_bot_response():
    u = request.args.get('msg')
    #return str(english_bot.get_response(userText))
    if  "hello" in str(u):
        time.sleep(randint(1,5))
        b = str("Hello! I am Vic")
        return b
        time.sleep(randint(1,5))
        return "How can I help you?" 

jquery代码:

function getBotResponse() { var rawText = $("#textInput").val(); var userHtml = '<p class="userText"><span>' + rawText + '</span></p>'; $("#textInput").val(""); $("#chatbox").append(userHtml); document.getElementById('userInput').scrollIntoView({block: 'start', behavior: 'smooth'}); $('#chatbox').animate({scrollTop: $('#chatbox').prop("scrollHeight")}); $.get("/get", { msg: rawText }).done(function(data) { var botHtml = '<p class="botText"><span>' + data + '</span></p>'; $("#chatbox").append(botHtml); document.getElementById('userInput').scrollIntoView({block: 'start', behavior: 'smooth'}); $('#chatbox').animate({scrollTop: $('#chatbox').prop("scrollHeight")}); }); } getBotResponse(); $("#textInput").keypress(function(e) { if(e.which == 13) { getBotResponse(); var scroll=$('#chatbox'); scroll.animate({scrollTop: scroll.prop("scrollHeight")}); } });
<div id="chatbox">

<p class="botText"><span>Hello</span></p>
</div>
<div id="userInput">
<input id="textInput" type="text" name="msg" placeholder="Message..." autofocus>
</div>

Tags: divgetreturnvarbotfunctionmsgtextinput
1条回答
网友
1楼 · 发布于 2024-10-17 08:22:06

您可以尝试使用: -马尔可夫链是谷歌在谷歌助手使用的一种算法 -您可以尝试使用深度学习、tensorflow和递归神经网络来构建模型 -你可以试着用问句做一个数组,询问潜在的回答,然后看看这个问题是否与部分回答相等 -在python中有不同的库,它们非常容易用于构建聊天机器人,您可以搜索它们 -或者您可以使用JavaScript从客户端进行尝试

无论如何,所有这些都必须使用套接字进行实时通信

相关问题 更多 >