bstraehle commited on
Commit
716d63d
·
verified ·
1 Parent(s): cdffa96

Update multi_agent.py

Browse files
Files changed (1) hide show
  1. multi_agent.py +12 -4
multi_agent.py CHANGED
@@ -15,8 +15,6 @@ from langchain_openai import ChatOpenAI
15
 
16
  from langgraph.graph import StateGraph, END
17
 
18
- num_moves = 0
19
-
20
  class AgentState(TypedDict):
21
  messages: Annotated[Sequence[BaseMessage], operator.add]
22
  next: str
@@ -51,6 +49,8 @@ def agent_node(state, agent, name):
51
 
52
  board = None
53
  board_svgs = None
 
 
54
 
55
  @tool
56
  def get_legal_moves() -> Annotated[str, "A list of legal moves in UCI format"]:
@@ -71,6 +71,12 @@ def make_move(move: Annotated[str, "A move in UCI format."]) -> Annotated[str, "
71
  The input should always be a move in UCI format,
72
  and this function will always return the result of the move in UCI format."""
73
  try:
 
 
 
 
 
 
74
  move = chess.Move.from_uci(move)
75
  board.push_uci(str(move))
76
 
@@ -171,7 +177,7 @@ def create_graph(llm_board, llm_white, llm_black):
171
  "You are a chess Grandmaster and you play as white. "
172
  "1. First call get_legal_moves(), to get a list of legal moves. "
173
  "2. If no legal move is possible, reply FINISH. "
174
- "3. Else choose a move returned in step one and call make_move(move). "
175
  "4. Analyze the move in 3 bullet points. Respond in format **Analysis:** move in UCI format (piece emoji), unordered list.")
176
  player_white_node = functools.partial(agent_node, agent=player_white_agent, name="player_white")
177
 
@@ -179,7 +185,7 @@ def create_graph(llm_board, llm_white, llm_black):
179
  "You are a chess Grandmaster and you play as black. "
180
  "1. First call get_legal_moves(), to get a list of legal moves. "
181
  "2. If no legal move is possible, reply FINISH. "
182
- "3. Else choose a move returned in step one and call make_move(move). "
183
  "4. Analyze the move in 3 bullet points. Respond in format **Analysis:** move in UCI format (piece emoji), unordered list.")
184
  player_black_node = functools.partial(agent_node, agent=player_black_agent, name="player_black")
185
 
@@ -204,6 +210,8 @@ def run_multi_agent(llm_board, llm_white, llm_black, moves_num):
204
 
205
  num_moves = moves_num
206
 
 
 
207
  graph = create_graph(llm_board, llm_white, llm_black)
208
 
209
  result = ""
 
15
 
16
  from langgraph.graph import StateGraph, END
17
 
 
 
18
  class AgentState(TypedDict):
19
  messages: Annotated[Sequence[BaseMessage], operator.add]
20
  next: str
 
49
 
50
  board = None
51
  board_svgs = None
52
+ num_moves = 0
53
+ move_num = 0
54
 
55
  @tool
56
  def get_legal_moves() -> Annotated[str, "A list of legal moves in UCI format"]:
 
71
  The input should always be a move in UCI format,
72
  and this function will always return the result of the move in UCI format."""
73
  try:
74
+ global move_num
75
+
76
+ move_num += 1
77
+
78
+ print("## move_num=" + str(move_num))
79
+
80
  move = chess.Move.from_uci(move)
81
  board.push_uci(str(move))
82
 
 
177
  "You are a chess Grandmaster and you play as white. "
178
  "1. First call get_legal_moves(), to get a list of legal moves. "
179
  "2. If no legal move is possible, reply FINISH. "
180
+ "3. Else choose a move returned in step one and call make_move(move). IMPORTANT: Do NOT make illegal moves. "
181
  "4. Analyze the move in 3 bullet points. Respond in format **Analysis:** move in UCI format (piece emoji), unordered list.")
182
  player_white_node = functools.partial(agent_node, agent=player_white_agent, name="player_white")
183
 
 
185
  "You are a chess Grandmaster and you play as black. "
186
  "1. First call get_legal_moves(), to get a list of legal moves. "
187
  "2. If no legal move is possible, reply FINISH. "
188
+ "3. Else choose a move returned in step one and call make_move(move). IMPORTANT: Do NOT make illegal moves. "
189
  "4. Analyze the move in 3 bullet points. Respond in format **Analysis:** move in UCI format (piece emoji), unordered list.")
190
  player_black_node = functools.partial(agent_node, agent=player_black_agent, name="player_black")
191
 
 
210
 
211
  num_moves = moves_num
212
 
213
+ print("## num_moves=" + num_moves)
214
+
215
  graph = create_graph(llm_board, llm_white, llm_black)
216
 
217
  result = ""