模块化可组合聊天机器人开发
项目描述
Puppet
Puppet 是一个使用会话组件构建聊天机器人的高级工具包。如果您想使用可组合组件使聊天机器人模块化,请尝试使用 Puppet。
安装
# optional: create a virtual environment
python3 -m venv myvenv
source myvenv/bin/activate
# install puppet
pip3 install coco-puppet
其他依赖项
您可能需要安装依赖项以连接到例如 telegram 和 discord 的通道或共享 cocohub 上的组件。
可用依赖项
- discord - 将您的机器人/组件连接到 discord
- telegram - 将您的机器人/组件连接到 telegram
- msbf - 将您的机器人/组件连接到微软机器人框架
- vendor - 在 cocohub 上发布组件
- dsl - Hy DSL,用于构建组件/nlu 时语法更佳
示例
pip install coco-puppet[telegram]
# or for multiple dependecies
pip install coco-puppet[telegram,dsl]
入门
创建您的第一个机器人
Puppet 组件是 Python 协程(注意 async def
)
我们以 state
作为第一个参数 - 这是一个对象,允许我们与环境交互,该组件/机器人正在运行
async def mybot(state):
# state.user_input() waits for the next user input
user_input = await state.user_input()
# state.say sends a response
await state.say(user_input)
将此代码粘贴到名为 example.py 的文件中
在终端中尝试它
python3 -m puppet example.mybot
通道
连接到通道很容易,只需使用常规 Puppet 组件即可
Telegram
确保安装具有 Telegram 支持的 Puppet - pip install coco-puppet[telegram]
创建一个新的机器人,并使用此指南从 Telegram botfather 获取 Telegram 令牌:[https://core.telegram.org/bots#6-botfather](https://core.telegram.org/bots#6-botfather)
export TELEGRAM_TOKEN=<Your telegram bot token>
python3 -m puppet.channels.telegram example.mybot
Discord
确保安装具有 Discord 支持的 Puppet - pip install coco-puppet[discord]
按照此指南创建新的机器人账户并获取令牌:https://discordpy.readthedocs.io/en/latest/discord.html
export DISCORD_KEY=<Your discord bot token>
python3 -m puppet.channels.discord example.mybot
Microsoft 机器人框架
请确保安装支持 Microsoft 机器人框架的 puppet - pip install coco-puppet[msbf]
export MicrosoftAppId=<Your bot Microsft App Id>
export MicrosoftAppPassword=<Your bot Microsoft App Password>
python3 -m puppet.channels.msbf example.mybot
基本语言理解
在 puppet.nlu 中,我们有一个简单的正则表达式编译器来执行基本理解任务
将简单单词模式编译为正则表达式
一些示例
intent = Intent(
Pattern("the", "boy", "ate", "an", "apple")
)
assert intent("the boy ate an apple") == True
assert intent("the boy ate an orange") == False
intent = Intent(
Pattern("the", "boy", "ate", "an", AnyWords(min=1, max=1))
)
assert intent("the boy ate an apple") == True
assert intent("the boy ate an orange") == True
intent = Intent(
Pattern("the", Words("boy", "girl"), "ate", "an", AnyWords(min=1, max=1))
)
assert intent("the boy ate an apple") == True
assert intent("the boy ate an orange") == True
assert intent("the girl ate an orange") == True
assert intent("the girl ate a banana") == False
intent = Intent(
Pattern("the", ("boy", "girl"), "ate", WordsRegex(r"an?"), AnyWords(min=1, max=1))
)
assert intent("the boy ate an apple") == True
assert intent("the boy ate an orange") == True
assert intent("the girl ate an orange") == True
assert intent("the girl ate a banana") == True
assert intent("a nice boy ate an apple") == False
intent = Intent(
Pattern(WILDCARD, Words("boy", "girl"), "ate", WordsRegex(r"an?"), AnyWords(min=1, max=1))
)
assert intent("a nice boy ate an apple") == True
Pattern
接收句子元素并将每个元素转换为优化的正则表达式。
Intent
将多个模式分组,因此如果任何模式匹配意图,则评估为 True
项目详情
下载文件
下载您平台上的文件。如果您不确定选择哪个,请了解有关 安装包 的更多信息。