Custom Chat
Tip
There are some changes worth noting since the recording of this video.
-
The
Chatutility has added a few new options. You can now provideprompt_prefix,response_prefix,exit_keywordsand more.Review the
Chat Utilityreference guide for a full breakdown. -
Agent Output has changed from:
agent_result.output_task.output.valuetoagent_result.output_task.output.value.This change is reflected in the code in the course, but not in the video at this time.
While the chatbot is working, it's not very user-friendly yet. The User: and Assistant: prompts don't make for the most engaging for a user experience.
In this step, we'll implement a manual chat experience, giving us more control over the conversation with our chatbot. We'll remove the Chat utility and create our own custom functions to facilitate interactive and dynamic conversations.
Let's get started!
Remove the Chat Utility
To implement our custom manual chat functionality, we'll remove the dependency on the Chat utility provided by Griptape. We'll no longer need the line from griptape.utils import Chat in our code.
Update the code by commenting out or removing the following line:
Don't forget to remove or comment out the line where we use the Chat utility with the agent at the bottom of the script:
With the Chat utility out of the picture, we're ready to take charge and create our own chat function.
Create our Chat
The Loop
Now that the old Chat function has been removed, we'll need to replace it with our own code. Let's start by with a simple loop that takes the user input until they type exit.
# Keep track of when we're chatting
is_chatting = True
while is_chatting: # While chatting is still true
user_input = input("Chat with kiwi: ")
if user_input == "exit":
is_chatting = False
else:
print(f"Kiwi: Hah! you said: {user_input}!")
If you just run this code on it's own, you'll see that it allows the user to keep entering information over and over again until they type exit.
It's not very amazing, and certainly doesn't interact with the agent yet, so let's modify the code to handle that.
Add the Agent
After the else: statement, change the code to call agent.run():
while is_chatting:
# ... truncated for brevity ... #
else:
agent_result = agent.run(user_input)
print (f"Kiwi: {agent_result.output_task.output.value}")
As you can see now, the agent runs, and we get the output stored in the variable agent_result. We can then print that output by using the output.value attribute.
Chat Function
Create
Let's clean this up a bit and define a custom chat function that will hold all this code instead of placing it at the end of our script.
Here's the code for the chat function and the way we can call it:
# Chat function
def chat(agent):
is_chatting = True
while is_chatting:
user_input = input("Chat with Kiwi: ")
if user_input == "exit":
is_chatting = False
else:
# Keep on chatting
agent_result = agent.run(user_input)
print (f"Kiwi: {agent_result.output_task.output.value}")
Call
Once the chat function has been created, we can just call it and pass the agent.
The chat function takes the agent as an argument.
You shouldn't notice any difference to how you ran this before, it's just a bit cleaner.
Engage in stimulating conversations, explore various topics, and enjoy the interactive experience as you communicate with your chatbot.
Code Checkpoint
We made a lot of important changes in this stage. Before we move forward, let's compare code. Changed lines are highlighted.
Next Steps
Congratulations on implementing manual chat functionality and taking control of the conversation! In the next section: Manners Maketh the Bot, we'll give the bot some manners and create our own Agent class to make working with the agent more consistent.