Update multi_agent.py
Browse files- multi_agent.py +126 -0
multi_agent.py
CHANGED
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import chess, chess.svg, getpass, os
|
2 |
+
from autogen import ConversableAgent, register_function
|
3 |
+
from IPython.display import SVG, display
|
4 |
+
from typing_extensions import Annotated
|
5 |
+
|
6 |
+
made_move = False
|
7 |
+
|
8 |
+
board = chess.Board()
|
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 |
+
display(SVG(chess.svg.board(
|
22 |
+
board,
|
23 |
+
arrows=[(move.from_square, move.to_square)],
|
24 |
+
fill={move.from_square: "gray"},
|
25 |
+
size=200
|
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 |
+
return f"Moved {piece_name} ({piece_symbol}) from "\
|
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 |
+
if made_move:
|
42 |
+
made_move = False
|
43 |
+
return True
|
44 |
+
else:
|
45 |
+
return False
|
46 |
+
|
47 |
+
def run_multi_agent(llm):
|
48 |
+
llm_config = {"model": llm}
|
49 |
+
|
50 |
+
board_proxy = ConversableAgent(
|
51 |
+
name="Board Proxy",
|
52 |
+
llm_config=False,
|
53 |
+
is_termination_msg=check_made_move,
|
54 |
+
default_auto_reply="Please make a move.",
|
55 |
+
human_input_mode="NEVER",
|
56 |
+
)
|
57 |
+
|
58 |
+
player_white = ConversableAgent(
|
59 |
+
name="Player White",
|
60 |
+
system_message="You are a chess player and you play as white. "
|
61 |
+
"First call get_legal_moves(), to get a list of legal moves. "
|
62 |
+
"Then call make_move(move) to make a move. "
|
63 |
+
"After a move is made, chitchat about the move in chess jargon.",
|
64 |
+
llm_config=llm_config,
|
65 |
+
)
|
66 |
+
|
67 |
+
player_black = ConversableAgent(
|
68 |
+
name="Player Black",
|
69 |
+
system_message="You are a chess player and you play as black. "
|
70 |
+
"First call get_legal_moves(), to get a list of legal moves. "
|
71 |
+
"Then call make_move(move) to make a move. "
|
72 |
+
"After a move is made, chitchat about the move in chess jargon.",
|
73 |
+
llm_config=llm_config,
|
74 |
+
)
|
75 |
+
|
76 |
+
for caller in [player_white, player_black]:
|
77 |
+
register_function(
|
78 |
+
get_legal_moves,
|
79 |
+
caller=caller,
|
80 |
+
executor=board_proxy,
|
81 |
+
name="get_legal_moves",
|
82 |
+
description="Get legal moves.",
|
83 |
+
)
|
84 |
+
|
85 |
+
register_function(
|
86 |
+
make_move,
|
87 |
+
caller=caller,
|
88 |
+
executor=board_proxy,
|
89 |
+
name="make_move",
|
90 |
+
description="Call this tool to make a move.",
|
91 |
+
)
|
92 |
+
|
93 |
+
player_white.register_nested_chats(
|
94 |
+
trigger=player_black,
|
95 |
+
chat_queue=[
|
96 |
+
{
|
97 |
+
"sender": board_proxy,
|
98 |
+
"recipient": player_white,
|
99 |
+
"summary_method": "last_msg",
|
100 |
+
"silent": True,
|
101 |
+
}
|
102 |
+
],
|
103 |
+
)
|
104 |
+
|
105 |
+
player_black.register_nested_chats(
|
106 |
+
trigger=player_white,
|
107 |
+
chat_queue=[
|
108 |
+
{
|
109 |
+
"sender": board_proxy,
|
110 |
+
"recipient": player_black,
|
111 |
+
"summary_method": "last_msg",
|
112 |
+
"silent": True,
|
113 |
+
}
|
114 |
+
],
|
115 |
+
)
|
116 |
+
|
117 |
+
chat_result = player_black.initiate_chat(
|
118 |
+
player_white,
|
119 |
+
message="Let's play chess! Your move.",
|
120 |
+
max_turns=3,
|
121 |
+
verbose=False
|
122 |
+
)
|
123 |
+
|
124 |
+
print(chat_result)
|
125 |
+
|
126 |
+
return chat_result
|