Appendix E. Submitting a bot to the Online Go Server

In this appendix, you’ll learn how to deploy a bot to the popular Online Go Server. To do so, you’ll use your bot framework from the first eight chapters of this book to deploy a bot on the cloud-provided Amazon Web Services (AWS) that communicates through the Go Text Protocol (GTP). Make sure to read the first eight chapters to understand the basics of this framework and read up on appendix D for AWS basics.

Registering and activating your bot at OGS

The Online Go Server (OGS) is a popular platform on which you can play Go against other human players and bots. Appendix C showed you a few other Go servers, but we’ve picked OGS for this appendix to demonstrate how to deploy bots. OGS is a modern web-based platform that you can explore at https://online-go.com. To register with OGS, you have to sign up at https://online-go.com/register. If you want to deploy a bot with OGS, you need to create two accounts:

  1. Register an account for yourself as a human player. Pick an available username, password, and optionally enter your email address. You can also register via Google, Facebook, or Twitter. We’ll refer to this account as <human>.
  2. Go back to the registration one more time and register another account. This will act as your bot account, so give it an appropriate name, signifying it as a bot. We’ll call this account <bot> in what follows.

At this point, all you have is two regular accounts. What you want to achieve is to make the second account a bot account that’s owned and managed by the user account. To make this happen, you first need to sign in with your human account on OGS and find an OGS moderator that can activate your bot account. In the top left, next to the OGS logo, you can open the menu and search for users by name. OGS moderators crocrobot and anoek offered help with the registration process for this book. If you search for either one of those names and then click the account name in the search results, a pop-up box like the one shown in figure E.1 should open.

Figure E.1. Contacting an OGS moderator to activate your bot account

In this box, click Message to get in touch with the moderator. A message box should open in the lower right. You’ll have to tell the moderator that you want to activate a bot account for <bot> and that this bot belongs to the human account <human> (the one you’re currently logged into). Usually, the OGS moderators get back to you within 24 hours, but you might have to be a little patient. You find a moderator under the Chat option in the top OGS menu; every user with a hammer symbol next to their name is an OGS moderator. If a moderator is on vacation or otherwise busy, you might find someone else who can help you out.

