使用 Python 创建端到端聊天机器人

这篇具有很好参考价值的文章主要介绍了使用 Python 创建端到端聊天机器人。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

聊天机器人是一种计算机程序,它了解您的查询意图以使用解决方案进行回答。聊天机器人是业内最受欢迎的自然语言处理应用。因此,如果您想构建端到端聊天机器人,本文适合您。在本文中,我将带您了解如何使用 Python 创建端到端聊天机器人。

1. 效果图

训练的意图及回复越多,机器人将越灵活准确。
用例少不准确或者基本是提供的内容的回答。

使用 Python 创建端到端聊天机器人
使用 Python 创建端到端聊天机器人

2. 原理

2.1 什么是端到端聊天机器人?

端到端聊天机器人是指可以在不需要人工帮助的情况下从头到尾处理完整对话的聊天机器人。

要创建端到端聊天机器人,需要编写一个计算机程序,该程序可以理解用户请求,生成适当的响应,并在必要时采取行动。这包括收集数据,选择编程语言和NLP工具,训练聊天机器人,以及在将其提供给用户之前对其进行测试和完善。 部署后,用户可以通过向聊天机器人发送多个请求来与聊天机器人进行交互,聊天机器人可以自行处理整个对话。

2.2 创建端到端聊天机器人步骤

  1. 定义意向
  2. 创建训练数据
  3. 训练聊天机器人
  4. 构建聊天机器人
  5. 测试聊天机器人
  6. 部署聊天机器人

3. 源码

3.1 streamlit安装

python3.7.6+ streamlit 0.68.0,1.20.0,1.21.0 都不行,只有1.9版本ok

pip install streamlit==1.9

使用 Python 创建端到端聊天机器人文章来源地址https://www.toymoban.com/news/detail-429862.html

3.2 源码

# 端到端聊天机器人
# streamlit run ete_chatbot.py

import os
import nltk
import ssl
import streamlit as st
import random
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression

ssl._create_default_https_context = ssl._create_unverified_context
nltk.data.path.append(os.path.abspath("nltk_data"))
nltk.download('punkt')

# 定义聊天机器人的一些意图。可以添加更多意图,使聊天机器人更有用且功能更强大
intents = [
    {
        "tag": "greeting",
        "patterns": ["Hi", "Hello", "Hey", "How are you", "What's up"],
        "responses": ["Hi there", "Hello", "Hey", "I'm fine, thank you", "Nothing much"]
    },
    {
        "tag": "goodbye",
        "patterns": ["Bye", "See you later", "Goodbye", "Take care"],
        "responses": ["Goodbye", "See you later", "Take care"]
    },
    {
        "tag": "thanks",
        "patterns": ["Thank you", "Thanks", "Thanks a lot", "I appreciate it"],
        "responses": ["You're welcome", "No problem", "Glad I could help"]
    },
    {
        "tag": "about",
        "patterns": ["What can you do", "Who are you", "What are you", "What is your purpose"],
        "responses": ["I am a chatbot", "My purpose is to assist you", "I can answer questions and provide assistance"]
    },
    {
        "tag": "help",
        "patterns": ["Help", "I need help", "Can you help me", "What should I do"],
        "responses": ["Sure, what do you need help with?", "I'm here to help. What's the problem?", "How can I assist you?"]
    },
    {
        "tag": "age",
        "patterns": ["How old are you", "What's your age"],
        "responses": ["I don't have an age. I'm a chatbot.", "I was just born in the digital world.", "Age is just a number for me."]
    },
    {
        "tag": "weather",
        "patterns": ["What's the weather like", "How's the weather today"],
        "responses": ["I'm sorry, I cannot provide real-time weather information.", "You can check the weather on a weather app or website."]
    },
    {
        "tag": "budget",
        "patterns": ["How can I make a budget", "What's a good budgeting strategy", "How do I create a budget"],
        "responses": ["To make a budget, start by tracking your income and expenses. Then, allocate your income towards essential expenses like rent, food, and bills. Next, allocate some of your income towards savings and debt repayment. Finally, allocate the remainder of your income towards discretionary expenses like entertainment and hobbies.", "A good budgeting strategy is to use the 50/30/20 rule. This means allocating 50% of your income towards essential expenses, 30% towards discretionary expenses, and 20% towards savings and debt repayment.", "To create a budget, start by setting financial goals for yourself. Then, track your income and expenses for a few months to get a sense of where your money is going. Next, create a budget by allocating your income towards essential expenses, savings and debt repayment, and discretionary expenses."]
    },
    {
        "tag": "credit_score",
        "patterns": ["What is a credit score", "How do I check my credit score", "How can I improve my credit score"],
        "responses": ["A credit score is a number that represents your creditworthiness. It is based on your credit history and is used by lenders to determine whether or not to lend you money. The higher your credit score, the more likely you are to be approved for credit.", "You can check your credit score for free on several websites such as Credit Karma and Credit Sesame."]
    }
]

# Create the vectorizer and classifier 创建矢量化器和分类器
vectorizer = TfidfVectorizer()
clf = LogisticRegression(random_state=0, max_iter=10000)

# 准备意图数据并训练模型
tags = []
patterns = []
for intent in intents:
    for pattern in intent['patterns']:
        tags.append(intent['tag'])
        patterns.append(pattern)

# 训练模型
x = vectorizer.fit_transform(patterns)
y = tags
clf.fit(x, y)

def chatbot(input_text):
    input_text = vectorizer.transform([input_text])
    tag = clf.predict(input_text)[0]
    for intent in intents:
        if intent['tag'] == tag:
            response = random.choice(intent['responses'])
            return response


# 为了部署聊天机器人,将使用 Python 中的 streamlit 库,它提供了惊人的功能,只需几行代码即可为机器学习应用程序创建用户界面
counter = 0

