From aeb743f7bb66d360002164fbf23e6f8a013c27a6 Mon Sep 17 00:00:00 2001 From: Eric Zhu Date: Sun, 24 Mar 2024 01:15:22 -0700 Subject: [PATCH 1/3] Support for Mistral AI API and topic notebook. --- autogen/agentchat/conversable_agent.py | 38 +- website/.gitignore | 2 + .../about-using-nonopenai-models.md | 6 +- .../non-openai-models/cloud-mistralai.ipynb | 1010 +++++++++++++++++ 4 files changed, 1043 insertions(+), 13 deletions(-) create mode 100644 website/docs/topics/non-openai-models/cloud-mistralai.ipynb diff --git a/autogen/agentchat/conversable_agent.py b/autogen/agentchat/conversable_agent.py index 2a2de8b551e0..d0f933144511 100644 --- a/autogen/agentchat/conversable_agent.py +++ b/autogen/agentchat/conversable_agent.py @@ -698,8 +698,8 @@ def _print_received_message(self, message: Union[Dict, str], sender: Agent): id_key = "name" else: id_key = "tool_call_id" - - func_print = f"***** Response from calling {message['role']} \"{message[id_key]}\" *****" + id = message.get(id_key, "No id found") + func_print = f"***** Response from calling {message['role']} ({id}) *****" print(colored(func_print, "green"), flush=True) print(message["content"], flush=True) print(colored("*" * len(func_print), "green"), flush=True) @@ -716,7 +716,7 @@ def _print_received_message(self, message: Union[Dict, str], sender: Agent): if "function_call" in message and message["function_call"]: function_call = dict(message["function_call"]) func_print = ( - f"***** Suggested function Call: {function_call.get('name', '(No function name found)')} *****" + f"***** Suggested function call: {function_call.get('name', '(No function name found)')} *****" ) print(colored(func_print, "green"), flush=True) print( @@ -728,9 +728,9 @@ def _print_received_message(self, message: Union[Dict, str], sender: Agent): print(colored("*" * len(func_print), "green"), flush=True) if "tool_calls" in message and message["tool_calls"]: for tool_call in message["tool_calls"]: - id = tool_call.get("id", "(No id found)") + id = tool_call.get("id", "No tool call id found") function_call = dict(tool_call.get("function", {})) - func_print = f"***** Suggested tool Call ({id}): {function_call.get('name', '(No function name found)')} *****" + func_print = f"***** Suggested tool call ({id}): {function_call.get('name', '(No function name found)')} *****" print(colored(func_print, "green"), flush=True) print( "Arguments: \n", @@ -1311,6 +1311,12 @@ def _generate_oai_reply_from_client(self, llm_client, messages, cache) -> Union[ ) for tool_call in extracted_response.get("tool_calls") or []: tool_call["function"]["name"] = self._normalize_name(tool_call["function"]["name"]) + # Remove id and type if they are not present. + # This is to make the tool call object compatible with Mistral API. + if tool_call.get("id") is None: + tool_call.pop("id") + if tool_call.get("type") is None: + tool_call.pop("type") return extracted_response async def a_generate_oai_reply( @@ -1527,7 +1533,6 @@ def generate_tool_calls_reply( message = messages[-1] tool_returns = [] for tool_call in message.get("tool_calls", []): - id = tool_call["id"] function_call = tool_call.get("function", {}) func = self._function_map.get(function_call.get("name", None), None) if inspect.iscoroutinefunction(func): @@ -1545,13 +1550,24 @@ def generate_tool_calls_reply( loop.close() else: _, func_return = self.execute_function(function_call) - tool_returns.append( - { - "tool_call_id": id, + content = func_return.get("content", "") + if content is None: + content = "" + tool_call_id = tool_call.get("id", None) + if tool_call_id is not None: + tool_call_response = { + "tool_call_id": tool_call_id, "role": "tool", - "content": func_return.get("content", ""), + "content": content, } - ) + else: + # Do not include tool_call_id if it is not present. + # This is to make the tool call object compatible with Mistral API. + tool_call_response = { + "role": "tool", + "content": content, + } + tool_returns.append(tool_call_response) if tool_returns: return True, { "role": "tool", diff --git a/website/.gitignore b/website/.gitignore index 3988a173387f..957507b6b442 100644 --- a/website/.gitignore +++ b/website/.gitignore @@ -19,6 +19,8 @@ docs/topics/code-execution/*.mdx docs/topics/task_decomposition.mdx docs/topics/prompting-and-reasoning/*.mdx docs/topics/non-openai-models/*.mdx +docs/topics/non-openai-models/**/*.py +docs/topics/non-openai-models/**/*.svg # Misc .DS_Store diff --git a/website/docs/topics/non-openai-models/about-using-nonopenai-models.md b/website/docs/topics/non-openai-models/about-using-nonopenai-models.md index e202679f29e1..0e15d5ae8b11 100644 --- a/website/docs/topics/non-openai-models/about-using-nonopenai-models.md +++ b/website/docs/topics/non-openai-models/about-using-nonopenai-models.md @@ -21,7 +21,8 @@ These proxy servers can be cloud-based or running locally within your environmen By using cloud-based proxy servers, you are able to use models without requiring the hardware and software to run them. -These providers can host open source/weight models, like [Hugging Face](https://huggingface.co/), +These providers can host open source/weight models, like [Hugging Face](https://huggingface.co/) +and [Mistral AI](https://mistral.ai/), or their own closed models. When cloud-based proxy servers provide an OpenAI-compatible API, using them in AutoGen @@ -32,7 +33,8 @@ authentication which is usually handled through an API key. Examples of using cloud-based proxy servers providers that have an OpenAI-compatible API are provided below: -- [together.ai example](/docs/topics/non-openai-models/cloud-togetherai) +- [Together AI example](/docs/topics/non-openai-models/cloud-togetherai) +- [Mistral AI example](/docs/topics/non-openai-models/cloud-mistralai) ### Locally run proxy servers diff --git a/website/docs/topics/non-openai-models/cloud-mistralai.ipynb b/website/docs/topics/non-openai-models/cloud-mistralai.ipynb new file mode 100644 index 000000000000..7e7753d7cc52 --- /dev/null +++ b/website/docs/topics/non-openai-models/cloud-mistralai.ipynb @@ -0,0 +1,1010 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Mistral AI\n", + "\n", + "[Mistral AI](https://mistral.ai/) is a cloud based platform\n", + "serving Mistral's own LLMs.\n", + "You can use AutoGen with Mistral AI's API directly." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "First you need to install the `pyautogen` package to use AutoGen." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "! pip install pyautogen" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now you can set up the Mistral model you want to use." + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "\n", + "config_list = [\n", + " {\n", + " # Choose your model name.\n", + " \"model\": \"mistral-large-latest\",\n", + " \"base_url\": \"https://api.mistral.ai/v1\",\n", + " # You need to provide your API key here.\n", + " \"api_key\": os.environ.get(\"MISTRAL_API_KEY\"),\n", + " }\n", + "]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Two-Agent Coding Example\n", + "\n", + "In this example, we run a two-agent chat to count how many prime numbers between 1 and 10000 using coding." + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [], + "source": [ + "from pathlib import Path\n", + "from autogen import AssistantAgent, UserProxyAgent\n", + "from autogen.coding import LocalCommandLineCodeExecutor\n", + "\n", + "# Setting up the code executor.\n", + "workdir = Path(\"coding\")\n", + "workdir.mkdir(exist_ok=True)\n", + "code_executor = LocalCommandLineCodeExecutor(work_dir=workdir)\n", + "\n", + "# Setting up the agents.\n", + "user_proxy_agent = UserProxyAgent(\n", + " name=\"User\",\n", + " code_execution_config={\"executor\": code_executor},\n", + " is_termination_msg= lambda msg : \"TERMINATE\" in msg.get(\"content\"),\n", + ")\n", + "\n", + "assistant_agent = AssistantAgent(\n", + " name=\"Mistral Assistant\",\n", + " llm_config={\"config_list\": config_list},\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[33mUser\u001b[0m (to Mistral Assistant):\n", + "\n", + "Count how many prime numbers from 1 to 10000.\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[33mMistral Assistant\u001b[0m (to User):\n", + "\n", + "Sure, I can help with that. Here's a Python code snippet that counts the number of prime numbers from 1 to 10000.\n", + "\n", + "```python\n", + "# filename: prime_counter.py\n", + "\n", + "def is_prime(n):\n", + " if n <= 1:\n", + " return False\n", + " if n <= 3:\n", + " return True\n", + " if n % 2 == 0 or n % 3 == 0:\n", + " return False\n", + " i = 5\n", + " while i * i <= n:\n", + " if n % i == 0 or n % (i + 2) == 0:\n", + " return False\n", + " i += 6\n", + " return True\n", + "\n", + "count = 0\n", + "for num in range(1, 10001):\n", + " if is_prime(num):\n", + " count += 1\n", + "\n", + "print(count)\n", + "```\n", + "\n", + "Please save this code in a file named `prime_counter.py` and run it. The output will be the count of prime numbers from 1 to 10000.\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[31m\n", + ">>>>>>>> NO HUMAN INPUT RECEIVED.\u001b[0m\n", + "\u001b[31m\n", + ">>>>>>>> USING AUTO REPLY...\u001b[0m\n", + "\u001b[31m\n", + ">>>>>>>> EXECUTING CODE BLOCK (inferred language is python)...\u001b[0m\n", + "\u001b[33mUser\u001b[0m (to Mistral Assistant):\n", + "\n", + "exitcode: 0 (execution succeeded)\n", + "Code output: 1229\n", + "\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[33mMistral Assistant\u001b[0m (to User):\n", + "\n", + "Based on the output, the code I provided earlier has successfully executed and found that there are 1229 prime numbers between 1 and 10000. Here's how I approached this task step by step:\n", + "\n", + "1. I wrote a Python function `is_prime(n)` to check if a given number `n` is prime. This function returns `True` if `n` is prime and `False` otherwise.\n", + "\n", + "2. I used a for loop to iterate through numbers from 1 to 10000, then called the `is_prime` function to determine if the current number is prime. If it is, I incremented a counter variable `count` by 1.\n", + "\n", + "3. I printed the value of `count` after the loop to display the total number of prime numbers in the given range.\n", + "\n", + "The output `1229` confirms that there are indeed 1229 prime numbers between 1 and 10000.\n", + "\n", + "TERMINATE\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[31m\n", + ">>>>>>>> NO HUMAN INPUT RECEIVED.\u001b[0m\n" + ] + } + ], + "source": [ + "chat_result = user_proxy_agent.initiate_chat(\n", + " assistant_agent,\n", + " message=\"Count how many prime numbers from 1 to 10000.\",\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Tool Call Example\n", + "\n", + "In this example, instead of writing code, we will have two agent playing chess against each other using tool to make moves.\n", + "\n", + "First install the `chess` package by running the following command:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "! pip install chess" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Write function for making a move." + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, + "outputs": [], + "source": [ + "import chess\n", + "import chess.svg\n", + "# from IPython.display import display\n", + "import random\n", + "from typing_extensions import Annotated\n", + "\n", + "board = chess.Board()\n", + "\n", + "def make_move() -> Annotated[str, \"A move in UCI format\"]:\n", + " moves = list(board.legal_moves)\n", + " move = random.choice(moves)\n", + " board.push(move)\n", + " # Display the board.\n", + " display(chess.svg.board(board, size=400))\n", + " return str(move)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's create the agents. We have three different agents:\n", + "- `player_white` is the agent that plays white.\n", + "- `player_black` is the agent that plays black.\n", + "- `board_proxy` is the agent that moves the pieces on the board." + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [], + "source": [ + "from autogen import ConversableAgent, register_function\n", + "\n", + "player_white = ConversableAgent(\n", + " name=\"Player White\",\n", + " system_message=\"You are a chess player and you play as white. \"\n", + " \"Always call make_move() to make a move\",\n", + " llm_config={\"config_list\": config_list, \"cache_seed\": None},\n", + ")\n", + "\n", + "player_black = ConversableAgent(\n", + " name=\"Player Black\",\n", + " system_message=\"You are a chess player and you play as black. \"\n", + " \"Always call make_move() to make a move\",\n", + " llm_config={\"config_list\": config_list, \"cache_seed\": None},\n", + ")\n", + "\n", + "board_proxy = ConversableAgent(\n", + " name=\"Board Proxy\",\n", + " llm_config=False,\n", + " # The board proxy will only respond to the make_move function.\n", + " is_termination_msg= lambda msg : not \"tool_calls\" in msg,\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Register tools for the agents. See [tutorial chapter on tool use](/docs/tutorial/tool-use) \n", + "for more information." + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": {}, + "outputs": [], + "source": [ + "register_function(\n", + " make_move,\n", + " caller=player_white,\n", + " executor=board_proxy,\n", + " name=\"make_move\",\n", + " description=\"Make a move.\",\n", + ")\n", + "\n", + "register_function(\n", + " make_move,\n", + " caller=player_black,\n", + " executor=board_proxy,\n", + " name=\"make_move\",\n", + " description=\"Make a move.\",\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Register nested chats for the player agents.\n", + "Nested chats allows each player agent to chat with the board proxy agent\n", + "to make a move, before communicating with the other player agent.\n", + "See [nested chats tutorial chapter](/docs/tutorial/conversation-patterns#nested-chats)\n", + "for more information." + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [], + "source": [ + "player_white.register_nested_chats(\n", + " trigger=player_black,\n", + " chat_queue=[\n", + " {\n", + " \"sender\": board_proxy,\n", + " \"recipient\": player_white,\n", + " }\n", + " ]\n", + ")\n", + "\n", + "player_black.register_nested_chats(\n", + " trigger=player_white,\n", + " chat_queue=[\n", + " {\n", + " \"sender\": board_proxy,\n", + " \"recipient\": player_black,\n", + " }\n", + " ]\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Start the chess game." + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[33mPlayer White\u001b[0m (to Player Black):\n", + "\n", + "Let's play chess! Your move.\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[31m\n", + ">>>>>>>> USING AUTO REPLY...\u001b[0m\n", + "\u001b[34m\n", + "********************************************************************************\u001b[0m\n", + "\u001b[34mStarting a new chat....\n", + "\n", + "Message:\n", + "Let's play chess! Your move.\n", + "\n", + "Carryover: \n", + "\u001b[0m\n", + "\u001b[34m\n", + "********************************************************************************\u001b[0m\n", + "\u001b[33mBoard Proxy\u001b[0m (to Player Black):\n", + "\n", + "Let's play chess! Your move.\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[31m\n", + ">>>>>>>> USING AUTO REPLY...\u001b[0m\n", + "\u001b[33mPlayer Black\u001b[0m (to Board Proxy):\n", + "\n", + "\n", + "\u001b[32m***** Suggested tool call (No tool call id found): make_move *****\u001b[0m\n", + "Arguments: \n", + "{}\n", + "\u001b[32m******************************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[31m\n", + ">>>>>>>> USING AUTO REPLY...\u001b[0m\n", + "\u001b[35m\n", + ">>>>>>>> EXECUTING FUNCTION make_move...\u001b[0m\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",
+       ". . . . . . . .\n",
+       "P . . . . . . .\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. . . . . . . .\\nP . . . . . . .\\n. P 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[33mBoard Proxy\u001b[0m (to Player Black):\n", + "\n", + "\u001b[33mBoard Proxy\u001b[0m (to Player Black):\n", + "\n", + "\u001b[32m***** Response from calling tool (No id found) *****\u001b[0m\n", + "a2a3\n", + "\u001b[32m****************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[31m\n", + ">>>>>>>> USING AUTO REPLY...\u001b[0m\n", + "\u001b[33mPlayer Black\u001b[0m (to Board Proxy):\n", + "\n", + "You made a move: a2a3. It's my turn now.\n", + "\n", + "e2e4\n", + "\n", + "Your move.\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[31m\n", + ">>>>>>>> NO HUMAN INPUT RECEIVED.\u001b[0m\n", + "\u001b[33mPlayer Black\u001b[0m (to Player White):\n", + "\n", + "You made a move: a2a3. It's my turn now.\n", + "\n", + "e2e4\n", + "\n", + "Your move.\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[31m\n", + ">>>>>>>> USING AUTO REPLY...\u001b[0m\n", + "\u001b[34m\n", + "********************************************************************************\u001b[0m\n", + "\u001b[34mStarting a new chat....\n", + "\n", + "Message:\n", + "You made a move: a2a3. It's my turn now.\n", + "\n", + "e2e4\n", + "\n", + "Your move.\n", + "\n", + "Carryover: \n", + "\u001b[0m\n", + "\u001b[34m\n", + "********************************************************************************\u001b[0m\n", + "\u001b[33mBoard Proxy\u001b[0m (to Player White):\n", + "\n", + "You made a move: a2a3. It's my turn now.\n", + "\n", + "e2e4\n", + "\n", + "Your move.\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[31m\n", + ">>>>>>>> USING AUTO REPLY...\u001b[0m\n", + "\u001b[33mPlayer White\u001b[0m (to Board Proxy):\n", + "\n", + "\n", + "\u001b[32m***** Suggested tool call (No tool call id found): make_move *****\u001b[0m\n", + "Arguments: \n", + "{}\n", + "\u001b[32m******************************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[31m\n", + ">>>>>>>> USING AUTO REPLY...\u001b[0m\n", + "\u001b[35m\n", + ">>>>>>>> EXECUTING FUNCTION make_move...\u001b[0m\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",
+       ". . . . . . . .\n",
+       "P . . . . . . .\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. . . . . . . .\\nP . . . . . . .\\n. P 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[33mBoard Proxy\u001b[0m (to Player White):\n", + "\n", + "\u001b[33mBoard Proxy\u001b[0m (to Player White):\n", + "\n", + "\u001b[32m***** Response from calling tool (No id found) *****\u001b[0m\n", + "e7e5\n", + "\u001b[32m****************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[31m\n", + ">>>>>>>> USING AUTO REPLY...\u001b[0m\n", + "\u001b[33mPlayer White\u001b[0m (to Board Proxy):\n", + "\n", + "I made a move: e7e5. It's your turn now.\n", + "\n", + "Your move.\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[31m\n", + ">>>>>>>> NO HUMAN INPUT RECEIVED.\u001b[0m\n", + "\u001b[33mPlayer White\u001b[0m (to Player Black):\n", + "\n", + "I made a move: e7e5. It's your turn now.\n", + "\n", + "Your move.\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[31m\n", + ">>>>>>>> USING AUTO REPLY...\u001b[0m\n", + "\u001b[34m\n", + "********************************************************************************\u001b[0m\n", + "\u001b[34mStarting a new chat....\n", + "\n", + "Message:\n", + "I made a move: e7e5. It's your turn now.\n", + "\n", + "Your move.\n", + "\n", + "Carryover: \n", + "\u001b[0m\n", + "\u001b[34m\n", + "********************************************************************************\u001b[0m\n", + "\u001b[33mBoard Proxy\u001b[0m (to Player Black):\n", + "\n", + "I made a move: e7e5. It's your turn now.\n", + "\n", + "Your move.\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[31m\n", + ">>>>>>>> USING AUTO REPLY...\u001b[0m\n", + "\u001b[33mPlayer Black\u001b[0m (to Board Proxy):\n", + "\n", + "\n", + "\u001b[32m***** Suggested tool call (No tool call id found): make_move *****\u001b[0m\n", + "Arguments: \n", + "{}\n", + "\u001b[32m******************************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[31m\n", + ">>>>>>>> USING AUTO REPLY...\u001b[0m\n", + "\u001b[35m\n", + ">>>>>>>> EXECUTING FUNCTION make_move...\u001b[0m\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",
+       "P . . . . . . .\n",
+       ". 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\\nP . . . . . . .\\n. 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[33mBoard Proxy\u001b[0m (to Player Black):\n", + "\n", + "\u001b[33mBoard Proxy\u001b[0m (to Player Black):\n", + "\n", + "\u001b[32m***** Response from calling tool (No id found) *****\u001b[0m\n", + "h2h4\n", + "\u001b[32m****************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[31m\n", + ">>>>>>>> USING AUTO REPLY...\u001b[0m\n", + "\u001b[33mPlayer Black\u001b[0m (to Board Proxy):\n", + "\n", + "I made a move: h2h4. It's your turn now.\n", + "\n", + "Your move.\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[31m\n", + ">>>>>>>> NO HUMAN INPUT RECEIVED.\u001b[0m\n", + "\u001b[33mPlayer Black\u001b[0m (to Player White):\n", + "\n", + "I made a move: h2h4. It's your turn now.\n", + "\n", + "Your move.\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[31m\n", + ">>>>>>>> USING AUTO REPLY...\u001b[0m\n", + "\u001b[34m\n", + "********************************************************************************\u001b[0m\n", + "\u001b[34mStarting a new chat....\n", + "\n", + "Message:\n", + "I made a move: h2h4. It's your turn now.\n", + "\n", + "Your move.\n", + "\n", + "Carryover: \n", + "\u001b[0m\n", + "\u001b[34m\n", + "********************************************************************************\u001b[0m\n", + "\u001b[33mBoard Proxy\u001b[0m (to Player White):\n", + "\n", + "I made a move: h2h4. It's your turn now.\n", + "\n", + "Your move.\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[31m\n", + ">>>>>>>> USING AUTO REPLY...\u001b[0m\n", + "\u001b[33mPlayer White\u001b[0m (to Board Proxy):\n", + "\n", + "\n", + "\u001b[32m***** Suggested tool call (No tool call id found): make_move *****\u001b[0m\n", + "Arguments: \n", + "{}\n", + "\u001b[32m******************************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[31m\n", + ">>>>>>>> USING AUTO REPLY...\u001b[0m\n", + "\u001b[35m\n", + ">>>>>>>> EXECUTING FUNCTION make_move...\u001b[0m\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",
+       "P . . . . . . .\n",
+       ". P P P P P P .\n",
+       "R N B Q K B N R
" + ], + "text/plain": [ + "'
r n b q k b . r\\np p p p . p p p\\n. . . . . . . n\\n. . . . p . . .\\n. . . . . . . P\\nP . . . . . . .\\n. 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[33mBoard Proxy\u001b[0m (to Player White):\n", + "\n", + "\u001b[33mBoard Proxy\u001b[0m (to Player White):\n", + "\n", + "\u001b[32m***** Response from calling tool (No id found) *****\u001b[0m\n", + "g8h6\n", + "\u001b[32m****************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[31m\n", + ">>>>>>>> USING AUTO REPLY...\u001b[0m\n", + "\u001b[33mPlayer White\u001b[0m (to Board Proxy):\n", + "\n", + "You moved g8h6. I made a move: g1g3. It's your turn now.\n", + "\n", + "Your move.\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[31m\n", + ">>>>>>>> NO HUMAN INPUT RECEIVED.\u001b[0m\n", + "\u001b[33mPlayer White\u001b[0m (to Player Black):\n", + "\n", + "You moved g8h6. I made a move: g1g3. It's your turn now.\n", + "\n", + "Your move.\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[31m\n", + ">>>>>>>> USING AUTO REPLY...\u001b[0m\n", + "\u001b[34m\n", + "********************************************************************************\u001b[0m\n", + "\u001b[34mStarting a new chat....\n", + "\n", + "Message:\n", + "You moved g8h6. I made a move: g1g3. It's your turn now.\n", + "\n", + "Your move.\n", + "\n", + "Carryover: \n", + "\u001b[0m\n", + "\u001b[34m\n", + "********************************************************************************\u001b[0m\n", + "\u001b[33mBoard Proxy\u001b[0m (to Player Black):\n", + "\n", + "You moved g8h6. I made a move: g1g3. It's your turn now.\n", + "\n", + "Your move.\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[31m\n", + ">>>>>>>> USING AUTO REPLY...\u001b[0m\n", + "\u001b[33mPlayer Black\u001b[0m (to Board Proxy):\n", + "\n", + "\n", + "\u001b[32m***** Suggested tool call (No tool call id found): make_move *****\u001b[0m\n", + "Arguments: \n", + "{}\n", + "\u001b[32m******************************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[31m\n", + ">>>>>>>> USING AUTO REPLY...\u001b[0m\n", + "\u001b[35m\n", + ">>>>>>>> EXECUTING FUNCTION make_move...\u001b[0m\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",
+       "P . . . . . . N\n",
+       ". 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\\nP . . . . . . N\\n. 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[33mBoard Proxy\u001b[0m (to Player Black):\n", + "\n", + "\u001b[33mBoard Proxy\u001b[0m (to Player Black):\n", + "\n", + "\u001b[32m***** Response from calling tool (No id found) *****\u001b[0m\n", + "g1h3\n", + "\u001b[32m****************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[31m\n", + ">>>>>>>> USING AUTO REPLY...\u001b[0m\n", + "\u001b[33mPlayer Black\u001b[0m (to Board Proxy):\n", + "\n", + "You moved g8h6. I made a move: g1h3. You moved g1h3. It's my turn now.\n", + "\n", + "I made a move: d2d4. Your move.\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[31m\n", + ">>>>>>>> NO HUMAN INPUT RECEIVED.\u001b[0m\n", + "\u001b[33mPlayer Black\u001b[0m (to Player White):\n", + "\n", + "You moved g8h6. I made a move: g1h3. You moved g1h3. It's my turn now.\n", + "\n", + "I made a move: d2d4. Your move.\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[31m\n", + ">>>>>>>> USING AUTO REPLY...\u001b[0m\n", + "\u001b[34m\n", + "********************************************************************************\u001b[0m\n", + "\u001b[34mStarting a new chat....\n", + "\n", + "Message:\n", + "You moved g8h6. I made a move: g1h3. You moved g1h3. It's my turn now.\n", + "\n", + "I made a move: d2d4. Your move.\n", + "\n", + "Carryover: \n", + "\u001b[0m\n", + "\u001b[34m\n", + "********************************************************************************\u001b[0m\n", + "\u001b[33mBoard Proxy\u001b[0m (to Player White):\n", + "\n", + "You moved g8h6. I made a move: g1h3. You moved g1h3. It's my turn now.\n", + "\n", + "I made a move: d2d4. Your move.\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[31m\n", + ">>>>>>>> USING AUTO REPLY...\u001b[0m\n", + "\u001b[33mPlayer White\u001b[0m (to Board Proxy):\n", + "\n", + "\n", + "\u001b[32m***** Suggested tool call (No tool call id found): make_move *****\u001b[0m\n", + "Arguments: \n", + "{}\n", + "\u001b[32m******************************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[31m\n", + ">>>>>>>> USING AUTO REPLY...\u001b[0m\n", + "\u001b[35m\n", + ">>>>>>>> EXECUTING FUNCTION make_move...\u001b[0m\n" + ] + }, + { + "data": { + "image/svg+xml": [ + "
r n b . k b . r\n",
+       "p p p p . p p p\n",
+       ". . . . . . . n\n",
+       ". . . . p . . .\n",
+       ". . . . . . . q\n",
+       "P . . . . . . N\n",
+       ". P P P P P P .\n",
+       "R N B Q K B . R
" + ], + "text/plain": [ + "'
r n b . k b . r\\np p p p . p p p\\n. . . . . . . n\\n. . . . p . . .\\n. . . . . . . q\\nP . . . . . . N\\n. 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[33mBoard Proxy\u001b[0m (to Player White):\n", + "\n", + "\u001b[33mBoard Proxy\u001b[0m (to Player White):\n", + "\n", + "\u001b[32m***** Response from calling tool (No id found) *****\u001b[0m\n", + "d8h4\n", + "\u001b[32m****************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[31m\n", + ">>>>>>>> USING AUTO REPLY...\u001b[0m\n", + "\u001b[33mPlayer White\u001b[0m (to Board Proxy):\n", + "\n", + "You moved d8h4. I made a move: d4d5. Your move.\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[31m\n", + ">>>>>>>> NO HUMAN INPUT RECEIVED.\u001b[0m\n", + "\u001b[33mPlayer White\u001b[0m (to Player Black):\n", + "\n", + "You moved d8h4. I made a move: d4d5. Your move.\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[31m\n", + ">>>>>>>> USING AUTO REPLY...\u001b[0m\n", + "\u001b[34m\n", + "********************************************************************************\u001b[0m\n", + "\u001b[34mStarting a new chat....\n", + "\n", + "Message:\n", + "You moved d8h4. I made a move: d4d5. Your move.\n", + "\n", + "Carryover: \n", + "\u001b[0m\n", + "\u001b[34m\n", + "********************************************************************************\u001b[0m\n", + "\u001b[33mBoard Proxy\u001b[0m (to Player Black):\n", + "\n", + "You moved d8h4. I made a move: d4d5. Your move.\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[31m\n", + ">>>>>>>> USING AUTO REPLY...\u001b[0m\n", + "\u001b[33mPlayer Black\u001b[0m (to Board Proxy):\n", + "\n", + "\n", + "\u001b[32m***** Suggested tool call (No tool call id found): make_move *****\u001b[0m\n", + "Arguments: \n", + "{}\n", + "\u001b[32m******************************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[31m\n", + ">>>>>>>> USING AUTO REPLY...\u001b[0m\n", + "\u001b[35m\n", + ">>>>>>>> EXECUTING FUNCTION make_move...\u001b[0m\n" + ] + }, + { + "data": { + "image/svg+xml": [ + "
r n b . k b . r\n",
+       "p p p p . p p p\n",
+       ". . . . . . . n\n",
+       ". . . . p . . .\n",
+       ". . . . P . . q\n",
+       "P . . . . . . N\n",
+       ". P P P . P P .\n",
+       "R N B Q K B . R
" + ], + "text/plain": [ + "'
r n b . k b . r\\np p p p . p p p\\n. . . . . . . n\\n. . . . p . . .\\n. . . . P . . q\\nP . . . . . . N\\n. P P P . P P .\\nR N B Q K B . R
'" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[33mBoard Proxy\u001b[0m (to Player Black):\n", + "\n", + "\u001b[33mBoard Proxy\u001b[0m (to Player Black):\n", + "\n", + "\u001b[32m***** Response from calling tool (No id found) *****\u001b[0m\n", + "e2e4\n", + "\u001b[32m****************************************************\u001b[0m\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[31m\n", + ">>>>>>>> USING AUTO REPLY...\u001b[0m\n", + "\u001b[33mPlayer Black\u001b[0m (to Board Proxy):\n", + "\n", + "You made a move: e2e4. I made a move: d5e4. Your move.\n", + "\n", + "--------------------------------------------------------------------------------\n", + "\u001b[31m\n", + ">>>>>>>> NO HUMAN INPUT RECEIVED.\u001b[0m\n", + "\u001b[33mPlayer Black\u001b[0m (to Player White):\n", + "\n", + "You made a move: e2e4. I made a move: d5e4. Your move.\n", + "\n", + "--------------------------------------------------------------------------------\n" + ] + } + ], + "source": [ + "# Clear the board.\n", + "board = chess.Board()\n", + "\n", + "chat_result = player_white.initiate_chat(\n", + " player_black,\n", + " message=\"Let's play chess! Your move.\",\n", + " max_turns=4,\n", + ")" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "autogen", + "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.11.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} From e40600c6af7ad72bf17ad01e052d208594767cfd Mon Sep 17 00:00:00 2001 From: Eric Zhu Date: Sun, 24 Mar 2024 01:33:14 -0700 Subject: [PATCH 2/3] formatting --- .../non-openai-models/cloud-mistralai.ipynb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/website/docs/topics/non-openai-models/cloud-mistralai.ipynb b/website/docs/topics/non-openai-models/cloud-mistralai.ipynb index 7e7753d7cc52..e22f408ba460 100644 --- a/website/docs/topics/non-openai-models/cloud-mistralai.ipynb +++ b/website/docs/topics/non-openai-models/cloud-mistralai.ipynb @@ -81,7 +81,7 @@ "user_proxy_agent = UserProxyAgent(\n", " name=\"User\",\n", " code_execution_config={\"executor\": code_executor},\n", - " is_termination_msg= lambda msg : \"TERMINATE\" in msg.get(\"content\"),\n", + " is_termination_msg=lambda msg: \"TERMINATE\" in msg.get(\"content\"),\n", ")\n", "\n", "assistant_agent = AssistantAgent(\n", @@ -211,12 +211,14 @@ "source": [ "import chess\n", "import chess.svg\n", + "\n", "# from IPython.display import display\n", "import random\n", "from typing_extensions import Annotated\n", "\n", "board = chess.Board()\n", "\n", + "\n", "def make_move() -> Annotated[str, \"A move in UCI format\"]:\n", " moves = list(board.legal_moves)\n", " move = random.choice(moves)\n", @@ -246,15 +248,13 @@ "\n", "player_white = ConversableAgent(\n", " name=\"Player White\",\n", - " system_message=\"You are a chess player and you play as white. \"\n", - " \"Always call make_move() to make a move\",\n", + " system_message=\"You are a chess player and you play as white. \" \"Always call make_move() to make a move\",\n", " llm_config={\"config_list\": config_list, \"cache_seed\": None},\n", ")\n", "\n", "player_black = ConversableAgent(\n", " name=\"Player Black\",\n", - " system_message=\"You are a chess player and you play as black. \"\n", - " \"Always call make_move() to make a move\",\n", + " system_message=\"You are a chess player and you play as black. \" \"Always call make_move() to make a move\",\n", " llm_config={\"config_list\": config_list, \"cache_seed\": None},\n", ")\n", "\n", @@ -262,7 +262,7 @@ " name=\"Board Proxy\",\n", " llm_config=False,\n", " # The board proxy will only respond to the make_move function.\n", - " is_termination_msg= lambda msg : not \"tool_calls\" in msg,\n", + " is_termination_msg=lambda msg: \"tool_calls\" not in msg,\n", ")" ] }, @@ -321,7 +321,7 @@ " \"sender\": board_proxy,\n", " \"recipient\": player_white,\n", " }\n", - " ]\n", + " ],\n", ")\n", "\n", "player_black.register_nested_chats(\n", @@ -331,7 +331,7 @@ " \"sender\": board_proxy,\n", " \"recipient\": player_black,\n", " }\n", - " ]\n", + " ],\n", ")" ] }, From 0bc4d4445f11d21530ece5f0077b6d4f9ff43273 Mon Sep 17 00:00:00 2001 From: Eric Zhu Date: Sun, 24 Mar 2024 01:34:48 -0700 Subject: [PATCH 3/3] formatting --- website/docs/topics/non-openai-models/cloud-mistralai.ipynb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/website/docs/topics/non-openai-models/cloud-mistralai.ipynb b/website/docs/topics/non-openai-models/cloud-mistralai.ipynb index e22f408ba460..9c18d6d04089 100644 --- a/website/docs/topics/non-openai-models/cloud-mistralai.ipynb +++ b/website/docs/topics/non-openai-models/cloud-mistralai.ipynb @@ -211,8 +211,7 @@ "source": [ "import chess\n", "import chess.svg\n", - "\n", - "# from IPython.display import display\n", + "from IPython.display import display\n", "import random\n", "from typing_extensions import Annotated\n", "\n",