|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "## OpenAI Assistants in AutoGen\n", |
| 8 | + "\n", |
| 9 | + "This notebook shows a very basic example of the [`GPTAssistantAgent`](https://github.com/microsoft/autogen/blob/main/autogen/agentchat/contrib/gpt_assistant_agent.py#L16C43-L16C43), which is an experimental AutoGen agent class that leverages the [OpenAI Assistant API](https://platform.openai.com/docs/assistants/overview) for conversational capabilities, working with\n", |
| 10 | + "`UserProxyAgent` in AutoGen." |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "code", |
| 15 | + "execution_count": 1, |
| 16 | + "metadata": {}, |
| 17 | + "outputs": [ |
| 18 | + { |
| 19 | + "name": "stderr", |
| 20 | + "output_type": "stream", |
| 21 | + "text": [ |
| 22 | + "assistant_id was None, creating a new assistant\n" |
| 23 | + ] |
| 24 | + }, |
| 25 | + { |
| 26 | + "name": "stdout", |
| 27 | + "output_type": "stream", |
| 28 | + "text": [ |
| 29 | + "\u001b[33muser_proxy\u001b[0m (to assistant):\n", |
| 30 | + "\n", |
| 31 | + "Print hello world\n", |
| 32 | + "\n", |
| 33 | + "--------------------------------------------------------------------------------\n", |
| 34 | + "\u001b[33massistant\u001b[0m (to user_proxy):\n", |
| 35 | + "\n", |
| 36 | + "```python\n", |
| 37 | + "print(\"Hello, World!\")\n", |
| 38 | + "```\n", |
| 39 | + "\n", |
| 40 | + "Please run this Python code to print \"Hello, World!\" to the console.\n", |
| 41 | + "\n", |
| 42 | + "\n", |
| 43 | + "--------------------------------------------------------------------------------\n", |
| 44 | + "\u001b[31m\n", |
| 45 | + ">>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is python)...\u001b[0m\n" |
| 46 | + ] |
| 47 | + }, |
| 48 | + { |
| 49 | + "name": "stderr", |
| 50 | + "output_type": "stream", |
| 51 | + "text": [ |
| 52 | + "execute_code was called without specifying a value for use_docker. Since the python docker package is not available, code will be run natively. Note: this fallback behavior is subject to change\n" |
| 53 | + ] |
| 54 | + }, |
| 55 | + { |
| 56 | + "name": "stdout", |
| 57 | + "output_type": "stream", |
| 58 | + "text": [ |
| 59 | + "\u001b[33muser_proxy\u001b[0m (to assistant):\n", |
| 60 | + "\n", |
| 61 | + "exitcode: 0 (execution succeeded)\n", |
| 62 | + "Code output: \n", |
| 63 | + "Hello, World!\n", |
| 64 | + "\n", |
| 65 | + "\n", |
| 66 | + "--------------------------------------------------------------------------------\n", |
| 67 | + "\u001b[33massistant\u001b[0m (to user_proxy):\n", |
| 68 | + "\n", |
| 69 | + "The code executed successfully and printed \"Hello, World!\" as expected.\n", |
| 70 | + "\n", |
| 71 | + "TERMINATE\n", |
| 72 | + "\n", |
| 73 | + "\n", |
| 74 | + "--------------------------------------------------------------------------------\n" |
| 75 | + ] |
| 76 | + } |
| 77 | + ], |
| 78 | + "source": [ |
| 79 | + "import logging\n", |
| 80 | + "import os\n", |
| 81 | + " \n", |
| 82 | + "from autogen import config_list_from_json\n", |
| 83 | + "from autogen import AssistantAgent\n", |
| 84 | + "from autogen.agentchat.contrib.gpt_assistant_agent import GPTAssistantAgent\n", |
| 85 | + "from autogen import UserProxyAgent\n", |
| 86 | + "\n", |
| 87 | + "logger = logging.getLogger(__name__)\n", |
| 88 | + "logger.setLevel(logging.WARNING)\n", |
| 89 | + "\n", |
| 90 | + "assistant_id = os.environ.get(\"ASSISTANT_ID\", None)\n", |
| 91 | + "\n", |
| 92 | + "config_list = config_list_from_json(\"OAI_CONFIG_LIST\")\n", |
| 93 | + "llm_config = {\n", |
| 94 | + " \"config_list\": config_list,\n", |
| 95 | + " \"assistant_id\": assistant_id\n", |
| 96 | + "}\n", |
| 97 | + "\n", |
| 98 | + "gpt_assistant = GPTAssistantAgent(name=\"assistant\",\n", |
| 99 | + " instructions=AssistantAgent.DEFAULT_SYSTEM_MESSAGE,\n", |
| 100 | + " llm_config=llm_config)\n", |
| 101 | + "\n", |
| 102 | + "user_proxy = UserProxyAgent(name=\"user_proxy\",\n", |
| 103 | + " code_execution_config={\n", |
| 104 | + " \"work_dir\": \"coding\"\n", |
| 105 | + " },\n", |
| 106 | + " is_termination_msg=lambda msg: \"TERMINATE\" in msg[\"content\"],\n", |
| 107 | + " human_input_mode=\"NEVER\",\n", |
| 108 | + " max_consecutive_auto_reply=1)\n", |
| 109 | + "user_proxy.initiate_chat(gpt_assistant, message=\"Print hello world\")" |
| 110 | + ] |
| 111 | + }, |
| 112 | + { |
| 113 | + "cell_type": "code", |
| 114 | + "execution_count": 2, |
| 115 | + "metadata": {}, |
| 116 | + "outputs": [ |
| 117 | + { |
| 118 | + "name": "stdout", |
| 119 | + "output_type": "stream", |
| 120 | + "text": [ |
| 121 | + "\u001b[33muser_proxy\u001b[0m (to assistant):\n", |
| 122 | + "\n", |
| 123 | + "Write py code to eval 2 + 2\n", |
| 124 | + "\n", |
| 125 | + "--------------------------------------------------------------------------------\n", |
| 126 | + "\u001b[33massistant\u001b[0m (to user_proxy):\n", |
| 127 | + "\n", |
| 128 | + "```python\n", |
| 129 | + "# Let's write a simple Python code to evaluate 2 + 2 and print the result.\n", |
| 130 | + "\n", |
| 131 | + "result = 2 + 2\n", |
| 132 | + "print(result)\n", |
| 133 | + "```\n", |
| 134 | + "\n", |
| 135 | + "\n", |
| 136 | + "--------------------------------------------------------------------------------\n", |
| 137 | + "\u001b[31m\n", |
| 138 | + ">>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is python)...\u001b[0m\n" |
| 139 | + ] |
| 140 | + }, |
| 141 | + { |
| 142 | + "name": "stderr", |
| 143 | + "output_type": "stream", |
| 144 | + "text": [ |
| 145 | + "execute_code was called without specifying a value for use_docker. Since the python docker package is not available, code will be run natively. Note: this fallback behavior is subject to change\n" |
| 146 | + ] |
| 147 | + }, |
| 148 | + { |
| 149 | + "name": "stdout", |
| 150 | + "output_type": "stream", |
| 151 | + "text": [ |
| 152 | + "\u001b[33muser_proxy\u001b[0m (to assistant):\n", |
| 153 | + "\n", |
| 154 | + "exitcode: 0 (execution succeeded)\n", |
| 155 | + "Code output: \n", |
| 156 | + "4\n", |
| 157 | + "\n", |
| 158 | + "\n", |
| 159 | + "--------------------------------------------------------------------------------\n", |
| 160 | + "\u001b[33massistant\u001b[0m (to user_proxy):\n", |
| 161 | + "\n", |
| 162 | + "The Python code was executed successfully and the result of evaluating 2 + 2 is 4.\n", |
| 163 | + "\n", |
| 164 | + "TERMINATE\n", |
| 165 | + "\n", |
| 166 | + "\n", |
| 167 | + "--------------------------------------------------------------------------------\n" |
| 168 | + ] |
| 169 | + } |
| 170 | + ], |
| 171 | + "source": [ |
| 172 | + "user_proxy.initiate_chat(gpt_assistant, message=\"Write py code to eval 2 + 2\", clear_history=True)" |
| 173 | + ] |
| 174 | + }, |
| 175 | + { |
| 176 | + "cell_type": "code", |
| 177 | + "execution_count": 3, |
| 178 | + "metadata": {}, |
| 179 | + "outputs": [ |
| 180 | + { |
| 181 | + "name": "stderr", |
| 182 | + "output_type": "stream", |
| 183 | + "text": [ |
| 184 | + "Permanently deleting assistant...\n" |
| 185 | + ] |
| 186 | + } |
| 187 | + ], |
| 188 | + "source": [ |
| 189 | + "gpt_assistant.delete_assistant()" |
| 190 | + ] |
| 191 | + } |
| 192 | + ], |
| 193 | + "metadata": { |
| 194 | + "kernelspec": { |
| 195 | + "display_name": "Python 3", |
| 196 | + "language": "python", |
| 197 | + "name": "python3" |
| 198 | + }, |
| 199 | + "language_info": { |
| 200 | + "codemirror_mode": { |
| 201 | + "name": "ipython", |
| 202 | + "version": 3 |
| 203 | + }, |
| 204 | + "file_extension": ".py", |
| 205 | + "mimetype": "text/x-python", |
| 206 | + "name": "python", |
| 207 | + "nbconvert_exporter": "python", |
| 208 | + "pygments_lexer": "ipython3", |
| 209 | + "version": "3.10.12" |
| 210 | + } |
| 211 | + }, |
| 212 | + "nbformat": 4, |
| 213 | + "nbformat_minor": 2 |
| 214 | +} |
0 commit comments