Ask Kirsle: Self-learning RiveScript Bot?

May 9, 2020 (updated May 9, 2020) by Noah

Oliver asks:


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?


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:

RiveScript Playground example you can test in your web browser:

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.



There are 3 comments on this page. Add yours.

Avatar image
Oliver posted on May 12, 2020 @ 17:55 UTC

Thank you

Avatar image
Anonymous posted on March 17, 2021 @ 19:36 UTC (updated 3/17/21 19:58 UTC)

when using the macro online and on a chromebook where are those learned responses stored? I used the example and can't seem to find the responses to save them for future use. How can I access those responses?

Avatar image
Noah (@kirsle) posted on March 18, 2021 @ 00:30 UTC

The web demo version stores the learned responses in your web browser's localStorage, you can see them by opening your browser's developer console (F12 key, usually) and typing "localStorage" in the JS console, example:

> localStorage
Storage { "learned.rive": "! version = 2.0\n! local concat = none\n\n+ hello\n- World\n", length: 1 }

The web demo only stores them locally with your web browser, of course. A real deployment (which learns from multiple users) would need to store the learned file somewhere persistently (server-side filesystem, in a database, etc.)

Add a Comment

Used for your Gravatar and optional thread subscription. Privacy policy.
You may format your message using GitHub Flavored Markdown syntax.