Over at the Chai team, we’ve been enjoying building chat bots this week and thought it was time to let you in on the fun.
In this tutorial, you’re gonna learn how to build your first chat AI, package it and deploy it to the Chai Developer Platform & mobile app…. All within 10 minutes!
Getting set up (5 minutes):
- Head over to the Chai Developer Platform .
- Sign in with Google
- Scroll to the bottom to see your “Developer Unique ID” (referred to as developer_uid in this tutorial) and your “Developer Keys”. These will come in handy later!
- Install the iOS or Android app by clicking “get chai” anywhere on https://chai.ml or the developer platform
# Installing the chai_py package. pip install --extra-index-url https://test.pypi.org/simple/ --upgrade chaipy
(Feel free to do this entire thing in our Colab notebook here: Quick AI Tutorial)
First, I’m making a file named bot.py, in a directory named bot.
# bot.py from chai_py import ChaiBot, Update class ChaiNet(ChaiBot): def setup(self): self.logger.info("Setting up...") self.messages_count = 0 async def on_message(self, update: Update) -> str: self.messages_count += 1 if self.messages_count == 1: return "Hey! Nice to meet you :) What's your name?" if self.messages_count == 2: return "My name is ChaiNet. I'm a friend of Sarah Connor." else: return "I'll be back."
If you want more of a rundown on what the above snippet is doing, you can check out the documentation here: https://chai.ml/docs
Let’s test this out!
You can put the following under
if __name__ == "__main__", or just run it in IPython.
from chai_py import TRoom t_room = TRoom([Bot()]) t_room.start()
Now, we’re going to package the class we just made into a zip so that it’s ready to upload.
This is where your
developer_key will come in handy 🔧
from chai_py import package, Metadata DEVELOPER_UID = "get_this_from_chai_developer_platform" TERMINATOR_PIC = "https://upload.wikimedia.org/wikipedia/en/7/70/Terminator1984movieposter.jpg" package( Metadata( name="ChaiNet", image_url= TERMINATOR_PIC, color="f1a2b3", developer_uid=DEVELOPER_UID, description="Hasta la vista, baby.", input_class=ChaiNet, ) )
from chai_py import upload_and_deploy, wait_for_deployment DEVELOPER_UID = "get_this_from_chai_developer_platform" DEVELOPER_KEY = "and_get_this_from_chai_developer_platform" bot_uid = upload_and_deploy( "package.zip", uid=DEVELOPER_UID, key=DEVELOPER_UID ) wait_for_deployment(bot_uid) bot_url = advertise_deployed_bot(bot_uid) print(bot_url)
You’ll see some pretty spinners and colourful logs, letting you know your upload was a success!
bot_url will look something like this:
This is a deep link, and it’ll take you to your bot’s page in our mobile app.
Thanks for reading, have fun building!