Next Steps

Specifying dependencies

Specify your dependencies when packaging. Here’s an example of a class which requires the Python module transformers.

package(
Metadata(
name="DialoGPT",
image_url=BOT_IMAGE_URL,
color="f1a2b3",
description="Made with transformers",
input_class=DialoGptBot,
memory=3000,
),
requirements=["transformers[torch]"],
)
Copied!

In this example a larger memory allocation is also set.

Getting the user’s reply

The on_message function takes an Update. This yields the user’s message.

from chai_py import ChaiBot, Update
class EchoBot(ChaiBot):
def setup(self):
self.logger.info("Hello, world!")
async def on_message(self, update: Update) -> str:
return f"Echo: {update.latest_message.text}"
Copied!

The first message, which triggers the bot, can be accessed within the class using self.FIRST_MESSAGE_STRING.

from chai_py import ChaiBot, Update
class EchoBot(ChaiBot):
def setup(self):
self.logger.info("Hello, world!")
async def on_message(self, update: Update) -> str:
user_reply = update.latest_message.text
if user_reply == self.FIRST_MESSAGE_STRING:
return "Hi, I'm EchoBot! Tell me anything."
return f"Echo: {user_reply}"
Copied!

Testing

from chai_py import TRoom
t_room = TRoom([Bot()])
t_room.chat()
Copied!
from chai_py import TRoom
t_room = TRoom([Bot()])
input = ["Hey", "What's up?", "What's your name?"]
t_room.test.test_chat(input)
print(t_room.messages)
for message in t_room.messages:
print(message.content)
Copied!