代码之家  ›  专栏  ›  技术社区  ›  Iustin Beceneagă

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

  •  0
  • Iustin Beceneagă  · 技术社区  · 6 年前

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

    任何帮助都将不胜感激!

    这是我的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>
    1 回复  |  直到 6 年前
        1
  •  0
  •   Bogdan Bibina    6 年前

    您可以尝试使用: -markov链是google在google助手中使用的一种算法 -你可以尝试使用深度学习,张量流和递归神经网络,你可以尝试建立一个模型 -你可以试着用一个有问题的数组来问一个潜在的回答,然后看看这个问题是否等于一部分回答 -在python中有不同的库,它们非常易于构建聊天机器人,您可以搜索其中一个 -或者可以从客户端尝试使用javascript

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