I couldn't find the de facto standard chatbot framework for Python, so I made one.
Minette for Python https://github.com/uezo/minette-python
You can install it with the pip command, write just a few lines of code, and the bot will work without any database or application server settings.
Includes adapters for LINE and Clova. You can develop LINE BOT and Clova skills immediately by adding initialization code. In addition, the user-implemented part is not affected by the difference in channels, so development can proceed in the same procedure for both.
Sessions (contexts) can be used across remarks to realize context-aware dialogue scenarios. In addition, the acquisition and storage of user information is automatically performed without the developer being aware of it. Furthermore, since it has a task scheduler for automating regular processing, it can be completed with the resources of the BOT application without setting cron or the like.
If you have MeCab installed, you can automatically morphologically analyze the user's utterance and receive the result. It also supports Janome, which is pure Python, and can also support cloud services such as Google Cloud Platform by extending it by itself, so it is possible to realize advanced dialogue using natural language analysis in any environment. I will.
We are developing and operating a LINE BOT called Ito BOT, which has more than 10 types of skills (functions), and the know-how cultivated here is reflected in the architecture.
$ pip install minette
This is OK with one shot. The latest version under development can be installed from the following.
$ pip install git+https://github.com/uezo/minette-python
After starting the chatbot itself, throw the user's utterance and a response will be returned.
echo.py
from minette import Minette, EchoDialogService
#Start Minette using the Echolalia part (EchoDialogService)
bot = Minette(default_dialog_service=EchoDialogService)
#Start dialogue
while True:
req = input("user> ")
res = bot.chat(req)
for message in res.messages:
print("minette> " + message.text)
$ python echo.py
user>Hello
minette> You said:Hello
user>Is it working?
minette> You said:Is it working?
Very easy.
Next time, as a custom skill, I will make a BOT that rolls two dice no matter what.
dice.py
import random
from minette import Minette, DialogService
#Custom interactive parts
class DiceDialogService(DialogService):
#Process logic and store results in context
def process_request(self, request, context, connection):
context.data = {
"dice1": random.randint(1, 6),
"dice2": random.randint(1, 6)
}
#Assemble response data using contextual information
def compose_response(self, request, context, connection):
return "Dice1:{} / Dice2:{}".format(
str(context.data["dice1"]), str(context.data["dice2"]))
if __name__ == "__main__":
#Start BOT
bot = Minette(default_dialog_service=DiceDialogService)
#Start dialogue
while True:
req = input("user> ")
res = bot.chat(req)
for message in res.messages:
print("minette> " + message.text)
Execution result
$ python dice.py
user> test
minette> Dice1:3 / Dice2:5
user> d
minette> Dice1:4 / Dice2:3
Briefly, process_request
finds two random numbers from 1 to 6 and stores them in context.data with the keys dice1
and dice2
, respectively. compose_response
extracts those values and creates a response statement.
I would like to put all the processing of this level into compose_response
, but by separating the skill (function) and the character (personality), maintainability can be maintained even in a complicated dialogue scenario. We recommend that you keep them separate.
Also, by passing DiceDialogService
as the argument default_dialog_service
in the BOT generation process (Minette
), when BOT is executed, the process will be passed to the part implemented this time.
Please refer to the README on Github for a code example of the translated BOT. https://github.com/uezo/minette-python
For BOT development using Minette, let's check the overall architecture diagram to see what the developer should do specifically.
Basically, the core of development work is to implement the following four methods.
--register_intents ()
Set the dialogue part (DialogService) to be called according to the utterance intention (intent).
--ʻExtract_intent () Extract intents and entities from utterances --
process_request () Application logic processing --
compose_response () `Assemble the response message
-Create a LINE BOT with Minette for Python (LINE integration, use chat API) -Make a LINE BOT with Raspberry Pi Zero W
Recommended Posts