bstraehle commited on
Commit
57c16d1
1 Parent(s): 755c2f1

Update multi_agent.py

Browse files
Files changed (1) hide show
  1. 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
- made_move = False
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
- piece = board.piece_at(move.to_square)
29
- piece_symbol = piece.unicode_symbol()
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
- return f"Moved {piece_name} ({piece_symbol}) from "\
37
- f"{chess.SQUARE_NAMES[move.from_square]} to "\
38
- f"{chess.SQUARE_NAMES[move.to_square]}."
39
-
40
- def check_made_move(msg):
41
- global made_move
42
 
43
- if made_move:
44
- made_move = False
45
- return True
46
- else:
47
- return False
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
- if num_moves % 2 == 0:
57
- num_turns += 1
 
 
 
 
58
 
59
- return num_turns
60
-
61
- def run_multi_agent(llm_white, llm_black, num_moves):
62
- llm_config_white = {"model": llm_white}
63
- llm_config_black = {"model": llm_black}
64
-
65
- board_proxy = ConversableAgent(
66
- name="Board Proxy",
67
- llm_config=False,
68
- is_termination_msg=check_made_move,
69
- default_auto_reply="Please make a move.",
70
- human_input_mode="NEVER",
71
- )
72
 
73
- player_white = ConversableAgent(
74
- name="Player White",
75
- system_message="You are a chess player and you play as white. "
76
- "First call get_legal_moves(), to get a list of legal moves. "
77
- "Then call make_move(move) to make a move. "
78
- "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.",
79
- #"Then continue playing.",
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
- #"Then continue playing.",
90
- llm_config=llm_config_black,
91
- )
92
 
93
- for caller in [player_white, player_black]:
94
- register_function(
95
- get_legal_moves,
96
- caller=caller,
97
- executor=board_proxy,
98
- name="get_legal_moves",
99
- description="Call this tool to get legal moves.",
 
 
 
 
 
 
 
 
 
 
100
  )
101
-
102
- register_function(
103
- make_move,
104
- caller=caller,
105
- executor=board_proxy,
106
- name="make_move",
107
- description="Call this tool to make a move.",
 
108
  )
109
-
110
- player_white.register_nested_chats(
111
- trigger=player_black,
112
- chat_queue=[
113
- {
114
- "sender": board_proxy,
115
- "recipient": player_white,
116
- "summary_method": "last_msg",
117
- "silent": False,
118
- }
119
- ],
120
- )
121
-
122
- player_black.register_nested_chats(
123
- trigger=player_white,
124
- chat_queue=[
125
- {
126
- "sender": board_proxy,
127
- "recipient": player_black,
128
- "summary_method": "last_msg",
129
- "silent": False,
130
- }
131
- ],
132
- )
133
-
134
- chat_result = None
135
- chat_history = []
136
-
137
- try:
138
- chat_result = player_black.initiate_chat(
139
- player_white,
140
- message="Let's play chess!",
141
- max_turns=get_num_turns(num_moves),
142
- verbose=True
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- result = ""
151
- num_move = 0
152
-
153
- for chat in chat_history:
154
- player = ""
155
 
156
- if num_move % 2 == 0:
157
- player = "Player Black"
158
- else:
159
- player = "Player White"
160
-
161
- if num_move > 0:
162
- result += f"**{player}, Move {num_move}**<br>{chat.get('content')}<br>{board_svgs[num_move - 1]}<br><br>"
 
 
 
 
 
163
 
164
- num_move += 1
165
-
166
- if num_moves % 2 == 0 and num_move == num_moves + 1:
167
- break
168
-
169
- return result
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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