Welcome to the fifth course of the AutoGen series.
Previously, we learned how to write high quality content with AI using AutoGen. In this lesson, we’re diving into an exciting and practical example of leveraging conversational agents to build an interactive chess game.
We’ll create two agent players that can make legal moves on a chessboard using tools, and you’ll see how these agents communicate with each other to play the game.
By the end of this article, you’ll have a solid understanding of how to implement a similar setup in your projects.
Let’s jump right in and have some fun!

Setting Up Your Environment
Importing the Necessary Libraries
First things first, let’s set up our environment. We’ll start by importing the necessary configurations and libraries. We’ll be using the GPT-4 Turbo model as our agents, as they are significantly better chess players compared to the GPT-3.
from autogen.utils import get_openai_api_key
from autogen.configs import LLMConfig
from autogen.agents import ConversableAgent
import chess
# Importing the OpenAI API key
api_key = get_openai_api_key()
# Defining LLM Configuration
llm_config = LLMConfig(
model_name='gpt-4-turbo',
api_key=api_key,
temperature=0.7,
max_tokens=150
)Initializing the Chessboard and Tools
We’ll start by initializing the chessboard and defining the necessary tools for our agents to interact with the board. These tools will include functions to get legal moves and make moves.
# Initialize the chessboard
board = chess.Board()
# Variable to keep track of whether a move has been made
move_made = False
# Define the getLegalMoves tool
def get_legal_moves():
return ', '.join([move.uci() for move in board.legal_moves])
# Define the makeMove tool
def make_move(move):
global move_made
move = chess.Move.from_uci(move)
if move in board.legal_moves:
board.push(move)
move_made = True
return f"Moved {board.piece_at(move.from_square)} from {move.uci()[:2]} to {move.uci()[2:]}."
else:
return "Illegal move."Creating the Agents
We’ll create three agents: two player agents (one for each side) and a board agent that will manage the game state and check the legality of moves.
# Player White Agent
player_white = ConversableAgent(
name='PlayerWhite',
llm_config=llm_config,
human_input_mode='never'
)
player_white.set_system_message("You are a chess player. You play as white. First, call getLegalMoves to get a list of legal moves, and then call makeMove to make a move.")
# Player Black Agent
player_black = ConversableAgent(
name='PlayerBlack',
llm_config=llm_config,
human_input_mode='never'
)
player_black.set_system_message("You are a chess player. You play as black. First, call getLegalMoves to get a list of legal moves, and then call makeMove to make a move.")
# Board Agent
board_agent = ConversableAgent(
name='BoardAgent',
llm_config=llm_config,
human_input_mode='never'
)
board_agent.set_system_message("You are the board manager. Check if moves are legal and update the board status.")Registering the Tools
Next, we’ll register the tools with the player agents, allowing them to get legal moves and make moves.
from autogen import register_tool
# Registering tools for Player White
register_tool(player_white, get_legal_moves, 'getLegalMoves', "Get a list of legal moves in UCI format.")
register_tool(player_white, make_move, 'makeMove', "Make a move on the board in UCI format.")
# Registering tools for Player Black
register_tool(player_black, get_legal_moves, 'getLegalMoves', "Get a list of legal moves in UCI format.")
register_tool(player_black, make_move, 'makeMove', "Make a move on the board in UCI format.")Implementing the Game Loop
We’ll now implement a game loop that allows the two players to play against each other, with the board agent managing the game state.
def play_game():
global move_made
while not board.is_game_over():
for player in [player_white, player_black]:
move_made = False
while not move_made:
legal_moves = player.generate_reply("getLegalMoves")
chosen_move = player.generate_reply(f"makeMove {legal_moves.split(',')[0]}")
print(chosen_move)
board_agent.generate_reply("checkMadeMove")
print(board)
play_game()Output for the Chess Game
Here’s what the output might look like for a few moves in the chess game:
Output:
Initial Board:
r n b q k b n r
p p p p p p p p
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
P P P P P P P P
R N B Q K B N R
Player White: getLegalMoves
Player White: makeMove e2e4
Moved P from e2 to e4.
Current Board:
r n b q k b n r
p p p p p p p p
. . . . . . . .
. . . . . . . .
. . . . P . . .
. . . . . . . .
P P P P . P P P
R N B Q K B N R
Player Black: getLegalMoves
Player Black: makeMove e7e5
Moved p from e7 to e5.
Current Board:
r n b q k b n r
p p p p . p p p
. . . . . . . .
. . . . p . . .
. . . . P . . .
. . . . . . . .
P P P P . P P P
R N B Q K B N RConclusion
And there you have it! We’ve built an interactive chess game using AutoGen’s conversational agents.
By defining tools and registering them with the agents, we’ve enabled them to communicate and make legal moves on the chessboard.
This setup not only showcases the power of AutoGen but also opens up endless possibilities for creating interactive applications with conversational agents.
In our next adventure, we will explore adding coding capabilities to your agents and use the generated code to complete a financial analysis task.
You will get to build two agent systems where agents collaborate to solve the task at hand while asking for human feedback.
Let’s continue this exciting journey together and unlock the full potential of multi-agent systems in AI!
Discover more from AI For Developers
Subscribe to get the latest posts sent to your email.