Update multi_agent.py
Browse files- multi_agent.py +150 -151
multi_agent.py
CHANGED
@@ -2,168 +2,167 @@ import chess, chess.svg, math
|
|
2 |
from autogen import ConversableAgent, register_function
|
3 |
from typing_extensions import Annotated
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
board = chess.Board()
|
8 |
-
board_svgs = []
|
9 |
-
|
10 |
-
def get_legal_moves() -> Annotated[str, "A list of legal moves in UCI format"]:
|
11 |
-
return "Possible moves are: " + ",".join(
|
12 |
-
[str(move) for move in board.legal_moves]
|
13 |
-
)
|
14 |
-
|
15 |
-
def make_move(move: Annotated[str, "A move in UCI format."]) -> Annotated[str, "Result of the move."]:
|
16 |
-
move = chess.Move.from_uci(move)
|
17 |
-
board.push_uci(str(move))
|
18 |
-
global made_move
|
19 |
-
made_move = True
|
20 |
-
|
21 |
-
board_svgs.append(chess.svg.board(
|
22 |
-
board,
|
23 |
-
arrows=[(move.from_square, move.to_square)],
|
24 |
-
fill={move.from_square: "gray"},
|
25 |
-
size=250
|
26 |
-
))
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
piece_name = (
|
31 |
-
chess.piece_name(piece.piece_type).capitalize()
|
32 |
-
if piece_symbol.isupper()
|
33 |
-
else chess.piece_name(piece.piece_type)
|
34 |
-
)
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
def check_made_move(msg):
|
41 |
-
global made_move
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
def get_num_turns(num_moves):
|
50 |
-
# Each turn includes two moves (one by each player)
|
51 |
-
# The first move by player black kicks off the chat
|
52 |
-
# The first move by player white starts the game
|
53 |
-
|
54 |
-
num_turns = math.ceil(num_moves / 2)
|
55 |
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
human_input_mode="NEVER",
|
71 |
-
)
|
72 |
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
)
|
82 |
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
"Then call make_move(move) to make a move. "
|
88 |
-
"After a move is made, analyze the move in 3 bullet points. Respond in format **Analysis:** move from/to, unordered list, your turn player white.",
|
89 |
-
#"Then continue playing.",
|
90 |
-
llm_config=llm_config_black,
|
91 |
-
)
|
92 |
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
)
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
|
|
108 |
)
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
]
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
143 |
)
|
144 |
-
except Exception as e:
|
145 |
-
print(f"Error: {e}")
|
146 |
-
finally:
|
147 |
-
if chat_result != None:
|
148 |
-
chat_history = chat_result.chat_history
|
149 |
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
for chat in chat_history:
|
154 |
-
player = ""
|
155 |
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
|
|
|
|
|
|
|
|
|
|
163 |
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
from autogen import ConversableAgent, register_function
|
3 |
from typing_extensions import Annotated
|
4 |
|
5 |
+
class MultiAgent
|
6 |
+
made_move = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
+
board = chess.Board()
|
9 |
+
board_svgs = []
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
def get_legal_moves() -> Annotated[str, "A list of legal moves in UCI format"]:
|
12 |
+
return "Possible moves are: " + ",".join(
|
13 |
+
[str(move) for move in board.legal_moves]
|
14 |
+
)
|
|
|
|
|
15 |
|
16 |
+
def make_move(move: Annotated[str, "A move in UCI format."]) -> Annotated[str, "Result of the move."]:
|
17 |
+
move = chess.Move.from_uci(move)
|
18 |
+
board.push_uci(str(move))
|
19 |
+
global made_move
|
20 |
+
made_move = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
+
board_svgs.append(chess.svg.board(
|
23 |
+
board,
|
24 |
+
arrows=[(move.from_square, move.to_square)],
|
25 |
+
fill={move.from_square: "gray"},
|
26 |
+
size=250
|
27 |
+
))
|
28 |
|
29 |
+
piece = board.piece_at(move.to_square)
|
30 |
+
piece_symbol = piece.unicode_symbol()
|
31 |
+
piece_name = (
|
32 |
+
chess.piece_name(piece.piece_type).capitalize()
|
33 |
+
if piece_symbol.isupper()
|
34 |
+
else chess.piece_name(piece.piece_type)
|
35 |
+
)
|
36 |
+
|
37 |
+
return f"Moved {piece_name} ({piece_symbol}) from "\
|
38 |
+
f"{chess.SQUARE_NAMES[move.from_square]} to "\
|
39 |
+
f"{chess.SQUARE_NAMES[move.to_square]}."
|
|
|
|
|
40 |
|
41 |
+
def check_made_move(msg):
|
42 |
+
global made_move
|
43 |
+
|
44 |
+
if made_move:
|
45 |
+
made_move = False
|
46 |
+
return True
|
47 |
+
else:
|
48 |
+
return False
|
|
|
49 |
|
50 |
+
def get_num_turns(num_moves):
|
51 |
+
# Each turn includes two moves (one by each player)
|
52 |
+
# The first move by player black kicks off the chat
|
53 |
+
# The first move by player white starts the game
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
+
num_turns = math.ceil(num_moves / 2)
|
56 |
+
|
57 |
+
if num_moves % 2 == 0:
|
58 |
+
num_turns += 1
|
59 |
+
|
60 |
+
return num_turns
|
61 |
+
|
62 |
+
def run_multi_agent(llm_white, llm_black, num_moves):
|
63 |
+
llm_config_white = {"model": llm_white}
|
64 |
+
llm_config_black = {"model": llm_black}
|
65 |
+
|
66 |
+
board_proxy = ConversableAgent(
|
67 |
+
name="Board Proxy",
|
68 |
+
llm_config=False,
|
69 |
+
is_termination_msg=check_made_move,
|
70 |
+
default_auto_reply="Please make a move.",
|
71 |
+
human_input_mode="NEVER",
|
72 |
)
|
73 |
+
|
74 |
+
player_white = ConversableAgent(
|
75 |
+
name="Player White",
|
76 |
+
system_message="You are a chess player and you play as white. "
|
77 |
+
"First call get_legal_moves(), to get a list of legal moves. "
|
78 |
+
"Then call make_move(move) to make a move. "
|
79 |
+
"After a move is made, analyze the move in 3 bullet points. Respond in format **Analysis:** move from/to, unordered list, your turn player black.",
|
80 |
+
llm_config=llm_config_white,
|
81 |
)
|
82 |
+
|
83 |
+
player_black = ConversableAgent(
|
84 |
+
name="Player Black",
|
85 |
+
system_message="You are a chess player and you play as black. "
|
86 |
+
"First call get_legal_moves(), to get a list of legal moves. "
|
87 |
+
"Then call make_move(move) to make a move. "
|
88 |
+
"After a move is made, analyze the move in 3 bullet points. Respond in format **Analysis:** move from/to, unordered list, your turn player white.",
|
89 |
+
llm_config=llm_config_black,
|
90 |
+
)
|
91 |
+
|
92 |
+
for caller in [player_white, player_black]:
|
93 |
+
register_function(
|
94 |
+
get_legal_moves,
|
95 |
+
caller=caller,
|
96 |
+
executor=board_proxy,
|
97 |
+
name="get_legal_moves",
|
98 |
+
description="Call this tool to get legal moves.",
|
99 |
+
)
|
100 |
+
|
101 |
+
register_function(
|
102 |
+
make_move,
|
103 |
+
caller=caller,
|
104 |
+
executor=board_proxy,
|
105 |
+
name="make_move",
|
106 |
+
description="Call this tool to make a move.",
|
107 |
+
)
|
108 |
+
|
109 |
+
player_white.register_nested_chats(
|
110 |
+
trigger=player_black,
|
111 |
+
chat_queue=[
|
112 |
+
{
|
113 |
+
"sender": board_proxy,
|
114 |
+
"recipient": player_white,
|
115 |
+
"summary_method": "last_msg",
|
116 |
+
"silent": False,
|
117 |
+
}
|
118 |
+
],
|
119 |
+
)
|
120 |
+
|
121 |
+
player_black.register_nested_chats(
|
122 |
+
trigger=player_white,
|
123 |
+
chat_queue=[
|
124 |
+
{
|
125 |
+
"sender": board_proxy,
|
126 |
+
"recipient": player_black,
|
127 |
+
"summary_method": "last_msg",
|
128 |
+
"silent": False,
|
129 |
+
}
|
130 |
+
],
|
131 |
)
|
|
|
|
|
|
|
|
|
|
|
132 |
|
133 |
+
chat_result = None
|
134 |
+
chat_history = []
|
|
|
|
|
|
|
135 |
|
136 |
+
try:
|
137 |
+
chat_result = player_black.initiate_chat(
|
138 |
+
player_white,
|
139 |
+
message="Let's play chess!",
|
140 |
+
max_turns=get_num_turns(num_moves),
|
141 |
+
verbose=True
|
142 |
+
)
|
143 |
+
except Exception as e:
|
144 |
+
print(f"Error: {e}")
|
145 |
+
finally:
|
146 |
+
if chat_result != None:
|
147 |
+
chat_history = chat_result.chat_history
|
148 |
|
149 |
+
result = ""
|
150 |
+
num_move = 0
|
151 |
+
|
152 |
+
for chat in chat_history:
|
153 |
+
player = ""
|
154 |
+
|
155 |
+
if num_move % 2 == 0:
|
156 |
+
player = "Player Black"
|
157 |
+
else:
|
158 |
+
player = "Player White"
|
159 |
+
|
160 |
+
if num_move > 0:
|
161 |
+
result += f"**{player}, Move {num_move}**<br>{chat.get('content')}<br>{board_svgs[num_move - 1]}<br><br>"
|
162 |
+
|
163 |
+
num_move += 1
|
164 |
+
|
165 |
+
if num_moves % 2 == 0 and num_move == num_moves + 1:
|
166 |
+
break
|
167 |
+
|
168 |
+
return result
|