{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "\"Open" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Auto Generated Agent Chat: Chess Game Playing While Chitchatting by GPT-4 Agents\n", "\n", "Modified based on https://github.com/ekzhu/FLAML/blob/evaluation/evaluation/chess/play_chess.ipynb\n", "\n", "## Requirements\n", "\n", "FLAML requires `Python>=3.8`. To run this notebook example, please install flaml with the [autogen] option:\n", "```bash\n", "pip install flaml[autogen]\n", "```" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "%%capture --no-stderr\n", "# %pip install flaml[autogen]~=2.0.0rc4\n", "%pip install chess -U" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import chess\n", "import chess.svg" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Set your API Endpoint\n", "\n", "The [`config_list_from_json`](https://microsoft.github.io/FLAML/docs/reference/autogen/oai/openai_utils#config_list_from_json) function loads a list of configurations from an environment variable or a json file." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "from flaml import autogen\n", "\n", "config_list_gpt4 = autogen.config_list_from_json(\n", " \"OAI_CONFIG_LIST\",\n", " filter_dict={\n", " \"model\": [\"gpt-4\", \"gpt4\", \"gpt-4-32k\", \"gpt-4-32k-0314\"],\n", " },\n", ")\n", "# config_list_gpt35 = autogen.config_list_from_json(\n", "# \"OAI_CONFIG_LIST\",\n", "# filter_dict={\n", "# \"model\": {\n", "# \"gpt-3.5-turbo\",\n", "# \"gpt-3.5-turbo-16k\",\n", "# \"gpt-3.5-turbo-0301\",\n", "# \"chatgpt-35-turbo-0301\",\n", "# \"gpt-35-turbo-v0301\",\n", "# },\n", "# },\n", "# )" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "It first looks for environment variable \"OAI_CONFIG_LIST\" which needs to be a valid json string. If that variable is not found, it then looks for a json file named \"OAI_CONFIG_LIST\". It filters the configs by models (you can filter by other keys as well). Only the gpt-4 models are kept in the list based on the filter condition.\n", "\n", "The config list looks like the following:\n", "```python\n", "config_list = [\n", " {\n", " 'model': 'gpt-4',\n", " 'api_key': '',\n", " },\n", " {\n", " 'model': 'gpt-4',\n", " 'api_key': '',\n", " 'api_base': '',\n", " 'api_type': 'azure',\n", " 'api_version': '2023-06-01-preview',\n", " },\n", " {\n", " 'model': 'gpt-4-32k',\n", " 'api_key': '',\n", " 'api_base': '',\n", " 'api_type': 'azure',\n", " 'api_version': '2023-06-01-preview',\n", " },\n", "]\n", "```\n", "\n", "If you open this notebook in colab, you can upload your files by clicking the file icon on the left panel and then choose \"upload file\" icon.\n", "\n", "You can set the value of config_list in other ways you prefer, e.g., loading from a YAML file." ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Define Agents\n", "\n", "We'll define a BoardAgent and a ChessPlayerAgent class." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "from collections import defaultdict\n", "from typing import Any, Dict, List, Optional, Union\n", "\n", "sys_msg = \"\"\"You are an AI-powered chess board agent.\n", "You translate user's natural language input into legal UCI moves.\n", "You should only reply with a UCI move string extracted from user's input.\"\"\"\n", "\n", "# user_msg_1 = \"\"\"Alright, let's get this game started. I'll move pawn from e2 to e4. That's a classic opening, isn't it? Your turn.\"\"\"\n", "# asst_msg_1 = \"e2e4\"\n", "# user_msg_2 = \"\"\"I am going to move my pawn from e7 to e5.\"\"\"\n", "# asst_msg_2 = \"e7e5\"\n", "\n", "# examples = [\n", "# {\"role\": \"user\", \"content\": user_msg_1},\n", "# {\"role\": \"assistant\", \"content\": asst_msg_1},\n", "# {\"role\": \"user\", \"content\": user_msg_2},\n", "# {\"role\": \"assistant\", \"content\": asst_msg_2},\n", "# ]\n", "\n", "class BoardAgent(autogen.AssistantAgent):\n", "\n", " def __init__(self, board: chess.Board):\n", " super().__init__(\n", " name=\"BoardAgent\",\n", " system_message=sys_msg,\n", " llm_config={\"temperature\": 0.0, \"config_list\": config_list_gpt4},\n", " max_consecutive_auto_reply=10,\n", " )\n", " self.register_auto_reply(autogen.ResponsiveAgent, BoardAgent._generate_board_reply)\n", " self._board = board\n", " self._correct_move_messages = defaultdict(list)\n", "\n", " def _generate_board_reply(\n", " self,\n", " messages: Optional[List[Dict]] = None,\n", " sender: Optional[autogen.Agent] = None,\n", " context: Optional[Any] = None,\n", " ) -> Union[str, Dict, None]:\n", " # Filter for messages that do not contain error.\n", " if messages is None:\n", " messages = self._oai_messages[sender]\n", " message = messages[-1]\n", " assert message.get(\"role\") == \"user\"\n", " # extract a UCI move from player's message\n", " reply = self.generate_reply(self._correct_move_messages[sender] + [message], sender, exclude=[BoardAgent._generate_board_reply])\n", " if isinstance(reply, str):\n", " uci_move = reply\n", " else:\n", " uci_move = str(reply[\"content\"])\n", " try:\n", " self._board.push_uci(uci_move)\n", " except ValueError as e:\n", " # invalid move\n", " error = f\"Error: {e}\"\n", " return True, error\n", " else:\n", " # valid move\n", " m = chess.Move.from_uci(uci_move)\n", " display(chess.svg.board(self._board, arrows=[(m.from_square, m.to_square)], fill={m.from_square: \"gray\"}, size=200))\n", " self._correct_move_messages[sender].extend([message, self._message_to_dict(uci_move)])\n", " self._correct_move_messages[sender][-1][\"role\"] = \"assistant\"\n", " return True, uci_move\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "sys_msg_tmpl = \"\"\"Your name is {name} and you are a chess player. \n", "You are playing against {opponent_name}. \n", "You are playing as {color}. \n", "You communicate your move using universal chess interface language.\n", "You also chit-chat with your opponent when you communicate a move to light up the mood.\n", "You should make sure both you and the opponent are making legal moves.\n", "Do not apologize for making illegal moves.\"\"\"\n", "\n", "\n", "class ChessPlayerAgent(autogen.AssistantAgent):\n", "\n", " def __init__(\n", " self,\n", " color: str,\n", " board_agent: BoardAgent,\n", " max_turns: int,\n", " **kwargs,\n", " ):\n", " if color not in [\"white\", \"black\"]:\n", " raise ValueError(f\"color must be either white or black, but got {color}\")\n", " if color == \"white\":\n", " opponent_color = \"black\"\n", " else:\n", " opponent_color = \"white\"\n", " name = f\"Player {color}\"\n", " opponent_name = f\"Player {opponent_color}\"\n", " sys_msg = sys_msg_tmpl.format(\n", " name=name,\n", " opponent_name=opponent_name,\n", " color=color,\n", " )\n", " super().__init__(\n", " name=name,\n", " system_message=sys_msg,\n", " max_consecutive_auto_reply=max_turns,\n", " **kwargs,\n", " )\n", " self.register_auto_reply(BoardAgent, ChessPlayerAgent._generate_reply_for_board)\n", " self.register_auto_reply(ChessPlayerAgent, ChessPlayerAgent._generate_reply_for_player)\n", " self._board_agent = board_agent\n", " self.update_max_consecutive_auto_reply(self._board_agent.max_consecutive_auto_reply(), self._board_agent)\n", "\n", " def _generate_reply_for_board(\n", " self,\n", " messages: Optional[List[Dict]] = None,\n", " sender: Optional[autogen.Agent] = None,\n", " context: Optional[Any] = None,\n", " ) -> Union[str, Dict, None]:\n", " if messages is None:\n", " messages = self._oai_messages[sender]\n", " # add a system message about the current state of the board.\n", " board_state_msg = [{\"role\": \"system\", \"content\": f\"Current board:\\n{self._board_agent._board}\"}]\n", " last_message = messages[-1]\n", " if last_message[\"content\"].startswith(\"Error\"):\n", " # try again\n", " last_message[\"role\"] = \"system\"\n", " return True, self.generate_reply(messages + board_state_msg, sender, exclude=[ChessPlayerAgent._generate_reply_for_board])\n", " else:\n", " return True, None\n", "\n", " def _generate_reply_for_player(\n", " self,\n", " messages: Optional[List[Dict]] = None,\n", " sender: Optional[autogen.Agent] = None,\n", " context: Optional[Any] = None,\n", " ) -> Union[str, Dict, None]:\n", " if messages is None:\n", " messages = self._oai_messages[sender]\n", " # add a system message about the current state of the board.\n", " board_state_msg = [{\"role\": \"system\", \"content\": f\"Current board:\\n{self._board_agent._board}\"}]\n", " # propose a reply which will be sent to the board agent for verification.\n", " message = self.generate_reply(messages + board_state_msg, sender, exclude=[ChessPlayerAgent._generate_reply_for_player])\n", " if message is None:\n", " return True, None\n", " # converse with the board until a legal move is made or max allowed retries.\n", " self.initiate_chat(self._board_agent, clear_history=False, message=message)\n", " # last message sent by the board agent\n", " last_message = self._oai_messages[self._board_agent][-1]\n", " if last_message[\"role\"] == \"assistant\":\n", " # didn't make a legal move after a limit times of retries.\n", " print(f\"{self.name}: I yield.\")\n", " return True, None\n", " return True, self._oai_messages[self._board_agent][-2]\n" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Construct Agents" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "max_turn = 10\n", "\n", "board = chess.Board()\n", "board_agent = BoardAgent(board=board)\n", "player_black = ChessPlayerAgent(\n", " color=\"black\",\n", " board_agent=board_agent,\n", " max_turns=max_turn,\n", " llm_config={\"temperature\": 0.5, \"seed\": 1, \"config_list\": config_list_gpt4},\n", ")\n", "player_white = ChessPlayerAgent(\n", " color=\"white\",\n", " board_agent=board_agent,\n", " max_turns=max_turn,\n", " llm_config={\"temperature\": 0.5, \"seed\": 2, \"config_list\": config_list_gpt4},\n", ")" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Start Game" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[33mPlayer black\u001b[0m (to Player white):\n", "\n", "Your turn.\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mPlayer white\u001b[0m (to BoardAgent):\n", "\n", "e2e4. \n", "A classic opening move, my friend. Let's see how this game unfolds. Your move.\n", "\n", "--------------------------------------------------------------------------------\n" ] }, { "data": { "image/svg+xml": [ "
r n b q k b n r\n",
       "p p p p p p p p\n",
       ". . . . . . . .\n",
       ". . . . . . . .\n",
       ". . . . P . . .\n",
       ". . . . . . . .\n",
       "P P P P . P P P\n",
       "R N B Q K B N R
" ], "text/plain": [ "'
r n b q k b n r\\np p p p p p p p\\n. . . . . . . .\\n. . . . . . . .\\n. . . . P . . .\\n. . . . . . . .\\nP P P P . P P P\\nR N B Q K B N R
'" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[33mBoardAgent\u001b[0m (to Player white):\n", "\n", "e2e4\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mPlayer white\u001b[0m (to Player black):\n", "\n", "e2e4. \n", "A classic opening move, my friend. Let's see how this game unfolds. Your move.\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mPlayer black\u001b[0m (to BoardAgent):\n", "\n", "e7e5. \n", "Matching your classic opening with one of my own. Let's keep the game interesting. Your move.\n", "\n", "--------------------------------------------------------------------------------\n" ] }, { "data": { "image/svg+xml": [ "
r n b q k b n r\n",
       "p p p p . p p p\n",
       ". . . . . . . .\n",
       ". . . . p . . .\n",
       ". . . . P . . .\n",
       ". . . . . . . .\n",
       "P P P P . P P P\n",
       "R N B Q K B N R
" ], "text/plain": [ "'
r n b q k b n r\\np p p p . p p p\\n. . . . . . . .\\n. . . . p . . .\\n. . . . P . . .\\n. . . . . . . .\\nP P P P . P P P\\nR N B Q K B N R
'" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[33mBoardAgent\u001b[0m (to Player black):\n", "\n", "e7e5\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mPlayer black\u001b[0m (to Player white):\n", "\n", "e7e5. \n", "Matching your classic opening with one of my own. Let's keep the game interesting. Your move.\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mPlayer white\u001b[0m (to BoardAgent):\n", "\n", "g1f3. \n", "Aiming to control the center of the board. Your move.\n", "\n", "--------------------------------------------------------------------------------\n" ] }, { "data": { "image/svg+xml": [ "
r n b q k b n r\n",
       "p p p p . p p p\n",
       ". . . . . . . .\n",
       ". . . . p . . .\n",
       ". . . . P . . .\n",
       ". . . . . N . .\n",
       "P P P P . P P P\n",
       "R N B Q K B . R
" ], "text/plain": [ "'
r n b q k b n r\\np p p p . p p p\\n. . . . . . . .\\n. . . . p . . .\\n. . . . P . . .\\n. . . . . N . .\\nP P P P . P P P\\nR N B Q K B . R
'" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[33mBoardAgent\u001b[0m (to Player white):\n", "\n", "g1f3\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mPlayer white\u001b[0m (to Player black):\n", "\n", "g1f3. \n", "Aiming to control the center of the board. Your move.\n", "\n", "--------------------------------------------------------------------------------\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[33mPlayer black\u001b[0m (to BoardAgent):\n", "\n", "g8f6. \n", "Aiming to control the center as well. Your move.\n", "\n", "--------------------------------------------------------------------------------\n" ] }, { "data": { "image/svg+xml": [ "
r n b q k b . r\n",
       "p p p p . p p p\n",
       ". . . . . n . .\n",
       ". . . . p . . .\n",
       ". . . . P . . .\n",
       ". . . . . N . .\n",
       "P P P P . P P P\n",
       "R N B Q K B . R
" ], "text/plain": [ "'
r n b q k b . r\\np p p p . p p p\\n. . . . . n . .\\n. . . . p . . .\\n. . . . P . . .\\n. . . . . N . .\\nP P P P . P P P\\nR N B Q K B . R
'" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[33mBoardAgent\u001b[0m (to Player black):\n", "\n", "g8f6\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mPlayer black\u001b[0m (to Player white):\n", "\n", "g8f6. \n", "Aiming to control the center as well. Your move.\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mPlayer white\u001b[0m (to BoardAgent):\n", "\n", "b1c3. \n", "Developing my knight to a good square. Your move.\n", "\n", "--------------------------------------------------------------------------------\n" ] }, { "data": { "image/svg+xml": [ "
r n b q k b . r\n",
       "p p p p . p p p\n",
       ". . . . . n . .\n",
       ". . . . p . . .\n",
       ". . . . P . . .\n",
       ". . N . . N . .\n",
       "P P P P . P P P\n",
       "R . B Q K B . R
" ], "text/plain": [ "'
r n b q k b . r\\np p p p . p p p\\n. . . . . n . .\\n. . . . p . . .\\n. . . . P . . .\\n. . N . . N . .\\nP P P P . P P P\\nR . B Q K B . R
'" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[33mBoardAgent\u001b[0m (to Player white):\n", "\n", "b1c3\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mPlayer white\u001b[0m (to Player black):\n", "\n", "b1c3. \n", "Developing my knight to a good square. Your move.\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mPlayer black\u001b[0m (to BoardAgent):\n", "\n", "b8c6. \n", "Developing my knight to match yours. Your move.\n", "\n", "--------------------------------------------------------------------------------\n" ] }, { "data": { "image/svg+xml": [ "
r . b q k b . r\n",
       "p p p p . p p p\n",
       ". . n . . n . .\n",
       ". . . . p . . .\n",
       ". . . . P . . .\n",
       ". . N . . N . .\n",
       "P P P P . P P P\n",
       "R . B Q K B . R
" ], "text/plain": [ "'
r . b q k b . r\\np p p p . p p p\\n. . n . . n . .\\n. . . . p . . .\\n. . . . P . . .\\n. . N . . N . .\\nP P P P . P P P\\nR . B Q K B . R
'" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[33mBoardAgent\u001b[0m (to Player black):\n", "\n", "b8c6\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mPlayer black\u001b[0m (to Player white):\n", "\n", "b8c6. \n", "Developing my knight to match yours. Your move.\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mPlayer white\u001b[0m (to BoardAgent):\n", "\n", "d2d4.\n", "Challenging your pawn in the center. Your move.\n", "\n", "--------------------------------------------------------------------------------\n" ] }, { "data": { "image/svg+xml": [ "
r . b q k b . r\n",
       "p p p p . p p p\n",
       ". . n . . n . .\n",
       ". . . . p . . .\n",
       ". . . P P . . .\n",
       ". . N . . N . .\n",
       "P P P . . P P P\n",
       "R . B Q K B . R
" ], "text/plain": [ "'
r . b q k b . r\\np p p p . p p p\\n. . n . . n . .\\n. . . . p . . .\\n. . . P P . . .\\n. . N . . N . .\\nP P P . . P P P\\nR . B Q K B . R
'" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[33mBoardAgent\u001b[0m (to Player white):\n", "\n", "d2d4\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mPlayer white\u001b[0m (to Player black):\n", "\n", "d2d4.\n", "Challenging your pawn in the center. Your move.\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mPlayer black\u001b[0m (to BoardAgent):\n", "\n", "e5d4. \n", "Taking your pawn. Your move.\n", "\n", "--------------------------------------------------------------------------------\n" ] }, { "data": { "image/svg+xml": [ "
r . b q k b . r\n",
       "p p p p . p p p\n",
       ". . n . . n . .\n",
       ". . . . . . . .\n",
       ". . . p P . . .\n",
       ". . N . . N . .\n",
       "P P P . . P P P\n",
       "R . B Q K B . R
" ], "text/plain": [ "'
r . b q k b . r\\np p p p . p p p\\n. . n . . n . .\\n. . . . . . . .\\n. . . p P . . .\\n. . N . . N . .\\nP P P . . P P P\\nR . B Q K B . R
'" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[33mBoardAgent\u001b[0m (to Player black):\n", "\n", "e5d4\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mPlayer black\u001b[0m (to Player white):\n", "\n", "e5d4. \n", "Taking your pawn. Your move.\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mPlayer white\u001b[0m (to BoardAgent):\n", "\n", "f3d4.\n", "Taking back the pawn and keeping the center balanced. Your move.\n", "\n", "--------------------------------------------------------------------------------\n" ] }, { "data": { "image/svg+xml": [ "
r . b q k b . r\n",
       "p p p p . p p p\n",
       ". . n . . n . .\n",
       ". . . . . . . .\n",
       ". . . N P . . .\n",
       ". . N . . . . .\n",
       "P P P . . P P P\n",
       "R . B Q K B . R
" ], "text/plain": [ "'
r . b q k b . r\\np p p p . p p p\\n. . n . . n . .\\n. . . . . . . .\\n. . . N P . . .\\n. . N . . . . .\\nP P P . . P P P\\nR . B Q K B . R
'" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[33mBoardAgent\u001b[0m (to Player white):\n", "\n", "f3d4\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mPlayer white\u001b[0m (to Player black):\n", "\n", "f3d4.\n", "Taking back the pawn and keeping the center balanced. Your move.\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mPlayer black\u001b[0m (to BoardAgent):\n", "\n", "c6d4. \n", "Taking your knight with my knight. Your move.\n", "\n", "--------------------------------------------------------------------------------\n" ] }, { "data": { "image/svg+xml": [ "
r . b q k b . r\n",
       "p p p p . p p p\n",
       ". . . . . n . .\n",
       ". . . . . . . .\n",
       ". . . n P . . .\n",
       ". . N . . . . .\n",
       "P P P . . P P P\n",
       "R . B Q K B . R
" ], "text/plain": [ "'
r . b q k b . r\\np p p p . p p p\\n. . . . . n . .\\n. . . . . . . .\\n. . . n P . . .\\n. . N . . . . .\\nP P P . . P P P\\nR . B Q K B . R
'" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[33mBoardAgent\u001b[0m (to Player black):\n", "\n", "c6d4\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mPlayer black\u001b[0m (to Player white):\n", "\n", "c6d4. \n", "Taking your knight with my knight. Your move.\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mPlayer white\u001b[0m (to BoardAgent):\n", "\n", "q1d4.\n", "Taking back the knight with my queen. Your move.\n", "\n", "--------------------------------------------------------------------------------\n" ] }, { "data": { "image/svg+xml": [ "
r . b q k b . r\n",
       "p p p p . p p p\n",
       ". . . . . n . .\n",
       ". . . . . . . .\n",
       ". . . Q P . . .\n",
       ". . N . . . . .\n",
       "P P P . . P P P\n",
       "R . B . K B . R
" ], "text/plain": [ "'
r . b q k b . r\\np p p p . p p p\\n. . . . . n . .\\n. . . . . . . .\\n. . . Q P . . .\\n. . N . . . . .\\nP P P . . P P P\\nR . B . K B . R
'" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[33mBoardAgent\u001b[0m (to Player white):\n", "\n", "d1d4\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mPlayer white\u001b[0m (to Player black):\n", "\n", "q1d4.\n", "Taking back the knight with my queen. Your move.\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mPlayer black\u001b[0m (to BoardAgent):\n", "\n", "d7d5. \n", "Pushing my pawn to challenge your Queen. Your move.\n", "\n", "--------------------------------------------------------------------------------\n" ] }, { "data": { "image/svg+xml": [ "
r . b q k b . r\n",
       "p p p . . p p p\n",
       ". . . . . n . .\n",
       ". . . p . . . .\n",
       ". . . Q P . . .\n",
       ". . N . . . . .\n",
       "P P P . . P P P\n",
       "R . B . K B . R
" ], "text/plain": [ "'
r . b q k b . r\\np p p . . p p p\\n. . . . . n . .\\n. . . p . . . .\\n. . . Q P . . .\\n. . N . . . . .\\nP P P . . P P P\\nR . B . K B . R
'" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[33mBoardAgent\u001b[0m (to Player black):\n", "\n", "d7d5\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mPlayer black\u001b[0m (to Player white):\n", "\n", "d7d5. \n", "Pushing my pawn to challenge your Queen. Your move.\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mPlayer white\u001b[0m (to BoardAgent):\n", "\n", "e4d5.\n", "Taking your pawn with my pawn. Your move.\n", "\n", "--------------------------------------------------------------------------------\n" ] }, { "data": { "image/svg+xml": [ "
r . b q k b . r\n",
       "p p p . . p p p\n",
       ". . . . . n . .\n",
       ". . . P . . . .\n",
       ". . . Q . . . .\n",
       ". . N . . . . .\n",
       "P P P . . P P P\n",
       "R . B . K B . R
" ], "text/plain": [ "'
r . b q k b . r\\np p p . . p p p\\n. . . . . n . .\\n. . . P . . . .\\n. . . Q . . . .\\n. . N . . . . .\\nP P P . . P P P\\nR . B . K B . R
'" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[33mBoardAgent\u001b[0m (to Player white):\n", "\n", "e4d5\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mPlayer white\u001b[0m (to Player black):\n", "\n", "e4d5.\n", "Taking your pawn with my pawn. Your move.\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mPlayer black\u001b[0m (to BoardAgent):\n", "\n", "f6d5. \n", "Taking your pawn with my knight. Your move.\n", "\n", "--------------------------------------------------------------------------------\n" ] }, { "data": { "image/svg+xml": [ "
r . b q k b . r\n",
       "p p p . . p p p\n",
       ". . . . . . . .\n",
       ". . . n . . . .\n",
       ". . . Q . . . .\n",
       ". . N . . . . .\n",
       "P P P . . P P P\n",
       "R . B . K B . R
" ], "text/plain": [ "'
r . b q k b . r\\np p p . . p p p\\n. . . . . . . .\\n. . . n . . . .\\n. . . Q . . . .\\n. . N . . . . .\\nP P P . . P P P\\nR . B . K B . R
'" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[33mBoardAgent\u001b[0m (to Player black):\n", "\n", "f6d5\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mPlayer black\u001b[0m (to Player white):\n", "\n", "f6d5. \n", "Taking your pawn with my knight. Your move.\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mPlayer white\u001b[0m (to BoardAgent):\n", "\n", "c3d5.\n", "Taking your knight with my knight. Your move.\n", "\n", "--------------------------------------------------------------------------------\n" ] }, { "data": { "image/svg+xml": [ "
r . b q k b . r\n",
       "p p p . . p p p\n",
       ". . . . . . . .\n",
       ". . . N . . . .\n",
       ". . . Q . . . .\n",
       ". . . . . . . .\n",
       "P P P . . P P P\n",
       "R . B . K B . R
" ], "text/plain": [ "'
r . b q k b . r\\np p p . . p p p\\n. . . . . . . .\\n. . . N . . . .\\n. . . Q . . . .\\n. . . . . . . .\\nP P P . . P P P\\nR . B . K B . R
'" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[33mBoardAgent\u001b[0m (to Player white):\n", "\n", "c3d5\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mPlayer white\u001b[0m (to Player black):\n", "\n", "c3d5.\n", "Taking your knight with my knight. Your move.\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mPlayer black\u001b[0m (to BoardAgent):\n", "\n", "c7c6. \n", "Challenging your knight. Your move.\n", "\n", "--------------------------------------------------------------------------------\n" ] }, { "data": { "image/svg+xml": [ "
r . b q k b . r\n",
       "p p . . . p p p\n",
       ". . p . . . . .\n",
       ". . . N . . . .\n",
       ". . . Q . . . .\n",
       ". . . . . . . .\n",
       "P P P . . P P P\n",
       "R . B . K B . R
" ], "text/plain": [ "'
r . b q k b . r\\np p . . . p p p\\n. . p . . . . .\\n. . . N . . . .\\n. . . Q . . . .\\n. . . . . . . .\\nP P P . . P P P\\nR . B . K B . R
'" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[33mBoardAgent\u001b[0m (to Player black):\n", "\n", "c7c6\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mPlayer black\u001b[0m (to Player white):\n", "\n", "c7c6. \n", "Challenging your knight. Your move.\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mPlayer white\u001b[0m (to BoardAgent):\n", "\n", "d5f6.\n", "Moving my knight to capture your pawn, putting your king in check. Your move.\n", "\n", "--------------------------------------------------------------------------------\n" ] }, { "data": { "image/svg+xml": [ "
r . b q k b . r\n",
       "p p . . . p p p\n",
       ". . p . . N . .\n",
       ". . . . . . . .\n",
       ". . . Q . . . .\n",
       ". . . . . . . .\n",
       "P P P . . P P P\n",
       "R . B . K B . R
" ], "text/plain": [ "'
r . b q k b . r\\np p . . . p p p\\n. . p . . N . .\\n. . . . . . . .\\n. . . Q . . . .\\n. . . . . . . .\\nP P P . . P P P\\nR . B . K B . R
'" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[33mBoardAgent\u001b[0m (to Player white):\n", "\n", "d5f6\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mPlayer white\u001b[0m (to Player black):\n", "\n", "d5f6.\n", "Moving my knight to capture your pawn, putting your king in check. Your move.\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mPlayer black\u001b[0m (to BoardAgent):\n", "\n", "g8f6. \n", "Moving my king to capture your knight and getting out of check. Your move.\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mBoardAgent\u001b[0m (to Player black):\n", "\n", "Error: illegal uci: 'g8f6' in r1bqkb1r/pp3ppp/2p2N2/8/3Q4/8/PPP2PPP/R1B1KB1R b KQkq - 1 9\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mPlayer black\u001b[0m (to BoardAgent):\n", "\n", "d8f6. \n", "My mistake, capturing your knight with my queen. Your move.\n", "\n", "--------------------------------------------------------------------------------\n" ] }, { "data": { "image/svg+xml": [ "
r . b . k b . r\n",
       "p p . . . p p p\n",
       ". . p . . q . .\n",
       ". . . . . . . .\n",
       ". . . Q . . . .\n",
       ". . . . . . . .\n",
       "P P P . . P P P\n",
       "R . B . K B . R
" ], "text/plain": [ "'
r . b . k b . r\\np p . . . p p p\\n. . p . . q . .\\n. . . . . . . .\\n. . . Q . . . .\\n. . . . . . . .\\nP P P . . P P P\\nR . B . K B . R
'" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[33mBoardAgent\u001b[0m (to Player black):\n", "\n", "d8f6\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mPlayer black\u001b[0m (to Player white):\n", "\n", "d8f6. \n", "My mistake, capturing your knight with my queen. Your move.\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mPlayer white\u001b[0m (to BoardAgent):\n", "\n", "d4d1.\n", "Moving my queen away from your queen's attack. Your move.\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mBoardAgent\u001b[0m (to Player white):\n", "\n", "Error: illegal uci: 'd1d4' in r1b1kb1r/pp3ppp/2p2q2/8/3Q4/8/PPP2PPP/R1B1KB1R w KQkq - 0 10\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mPlayer white\u001b[0m (to BoardAgent):\n", "\n", "e1g1.\n", "Moving my king to safety with a castle. Your move.\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mBoardAgent\u001b[0m (to Player white):\n", "\n", "Error: illegal uci: 'e1g1' in r1b1kb1r/pp3ppp/2p2q2/8/3Q4/8/PPP2PPP/R1B1KB1R w KQkq - 0 10\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mPlayer white\u001b[0m (to BoardAgent):\n", "\n", "g1e2.\n", "Moving my king to safety. Your move.\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mBoardAgent\u001b[0m (to Player white):\n", "\n", "Error: illegal uci: 'g1e2' in r1b1kb1r/pp3ppp/2p2q2/8/3Q4/8/PPP2PPP/R1B1KB1R w KQkq - 0 10\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mPlayer white\u001b[0m (to BoardAgent):\n", "\n", "f1e2.\n", "Moving my bishop to protect my king. Your move.\n", "\n", "--------------------------------------------------------------------------------\n" ] }, { "data": { "image/svg+xml": [ "
r . b . k b . r\n",
       "p p . . . p p p\n",
       ". . p . . q . .\n",
       ". . . . . . . .\n",
       ". . . Q . . . .\n",
       ". . . . . . . .\n",
       "P P P . B P P P\n",
       "R . B . K . . R
" ], "text/plain": [ "'
r . b . k b . r\\np p . . . p p p\\n. . p . . q . .\\n. . . . . . . .\\n. . . Q . . . .\\n. . . . . . . .\\nP P P . B P P P\\nR . B . K . . R
'" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[33mBoardAgent\u001b[0m (to Player white):\n", "\n", "f1e2\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mPlayer white\u001b[0m (to Player black):\n", "\n", "f1e2.\n", "Moving my bishop to protect my king. Your move.\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mPlayer black\u001b[0m (to BoardAgent):\n", "\n", "f8e7. \n", "Moving my bishop to protect my king. Your move.\n", "\n", "--------------------------------------------------------------------------------\n" ] }, { "data": { "image/svg+xml": [ "
r . b . k . . r\n",
       "p p . . b p p p\n",
       ". . p . . q . .\n",
       ". . . . . . . .\n",
       ". . . Q . . . .\n",
       ". . . . . . . .\n",
       "P P P . B P P P\n",
       "R . B . K . . R
" ], "text/plain": [ "'
r . b . k . . r\\np p . . b p p p\\n. . p . . q . .\\n. . . . . . . .\\n. . . Q . . . .\\n. . . . . . . .\\nP P P . B P P P\\nR . B . K . . R
'" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[33mBoardAgent\u001b[0m (to Player black):\n", "\n", "f8e7\n", "\n", "--------------------------------------------------------------------------------\n", "\u001b[33mPlayer black\u001b[0m (to Player white):\n", "\n", "f8e7. \n", "Moving my bishop to protect my king. Your move.\n", "\n", "--------------------------------------------------------------------------------\n" ] } ], "source": [ "player_black.initiate_chat(player_white, message=\"Your turn.\")" ] } ], "metadata": { "kernelspec": { "display_name": "flaml", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.17" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }