AI - 2 Chatbots Debating Each Other (Difficulty: 4)
-
Introduction
Sometimes you need to use more than one chatbots in your project, so that each chatbot will take on a different role or work on a different task. In this tutorial, you will learn to make 2 chatbots debate each other on any topic the user specifies.
Step 1 - Set up the stage
Create a new project in the CreatiCode playground, and remove the dog sprite. Then select a background for the stage. You can create an interesting background using the AI image generator. For example, you can use this prompt: 2 cute robots having a debate behind podiums on a stage, cartoon style. And you will get a background image like this:
Step 2 - Add an input box for the topic
Create an input box on the top left for the user to input a debate topic:
Here is the widget block you can use. Feel free to adjust the parameters.
Step 3 - Set the initial value for the input box
Since we will be testing our program many times during development, we don’t want to type in the topic every time. Therefore, we can set a default topic like this:
Step 4 - Add 2 Buttons
Next, please add 2 buttons to the right: “Start” and “Continue”:
Here are the blocks for them:
Step 5 - Add a chat window when the Start button is clicked
When the user click the Start button, we will add a chat window widget. However, we do not want to show the input box at the bottom of the chat window, since both parties debating are AI chatbots, so no human input is needed.
One simple solution is to move the chat window lower, so its bottom section is hidden below the bottom edge of the stage:
Below is the block for adding the chat window that way:
Step 6 - Write the common instruction
Now we will start to write the first prompt that will be given to both chatbots. Since the prompt will be very similar for both chatbots, we can store it in a variable named “instruction”:
Step 7 - Write the Pro side prompt
Next, let’s write the prompt for the first chatbot, which will debate on the “pro” side:
Note that we are asking the chatbot to use less than 100 words since we are just developing the program. You can change it later if you would like a more in-depth debate.
Step 8 - Call the first chatbot
Now we are ready to get the opening statement from the first chatbot. We will need to add 2 AI blocks:
Explanations:- The “select chatbot” block will ensure that the request below it will be sent to the chatbot with ID of 1. The ID can be 1 to 4, so there can be at most 4 chatbots.
- We are using the “LLM model” block with a “small” size. This is a relatively small AI model that runs very fast and is smart enough for many use cases.
- We are using the “waiting” mode, so this block will not finish until we get the response from the AI model and store it in the “result” variable.
- We are using a max length of 1000, which means 1000 tokens, or about 750 words. That’s more than enough, since we are asking the chatbot to use less than 100 words in our request.
- We are using a temperature of 1, which makes the chatbot the most creative.
- We are starting a “new chat” session, since this is the first request and we do not need to fetch any chat messages before this one.
Step 9 - Display the pro side opening statement
Once we have received the opening statement, we will add it to the chat window. We will name this chatbot the “Pro” bot, and display the speech as white text over green background on the left:
This is what it looks like when we click the Start button:
Step 10 - The con-side request
Next we will write the prompt for getting the opening statement of the con side. One difference from the pro side is that now we have “heard” the pro side opening statement (still stored in the “result” variable), so that should be inserted into the request as well:
Step 11 - Call the second chatbot
Now we are ready to get the opening statement from the second chatbot. The 2 blocks are very similar:
Explanations:
- We are selecting the chatbot with ID of 2, so any chatbot requests after this block will be sent to the second chatbot.
- We are still using the “small” model.
- This is still a “new chat”, since this is the first message we send to the second chatbot. Note that even if we set it to “continue” session, it would not pick up the response from chatbot 1, since they are 2 separate chatbots.
Step 12 - Display the con-side opening statement
Now we can add the response from the second chatbot to the chat window. This time we will make it aligned to the right, and show the text as white over purple:
Now when we press the Start button, we will get opening statements from both sides:
Step 13 - Refactor the code
Before we continue to more chats, this is a good time to refactor our code a bit. It is already clear that every time we ask a chatbot to say something, we will always use these 3 blocks:
- select a chatbot
- ask the chatbot to generate response
- add its output to the chat window.
Therefore, we can wrap these 3 blocks into a custom block. The only changing part is the actual request we send, so that can be specified as a block input.
For example, for the first chatbot, we can define a new block like this. Note that the request being used is the input block, not the “request” variable any more.
Similarly, we can define another block for the con side chatbot:
Now we can rewrite our main logic using these 2 new blocks like this:
Step 14 - Start the cross-examination
After the opening statements, our debate will enter the “cross-examination” phase, in which the pro side asks the con side questions.
When the user clicks the “Continue” button, we need to compose a new request to the pro side chatbot with the following information:
- The content of the opening statement from the con side
- The debate is entering the cross-examination phase
- Now the pro chatbot should ask the first question
Can you try to compose this request?
Below is an example request that’s concise and clear:
Note that it is very convenient to reuse the “pro chatbot” block. Now, if we click the Continue button, the pro chatbot will ask a question:
Step 15 - The “new chat” session type
There is an issue we need to fix before continuing. Currently, in the “pro chatbot” block’s definition, we are using the “new chat” session type when we use the AI chatbot. This will cause an issue, since every time we run the “pro chatbot” block, it will start a new chat session, and will not pick up any of the previous messages in the debate.
For example, when we run the Continue button, the pro chatbot will start a new session. It will see the con side’s opening statement because we include it in our request this time. However, it will not see its own opening statement, because we are starting a new session with no history.
To fix this issue, we simply need to change the session type to “continue”. Here we are using a special feature of the chatbot block: when the green flag button is clicked, the first time this block is used, even if we specify “continue”, it will always be a new session with no history. Therefore, it is safe to use “continue” type all the time, so long as the user start the program by clicking the green flag.
We can change both blocks like this:
Step 16 - Get the con side’s answer
Next, let’s ask the con side chatbot to answer the question:
When we click the “Continue” button, we get a question and then an answer:
Step 17 - Add a “phase” variable
At this time, the pro side has asked a question and the con side has answered it. If we press the Continue button again, we want them to do another round of Q&A. However, the request has to be a bit different since we no longer need to say, “Now you are in cross-examination,” or provide the opening statements.
Essentially, we want a different request to be sent based on whether we are entering the cross-examination the first time or not. We can use a new variable “phase” to keep track of the current phase of the debate.
First, when the Start button is pressed, we will set the phase to “Opening”:
Second, when the Continue button is clicked, we will check if we are in the “Opening” phase or not. If we are, then we set the phase to “QA”, and add our existing logic to that branch:
Step 18 - New round of QA
Now, we are ready to add the logic to make the 2 chatbots go through another Q&A session whenever we click the “Continue” button. The request will be simpler, since based on the chat history, both chatbots already know they are in the cross-examination phase.
Test it by clicking the “Continue” button, and we can keep going forever if we want to. Here is the complete demo:
Challenges
There are many ways to extend this project. Here are some examples:
-
Closing Statement: Add another button, which will trigger both chatbots to give a closing statement.
-
AI Judge: Add another button, which will invoke a third chatbot to serve as the judge, and evaluate both sides on their performance. Note that to make this happen, you would have to store all the messages from both chatbots in a list or table.
-
Other Debate Formats: you can modify the flow to use other formats for debate competitions, such as Lincoln-Douglas Debate, Public Forum Debate, Congressional Debate, etc.
-