bstraehle commited on
Commit
020fcea
·
verified ·
1 Parent(s): e2b89b9

Update multi_agent.py

Browse files
Files changed (1) hide show
  1. multi_agent.py +44 -44
multi_agent.py CHANGED
@@ -2,51 +2,51 @@ import chess, chess.svg, math
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(self) -> 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(self, 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(self, 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(self, num_moves):
51
  # Each turn includes two moves (one by each player)
52
  # The first move by player black kicks off the chat
@@ -58,7 +58,7 @@ class MultiAgent:
58
  num_turns += 1
59
 
60
  return num_turns
61
-
62
  def run_multi_agent(self, llm_white, llm_black, num_moves):
63
  llm_config_white = {"model": llm_white}
64
  llm_config_black = {"model": llm_black}
@@ -66,7 +66,7 @@ class MultiAgent:
66
  board_proxy = ConversableAgent(
67
  name="Board Proxy",
68
  llm_config=False,
69
- is_termination_msg=self.check_made_move,
70
  default_auto_reply="Please make a move.",
71
  human_input_mode="NEVER",
72
  )
@@ -91,7 +91,7 @@ class MultiAgent:
91
 
92
  for caller in [player_white, player_black]:
93
  register_function(
94
- self.get_legal_moves,
95
  caller=caller,
96
  executor=board_proxy,
97
  name="get_legal_moves",
@@ -99,7 +99,7 @@ class MultiAgent:
99
  )
100
 
101
  register_function(
102
- self.make_move,
103
  caller=caller,
104
  executor=board_proxy,
105
  name="make_move",
 
2
  from autogen import ConversableAgent, register_function
3
  from typing_extensions import Annotated
4
 
5
+ def get_legal_moves() -> Annotated[str, "A list of legal moves in UCI format"]:
6
+ return "Possible moves are: " + ",".join(
7
+ [str(move) for move in board.legal_moves]
8
+ )
9
+
10
+ def make_move(move: Annotated[str, "A move in UCI format."]) -> Annotated[str, "Result of the move."]:
11
+ move = chess.Move.from_uci(move)
12
+ board.push_uci(str(move))
13
+ global made_move
14
+ made_move = True
15
+
16
+ board_svgs.append(chess.svg.board(
17
+ board,
18
+ arrows=[(move.from_square, move.to_square)],
19
+ fill={move.from_square: "gray"},
20
+ size=250
21
+ ))
22
+
23
+ piece = board.piece_at(move.to_square)
24
+ piece_symbol = piece.unicode_symbol()
25
+ piece_name = (
26
+ chess.piece_name(piece.piece_type).capitalize()
27
+ if piece_symbol.isupper()
28
+ else chess.piece_name(piece.piece_type)
29
+ )
30
+
31
+ return f"Moved {piece_name} ({piece_symbol}) from "\
32
+ f"{chess.SQUARE_NAMES[move.from_square]} to "\
33
+ f"{chess.SQUARE_NAMES[move.to_square]}."
34
+
35
+ def check_made_move(msg):
36
+ global made_move
37
+
38
+ if made_move:
39
+ made_move = False
40
+ return True
41
+ else:
42
+ return False
43
+
44
  class MultiAgent:
45
  made_move = False
46
 
47
  board = chess.Board()
48
  board_svgs = []
49
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  def get_num_turns(self, num_moves):
51
  # Each turn includes two moves (one by each player)
52
  # The first move by player black kicks off the chat
 
58
  num_turns += 1
59
 
60
  return num_turns
61
+
62
  def run_multi_agent(self, llm_white, llm_black, num_moves):
63
  llm_config_white = {"model": llm_white}
64
  llm_config_black = {"model": llm_black}
 
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
  )
 
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",
 
99
  )
100
 
101
  register_function(
102
+ make_move,
103
  caller=caller,
104
  executor=board_proxy,
105
  name="make_move",