autogen/notebook/autogen_agentchat_groupchat.ipynb

320 lines
13 KiB
Plaintext
Raw Normal View History

{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"<a href=\"https://colab.research.google.com/github/microsoft/FLAML/blob/main/notebook/autogen_agentchat_groupchat.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Auto Generated Agent Chat: Group Chat\n",
"\n",
"`flaml.autogen` offers conversable agents powered by LLM, tool or human, which can be used to perform tasks collectively via automated chat. This framwork allows tool use and human participance through multi-agent conversation.\n",
"Please find documentation about this feature [here](https://microsoft.github.io/FLAML/docs/Use-Cases/Autogen#agents).\n",
"\n",
"This notebook is modified based on https://github.com/microsoft/FLAML/blob/4ea686af5c3e8ff24d9076a7a626c8b28ab5b1d7/notebook/autogen_multiagent_roleplay_chat.ipynb\n",
"\n",
"## Requirements\n",
"\n",
"FLAML requires `Python>=3.8`. To run this notebook example, please install flaml with the [autogen] option:\n",
"```bash\n",
"pip install flaml[autogen]\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"%%capture --no-stderr\n",
"# %pip install flaml[autogen]~=2.0.1"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Set your API Endpoint\n",
"\n",
"The [`config_list_from_json`](https://microsoft.github.io/FLAML/docs/reference/autogen/oai/openai_utils#config_list_from_json) function loads a list of configurations from an environment variable or a json file."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from flaml import autogen\n",
"\n",
"config_list_gpt4 = autogen.config_list_from_json(\n",
" \"OAI_CONFIG_LIST\",\n",
" filter_dict={\n",
" \"model\": [\"gpt-4\", \"gpt-4-0314\", \"gpt4\", \"gpt-4-32k\", \"gpt-4-32k-0314\", \"gpt-4-32k-v0314\"],\n",
" },\n",
")\n",
"# config_list_gpt35 = autogen.config_list_from_json(\n",
"# \"OAI_CONFIG_LIST\",\n",
"# filter_dict={\n",
"# \"model\": {\n",
"# \"gpt-3.5-turbo\",\n",
"# \"gpt-3.5-turbo-16k\",\n",
"# \"gpt-3.5-turbo-0301\",\n",
"# \"chatgpt-35-turbo-0301\",\n",
"# \"gpt-35-turbo-v0301\",\n",
"# },\n",
"# },\n",
"# )"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"It first looks for environment variable \"OAI_CONFIG_LIST\" which needs to be a valid json string. If that variable is not found, it then looks for a json file named \"OAI_CONFIG_LIST\". It filters the configs by models (you can filter by other keys as well). Only the gpt-4 models are kept in the list based on the filter condition.\n",
"\n",
"The config list looks like the following:\n",
"```python\n",
"config_list = [\n",
" {\n",
" 'model': 'gpt-4',\n",
" 'api_key': '<your OpenAI API key here>',\n",
" },\n",
" {\n",
" 'model': 'gpt-4',\n",
" 'api_key': '<your Azure OpenAI API key here>',\n",
" 'api_base': '<your Azure OpenAI API base here>',\n",
" 'api_type': 'azure',\n",
" 'api_version': '2023-06-01-preview',\n",
" },\n",
" {\n",
" 'model': 'gpt-4-32k',\n",
" 'api_key': '<your Azure OpenAI API key here>',\n",
" 'api_base': '<your Azure OpenAI API base here>',\n",
" 'api_type': 'azure',\n",
" 'api_version': '2023-06-01-preview',\n",
" },\n",
"]\n",
"```\n",
"\n",
"If you open this notebook in colab, you can upload your files by clicking the file icon on the left panel and then choose \"upload file\" icon.\n",
"\n",
"You can set the value of config_list in other ways you prefer, e.g., loading from a YAML file."
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Construct Agents"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"llm_config = {\"config_list\": config_list_gpt4}\n",
"human = autogen.UserProxyAgent(\n",
" name=\"Human\",\n",
" system_message=\"A human admin.\",\n",
" code_execution_config={\"last_n_messages\": 2, \"work_dir\": \"groupchat\"},\n",
")\n",
"alice = autogen.AssistantAgent(\n",
" name=\"Alice\",\n",
" llm_config=llm_config,\n",
")\n",
"bob = autogen.AssistantAgent(\n",
" name=\"Bob\",\n",
" system_message=\"Code and answer reviewer.\"\n",
" \"For code, prevent code execution if unsafe or missing important details, e.g., sort order in arxiv API. Suggest changes. Otherwise, approve and return the final code to execute.\"\n",
" \"For answer, carefully check the interpretation of code result and fix any errors. If the interpretation is correct, approve and return the final answer to the user.\",\n",
" llm_config=llm_config,\n",
")\n",
"groupchat = autogen.GroupChat(agents=[human, alice, bob], messages=[], max_round=12)\n",
"manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=llm_config)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Start Chat"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[33mHuman\u001b[0m (to chat_manager):\n",
"\n",
"Find a latest paper about gpt-4\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001b[33mAlice\u001b[0m (to chat_manager):\n",
"\n",
"To find the latest papers about gpt-4, we can use APIs of scholarly databases like Google Scholar, PubMed, Arxiv, etc., or automation tools like Beautiful Soup to scrape the information from webpages. However, a significant number of these methods are in violation of the terms of service of these platforms. Therefore, it's recommended to manually search these databases. \n",
"\n",
"But in our case, we'll use the arXiv API, which is a freely accessible database. It holds a vast collection of articles in the field of computer science and many other disciplines. It is often used by researchers to share their papers before they are published, so it could include articles about GPT-4. \n",
"\n",
"The following Python code uses the requests and feedparser libraries to do this. Requests is used to make a GET request to the arXiv API with the search query as a parameter. Feedparser is used to parse the returned RSS feed. The result is the information of the most recent articles based on publication time related to gpt-4 from arXiv. \n",
"\n",
"```python\n",
"# python code\n",
"import requests\n",
"import feedparser\n",
"\n",
"# Search arXiv API for papers related to gpt-4\n",
"url = 'http://export.arxiv.org/api/query'\n",
"params = {'search_query': 'all:gpt-4', 'sortBy': 'submittedDate', 'sortOrder': 'descending'}\n",
"response = requests.get(url, params=params)\n",
"\n",
"# Parse the response\n",
"feeds = feedparser.parse(response.content)\n",
"\n",
"# should check if feeds.entries is empty\n",
"\n",
"# Get the first paper's information\n",
"latest_paper = feeds.entries[0]\n",
"\n",
"# Print the paper's title, authors, and published date\n",
"print('Title: ', latest_paper['title'])\n",
"print('Authors: ', latest_paper['author'])\n",
"print('Published Date: ', latest_paper['published'])\n",
"```\n",
"You need to install requests and feedparser in your python environment, if they are not installed yet.\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001b[33mBob\u001b[0m (to chat_manager):\n",
"\n",
"The provided code is mostly correct, but it is missing some important error handling measures. As mentioned in the comments, it does not check if the feed entries are empty. It also does not check if the request was successful before trying to parse the response.\n",
"\n",
"Additionally, the code does not handle any exceptions that may occur during the process, such adding try/except clause would make it more robust.\n",
"\n",
"Here's a suggestion on how to improve it:\n",
"\n",
"```python\n",
"# python code\n",
"import requests\n",
"import feedparser\n",
"\n",
"# Search arXiv API for papers related to gpt-4\n",
"url = 'http://export.arxiv.org/api/query'\n",
"params = {'search_query': 'all:gpt-4', 'sortBy': 'submittedDate', 'sortOrder': 'descending'}\n",
"\n",
"try:\n",
" response = requests.get(url, params=params)\n",
" response.raise_for_status()\n",
"except requests.HTTPError as http_err:\n",
" print(f'HTTP error occurred: {http_err}')\n",
"except requests.ConnectionError as conn_err:\n",
" print(f'Error connecting: {conn_err}')\n",
"except requests.Timeout as to_err:\n",
" print(f'Timeout error: {to_err}')\n",
"except requests.RequestException as err:\n",
" print(f'An error occurred: {err}')\n",
"else:\n",
" # Parse the response\n",
" feeds = feedparser.parse(response.content)\n",
"\n",
" # Check if feeds.entries is empty\n",
" if not feeds.entries:\n",
" print(\"No results found.\")\n",
" else: \n",
" # Get the first paper's information\n",
" latest_paper = feeds.entries[0]\n",
"\n",
" # Print the paper's title, authors, and published date\n",
" print('Title: ', latest_paper['title'])\n",
" print('Authors: ', latest_paper['author'])\n",
" print('Published Date: ', latest_paper['published'])\n",
"```\n",
"This version of the script will handle the HTTP, Connection, and Timeout errors as well as any other request error. It also checks that there are results before trying to print.\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 0 (inferred language is python)...\u001b[0m\n",
"\u001b[33mHuman\u001b[0m (to chat_manager):\n",
"\n",
"exitcode: 0 (execution succeeded)\n",
"Code output: \n",
"Title: Language as Reality: A Co-Creative Storytelling Game Experience in 1001\n",
" Nights using Generative AI\n",
"Authors: Ali Asadipour\n",
"Published Date: 2023-08-24T16:42:23Z\n",
"\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001b[33mAlice\u001b[0m (to chat_manager):\n",
"\n",
"Based on the output of the code, the latest paper on GPT-4 found was \"Language as Reality: A Co-Creative Storytelling Game Experience in 1001 Nights using Generative AI\" by Ali Asadipour, published on 24th August 2023. \n",
"\n",
"This shows the latest found document related to the search term \"GPT-4\" in the arXiv scholarly paper database. Please note that the search query includes all parts of the articles: the title, the abstract, and the full text if available, so it might not be necessarily about GPT-4 but includes the term in some section of the paper.\n",
"\n",
"TERMINATE\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001b[33mBob\u001b[0m (to chat_manager):\n",
"\n",
"The interpretation of the code output is correct. The latest document retrieved related to \"GPT-4\" is indeed \"Language as Reality: A Co-Creative Storytelling Game Experience in 1001 Nights using Generative AI\" by Ali Asadipour, published on 24th August 2023.\n",
"\n",
"Make sure to understand that the document might not be solely about GPT-4 as the search used covered all aspects of the document. The term \"GPT-4\" could be mentioned anywhere in the paper.\n",
"\n",
"APPROVED\n",
"\n",
"--------------------------------------------------------------------------------\n"
]
}
],
"source": [
"human.initiate_chat(manager, message=\"Find a latest paper about gpt-4\")\n",
"# type exit to terminate the chat"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "flaml",
"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.9.17"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}