|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "attachments": {}, |
| 5 | + "cell_type": "markdown", |
| 6 | + "id": "9a71fa36", |
| 7 | + "metadata": { |
| 8 | + "editable": true, |
| 9 | + "slideshow": { |
| 10 | + "slide_type": "" |
| 11 | + }, |
| 12 | + "tags": [] |
| 13 | + }, |
| 14 | + "source": [ |
| 15 | + "# Groupchat with Llamaindex agents\n", |
| 16 | + "\n", |
| 17 | + "[Llamaindex agents](https://docs.llamaindex.ai/en/stable/optimizing/agentic_strategies/agentic_strategies/) have the ability to use planning strategies to answer user questions. They can be integrated in Autogen in easy ways\n", |
| 18 | + "\n", |
| 19 | + "## Requirements" |
| 20 | + ] |
| 21 | + }, |
| 22 | + { |
| 23 | + "cell_type": "code", |
| 24 | + "execution_count": null, |
| 25 | + "id": "c528cd6d", |
| 26 | + "metadata": {}, |
| 27 | + "outputs": [], |
| 28 | + "source": [ |
| 29 | + "! pip install pyautogen\n", |
| 30 | + "! pip install llama-index\n", |
| 31 | + "! pip install llama-index-tools-wikipedia\n", |
| 32 | + "! pip install llama-index-readers-wikipedia\n", |
| 33 | + "! pip install wikipedia" |
| 34 | + ] |
| 35 | + }, |
| 36 | + { |
| 37 | + "attachments": {}, |
| 38 | + "cell_type": "markdown", |
| 39 | + "id": "5ebd2397", |
| 40 | + "metadata": { |
| 41 | + "editable": true, |
| 42 | + "slideshow": { |
| 43 | + "slide_type": "" |
| 44 | + }, |
| 45 | + "tags": [] |
| 46 | + }, |
| 47 | + "source": [ |
| 48 | + "## Set your API Endpoint" |
| 49 | + ] |
| 50 | + }, |
| 51 | + { |
| 52 | + "cell_type": "code", |
| 53 | + "execution_count": 2, |
| 54 | + "id": "dca301a4", |
| 55 | + "metadata": { |
| 56 | + "editable": true, |
| 57 | + "slideshow": { |
| 58 | + "slide_type": "" |
| 59 | + }, |
| 60 | + "tags": [] |
| 61 | + }, |
| 62 | + "outputs": [], |
| 63 | + "source": [ |
| 64 | + "import os\n", |
| 65 | + "\n", |
| 66 | + "import autogen\n", |
| 67 | + "\n", |
| 68 | + "config_list = [{\"model\": \"gpt-3.5-turbo-0125\", \"api_key\": os.getenv(\"OPENAI_API_KEY\")}]" |
| 69 | + ] |
| 70 | + }, |
| 71 | + { |
| 72 | + "cell_type": "markdown", |
| 73 | + "id": "76c11ea8", |
| 74 | + "metadata": {}, |
| 75 | + "source": [ |
| 76 | + "## Set Llamaindex" |
| 77 | + ] |
| 78 | + }, |
| 79 | + { |
| 80 | + "cell_type": "code", |
| 81 | + "execution_count": 3, |
| 82 | + "id": "2d3d298e", |
| 83 | + "metadata": {}, |
| 84 | + "outputs": [], |
| 85 | + "source": [ |
| 86 | + "from llama_index.core import Settings\n", |
| 87 | + "from llama_index.core.agent import ReActAgent\n", |
| 88 | + "from llama_index.embeddings.openai import OpenAIEmbedding\n", |
| 89 | + "from llama_index.llms.openai import OpenAI\n", |
| 90 | + "from llama_index.tools.wikipedia import WikipediaToolSpec\n", |
| 91 | + "\n", |
| 92 | + "llm = OpenAI(\n", |
| 93 | + " model=\"gpt-3.5-turbo-0125\",\n", |
| 94 | + " temperature=0.0,\n", |
| 95 | + " api_key=os.environ.get(\"OPENAPI_API_KEY\", \"\"),\n", |
| 96 | + ")\n", |
| 97 | + "\n", |
| 98 | + "embed_model = OpenAIEmbedding(\n", |
| 99 | + " model=\"text-embedding-ada-002\",\n", |
| 100 | + " temperature=0.0,\n", |
| 101 | + " api_key=os.environ.get(\"OPENAPI_API_KEY\", \"\"),\n", |
| 102 | + ")\n", |
| 103 | + "\n", |
| 104 | + "Settings.llm = llm\n", |
| 105 | + "Settings.embed_model = embed_model\n", |
| 106 | + "\n", |
| 107 | + "# create a react agent to use wikipedia tool\n", |
| 108 | + "wiki_spec = WikipediaToolSpec()\n", |
| 109 | + "# Get the search wikipedia tool\n", |
| 110 | + "wikipedia_tool = wiki_spec.to_tool_list()[1]\n", |
| 111 | + "\n", |
| 112 | + "location_specialist = ReActAgent.from_tools(tools=[wikipedia_tool], llm=llm, max_iterations=10, verbose=True)" |
| 113 | + ] |
| 114 | + }, |
| 115 | + { |
| 116 | + "attachments": {}, |
| 117 | + "cell_type": "markdown", |
| 118 | + "id": "2b9526e7", |
| 119 | + "metadata": {}, |
| 120 | + "source": [ |
| 121 | + "## Create agents\n", |
| 122 | + "\n", |
| 123 | + "In this example, we will create a Llamaindex agent to answer questions fecting data from wikipedia and a user proxy agent." |
| 124 | + ] |
| 125 | + }, |
| 126 | + { |
| 127 | + "cell_type": "code", |
| 128 | + "execution_count": null, |
| 129 | + "id": "1a10c9fe-1fbc-40c6-b655-5d2256864ce8", |
| 130 | + "metadata": {}, |
| 131 | + "outputs": [], |
| 132 | + "source": [ |
| 133 | + "from llamaindex_conversable_agent import LLamaIndexConversableAgent\n", |
| 134 | + "\n", |
| 135 | + "llm_config = {\n", |
| 136 | + " \"temperature\": 0,\n", |
| 137 | + " \"config_list\": config_list,\n", |
| 138 | + "}\n", |
| 139 | + "\n", |
| 140 | + "trip_assistant = LLamaIndexConversableAgent(\n", |
| 141 | + " \"trip_specialist\",\n", |
| 142 | + " llama_index_agent=location_specialist,\n", |
| 143 | + " system_message=\"You help customers finding more about places they would like to visit. You can use external resources to provide more details as you engage with the customer.\",\n", |
| 144 | + " description=\"This agents helps customers discover locations to visit, things to do, and other details about a location. It can use external resources to provide more details. This agent helps in finding attractions, history and all that there si to know about a place\",\n", |
| 145 | + ")\n", |
| 146 | + "\n", |
| 147 | + "user_proxy = autogen.UserProxyAgent(\n", |
| 148 | + " name=\"Admin\",\n", |
| 149 | + " human_input_mode=\"ALWAYS\",\n", |
| 150 | + " code_execution_config=False,\n", |
| 151 | + ")" |
| 152 | + ] |
| 153 | + }, |
| 154 | + { |
| 155 | + "attachments": {}, |
| 156 | + "cell_type": "markdown", |
| 157 | + "id": "966c96a4-cc8a-4400-b8db-a21b7142e33c", |
| 158 | + "metadata": {}, |
| 159 | + "source": [ |
| 160 | + "Next, let's set up our group chat." |
| 161 | + ] |
| 162 | + }, |
| 163 | + { |
| 164 | + "cell_type": "code", |
| 165 | + "execution_count": 3, |
| 166 | + "id": "354b4a8f-7a96-455b-9f17-cbc19d880462", |
| 167 | + "metadata": {}, |
| 168 | + "outputs": [], |
| 169 | + "source": [ |
| 170 | + "groupchat = autogen.GroupChat(\n", |
| 171 | + " agents=[trip_assistant, user_proxy],\n", |
| 172 | + " messages=[],\n", |
| 173 | + " max_round=500,\n", |
| 174 | + " speaker_selection_method=\"round_robin\",\n", |
| 175 | + " enable_clear_history=True,\n", |
| 176 | + ")\n", |
| 177 | + "manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=llm_config)" |
| 178 | + ] |
| 179 | + }, |
| 180 | + { |
| 181 | + "cell_type": "code", |
| 182 | + "execution_count": null, |
| 183 | + "id": "d5518947", |
| 184 | + "metadata": {}, |
| 185 | + "outputs": [], |
| 186 | + "source": [ |
| 187 | + "chat_result = user_proxy.initiate_chat(\n", |
| 188 | + " manager,\n", |
| 189 | + " message=\"\"\"\n", |
| 190 | + "What can i find in Tokyo related to Hayao Miyazaki and its moveis like Spirited Away?.\n", |
| 191 | + "\"\"\",\n", |
| 192 | + ")" |
| 193 | + ] |
| 194 | + } |
| 195 | + ], |
| 196 | + "metadata": { |
| 197 | + "front_matter": { |
| 198 | + "description": "Integrate llamaindex agents with Autogen.", |
| 199 | + "tags": [ |
| 200 | + "react", |
| 201 | + "llama index", |
| 202 | + "software engineering" |
| 203 | + ] |
| 204 | + }, |
| 205 | + "kernelspec": { |
| 206 | + "display_name": "Python 3 (ipykernel)", |
| 207 | + "language": "python", |
| 208 | + "name": "python3" |
| 209 | + }, |
| 210 | + "language_info": { |
| 211 | + "codemirror_mode": { |
| 212 | + "name": "ipython", |
| 213 | + "version": 3 |
| 214 | + }, |
| 215 | + "file_extension": ".py", |
| 216 | + "mimetype": "text/x-python", |
| 217 | + "name": "python", |
| 218 | + "nbconvert_exporter": "python", |
| 219 | + "pygments_lexer": "ipython3", |
| 220 | + "version": "3.11.9" |
| 221 | + } |
| 222 | + }, |
| 223 | + "nbformat": 4, |
| 224 | + "nbformat_minor": 5 |
| 225 | +} |
0 commit comments