Kirsle.net logo Kirsle.net

Tagged as: ask

Ask Kirsle: Self-learning RiveScript Bot?
May 9, 2020 (updated May 9, 2020) by Noah

Oliver asks:

Hello,

I'm looking for a way to solve the following scenario using Rivescript and Python. The chatbot has no answer to a question, in this case it asks the user for the correct answer and records it in the rivescript file.

Do you have an idea? Does it even work in Rivescript?

Thanks

Best wishes Oliver

Hey Oliver

This can be done with a little bit of creativity. Self-learning bots aren't an officially supported feature in RiveScript, but you can get something to work using object macros that write new replies out to .rive files to grow the bot's brain over time.

There's an example for this in the JavaScript edition of RiveScript, but its logic could be ported over to Python easily enough: https://github.com/aichaos/rivescript-js/tree/master/eg/learning

RiveScript Playground example you can test in your web browser: https://play.rivescript.com/s/0r4ZYvklR9

The basic ideas are:

  1. A RiveScript trigger of + when i say * you say * allows the user to teach the bot a new reply.
    • e.g.: "when I say hello bot you say Hello human!"
  2. To preserve punctuation and formatting, the Python script running the RiveScript module can store the user's raw, original message into a user variable which the object macro can access:
    • rs.set_uservar(username, "origMessage", message)
  3. The object macro behind the when i say * you say * trigger re-parses the user's message to get the original punctuation and formatting they used.
  4. The object macro appends the new trigger/reply into a learned.rive file and also calls stream() to load the new reply for immediate use by the bot "right now."

The JavaScript example linked above works whether or not the program set the origMessage variable: if the program did not set the variable, the learned reply is just somewhat limited to only include simple text with very few punctuation symbols preserved.

Tags: 1 comment | Permalink