|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Standalone AssistantAgent\n", |
| 8 | + "\n", |
| 9 | + "AG2 supports running `AssistantAgent` as a standalone agent with the ability to execute simple tasks without the need for interacting with other agents.\n", |
| 10 | + "\n", |
| 11 | + "To enable our assistant agent to surf the web, we will use `BrowserUseTool` fow which we need to install the browser-use optional dependency and [playwright](https://playwright.dev/python/docs/intro)\n", |
| 12 | + "\n", |
| 13 | + "````{=mdx}\n", |
| 14 | + ":::info Requirements\n", |
| 15 | + "Install `ag2`:\n", |
| 16 | + "```bash\n", |
| 17 | + "pip install ag2[browser-use]\n", |
| 18 | + "playwright install\n", |
| 19 | + "```\n", |
| 20 | + "````\n" |
| 21 | + ] |
| 22 | + }, |
| 23 | + { |
| 24 | + "cell_type": "code", |
| 25 | + "execution_count": null, |
| 26 | + "metadata": {}, |
| 27 | + "outputs": [], |
| 28 | + "source": [ |
| 29 | + "import autogen\n", |
| 30 | + "from autogen import AssistantAgent\n", |
| 31 | + "from autogen.tools.experimental.browser_use.browser_use import BrowserUseTool" |
| 32 | + ] |
| 33 | + }, |
| 34 | + { |
| 35 | + "cell_type": "code", |
| 36 | + "execution_count": 2, |
| 37 | + "metadata": {}, |
| 38 | + "outputs": [], |
| 39 | + "source": [ |
| 40 | + "import nest_asyncio\n", |
| 41 | + "\n", |
| 42 | + "nest_asyncio.apply()" |
| 43 | + ] |
| 44 | + }, |
| 45 | + { |
| 46 | + "cell_type": "markdown", |
| 47 | + "metadata": {}, |
| 48 | + "source": [ |
| 49 | + "## Set your API Endpoint\n", |
| 50 | + "\n", |
| 51 | + "The [`config_list_from_json`](https://docs.ag2.ai/reference/autogen/config_list_from_json#config-list-from-json) function loads a list of configurations from an environment variable or a json file." |
| 52 | + ] |
| 53 | + }, |
| 54 | + { |
| 55 | + "cell_type": "code", |
| 56 | + "execution_count": 3, |
| 57 | + "metadata": {}, |
| 58 | + "outputs": [], |
| 59 | + "source": [ |
| 60 | + "config_list = autogen.config_list_from_json(\n", |
| 61 | + " \"OAI_CONFIG_LIST\",\n", |
| 62 | + " filter_dict={\n", |
| 63 | + " \"tags\": [\"gpt-4o-mini\"],\n", |
| 64 | + " },\n", |
| 65 | + ")\n", |
| 66 | + "\n", |
| 67 | + "llm_config = {\n", |
| 68 | + " \"timeout\": 600,\n", |
| 69 | + " \"config_list\": config_list,\n", |
| 70 | + " \"temperature\": 0.8,\n", |
| 71 | + "}" |
| 72 | + ] |
| 73 | + }, |
| 74 | + { |
| 75 | + "cell_type": "markdown", |
| 76 | + "metadata": {}, |
| 77 | + "source": [ |
| 78 | + "# Configure your assistant agent\n", |
| 79 | + "\n", |
| 80 | + "Here we will configure two assistant agents:\n", |
| 81 | + "1. x_assistant, tasked with exploring the trending topics on X (Formally Twitter)\n", |
| 82 | + "2. arxiv_researcher, tasked with discovery of papers that allign with the hot topic of the day" |
| 83 | + ] |
| 84 | + }, |
| 85 | + { |
| 86 | + "cell_type": "code", |
| 87 | + "execution_count": null, |
| 88 | + "metadata": {}, |
| 89 | + "outputs": [], |
| 90 | + "source": [ |
| 91 | + "x_assistant = AssistantAgent(name=\"x_assistant\", llm_config=llm_config)\n", |
| 92 | + "\n", |
| 93 | + "arxiv_researcher = AssistantAgent(name=\"arxiv\", llm_config=llm_config)\n", |
| 94 | + "\n", |
| 95 | + "browser_use_tool = BrowserUseTool(\n", |
| 96 | + " llm_config=llm_config,\n", |
| 97 | + ")" |
| 98 | + ] |
| 99 | + }, |
| 100 | + { |
| 101 | + "cell_type": "markdown", |
| 102 | + "metadata": {}, |
| 103 | + "source": [ |
| 104 | + "## Running the assistant agents" |
| 105 | + ] |
| 106 | + }, |
| 107 | + { |
| 108 | + "cell_type": "markdown", |
| 109 | + "metadata": {}, |
| 110 | + "source": [ |
| 111 | + "Lets run our x_assistant to discover the hot topic of the day\n", |
| 112 | + "To be able to do this let's give our assistant the capability to browse the web using a `BrowserUseTool`" |
| 113 | + ] |
| 114 | + }, |
| 115 | + { |
| 116 | + "cell_type": "code", |
| 117 | + "execution_count": null, |
| 118 | + "metadata": {}, |
| 119 | + "outputs": [], |
| 120 | + "source": [ |
| 121 | + "hot_topic_res = x_assistant.run(\n", |
| 122 | + " \"Find out today's hot topic and an influencer who is talking about it on X using a web search\",\n", |
| 123 | + " tools=browser_use_tool,\n", |
| 124 | + ")\n", |
| 125 | + "\n", |
| 126 | + "print(hot_topic_res)" |
| 127 | + ] |
| 128 | + }, |
| 129 | + { |
| 130 | + "cell_type": "markdown", |
| 131 | + "metadata": {}, |
| 132 | + "source": [ |
| 133 | + "After discovering the hot topic, lets run the discovery of papers that allign with the topic" |
| 134 | + ] |
| 135 | + }, |
| 136 | + { |
| 137 | + "cell_type": "code", |
| 138 | + "execution_count": null, |
| 139 | + "metadata": {}, |
| 140 | + "outputs": [], |
| 141 | + "source": [ |
| 142 | + "paper_abstract = arxiv_researcher.run(\n", |
| 143 | + " \"Get the abstract of a relevant paper based on \" + hot_topic_res,\n", |
| 144 | + ")\n", |
| 145 | + "\n", |
| 146 | + "print(paper_abstract)" |
| 147 | + ] |
| 148 | + }, |
| 149 | + { |
| 150 | + "cell_type": "markdown", |
| 151 | + "metadata": {}, |
| 152 | + "source": [ |
| 153 | + "Now, Lets create a twitter post using our x_assistant" |
| 154 | + ] |
| 155 | + }, |
| 156 | + { |
| 157 | + "cell_type": "code", |
| 158 | + "execution_count": null, |
| 159 | + "metadata": {}, |
| 160 | + "outputs": [], |
| 161 | + "source": [ |
| 162 | + "# Secneario 1. This task requires x_assistant's past state\n", |
| 163 | + "post = x_assistant.run(\n", |
| 164 | + " \"Create an X post based on the hot topic and this \" + paper_abstract + \"and mention the influencer\",\n", |
| 165 | + ")\n", |
| 166 | + "\n", |
| 167 | + "print(post)" |
| 168 | + ] |
| 169 | + }, |
| 170 | + { |
| 171 | + "cell_type": "markdown", |
| 172 | + "metadata": {}, |
| 173 | + "source": [ |
| 174 | + "Finally, lets ask our x_assistant who should we follow on twitter" |
| 175 | + ] |
| 176 | + }, |
| 177 | + { |
| 178 | + "cell_type": "code", |
| 179 | + "execution_count": null, |
| 180 | + "metadata": {}, |
| 181 | + "outputs": [], |
| 182 | + "source": [ |
| 183 | + "# Scenario 2. Doing another task that does not require history or past state\n", |
| 184 | + "\n", |
| 185 | + "influencer_choice = x_assistant.run(\n", |
| 186 | + " \"Find a influencer I should follow on Twitter by searching the web\",\n", |
| 187 | + " clear_history=True,\n", |
| 188 | + " tools=browser_use_tool,\n", |
| 189 | + ")\n", |
| 190 | + "\n", |
| 191 | + "print(influencer_choice)" |
| 192 | + ] |
| 193 | + } |
| 194 | + ], |
| 195 | + "metadata": { |
| 196 | + "kernelspec": { |
| 197 | + "display_name": ".venv", |
| 198 | + "language": "python", |
| 199 | + "name": "python3" |
| 200 | + }, |
| 201 | + "language_info": { |
| 202 | + "codemirror_mode": { |
| 203 | + "name": "ipython", |
| 204 | + "version": 3 |
| 205 | + }, |
| 206 | + "file_extension": ".py", |
| 207 | + "mimetype": "text/x-python", |
| 208 | + "name": "python", |
| 209 | + "nbconvert_exporter": "python", |
| 210 | + "pygments_lexer": "ipython3", |
| 211 | + "version": "3.11.11" |
| 212 | + } |
| 213 | + }, |
| 214 | + "nbformat": 4, |
| 215 | + "nbformat_minor": 2 |
| 216 | +} |
0 commit comments