Update multi_agent.py
Browse files- multi_agent.py +10 -58
multi_agent.py
CHANGED
@@ -5,19 +5,13 @@ from typing_extensions import Annotated
|
|
5 |
board = None
|
6 |
board_svgs = None
|
7 |
made_move = None
|
8 |
-
game_over = False
|
9 |
-
move_num = 0
|
10 |
|
11 |
def get_legal_moves() -> Annotated[str, "A list of legal moves in UCI format"]:
|
12 |
-
print("###### get_legal_moves")
|
13 |
return "Possible moves are: " + ",".join(
|
14 |
[str(move) for move in board.legal_moves]
|
15 |
)
|
16 |
|
17 |
def make_move(move: Annotated[str, "A move in UCI format."]) -> Annotated[str, "Result of the move."]:
|
18 |
-
global move_num
|
19 |
-
move_num += 1
|
20 |
-
print("###### make_move: " + str(move_num))
|
21 |
move = chess.Move.from_uci(move)
|
22 |
board.push_uci(str(move))
|
23 |
global made_move
|
@@ -42,33 +36,15 @@ def make_move(move: Annotated[str, "A move in UCI format."]) -> Annotated[str, "
|
|
42 |
f"{chess.SQUARE_NAMES[move.from_square]} to "\
|
43 |
f"{chess.SQUARE_NAMES[move.to_square]}."
|
44 |
|
45 |
-
def set_game_over():
|
46 |
-
print("###### set_game_over")
|
47 |
-
global game_over
|
48 |
-
|
49 |
-
game_over = True
|
50 |
-
|
51 |
def check_made_move(msg):
|
52 |
global made_move
|
53 |
-
|
54 |
-
print("###### check_made_move: " + str(made_move))
|
55 |
-
|
56 |
if made_move:
|
57 |
made_move = False
|
58 |
return True
|
59 |
else:
|
60 |
return False
|
61 |
|
62 |
-
def check_game_over(msg):
|
63 |
-
global game_over
|
64 |
-
|
65 |
-
print("###### check_game_over: " + str(game_over))
|
66 |
-
|
67 |
-
if game_over:
|
68 |
-
return True
|
69 |
-
else:
|
70 |
-
return False
|
71 |
-
|
72 |
def get_num_turns(num_moves):
|
73 |
# Each turn includes two moves (one by each player)
|
74 |
# The first move by player black kicks off the chat
|
@@ -95,46 +71,30 @@ def run_multi_agent(llm_white, llm_black, num_moves):
|
|
95 |
|
96 |
board_proxy = ConversableAgent(
|
97 |
name="Board Proxy",
|
98 |
-
llm_config=
|
99 |
is_termination_msg=check_made_move,
|
100 |
default_auto_reply="Please make a move.",
|
101 |
human_input_mode="NEVER",
|
102 |
-
system_message = "Your loyal assistant! Reply 'TERMINATE' when no legal move is possible."
|
103 |
)
|
104 |
|
105 |
player_white = ConversableAgent(
|
106 |
name="Player White",
|
107 |
system_message="You are a chess Grandmaster and you play as white. "
|
108 |
"First call get_legal_moves(), to get a list of legal moves. "
|
109 |
-
"
|
110 |
-
"
|
111 |
-
#"Analyze the move in 3 bullet points. Respond in format **Analysis:** move in UCI format (emoji of piece), unordered list.",
|
112 |
llm_config=llm_config_white,
|
113 |
-
human_input_mode="NEVER",
|
114 |
-
is_termination_msg=check_game_over
|
115 |
)
|
116 |
|
117 |
player_black = ConversableAgent(
|
118 |
name="Player Black",
|
119 |
system_message="You are a chess Grandmaster and you play as black. "
|
120 |
"First call get_legal_moves(), to get a list of legal moves. "
|
121 |
-
"
|
122 |
-
"
|
123 |
-
#"Analyze the move in 3 bullet points. Respond in format **Analysis:** move in UCI format (emoji of piece), unordered list.",
|
124 |
llm_config=llm_config_black,
|
125 |
-
human_input_mode="NEVER",
|
126 |
-
is_termination_msg=check_game_over
|
127 |
)
|
128 |
-
|
129 |
-
msg1 = player_white.system_message
|
130 |
-
print("msg1=" + msg1)
|
131 |
-
|
132 |
-
msg2 = player_black.system_message
|
133 |
-
print("msg2=" + msg2)
|
134 |
-
|
135 |
-
msg3 = board_proxy.system_message
|
136 |
-
print("msg3=" + msg3)
|
137 |
-
|
138 |
for caller in [player_white, player_black]:
|
139 |
register_function(
|
140 |
get_legal_moves,
|
@@ -151,14 +111,6 @@ def run_multi_agent(llm_white, llm_black, num_moves):
|
|
151 |
name="make_move",
|
152 |
description="Call this tool to make a move.",
|
153 |
)
|
154 |
-
|
155 |
-
register_function(
|
156 |
-
set_game_over,
|
157 |
-
caller=caller,
|
158 |
-
executor=board_proxy,
|
159 |
-
name="set_game_over",
|
160 |
-
description="Call this tool to end the game.",
|
161 |
-
)
|
162 |
|
163 |
player_white.register_nested_chats(
|
164 |
trigger=player_black,
|
@@ -219,8 +171,8 @@ def run_multi_agent(llm_white, llm_black, num_moves):
|
|
219 |
if num_moves % 2 == 0 and num_move == num_moves + 1:
|
220 |
break
|
221 |
|
222 |
-
print("===")
|
223 |
-
print(result)
|
224 |
-
print("===")
|
225 |
|
226 |
return result
|
|
|
5 |
board = None
|
6 |
board_svgs = None
|
7 |
made_move = None
|
|
|
|
|
8 |
|
9 |
def get_legal_moves() -> Annotated[str, "A list of legal moves in UCI format"]:
|
|
|
10 |
return "Possible moves are: " + ",".join(
|
11 |
[str(move) for move in board.legal_moves]
|
12 |
)
|
13 |
|
14 |
def make_move(move: Annotated[str, "A move in UCI format."]) -> Annotated[str, "Result of the move."]:
|
|
|
|
|
|
|
15 |
move = chess.Move.from_uci(move)
|
16 |
board.push_uci(str(move))
|
17 |
global made_move
|
|
|
36 |
f"{chess.SQUARE_NAMES[move.from_square]} to "\
|
37 |
f"{chess.SQUARE_NAMES[move.to_square]}."
|
38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
def check_made_move(msg):
|
40 |
global made_move
|
41 |
+
|
|
|
|
|
42 |
if made_move:
|
43 |
made_move = False
|
44 |
return True
|
45 |
else:
|
46 |
return False
|
47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
def get_num_turns(num_moves):
|
49 |
# Each turn includes two moves (one by each player)
|
50 |
# The first move by player black kicks off the chat
|
|
|
71 |
|
72 |
board_proxy = ConversableAgent(
|
73 |
name="Board Proxy",
|
74 |
+
llm_config=False,
|
75 |
is_termination_msg=check_made_move,
|
76 |
default_auto_reply="Please make a move.",
|
77 |
human_input_mode="NEVER",
|
|
|
78 |
)
|
79 |
|
80 |
player_white = ConversableAgent(
|
81 |
name="Player White",
|
82 |
system_message="You are a chess Grandmaster and you play as white. "
|
83 |
"First call get_legal_moves(), to get a list of legal moves. "
|
84 |
+
"Then call make_move(move) to make a legal move. "
|
85 |
+
"Analyze the move in 3 bullet points. Respond in format **Analysis:** move in UCI format (piece emoji), unordered list.",
|
|
|
86 |
llm_config=llm_config_white,
|
|
|
|
|
87 |
)
|
88 |
|
89 |
player_black = ConversableAgent(
|
90 |
name="Player Black",
|
91 |
system_message="You are a chess Grandmaster and you play as black. "
|
92 |
"First call get_legal_moves(), to get a list of legal moves. "
|
93 |
+
"Then call make_move(move) to make a legal move. "
|
94 |
+
"Analyze the move in 3 bullet points. Respond in format **Analysis:** move in UCI format (piece emoji), unordered list.",
|
|
|
95 |
llm_config=llm_config_black,
|
|
|
|
|
96 |
)
|
97 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
for caller in [player_white, player_black]:
|
99 |
register_function(
|
100 |
get_legal_moves,
|
|
|
111 |
name="make_move",
|
112 |
description="Call this tool to make a move.",
|
113 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
|
115 |
player_white.register_nested_chats(
|
116 |
trigger=player_black,
|
|
|
171 |
if num_moves % 2 == 0 and num_move == num_moves + 1:
|
172 |
break
|
173 |
|
174 |
+
#print("===")
|
175 |
+
#print(result)
|
176 |
+
#print("===")
|
177 |
|
178 |
return result
|