Mission Chai

šŸ”„ Your First Chat AI in 10 minutes šŸŽ‰ šŸ’ā€ā™€ļø

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):

  1. Head over to the Chai Developer Platform .
  2. Sign in with Google
  3. 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!
  4. Install the iOS or Android app by clicking ā€œget chaiā€ anywhere on https://chai.ml or the developer platform

Installation

# 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)

Creating your first Chat AI

A simple one

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

Chatting with the bot

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()

alt text

Packaging

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_uid and 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,
    )
)

Deploying and Uploading

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:

chai://chai.ml/_bot_really-long-code-8910

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!