š„ 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):
- 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
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()
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!