The SHEP AI Project

Home About Downloads Contact

Setting up an AI server

Posted 26/August/2020

Overview

In this tutorial we will be setting up an AI server where you will be able to communicate with your AI via a network. This could be used for a chatbot or the self-learning AI chatbot. For simplicity we will be using the self-learning AI, however the networking principles are the same.

Preparing the hardware

You will need two devices connected on the same network. This can be either wired or wireless.

Setting up software

Firstly you will need to download the SHEP software of your choosing. Once this is done we will start to write some basic network code. We will want the AI to be constantly there to receive requests and answer them. As the AI bases responses on individual conversation we will want to create a new AI for each client. Lets open up an AI on port 5000.

import asyncio
import websockets

async def client(websocket, path):
    try:
        async for message in websocket:
            print(message)
    except websockets.exceptions.ConnectionClosedError:
        print("User disconnected")

print("Starting server")
asyncio.get_event_loop().run_until_complete(
websockets.serve(client, port=5000)) #listen for clients

This code will simply run and look for client requests. When the client connects an object is created in the memory. We could create a dictionary to hold user client objects and the AI which is connected. This can be done like the following:
import asyncio
import websockets
from AI import CB

clients={}
path="/"
async def client(websocket, path):
    try:
        async for message in websocket:
            print(message)
            if websocket in clients:
                value=clients[websocket].chat(message)
                await websocket.send(value)
            else:
                clients[websocket]=CB(path)
    except websockets.exceptions.ConnectionClosedError:
        print("User disconnected")

asyncio.get_event_loop().run_until_complete(
websockets.serve(client, port=5000)) #listen for clients

Setting up a client

This can be done in many ways, you could use python, or you could also use html and JavaScript. In this tutorial we will use JavaScript. You will want to design a webpage which has an input box, and an output label. This is the simplest form of the user interface, but you can edit it with CSS to make it perfect for your application. I have written the code for you using the WebSocket library for JavaScript.
var con=new WebSocket("ws://your.local.ip/:5000");
con.onmessage = function(event) {
  alert(event.data);
};
function send(value)
{
	con.send(value);
}
All what is left is to make it so when you enter your message, it invokes the send function with the value of your data. You can edit the code where it says “alert” to instead post the message from the AI elsewhere. It is up to you how you wish to modify this code and make your own web app.

Taking it further

If you want to make it so you can access it outside your home network, you will have to go into your router by putting http://192.168.0.1/ in your URL. You will then sign into your router using the code which is likely written on the side. Once in you will want to find port forwarding, for me using the Virgin media hub that is under advanced settings, and security. Here you can simply make a new rule which is linked to the local IP address of your device. This means when you are not on the network you can still access your device. This is done by typing your public IP into the URL. You can find out your public IP by asking the google search engine.