Update multi_agent.py
Browse files- multi_agent.py +19 -16
multi_agent.py
CHANGED
|
@@ -26,7 +26,7 @@ def get_legal_moves() -> Annotated[str, "A list of legal moves in UCI format"]:
|
|
| 26 |
The input should always be an empty string,
|
| 27 |
and this function will always return legal moves in UCI format."""
|
| 28 |
try:
|
| 29 |
-
print("##
|
| 30 |
global legal_moves
|
| 31 |
legal_moves = ",".join([str(move) for move in board.legal_moves])
|
| 32 |
return legal_moves
|
|
@@ -40,13 +40,13 @@ def make_move(move: Annotated[str, "A move in UCI format."]) -> Annotated[str, "
|
|
| 40 |
The input should always be a move in UCI format,
|
| 41 |
and this function will always return the result of the move in UCI format."""
|
| 42 |
try:
|
| 43 |
-
print("##
|
| 44 |
move = chess.Move.from_uci(move)
|
| 45 |
board.push_uci(str(move))
|
| 46 |
|
| 47 |
global move_num
|
| 48 |
move_num += 1
|
| 49 |
-
print("##
|
| 50 |
|
| 51 |
board_svgs.append(chess.svg.board(
|
| 52 |
board,
|
|
@@ -94,10 +94,10 @@ def create_agent(llm: ChatOpenAI, tools: list, system_prompt: str):
|
|
| 94 |
|
| 95 |
def agent_node(state, agent, name):
|
| 96 |
try:
|
|
|
|
| 97 |
result = agent.invoke(state)
|
| 98 |
-
print("##
|
| 99 |
-
print("##
|
| 100 |
-
print("### result['output']=" + result["output"])
|
| 101 |
return {
|
| 102 |
"messages": [HumanMessage(content=result["output"], name=name)]
|
| 103 |
}
|
|
@@ -106,18 +106,16 @@ def agent_node(state, agent, name):
|
|
| 106 |
return {"messages": [HumanMessage(content=f"Error: {e}", name=name)]}
|
| 107 |
|
| 108 |
def create_graph():
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
llm_player_black = ChatOpenAI(model="gpt-4o")
|
| 112 |
-
|
| 113 |
system_prompt = (
|
| 114 |
"You are a Chess Board Proxy tasked with managing a game of chess "
|
| 115 |
"between player_white and player_black. player_white makes the first move, "
|
| 116 |
"then the players take turns."
|
| 117 |
)
|
| 118 |
|
| 119 |
-
options =
|
| 120 |
-
|
| 121 |
function_def = {
|
| 122 |
"name": "route",
|
| 123 |
"description": "Select the next player.",
|
|
@@ -147,7 +145,11 @@ def create_graph():
|
|
| 147 |
"Select one of: {options}.",
|
| 148 |
),
|
| 149 |
]
|
| 150 |
-
).partial(options=str(options), members=", ".join(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
supervisor_chain = (
|
| 153 |
prompt
|
|
@@ -190,7 +192,7 @@ def create_graph():
|
|
| 190 |
{"chess_board_proxy": "chess_board_proxy", END: END}
|
| 191 |
)
|
| 192 |
|
| 193 |
-
conditional_map = {k: k for k in
|
| 194 |
conditional_map["END"] = END
|
| 195 |
|
| 196 |
graph.add_conditional_edges("chess_board_proxy", lambda x: x["next"], conditional_map)
|
|
@@ -222,10 +224,11 @@ def initialize():
|
|
| 222 |
legal_moves = ""
|
| 223 |
|
| 224 |
def run_multi_agent(moves_num):
|
|
|
|
|
|
|
| 225 |
global num_moves
|
| 226 |
-
num_moves = moves_num
|
| 227 |
|
| 228 |
-
|
| 229 |
|
| 230 |
graph = create_graph()
|
| 231 |
|
|
|
|
| 26 |
The input should always be an empty string,
|
| 27 |
and this function will always return legal moves in UCI format."""
|
| 28 |
try:
|
| 29 |
+
print("## get_legal_moves")
|
| 30 |
global legal_moves
|
| 31 |
legal_moves = ",".join([str(move) for move in board.legal_moves])
|
| 32 |
return legal_moves
|
|
|
|
| 40 |
The input should always be a move in UCI format,
|
| 41 |
and this function will always return the result of the move in UCI format."""
|
| 42 |
try:
|
| 43 |
+
print("## make_move")
|
| 44 |
move = chess.Move.from_uci(move)
|
| 45 |
board.push_uci(str(move))
|
| 46 |
|
| 47 |
global move_num
|
| 48 |
move_num += 1
|
| 49 |
+
print("## move_num=" + str(move_num))
|
| 50 |
|
| 51 |
board_svgs.append(chess.svg.board(
|
| 52 |
board,
|
|
|
|
| 94 |
|
| 95 |
def agent_node(state, agent, name):
|
| 96 |
try:
|
| 97 |
+
print("## agent_node=" + name)
|
| 98 |
result = agent.invoke(state)
|
| 99 |
+
print("## result=" + str(result))
|
| 100 |
+
print("## result['output']=" + result["output"])
|
|
|
|
| 101 |
return {
|
| 102 |
"messages": [HumanMessage(content=result["output"], name=name)]
|
| 103 |
}
|
|
|
|
| 106 |
return {"messages": [HumanMessage(content=f"Error: {e}", name=name)]}
|
| 107 |
|
| 108 |
def create_graph():
|
| 109 |
+
players = ["player_white", "player_black"]
|
| 110 |
+
|
|
|
|
|
|
|
| 111 |
system_prompt = (
|
| 112 |
"You are a Chess Board Proxy tasked with managing a game of chess "
|
| 113 |
"between player_white and player_black. player_white makes the first move, "
|
| 114 |
"then the players take turns."
|
| 115 |
)
|
| 116 |
|
| 117 |
+
options = players
|
| 118 |
+
|
| 119 |
function_def = {
|
| 120 |
"name": "route",
|
| 121 |
"description": "Select the next player.",
|
|
|
|
| 145 |
"Select one of: {options}.",
|
| 146 |
),
|
| 147 |
]
|
| 148 |
+
).partial(options=str(options), members=", ".join(players), verbose=True)
|
| 149 |
+
|
| 150 |
+
llm_chess_board_proxy = ChatOpenAI(model="gpt-4o")
|
| 151 |
+
llm_player_white = ChatOpenAI(model="gpt-4o")
|
| 152 |
+
llm_player_black = ChatOpenAI(model="gpt-4o")
|
| 153 |
|
| 154 |
supervisor_chain = (
|
| 155 |
prompt
|
|
|
|
| 192 |
{"chess_board_proxy": "chess_board_proxy", END: END}
|
| 193 |
)
|
| 194 |
|
| 195 |
+
conditional_map = {k: k for k in players}
|
| 196 |
conditional_map["END"] = END
|
| 197 |
|
| 198 |
graph.add_conditional_edges("chess_board_proxy", lambda x: x["next"], conditional_map)
|
|
|
|
| 224 |
legal_moves = ""
|
| 225 |
|
| 226 |
def run_multi_agent(moves_num):
|
| 227 |
+
initialize()
|
| 228 |
+
|
| 229 |
global num_moves
|
|
|
|
| 230 |
|
| 231 |
+
num_moves = moves_num
|
| 232 |
|
| 233 |
graph = create_graph()
|
| 234 |
|