def main():
    global counter
    st.title("Chatbot")
    st.write("Welcome to the chatbot. Please type a message and press Enter to start the conversation.")

    counter += 1
    user_input = st.text_input("You:", key=f"user_input_{counter}")

    if user_input:
        response = chatbot(user_input)
        st.text_area("Chatbot:", value=response, height=100, max_chars=None, key=f"chatbot_response_{counter}")

        if response.lower() in ['goodbye', 'bye']:
            st.write("Thank you for chatting with me. Have a great day!")
            st.stop()

if __name__ == '__main__':
    main()

参考

  • https://thecleverprogrammer.com/2023/03/27/end-to-end-chatbot-using-python/

到了这里,关于使用 Python 创建端到端聊天机器人的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 使用Java和ChatGPT Api来创建自己的大模型聊天机器人

    什么是大模型? 大型语言模型(LLM)是一种深度学习模型,它使用大量数据进行预训练,并能够通过提示工程解决各种下游任务。LLM 的出发点是建立一个适用于自然语言处理的基础模型,通过预训练和提示工程的方式实现模型在新的数据分布和任务上的强大泛化能力。LLM 旨

    2024年02月12日
    浏览(42)
  • 如何使用OpenAI API和Python SDK构建自己的聊天机器人

    近日,OpenAI公司的ChatGPT模型走红网络。同时,OpenAI也推出了Chat API和gpt-3.5-turbo模型,让开发者能够更轻松地使用与ChatGPT类似的自然语言处理模型。 通过OpenAI API,我们可以使用gpt-3.5-turbo模型,实现多种任务,包括:撰写电子邮件或其他文本内容,编写Python代码,创建对话代

    2024年02月01日
    浏览(39)
  • 简介:在这篇教程中,我们将使用React.js框架创建一个简单的聊天机器人的前端界面,并利用Dialogflo

    作者:禅与计算机程序设计艺术 介绍及动机 聊天机器人(Chatbot)一直是互联网领域中的热门话题。而很多聊天机器人的功能都依赖于人工智能(AI)技术。越来越多的企业希望拥有自己的聊天机器人系统,从而提升自己的竞争力。为此,业界也出现了很多基于开源技术或云

    2024年02月06日
    浏览(52)
  • 制作一个Python聊天机器人

    我们学习一下如何使用 ChatterBot 库在 Python 中创建聊天机器人,该库实现了各种机器学习算法来生成响应对话,还是挺不错的 聊天机器人也称为聊天机器人、机器人、人工代理等,基本上是由人工智能驱动的软件程序,其目的是通过文本或语音与用户进行对话。 我们日常接触

    2024年01月19日
    浏览(59)
  • 【NLP开发】Python实现聊天机器人(微软Azure机器人服务)

    🍺NLP开发系列相关文章编写如下🍺: 1 🎈【小沐学NLP】Python实现词云图🎈 2 🎈【小沐学NLP】Python实现图片文字识别🎈 3 🎈【小沐学NLP】Python实现中文、英文分词🎈 4 🎈【小沐学NLP】Python实现聊天机器人(ELIZA))🎈 5 🎈【小沐学NLP】Python实现聊天机器人(ALICE)🎈 6

    2024年02月04日
    浏览(49)
  • [LLM]Streamlit+LLM(大型语言模型)创建实用且强大的Web聊天机器人

    Streamlit 是一个开源框架,使开发人员能够快速构建和共享用于机器学习和数据科学项目的交互式 Web 应用程序。它还提供了一系列小部件,只需要一行 Python 代码即可创建,例如 st.table(…) 。对于我们创建一个简单的用于私人使用的聊天机器人网站来说,Streamlit 是一个非常合

    2024年01月16日
    浏览(39)
  • 【小沐学NLP】Python实现聊天机器人(微软Azure机器人服务)

    🍺NLP开发系列相关文章编写如下🍺: 1 🎈【小沐学NLP】Python实现词云图🎈 2 🎈【小沐学NLP】Python实现图片文字识别🎈 3 🎈【小沐学NLP】Python实现中文、英文分词🎈 4 🎈【小沐学NLP】Python实现聊天机器人(ELIZA))🎈 5 🎈【小沐学NLP】Python实现聊天机器人(ALICE)🎈 6

    2024年02月12日
    浏览(40)
  • 【python+wechaty+docker+nodejs】24年从0开始搭建使用python-wechaty接入微信聊天机器人全过程记录

    全网搜索了所有相关文章,由于个人原是java老程序员,对python有点兴趣,正好这个机器人的python资料比较多,因此就着手尝试。 在网上基本没有找到python-wechaty的完整说明的使用手册因此自己写一个记录一下全过程。 真正的从0开始。只有系统。没有其他的情况下,都是全新

    2024年01月24日
    浏览(49)
  • 我用python/C++自制了一个聊天机器人

    2015年, OpenAI 由马斯克、美国创业孵化器Y Combinator总裁阿尔特曼、全球在线支付平台PayPal联合创始人彼得·蒂尔等硅谷科技大亨创立,公司核心宗旨在于 实现安全的通用人工智能(AGI) ,使其有益于人类。 ChatGPT 则是近期 OpenAI 推出的一个基于对话的原型 AI 聊天机器人,2022年

    2023年04月17日
    浏览(36)
  • NoneBot2,基于Python的聊天机器人

    NoneBot2 是一个现代、跨平台、可扩展的 Python 聊天机器人框架,它基于 Python 的类型注解和异步特性,能够为你的需求实现提供便捷灵活的支持。 NoneBot2 具有丰富的插件生态系统,可以实现多种功能,例如自动回复、天气查询、消息推送等等。此外,它还提供了完善的文档和

    2023年04月16日
    浏览(42)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包