Update multi_agent.py
Browse files- multi_agent.py +44 -47
multi_agent.py
CHANGED
|
@@ -15,52 +15,6 @@ class AgentState(TypedDict):
|
|
| 15 |
black_moves: Annotated[list[chess.Move], operator.add]
|
| 16 |
board_svgs: Annotated[list[str], operator.add]
|
| 17 |
|
| 18 |
-
@tool
|
| 19 |
-
def get_legal_moves() -> Annotated[str, "A list of legal moves in UCI format"]:
|
| 20 |
-
"""Returns a list of legal moves in UCI format.
|
| 21 |
-
The input should always be an empty string,
|
| 22 |
-
and this function will always return legal moves in UCI format."""
|
| 23 |
-
try:
|
| 24 |
-
return "Possible moves are: " + ",".join(
|
| 25 |
-
[str(move) for move in board.legal_moves]
|
| 26 |
-
)
|
| 27 |
-
except Exception as e:
|
| 28 |
-
print(f"An error occurred in get_legal_moves: {e}")
|
| 29 |
-
return "Error: unable to get legal moves"
|
| 30 |
-
|
| 31 |
-
@tool
|
| 32 |
-
def make_move(move: Annotated[str, "A move in UCI format."]) -> Annotated[str, "Result of the move."]:
|
| 33 |
-
"""Makes a move.
|
| 34 |
-
The input should always be a move in UCI format,
|
| 35 |
-
and this function will always return the result of the move in UCI format."""
|
| 36 |
-
try:
|
| 37 |
-
move = chess.Move.from_uci(move)
|
| 38 |
-
board.push_uci(str(move))
|
| 39 |
-
global made_move
|
| 40 |
-
made_move = True
|
| 41 |
-
|
| 42 |
-
board_svgs.append(chess.svg.board(
|
| 43 |
-
board,
|
| 44 |
-
arrows=[(move.from_square, move.to_square)],
|
| 45 |
-
fill={move.from_square: "gray"},
|
| 46 |
-
size=250
|
| 47 |
-
))
|
| 48 |
-
|
| 49 |
-
piece = board.piece_at(move.to_square)
|
| 50 |
-
piece_symbol = piece.unicode_symbol()
|
| 51 |
-
piece_name = (
|
| 52 |
-
chess.piece_name(piece.piece_type).capitalize()
|
| 53 |
-
if piece_symbol.isupper()
|
| 54 |
-
else chess.piece_name(piece.piece_type)
|
| 55 |
-
)
|
| 56 |
-
|
| 57 |
-
return f"Moved {piece_name} ({piece_symbol}) from "\
|
| 58 |
-
f"{chess.SQUARE_NAMES[move.from_square]} to "\
|
| 59 |
-
f"{chess.SQUARE_NAMES[move.to_square]}."
|
| 60 |
-
except Exception as e:
|
| 61 |
-
print(f"An error occurred in make_move: {e}")
|
| 62 |
-
return f"Error: unable to make move {move}"
|
| 63 |
-
|
| 64 |
def create_graph(llm_board, llm_white, llm_black):
|
| 65 |
llm_board = ChatOpenAI(model=llm_board)
|
| 66 |
llm_white = ChatOpenAI(model=llm_white)
|
|
@@ -146,7 +100,50 @@ def black_node(state: AgentState):
|
|
| 146 |
return {
|
| 147 |
"black_moves": response.content
|
| 148 |
}
|
| 149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
def run_multi_agent(llm_board, llm_white, llm_black, max_moves):
|
| 151 |
graph = create_graph(llm_board, llm_white, llm_black, max_moves)
|
| 152 |
|
|
|
|
| 15 |
black_moves: Annotated[list[chess.Move], operator.add]
|
| 16 |
board_svgs: Annotated[list[str], operator.add]
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
def create_graph(llm_board, llm_white, llm_black):
|
| 19 |
llm_board = ChatOpenAI(model=llm_board)
|
| 20 |
llm_white = ChatOpenAI(model=llm_white)
|
|
|
|
| 100 |
return {
|
| 101 |
"black_moves": response.content
|
| 102 |
}
|
| 103 |
+
|
| 104 |
+
@tool
|
| 105 |
+
def get_legal_moves() -> Annotated[str, "A list of legal moves in UCI format"]:
|
| 106 |
+
"""Returns a list of legal moves in UCI format.
|
| 107 |
+
The input should always be an empty string,
|
| 108 |
+
and this function will always return legal moves in UCI format."""
|
| 109 |
+
try:
|
| 110 |
+
return "Possible moves are: " + ",".join(
|
| 111 |
+
[str(move) for move in board.legal_moves]
|
| 112 |
+
)
|
| 113 |
+
except Exception as e:
|
| 114 |
+
print(f"An error occurred in get_legal_moves: {e}")
|
| 115 |
+
return "Error: unable to get legal moves"
|
| 116 |
+
|
| 117 |
+
@tool
|
| 118 |
+
def make_move(move: Annotated[str, "A move in UCI format."]) -> Annotated[str, "Result of the move."]:
|
| 119 |
+
"""Makes a move.
|
| 120 |
+
The input should always be a move in UCI format,
|
| 121 |
+
and this function will always return the result of the move in UCI format."""
|
| 122 |
+
try:
|
| 123 |
+
move = chess.Move.from_uci(move)
|
| 124 |
+
board.push_uci(str(move))
|
| 125 |
+
piece = board.piece_at(move.to_square)
|
| 126 |
+
piece_symbol = piece.unicode_symbol()
|
| 127 |
+
piece_name = (
|
| 128 |
+
chess.piece_name(piece.piece_type).capitalize()
|
| 129 |
+
if piece_symbol.isupper()
|
| 130 |
+
else chess.piece_name(piece.piece_type)
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
board_svgs.append(chess.svg.board(
|
| 134 |
+
board,
|
| 135 |
+
arrows=[(move.from_square, move.to_square)],
|
| 136 |
+
fill={move.from_square: "gray"},
|
| 137 |
+
size=250
|
| 138 |
+
))
|
| 139 |
+
|
| 140 |
+
return f"Moved {piece_name} ({piece_symbol}) from "\
|
| 141 |
+
f"{chess.SQUARE_NAMES[move.from_square]} to "\
|
| 142 |
+
f"{chess.SQUARE_NAMES[move.to_square]}."
|
| 143 |
+
except Exception as e:
|
| 144 |
+
print(f"An error occurred in make_move: {e}")
|
| 145 |
+
return f"Error: unable to make move {move}"
|
| 146 |
+
|
| 147 |
def run_multi_agent(llm_board, llm_white, llm_black, max_moves):
|
| 148 |
graph = create_graph(llm_board, llm_white, llm_black, max_moves)
|
| 149 |
|