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::HTTPd Ported to Python
 
 

To those that don’t know what Python::HTTPd is, it’s a simple web server designed for quickly being able to test and develop RiveScript bots from your local computer, especially in the context of web application-based bots.

The HTTP daemon was originally written in Perl, but now I rewrote the daemon in Python. The old legacy Perl version is still available in the “_perl_legacy_” folder in the github repo, but it will probably be going away soon.

https://github.com/kirsle/rivescript-httpd

Reasons for replacing the daemon with Python:

* Python is easier to compile into an EXE for Windows
* The code is cleaner and easier to maintain
* When I was testing the Perl version on Windows, it was having trouble spawning the subprocesses to handle CGI scripts. The Python version does this just fine.

So, shortly now I’ll use py2exe and get a stand-alone exe version of the HTTP daemon zipped up for end users to play around with. The exe version will be capable of running the Python CGI scripts on the web server by executing them itself, but Perl will need to be installed separately for the Perl CGI scripts to work. The JavaScript client of course works everywhere without needing Perl or Python. wink

 

 
  [ # 1 ]

Hi Noah. I’m going to have a go at setting this up as a web app like I did with the Perl version. However, the advantage here is is that if I can figure it out, I can build a VIsual Studio version that will enable it to run without CGI on a Windows server.

My first question, and apologies if this is something ridiculously basic…...... but what file contains the modules referred to in bot.py:

import os
import json
import cgi
import Cookie
import datetime
import random
import hashlib
import rivescript 

In the Perl version there was rivescript.pm, but I don’t see an equivalent of that file for Python.

 

 
  [ # 2 ]

The Python lib was turned into a proper package (https://github.com/kirsle/rivescript-python), so there isn’t a rivescript.py anymore, but a folder named “rivescript”

So for the web server, put the “rivescript” folder next to rivescript-httpd.py (for running the server locally), or next to bot.py for deploying on a real server. Or best yet, install the python module properly, i.e. through `pip install python-rivescript` so it ends up in your /usr/local/lib/python area.

 

 
  [ # 3 ]

Ah ok. I guess the ‘import .... ’ in the bot.py file automatically looks in the rivesctript sub-directory? Or does it look in all sub directories? I just used to php having to be very explicited with its ‘includes’.

I’m going for a real (shared) hosting setup.

I’ll setup this up first on an Apache server to make sure I first know what I’m doing. If successful, I’ll try to get it working in Visual Studio with the IronPython extension.

 

 
  [ # 4 ]

Yeah, in Python when you import something, that something could be “something.py”, or it could be a folder named “something” that includes an __init__.py file (in which case, “import something” actually loads “something/__init__.py”). In RiveScript, the majority of the code is in __init__.py wink

 

 
  [ # 5 ]

Another thing, when using the Python version (which I have working now on an Apache server, just need to figure out how this translates to Visual Studio), is it possible I can still use the Perl objects (in the .rs files) that I created before? I’m on a server that supports both perl and python.

 

 
  [ # 6 ]

Ok, I just came across the info that object macros in other languages need the programmer to write this handler themselves, I didn’t see this before, so don’t worry about the question. I’ll probably just re-write the objects in Python.

 

 
  [ # 7 ]

If you need a bit of inspiration, the Java version of RiveScript supports running Perl object macros. wink

https://github.com/kirsle/rivescript-java/blob/master/com/rivescript/lang/Perl.java

It basically just stores the Perl source from the object macro until you actually go to call it, and then it makes a JSON data structure that includes the Perl code, some info about the message (and the username who sent it), the user’s variables, etc., and sends all of that to a Perl script:

https://github.com/kirsle/rivescript-java/blob/master/lang/rsp4j.pl

when then dynamically loads RiveScript code that recreates the Perl object, uses RiveScript.pm to get a reply, then communicates all the data back to Java (in case the RS code changed any user vars, etc.) in JSON… which Java puts back into its own data.

It’s a huge hack, but it works. wink It’s probably easier just to port your code to Python though.

 

 
  [ # 8 ]

Well, I thought it would be an interesting challenge to go ahead and write an example of calling Perl object macros from within Python. wink

https://github.com/kirsle/rivescript-python/tree/master/eg/perl-objects

You will require the very latest greatest version of rivescript-python from git, though, because in writing this I uncovered a couple of bugs that needed fixing. Basically, the back-end code for the programming language handler needed to be reworked, because the RiveScript object should’ve passed the user’s ID along with the call request (the Java version did this), and that’s helpful because that Perl example needs to copy all the user’s variables to give to the Perl script, and it needs the user’s name to do that. wink

The other bug it uncovered is not yet fixed and might be a bit trickier, and that is: from within an object macro, you can’t figure out the user ID of the person you’re answering. I wanted to test whether the Perl code changing the user’s variables was working, but hit a snag when I realized the Perl code has no way of knowing which user it’s answering. wink This is a problem that affects ALL the RiveScript libraries. Since it’s a little too late to change the list of arguments that an object macro receives, I’ll probably fix this by adding a method to the RiveScript object itself, so that you could do, e.g.,

object setname perl
   my 
($rs, @args) = @_;
   
my $user $rs->currentUser();
   
$rs->setUservar($user"name"join(" ",@args));
object 

But anyway, with the latest update to the Python version, you can run Perl objects from it using something like the example linked above (there’s also a JavaScript example in the git repo which might be helpful if you’re making a web based bot at all). Its only been lightly tested (literally just finished it a few minutes ago), but should be able to do most things that Perl RiveScript can do w/ the Perl objects. wink

 

 
  [ # 9 ]

Awesome, thanks….. going to try this now.

 

 
  login or register to react