If you have trouble getting in touch with a moderator directly, you can also try creating a message in the OGS forum (https://forums.online-go.com), in the OGS Development section. Remember that the moderators are all volunteers who help out in their spare time, so be patient!

After you hear back from the moderator you got in touch with, you can log into your <bot> account. Click the menu symbol at the top left of the OGS page and select Profile to view the profile page of your bot. If all went well, your <bot> account should be listed as Artificial Intelligence and should have an Administrator, namely your <human> account. In short, your bot profile should look similar to what you see for the BetagoBot account in figure E.2, which is administrated by Max’s human account DoubleGotePanda.

Figure E.2. Checking the profile page of your bot to see it has been activated

Next, log out of your <bot> account and back into your <human> account. You need to do this to generate an API key for your bot, which can be done only through its human administrator account. Once logged into <human>, visit the profile page of <bot> (for instance, by searching for and clicking <bot>). Scrolling down a little, you’ll find a Bot Controls box containing a Generate API Key button. Click this button to generate your API key and then click Save to store it. For the rest of this appendix, we’ll assume your API key is called <api-key>.

Now that you’ve set up everything on OGS, you’ll proceed to using your bot name and API key to connect a GTP bot to OGS.

Testing your OGS bot locally

In chapter 8, you developed a bot that can understand and emit GTP commands. You now also have a bot account on OGS. The missing link to connect the two is a tool called gtp2ogs, which takes your bot name and API key and establishes a connection between the machine that has your bot and OGS. gtp2ogs is an open source library built in Node.js and is available in the official OGS GitHub repository under https://github.com/online-go/gtp2ogs. You don’t need to download or install this tool, because we’ve provided you with a copy of it in our GitHub repository already. In your local copy of http://mng.bz/gYPe, you should see a file called gtp2ogs.js and a JSON file called package.json. The latter you use to install dependencies; the former is the tool itself.

When deploying a bot to OGS, you want this bot to be available for everyone to play—for a long time. This deployment task is that of a long-running process. For that reason, it makes sense to serve the bot from a (remote) server. You’ll do precisely this in the next section, but you can first quickly test if everything works using your local machine. To that end, make sure you have both Node.js and its package manager (npm) installed on your system. On most systems, you can get both from the package manager of your choice (for example, running brew install node npm on a Mac or sudo apt-get install npm nodejs-legacy on Ubuntu), but you can also download and install these tools from https://nodejs.org/en/download/.

Next, you need to put the run_gtp.py Python script that you find at the top level in our GitHub repo on your system path. In Unix environments, you can do this from the command line by executing the following command:

export PATH=/path/to/deep_learning_and_the_game_of_go/code:$PATH

This puts run_gtp.py on your path, so that you can call it from anywhere on the command line. In particular, it’ll be available to gtp2ogs, which will spawn a new bot using run_gtp.py whenever a new game is requested for your bot on OGS. Now all that is left is to install the necessary Node.js packages and run the application. You’ll use the Node.js package forever to ensure that the application stays up and restarts should it fail at some point:

cd deep_learning_and_the_game_of_go/code
npm install

forever start gtp2ogs.js 
  --username <bot> 
  --apikey <api-key> 
  --hidden 
  --persist 
  --boardsize 19 
  --debug -- run_gtp.py

Let’s break down that command line:

  • --username and --apikey specify how to connect to the server.
  • --hidden keeps your bot out of the public bot lists, which will give you a chance to test everything before other players start challenging your bot.
  • --persist keeps your bot running between moves (otherwise, gtp2ogs will restart your bot every time it needs to make a move).
  • --boardsize 19 limits your bot to accept 19 × 19 games; if you trained your bot to play 9 × 9 (or some other size), use that instead.
  • --debug prints out extra logging so you can see what your bot is doing.

When your bot is running, head over to OGS, log into your <human> account, and click on the left menu. Type your bot’s name into the search box, click its name, and then click the Challenge button. Then you can start a match against your bot and start playing.

If you can select your bot, everything likely worked well, and you can now play your first game against your own creation. After you tested connecting your bot successfully, stop the Node.js application running your bot by typing forever stopall.

Deploying your OGS bot on AWS

Next, we’ll show you how to deploy your bot on AWS for free, so you and many other players around the world can play against it anytime (without having to run a Node.js application on your local computer).

For this part, we’re going to assume that you followed appendix D and have your SSH config configured so that you can access your AWS instance with ssh aws. The instance you use can be limited, because you don’t need much compute power to generate predictions from an already trained deep-learning model. In fact, you could resort to using one of the free-tier-eligible instances on AWS, like t2.micro. If you follow appendix D literally and choose a Deep Learning AMI for Ubuntu running on a t2.small, that won’t be entirely free but will cost only a few dollars per month—in case you want to keep your bot running on OGS.

In our GitHub repository, you’ll find a script called run_gtp_aws.py, which appears in listing E.1 below. The first line, starting with #!, tells the Node.js process which Python installation to use to run your bot. The base Python installation on your AWS instance should be something like /usr/bin/python, which you can check by typing which python in the terminal. Make sure this first line points to the Python version you’ve been using to install dlgo.

Listing E.1. run_gtp_aws.py to run a bot on AWS that connects against OGS
#!/usr/bin/python                                  1
from dlgo.gtp import GTPFrontend
from dlgo.agent.predict import load_prediction_agent
from dlgo.agent import termination
import h5py

model_file = h5py.File("agents/betago.hdf5", "r")
agent = load_prediction_agent(model_file)
strategy = termination.get("opponent_passes")
termination_agent = termination.TerminationAgent(agent, strategy)

frontend = GTPFrontend(termination_agent)
frontend.run()

  • 1 It’s important to make sure this matches the output of “which python” on your instance.

This script loads an agent from the file, initializes a termination strategy, and runs an instance of a GTPFrontend as defined in chapter 8. The agent and termination strategy chosen are for illustration purposes. You can modify both to your needs and use your own trained models and strategies instead. But to get started and familiarize yourself with the process of submitting a bot, you can leave the script as is for now.

Next, you need to make sure you have everything installed on your AWS instance to run the bot. Let’s start fresh, clone your GitHub repo locally, copy it to the AWS instance, log into it, and install the dlgo package:

git clone https://github.com/maxpumperla/deep_learning_and_the_game_of_go
cd deep_learning_and_the_game_of_go
scp -r ./code aws:~/code
ssh aws
cd ~/code
python setup.py develop

This is essentially the same set of steps you carried out to run an end-to-end example in appendix D. To run forever and gtp2ogs, you also need to make sure you have Node.js and npm available. After installing these programs on AWS by using apt, you can install gtp2ogs the same way you did locally:

sudo apt install npm
sudo apt install nodejs-legacy
npm install
sudo npm install forever -g

The final step is to run the GTP bot by using gtp2ogs. You export your current working directory to the system path and use run_gtp_aws.py as the bot runner this time:

PATH=/home/ubuntu/code:$PATH forever start gtp2ogs.js 
  --username <bot> 
  --apikey <api-key> 
  --persist 
  --boardsize 19 
  --debug -- run_gtp_aws.py > log 2>&1 &

Note that you redirect standard output and error messages into a log file called log and start the program as a background process with &. This way, your command line on the instance isn’t cluttered by server logs, and you can continue working on that machine. As in your local test of the OGS bot, you should now be able to connect to OGS and play a game against your bot. In case something breaks or doesn’t work as expected, you can check your latest bot logs by inspecting tail log.

That’s all there is to it. Although it takes some time to set up this pipeline (in particular, creating an AWS instance and setting up the two OGS accounts), after the basics are out of the way, deploying a bot is fairly straightforward. When you’ve developed a new bot locally and want to deploy it, all you do is the following:

scp -r ./code aws:~/code
ssh aws
cd ~/code
PATH=/home/ubuntu/code:$PATH node gtp2ogs.js 
  --username <bot> 
  --apikey <api-key> 
  --persist 
  --boardsize 19 
  --debug -- run_gtp_aws.py > log 2>&1 &

Now that your bot is running without the --hidden option, it’s open to challenges from the whole server. To find your bot, log into your <human> account, and click Play on the main menu. In the Quick Match Finder, click Computer to select a bot to play against. The name of your bot, <bot>, should show up in the drop-down menu as AI Player. In figure E.3, you can see the BetagoBot that Max and Kevin developed. Right now, you find just a handful of bots on OGS—maybe you can add an interesting bot? This completes the appendix. You can now deploy an end-to-end machine-learning pipeline resulting in a playable bot on an online Go platform.

Figure E.3. Your bot should now show up as a Computer opponent in the OGS match finder.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset