autogen/notebook/autogen_agentchat_groupchat.ipynb

312 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.2"
]
},
{
"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=\"Scientist. Provide expert knowledge.\",\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 on arxiv and find its potential application.\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001b[33mAlice\u001b[0m (to chat_manager):\n",
"\n",
"To find the latest paper about GPT-4 on arxiv, I will use the arxiv API to query and filter the search results. First, I'll execute the following code to get the latest paper information.\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001b[33mAlice\u001b[0m (to chat_manager):\n",
"\n",
"```python\n",
"import requests\n",
"from bs4 import BeautifulSoup\n",
"import re\n",
"\n",
"\n",
"def search_arxiv(query, sort_by=\"submittedDate\", order=\"descending\", max_results=1):\n",
" search_url = \"http://export.arxiv.org/api/query\"\n",
" query_string = f\"search_query=all:{query.replace(' ', '+')}&sortBy={sort_by}&sortOrder={order}&max_results={max_results}\"\n",
" url = f\"{search_url}?{query_string}\"\n",
" response = requests.get(url)\n",
" return response.text\n",
"\n",
"\n",
"def parse_paper_info(xml_response):\n",
" soup = BeautifulSoup(xml_response, \"lxml-xml\")\n",
" entry = soup.find(\"entry\")\n",
" if not entry:\n",
" return {}\n",
"\n",
" paper_info = {\n",
" \"title\": entry.find(\"title\").text.strip(),\n",
" \"authors\": [author.text for author in entry.find_all(\"author\")],\n",
" \"published\": entry.find(\"published\").text,\n",
" \"updated\": entry.find(\"updated\").text,\n",
" \"summary\": entry.find(\"summary\").text.strip(),\n",
" \"url\": entry.find(\"id\").text,\n",
" }\n",
" return paper_info\n",
"\n",
"\n",
"query = \"gpt-4\"\n",
"arxiv_response = search_arxiv(query)\n",
"paper_info = parse_paper_info(arxiv_response)\n",
"\n",
"if paper_info:\n",
" print(f\"Title: {paper_info['title']}\")\n",
" print(f\"Authors: {', '.join(paper_info['authors'])}\")\n",
" print(f\"Published: {paper_info['published']}\")\n",
" print(f\"Updated: {paper_info['updated']}\")\n",
" print(f\"Abstract: {paper_info['summary']}\")\n",
" print(f\"URL: {paper_info['url']}\")\n",
"else:\n",
" print(\"No papers about GPT-4 were found on arxiv.\")\n",
"```\n",
"\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: Breaking the Bank with ChatGPT: Few-Shot Text Classification for Finance\n",
"Authors: \n",
"Lefteris Loukas\n",
", \n",
"Ilias Stogiannidis\n",
", \n",
"Prodromos Malakasiotis\n",
", \n",
"Stavros Vassos\n",
"\n",
"Published: 2023-08-28T15:04:16Z\n",
"Updated: 2023-08-28T15:04:16Z\n",
"Abstract: We propose the use of conversational GPT models for easy and quick few-shot\n",
"text classification in the financial domain using the Banking77 dataset. Our\n",
"approach involves in-context learning with GPT-3.5 and GPT-4, which minimizes\n",
"the technical expertise required and eliminates the need for expensive GPU\n",
"computing while yielding quick and accurate results. Additionally, we fine-tune\n",
"other pre-trained, masked language models with SetFit, a recent contrastive\n",
"learning technique, to achieve state-of-the-art results both in full-data and\n",
"few-shot settings. Our findings show that querying GPT-3.5 and GPT-4 can\n",
"outperform fine-tuned, non-generative models even with fewer examples. However,\n",
"subscription fees associated with these solutions may be considered costly for\n",
"small organizations. Lastly, we find that generative models perform better on\n",
"the given task when shown representative samples selected by a human expert\n",
"rather than when shown random ones. We conclude that a) our proposed methods\n",
"offer a practical solution for few-shot tasks in datasets with limited label\n",
"availability, and b) our state-of-the-art results can inspire future work in\n",
"the area.\n",
"URL: http://arxiv.org/abs/2308.14634v1\n",
"\n",
"\n",
"--------------------------------------------------------------------------------\n",
"\u001b[33mBob\u001b[0m (to chat_manager):\n",
"\n",
"The latest paper about GPT-4 on arxiv is titled \"Breaking the Bank with ChatGPT: Few-Shot Text Classification for Finance\" by Lefteris Loukas, Ilias Stogiannidis, Prodromos Malakasiotis, and Stavros Vassos. It was published on 2023-08-28.\n",
"\n",
"The potential application of GPT-4 in this paper is for few-shot text classification in the financial domain using the Banking77 dataset. The authors propose using conversational GPT models like GPT-3.5 and GPT-4 for in-context learning, which minimizes the technical expertise required and eliminates the need for expensive GPU computing while yielding quick and accurate results.\n",
"\n",
"Additionally, the paper explores fine-tuning other pre-trained, masked language models with SetFit, a recent contrastive learning technique, to achieve state-of-the-art results in both full-data and few-shot settings.\n",
"\n",
"The findings of this paper show that querying GPT-3.5 and GPT-4 can outperform fine-tuned, non-generative models even with fewer examples. However, subscription fees associated with these solutions may be considered costly for small organizations. The authors also find that generative models perform better on the given task when shown representative samples selected by a human expert rather than when shown random ones. The proposed methods offer a practical solution for few-shot tasks in datasets with limited label availability, and the state-of-the-art results can inspire future work in the area.\n",
"\n",
"You can access the full paper [here](http://arxiv.org/abs/2308.14634v1).\n",
"\n",
"--------------------------------------------------------------------------------\n"
]
}
],
"source": [
"human.initiate_chat(manager, message=\"Find a latest paper about gpt-4 on arxiv and find its potential application.\")\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
}