Skip to content

Commit 52c2a70

Browse files
ekzhujackgerrits
andauthored
Fix chess sample (microsoft#4932)
--------- Co-authored-by: Jack Gerrits <[email protected]>
1 parent f113c9a commit 52c2a70

File tree

2 files changed

+30
-121
lines changed

2 files changed

+30
-121
lines changed

python/samples/core_chess_game/main.py

+30-23
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""This is an example of simulating a chess game with two agents
22
that play against each other, using tools to reason about the game state
3-
and make moves, and using a group chat manager to orchestrate the conversation."""
3+
and make moves. The agents subscribe to the default topic and publish their
4+
moves to the default topic."""
45

56
import argparse
67
import asyncio
@@ -19,7 +20,12 @@
1920
message_handler,
2021
)
2122
from autogen_core.model_context import BufferedChatCompletionContext, ChatCompletionContext
22-
from autogen_core.models import AssistantMessage, ChatCompletionClient, LLMMessage, SystemMessage, UserMessage
23+
from autogen_core.models import (
24+
ChatCompletionClient,
25+
LLMMessage,
26+
SystemMessage,
27+
UserMessage,
28+
)
2329
from autogen_core.tool_agent import ToolAgent, tool_agent_caller_loop
2430
from autogen_core.tools import FunctionTool, Tool, ToolSchema
2531
from chess import BLACK, SQUARE_NAMES, WHITE, Board, Move
@@ -33,7 +39,7 @@ class TextMessage(BaseModel):
3339

3440

3541
@default_subscription
36-
class ToolUseAgent(RoutedAgent):
42+
class PlayerAgent(RoutedAgent):
3743
def __init__(
3844
self,
3945
description: str,
@@ -59,14 +65,15 @@ async def handle_message(self, message: TextMessage, ctx: MessageContext) -> Non
5965
self,
6066
tool_agent_id=self._tool_agent_id,
6167
model_client=self._model_client,
62-
input_messages=(await self._model_context.get_messages()),
68+
input_messages=self._system_messages + (await self._model_context.get_messages()),
6369
tool_schema=self._tool_schema,
6470
cancellation_token=ctx.cancellation_token,
6571
)
66-
assert isinstance(messages[-1].content, str)
6772
# Add the assistant message to the model context.
68-
await self._model_context.add_message(AssistantMessage(content=messages[-1].content, source=self.id.type))
73+
for msg in messages:
74+
await self._model_context.add_message(msg)
6975
# Publish the final response.
76+
assert isinstance(messages[-1].content, str)
7077
await self.publish_message(TextMessage(content=messages[-1].content, source=self.id.type), DefaultTopicId())
7178

7279

@@ -203,39 +210,39 @@ def get_board_text() -> Annotated[str, "The current board state"]:
203210
# Register the agents.
204211
await ToolAgent.register(
205212
runtime,
206-
"ToolAgent",
207-
lambda: ToolAgent(description="Tool agent for chess game.", tools=black_tools + white_tools),
213+
"PlayerBlackToolAgent",
214+
lambda: ToolAgent(description="Tool agent for chess game.", tools=black_tools),
215+
)
216+
217+
await ToolAgent.register(
218+
runtime,
219+
"PlayerWhiteToolAgent",
220+
lambda: ToolAgent(description="Tool agent for chess game.", tools=white_tools),
208221
)
209222

210-
await ToolUseAgent.register(
223+
await PlayerAgent.register(
211224
runtime,
212225
"PlayerBlack",
213-
lambda: ToolUseAgent(
226+
lambda: PlayerAgent(
214227
description="Player playing black.",
215-
instructions="You are a chess player and you play as black. "
216-
"Use get_legal_moves() to get list of legal moves. "
217-
"Use get_board() to get the current board state. "
218-
"Think about your strategy and call make_move(thinking, move) to make a move.",
228+
instructions="You are a chess player and you play as black. Use the tool 'get_board' and 'get_legal_moves' to get the legal moves and 'make_move' to make a move.",
219229
model_client=model_client,
220230
model_context=BufferedChatCompletionContext(buffer_size=10),
221231
tool_schema=[tool.schema for tool in black_tools],
222-
tool_agent_type="ToolAgent",
232+
tool_agent_type="PlayerBlackToolAgent",
223233
),
224234
)
225235

226-
await ToolUseAgent.register(
236+
await PlayerAgent.register(
227237
runtime,
228238
"PlayerWhite",
229-
lambda: ToolUseAgent(
239+
lambda: PlayerAgent(
230240
description="Player playing white.",
231-
instructions="You are a chess player and you play as white. "
232-
"Use get_legal_moves() to get list of legal moves. "
233-
"Use get_board() to get the current board state. "
234-
"Think about your strategy and call make_move(thinking, move) to make a move.",
241+
instructions="You are a chess player and you play as white. Use the tool 'get_board' and 'get_legal_moves' to get the legal moves and 'make_move' to make a move.",
235242
model_client=model_client,
236243
model_context=BufferedChatCompletionContext(buffer_size=10),
237244
tool_schema=[tool.schema for tool in white_tools],
238-
tool_agent_type="ToolAgent",
245+
tool_agent_type="PlayerWhiteToolAgent",
239246
),
240247
)
241248

@@ -249,7 +256,7 @@ async def main(model_config: Dict[str, Any]) -> None:
249256
# orchestration.
250257
# Send an initial message to player white to start the game.
251258
await runtime.send_message(
252-
TextMessage(content="Game started.", source="System"),
259+
TextMessage(content="Game started, white player your move.", source="System"),
253260
AgentId("PlayerWhite", "default"),
254261
)
255262
await runtime.stop_when_idle()

python/samples/core_chess_game/utils.py

-98
This file was deleted.

0 commit comments

Comments
 (0)