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
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); }