"AutoGen offers conversable agents powered by LLM, tool, or human, which can be used to perform tasks collectively via automated chat. This framework allows tool use and human participants through multi-agent conversation. Please find documentation about this feature [here](https://microsoft.github.io/autogen/docs/Use-Cases/agent_chat).\n",
"In this notebook, we demonstrate how to use `AssistantAgent` and `UserProxyAgent` to make function calls with the new feature of OpenAI models (in model version 0613) with a set of Langchain-provided tools and toolkits, to demonstrate how to leverage the 35+ tools available. \n",
"A specified prompt and function configs must be passed to `AssistantAgent` to initialize the agent. The corresponding functions must be passed to `UserProxyAgent`, which will execute any function calls made by `AssistantAgent`. Besides this requirement of matching descriptions with functions, we recommend checking the system message in the `AssistantAgent` to ensure the instructions align with the function call descriptions.\n",
"AutoGen requires `Python>=3.8`. To run this notebook example, please install the [mathchat] option since we will import functions from `MathUserProxyAgent`:\n",
"Requirement already satisfied: pyautogen[mathchat]~=0.1.0 in /usr/local/lib/python3.10/dist-packages (0.1.4)\n",
"Requirement already satisfied: diskcache in /usr/local/lib/python3.10/dist-packages (from pyautogen[mathchat]~=0.1.0) (5.6.3)\n",
"Requirement already satisfied: flaml in /usr/local/lib/python3.10/dist-packages (from pyautogen[mathchat]~=0.1.0) (2.1.0)\n",
"Requirement already satisfied: openai in /usr/local/lib/python3.10/dist-packages (from pyautogen[mathchat]~=0.1.0) (0.28.1)\n",
"Requirement already satisfied: termcolor in /usr/local/lib/python3.10/dist-packages (from pyautogen[mathchat]~=0.1.0) (2.3.0)\n",
"Requirement already satisfied: pydantic==1.10.9 in /usr/local/lib/python3.10/dist-packages (from pyautogen[mathchat]~=0.1.0) (1.10.9)\n",
"Requirement already satisfied: sympy in /usr/local/lib/python3.10/dist-packages (from pyautogen[mathchat]~=0.1.0) (1.12)\n",
"Requirement already satisfied: wolframalpha in /usr/local/lib/python3.10/dist-packages (from pyautogen[mathchat]~=0.1.0) (5.0.0)\n",
"Requirement already satisfied: typing-extensions>=4.2.0 in /usr/local/lib/python3.10/dist-packages (from pydantic==1.10.9->pyautogen[mathchat]~=0.1.0) (4.5.0)\n",
"Requirement already satisfied: NumPy>=1.17.0rc1 in /usr/local/lib/python3.10/dist-packages (from flaml->pyautogen[mathchat]~=0.1.0) (1.23.5)\n",
"Requirement already satisfied: requests>=2.20 in /usr/local/lib/python3.10/dist-packages (from openai->pyautogen[mathchat]~=0.1.0) (2.31.0)\n",
"Requirement already satisfied: tqdm in /usr/local/lib/python3.10/dist-packages (from openai->pyautogen[mathchat]~=0.1.0) (4.66.1)\n",
"Requirement already satisfied: aiohttp in /usr/local/lib/python3.10/dist-packages (from openai->pyautogen[mathchat]~=0.1.0) (3.8.5)\n",
"Requirement already satisfied: mpmath>=0.19 in /usr/local/lib/python3.10/dist-packages (from sympy->pyautogen[mathchat]~=0.1.0) (1.3.0)\n",
"Requirement already satisfied: xmltodict in /usr/local/lib/python3.10/dist-packages (from wolframalpha->pyautogen[mathchat]~=0.1.0) (0.13.0)\n",
"Requirement already satisfied: more-itertools in /usr/local/lib/python3.10/dist-packages (from wolframalpha->pyautogen[mathchat]~=0.1.0) (10.1.0)\n",
"Requirement already satisfied: jaraco.context in /usr/local/lib/python3.10/dist-packages (from wolframalpha->pyautogen[mathchat]~=0.1.0) (4.3.0)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests>=2.20->openai->pyautogen[mathchat]~=0.1.0) (3.2.0)\n",
"Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests>=2.20->openai->pyautogen[mathchat]~=0.1.0) (3.4)\n",
"Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests>=2.20->openai->pyautogen[mathchat]~=0.1.0) (2.0.4)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests>=2.20->openai->pyautogen[mathchat]~=0.1.0) (2023.7.22)\n",
"Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->openai->pyautogen[mathchat]~=0.1.0) (23.1.0)\n",
"Requirement already satisfied: multidict<7.0,>=4.5 in /usr/local/lib/python3.10/dist-packages (from aiohttp->openai->pyautogen[mathchat]~=0.1.0) (6.0.4)\n",
"Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in /usr/local/lib/python3.10/dist-packages (from aiohttp->openai->pyautogen[mathchat]~=0.1.0) (4.0.3)\n",
"Requirement already satisfied: yarl<2.0,>=1.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->openai->pyautogen[mathchat]~=0.1.0) (1.9.2)\n",
"Requirement already satisfied: frozenlist>=1.1.1 in /usr/local/lib/python3.10/dist-packages (from aiohttp->openai->pyautogen[mathchat]~=0.1.0) (1.4.0)\n",
"Requirement already satisfied: aiosignal>=1.1.2 in /usr/local/lib/python3.10/dist-packages (from aiohttp->openai->pyautogen[mathchat]~=0.1.0) (1.3.1)\n"
]
}
],
"source": [
"%pip install \"pyautogen[mathchat]~=0.1.0\""
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "5ebd2397",
"metadata": {
"id": "5ebd2397"
},
"source": [
"## Set your API Endpoint\n",
"\n",
"The [`config_list_from_models`](https://microsoft.github.io/autogen/docs/reference/oai/openai_utils#config_list_from_models) function tries to create a list of configurations using Azure OpenAI endpoints and OpenAI endpoints for the provided list of models. It assumes the api keys and api bases are stored in the corresponding environment variables or local txt files:\n",
"\n",
"- OpenAI API key: os.environ[\"OPENAI_API_KEY\"] or `openai_api_key_file=\"key_openai.txt\"`.\n",
"- Azure OpenAI API key: os.environ[\"AZURE_OPENAI_API_KEY\"] or `aoai_api_key_file=\"key_aoai.txt\"`. Multiple keys can be stored, one per line.\n",
"- Azure OpenAI API base: os.environ[\"AZURE_OPENAI_API_BASE\"] or `aoai_api_base_file=\"base_aoai.txt\"`. Multiple bases can be stored, one per line.\n",
"\n",
"It's OK to have only the OpenAI API key, or only the Azure OpenAI API key + base.\n",
"If you open this notebook in google colab, you can upload your files by clicking the file icon on the left panel and then choosing \"upload file\" icon.\n",
"The following code excludes Azure OpenAI endpoints from the config list because some endpoints don't support functions yet. Remove the `exclude` argument if they do."
"In this example, we demonstrate function call execution with `AssistantAgent` and `UserProxyAgent`. With the default system prompt of `AssistantAgent`, we allow the LLM assistant to perform tasks with code, and the `UserProxyAgent` would extract code blocks from the LLM response and execute them. With the new \"function_call\" feature, we define functions and specify the description of the function in the OpenAI config for the `AssistantAgent`. Then we register the functions in `UserProxyAgent`.\n"
"# Register the tool and start the conversation\n",
"user_proxy.register_function(\n",
" function_map={\n",
" custom_tool.name: custom_tool._run,\n",
" read_file_tool.name: read_file_tool._run,\n",
" }\n",
")\n",
"\n",
"chatbot = autogen.AssistantAgent(\n",
" name=\"chatbot\",\n",
" system_message=\"For coding tasks, only use the functions you have been provided with. Reply TERMINATE when the task is done.\",\n",
" llm_config=llm_config,\n",
")\n",
"\n",
"user_proxy.initiate_chat(\n",
" chatbot,\n",
" message=\"Read the file with the path 'Test.txt', then calculate the circumference of a circle that has a radius of that files contents.\", #\" 7.81mm\" in the file\n",
"Thought:\u001b[32;1m\u001b[1;3mI see that there is a table called \"california_housing_train\" in the database. I can use the \"schema_sql_db\" tool to get more information about this table.\n",
"Thought:\u001b[32;1m\u001b[1;3mThe \"california_housing_train\" table has the following columns: longitude, latitude, housing_median_age, total_rooms, total_bedrooms, population, households, median_income, and median_house_value. It contains information about housing in California. I can now answer the question.\n",
"Action: None\n",
"Final Answer: The \"california_housing_train\" table has the following columns: longitude, latitude, housing_median_age, total_rooms, total_bedrooms, population, households, median_income, and median_house_value.\u001b[0m\n",
"'The \"california_housing_train\" table has the following columns: longitude, latitude, housing_median_age, total_rooms, total_bedrooms, population, households, median_income, and median_house_value.'"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Starndard Langchain example\n",
"agent_executor.run(\"Describe the california_housing_train table\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d94d45a9",
"metadata": {},
"outputs": [],
"source": [
"# LangChain direct tool usage instead of toolkit example\n",
"# from langchain.tools.spark_sql.tool import (\n",
"{'name': 'query_sql_db', 'description': '\\n Input to this tool is a detailed and correct SQL query, output is a result from the Spark SQL.\\n If the query is not correct, an error message will be returned.\\n If an error is returned, rewrite the query, check the query, and try again.\\n ', 'parameters': {'type': 'object', 'properties': {'query': {'title': 'Query', 'type': 'string'}}, 'required': []}}\n",
"{'name': 'schema_sql_db', 'description': '\\n Input to this tool is a comma-separated list of tables, output is the schema and sample rows for those tables.\\n Be sure that the tables actually exist by calling list_tables_sql_db first!\\n\\n Example Input: \"table1, table2, table3\"\\n ', 'parameters': {'type': 'object', 'properties': {'table_names': {'title': 'Table Names', 'type': 'string'}}, 'required': []}}\n",
"{'name': 'list_tables_sql_db', 'description': 'Input is an empty string, output is a comma separated list of tables in the Spark SQL.', 'parameters': {'type': 'object', 'properties': {'tool_input': {'title': 'Tool Input', 'default': '', 'type': 'string'}}, 'required': []}}\n",
"{'name': 'query_checker_sql_db', 'description': '\\n Use this tool to double check if your query is correct before executing it.\\n Always use this tool before executing a query with query_sql_db!\\n ', 'parameters': {'type': 'object', 'properties': {'query': {'title': 'Query', 'type': 'string'}}, 'required': []}}\n",
"user_proxy (to chatbot):\n",
"\n",
"Describe the table names california_housing_train\n",