AI Zone Admin Forum Add your forum

NEWS: Chatbots.org survey on 3000 US and UK consumers shows it is time for chatbot integration in customer service!read more..

Rivescript support for DB and working with Spark/Hadoop
 
 

I am complete newbie to Chatbot world and was recently looking for bot programming. I have come across RS and have been learning and started using a bit (with perl). I need to understand how RS would work with DB and is there way to connect to DB and get information in RS or would we need to have something like perl DBI code written inside to work with DB

Is it possible to generate automated RS file for interaction through perl/ any other language so that no manual typing (kind of an intelligent bot)

Also how many users could RS handle at a time (understand that it is more dependent on infrastructure but is there any limitation from RS perspective).

 

 
  [ # 1 ]

Perl Object Macros

You can get your bot to run Perl code as part of its reply in two ways: defining them in your Perl code via `$rs->setSubroutine()` or by defining them in-line in RiveScript code with `> object`

use RiveScript;
use 
DBI;

# Your DBI object. `our` to make it exportable from `package main`;
# if only using setSubroutine() this could be declared with `my` too,
# as long as it's in scope from your function.
our $dbh DBI->connect($dburl);

# Perl
my $rs = new RiveScript();
$rs->setSubroutine("zipcode"sub {
    my 
($self$args) = @_;
    
my $sth $dbh->prepare("SELECT * FROM cities WHERE zipcode = ? LIMIT 1");
    
my $res $sth->execute($args->[0]);
    return 
$res->{name};
});

###############

// You could also define that method in RiveScript like so
what city has zip code #
- <call>zipcode <star></call>

object zipcode perl
    my 
($self$args) = @_;
    
my $sth $main::dbh->prepare("SELECT * FROM cities WHERE zipcode = ? LIMIT 1");
    
my $res $sth->execute($args->[0]);
    return 
$res->{name};
object 

The main difference to note though: for inline `> object` functions, they are evaluated via `eval()` and live in the scope of the RiveScript package itself, not the `main` package like your main program. So to access global variables from your main program ($dbh in this case) you have to fully qualify the name, like `$main::dbh`—if it was in a different package, something like `$My::Bot::dbh` if $dbh was a global variable in the My::Bot module.

Auto-generated RiveScript

I have a JavaScript example of using RiveScript as a “router” (in a web app sense of the word), where you could generate all the triggers and Perl object macro handlers without writing any RiveScript code yourself.

The logic should be pretty portable to Perl or the other versions of RiveScript: https://github.com/aichaos/rivescript-js/tree/master/eg/router

Performance

RiveScript can respond to individual messages super fast and without consuming much CPU. A single server could probably handle thousands of simultaneous conversations easily, without needing to load balance and run multiple processes or anything. Your bottleneck is more likely to be at the network layer depending on your app’s architecture (handling 10K+ persistent connections is harder than handling 10K+ requests per minute like on a web server).

Where performance might break down is if your bot’s brain is just massively large. At the end of the day, the entire set of triggers for a given topic (“random” being the default topic in RiveScript) get put onto a big array in memory, which has to be searched through to find a match. When you port the Alice AIML brain to RiveScript, she performs pretty poorly on some versions of RiveScript because she has like 90,000+ patterns all in the main topic together.

Since RiveScript’s triggers support simplified regular expressions, you could compose a brain like Alice using significantly fewer triggers (for example, AIML needs different patterns for “hey”, “hello”, “hi”, and so-on that all redirect to a common response, but RiveScript could cover all those variations in a single trigger)... but a hand crafted RiveScript brain the size of Alice hasn’t been made yet. I have a GitHub repo tracking the performance of Alice on RiveScript here: https://github.com/aichaos/alice-benchmarks

 

 
  [ # 2 ]

Thank you for your reply Noah. Your answers have provided really good pointers

 

 
  login or register to react