|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "## RAG OpenAI Assistants in AutoGen\n", |
| 8 | + "\n", |
| 9 | + "This notebook shows an example of the [`GPTAssistantAgent`](https://github.com/microsoft/autogen/blob/main/autogen/agentchat/contrib/gpt_assistant_agent.py#L16C43-L16C43) with retrieval augmented generation. `GPTAssistantAgent` 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 | + "What is the name of the class of agents I gave you?\n", |
| 32 | + "\n", |
| 33 | + "--------------------------------------------------------------------------------\n", |
| 34 | + "\u001b[33massistant\u001b[0m (to user_proxy):\n", |
| 35 | + "\n", |
| 36 | + "The class of agents provided in the file is called `ConversableAgent`.\n", |
| 37 | + "\n", |
| 38 | + "\n", |
| 39 | + "--------------------------------------------------------------------------------\n", |
| 40 | + "\u001b[33muser_proxy\u001b[0m (to assistant):\n", |
| 41 | + "\n", |
| 42 | + "What does it do?\n", |
| 43 | + "\n", |
| 44 | + "--------------------------------------------------------------------------------\n", |
| 45 | + "\u001b[33massistant\u001b[0m (to user_proxy):\n", |
| 46 | + "\n", |
| 47 | + "The `ConversableAgent` class is designed as a generic conversable agent that can be configured to act either as an assistant or user proxy. When it receives a message, it automatically generates and sends a reply unless the message is a termination message. It features a method to initiate a chat with another agent and can have its auto-reply behavior adjusted by overriding the `generate_reply` method.\n", |
| 48 | + "\n", |
| 49 | + "Here are some specific functionalities and mechanisms included in the `ConversableAgent` class:\n", |
| 50 | + "\n", |
| 51 | + "- It can process received messages and decide whether or not a reply is necessary or requested.\n", |
| 52 | + "- It can reset consecutive auto-reply counters and clear chat history when starting a new conversation.\n", |
| 53 | + "- It allows initiating chats either synchronously or asynchronously with the ability to pass context information and control chattiness.\n", |
| 54 | + "- It has the ability to register reply functions with specific triggers and order them based on preference.\n", |
| 55 | + "- The class supports setting a maximum number of consecutive auto-replies, after which it can prompt for human input based on configured criteria (e.g., always, on termination, or never).\n", |
| 56 | + "- The auto-reply trigger mechanism can be finely controlled by associating reply functions with triggers such as instances of particular classes, specific strings, or custom callable conditions.\n", |
| 57 | + "\n", |
| 58 | + "This class provides an extensible framework for creating bots or agents that can interact in a chat-like context, with custom behavior that developers can tailor to specific applications.\n", |
| 59 | + "\n", |
| 60 | + "\n", |
| 61 | + "--------------------------------------------------------------------------------\n", |
| 62 | + "\u001b[31m\n", |
| 63 | + ">>>>>>>> NO HUMAN INPUT RECEIVED.\u001b[0m\n", |
| 64 | + "\u001b[31m\n", |
| 65 | + ">>>>>>>> USING AUTO REPLY...\u001b[0m\n", |
| 66 | + "\u001b[33muser_proxy\u001b[0m (to assistant):\n", |
| 67 | + "\n", |
| 68 | + "\n", |
| 69 | + "\n", |
| 70 | + "--------------------------------------------------------------------------------\n", |
| 71 | + "\u001b[33massistant\u001b[0m (to user_proxy):\n", |
| 72 | + "\n", |
| 73 | + "It seems that your request was incomplete. Could you please provide more information or clarify your request?\n", |
| 74 | + "\n", |
| 75 | + "\n", |
| 76 | + "--------------------------------------------------------------------------------\n" |
| 77 | + ] |
| 78 | + }, |
| 79 | + { |
| 80 | + "name": "stderr", |
| 81 | + "output_type": "stream", |
| 82 | + "text": [ |
| 83 | + "Permanently deleting assistant...\n" |
| 84 | + ] |
| 85 | + } |
| 86 | + ], |
| 87 | + "source": [ |
| 88 | + "import logging\n", |
| 89 | + "import os\n", |
| 90 | + " \n", |
| 91 | + "from autogen import config_list_from_json\n", |
| 92 | + "from autogen.agentchat.contrib.gpt_assistant_agent import GPTAssistantAgent\n", |
| 93 | + "from autogen import UserProxyAgent\n", |
| 94 | + "\n", |
| 95 | + "logger = logging.getLogger(__name__)\n", |
| 96 | + "logger.setLevel(logging.WARNING)\n", |
| 97 | + "\n", |
| 98 | + "assistant_id = os.environ.get(\"ASSISTANT_ID\", None)\n", |
| 99 | + "\n", |
| 100 | + "config_list = config_list_from_json(\"OAI_CONFIG_LIST\")\n", |
| 101 | + "llm_config = {\n", |
| 102 | + " \"config_list\": config_list,\n", |
| 103 | + " \"assistant_id\": assistant_id,\n", |
| 104 | + " \"tools\": [\n", |
| 105 | + " {\n", |
| 106 | + " \"type\": \"retrieval\"\n", |
| 107 | + " }\n", |
| 108 | + " ],\n", |
| 109 | + " \"file_ids\": [\"file-CmlT0YKLB3ZCdHmslF9FOv69\"] \n", |
| 110 | + " # add id of an existing file our openai account\n", |
| 111 | + " # in this case I added the implementation of conversable_agent.py\n", |
| 112 | + "}\n", |
| 113 | + "\n", |
| 114 | + "gpt_assistant = GPTAssistantAgent(name=\"assistant\",\n", |
| 115 | + " instructions=\"You are adapt at question answering\",\n", |
| 116 | + " llm_config=llm_config)\n", |
| 117 | + "\n", |
| 118 | + "user_proxy = UserProxyAgent(name=\"user_proxy\",\n", |
| 119 | + " code_execution_config=False,\n", |
| 120 | + " is_termination_msg=lambda msg: \"TERMINATE\" in msg[\"content\"],\n", |
| 121 | + " human_input_mode=\"ALWAYS\")\n", |
| 122 | + "user_proxy.initiate_chat(gpt_assistant, message=\"What is the name of the class of agents I gave you?\")\n", |
| 123 | + "\n", |
| 124 | + "gpt_assistant.delete_assistant()" |
| 125 | + ] |
| 126 | + } |
| 127 | + ], |
| 128 | + "metadata": { |
| 129 | + "kernelspec": { |
| 130 | + "display_name": "Python 3", |
| 131 | + "language": "python", |
| 132 | + "name": "python3" |
| 133 | + }, |
| 134 | + "language_info": { |
| 135 | + "codemirror_mode": { |
| 136 | + "name": "ipython", |
| 137 | + "version": 3 |
| 138 | + }, |
| 139 | + "file_extension": ".py", |
| 140 | + "mimetype": "text/x-python", |
| 141 | + "name": "python", |
| 142 | + "nbconvert_exporter": "python", |
| 143 | + "pygments_lexer": "ipython3", |
| 144 | + "version": "3.10.12" |
| 145 | + } |
| 146 | + }, |
| 147 | + "nbformat": 4, |
| 148 | + "nbformat_minor": 2 |
| 149 | +} |
0 commit comments