2023-03-01 06:19:11 +00:00
{
"cells": [
{
"cell_type": "markdown",
"id": "7094e328",
"metadata": {},
"source": [
2023-08-24 06:17:47 +00:00
"# CSV\n",
2023-03-01 06:19:11 +00:00
"\n",
2023-08-24 06:17:47 +00:00
"This notebook shows how to use agents to interact with data in `CSV` format. It is mostly optimized for question answering.\n",
2023-03-01 06:19:11 +00:00
"\n",
2023-03-01 17:40:31 +00:00
"**NOTE: this agent calls the Pandas DataFrame agent under the hood, which in turn calls the Python agent, which executes LLM generated Python code - this can be bad if the LLM generated Python code is harmful. Use cautiously.**\n",
"\n"
2023-03-01 06:19:11 +00:00
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "caae0bec",
"metadata": {},
"outputs": [],
"source": [
2023-06-15 15:18:50 +00:00
"from langchain.llms import OpenAI\n",
"from langchain.chat_models import ChatOpenAI\n",
2023-08-24 06:17:47 +00:00
"from langchain.agents.agent_types import AgentType\n",
"\n",
"from langchain.agents import create_csv_agent"
2023-06-15 15:18:50 +00:00
]
},
{
"cell_type": "markdown",
"id": "bd806175",
"metadata": {},
"source": [
2023-08-24 06:17:47 +00:00
"## Using `ZERO_SHOT_REACT_DESCRIPTION`\n",
2023-06-15 15:18:50 +00:00
"\n",
2023-08-24 06:17:47 +00:00
"This shows how to initialize the agent using the `ZERO_SHOT_REACT_DESCRIPTION` agent type. Note that this is an alternative to the above."
2023-03-01 06:19:11 +00:00
]
},
{
"cell_type": "code",
2023-06-15 15:18:50 +00:00
"execution_count": 9,
"id": "a1717204",
"metadata": {},
"outputs": [],
"source": [
"agent = create_csv_agent(\n",
2023-06-16 18:52:56 +00:00
" OpenAI(temperature=0),\n",
" \"titanic.csv\",\n",
" verbose=True,\n",
" agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,\n",
2023-06-15 15:18:50 +00:00
")"
]
},
{
"cell_type": "markdown",
"id": "c31bb8a6",
"metadata": {},
"source": [
"## Using OpenAI Functions\n",
"\n",
"This shows how to initialize the agent using the OPENAI_FUNCTIONS agent type. Note that this is an alternative to the above."
]
},
{
"cell_type": "code",
"execution_count": 3,
2023-03-01 06:19:11 +00:00
"id": "16c4dc59",
"metadata": {},
"outputs": [],
"source": [
2023-06-15 15:18:50 +00:00
"agent = create_csv_agent(\n",
2023-06-16 18:52:56 +00:00
" ChatOpenAI(temperature=0, model=\"gpt-3.5-turbo-0613\"),\n",
" \"titanic.csv\",\n",
" verbose=True,\n",
" agent_type=AgentType.OPENAI_FUNCTIONS,\n",
2023-06-15 15:18:50 +00:00
")"
2023-03-01 06:19:11 +00:00
]
},
{
"cell_type": "code",
2023-06-15 15:18:50 +00:00
"execution_count": 4,
2023-03-01 06:19:11 +00:00
"id": "46b9489d",
"metadata": {},
"outputs": [
2023-06-15 15:18:50 +00:00
{
"name": "stderr",
"output_type": "stream",
"text": [
"Error in on_chain_start callback: 'name'\n"
]
},
2023-03-01 06:19:11 +00:00
{
"name": "stdout",
"output_type": "stream",
"text": [
2023-06-15 15:18:50 +00:00
"\u001b[32;1m\u001b[1;3m\n",
"Invoking: `python_repl_ast` with `df.shape[0]`\n",
2023-03-01 06:19:11 +00:00
"\n",
"\n",
2023-06-15 15:18:50 +00:00
"\u001b[0m\u001b[36;1m\u001b[1;3m891\u001b[0m\u001b[32;1m\u001b[1;3mThere are 891 rows in the dataframe.\u001b[0m\n",
2023-03-01 06:19:11 +00:00
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
2023-06-15 15:18:50 +00:00
"'There are 891 rows in the dataframe.'"
2023-03-01 06:19:11 +00:00
]
},
2023-06-15 15:18:50 +00:00
"execution_count": 4,
2023-03-01 06:19:11 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"agent.run(\"how many rows are there?\")"
]
},
{
"cell_type": "code",
2023-06-15 15:18:50 +00:00
"execution_count": 5,
2023-03-01 06:19:11 +00:00
"id": "a96309be",
2023-08-24 06:17:47 +00:00
"metadata": {},
2023-03-01 06:19:11 +00:00
"outputs": [
2023-06-15 15:18:50 +00:00
{
"name": "stderr",
"output_type": "stream",
"text": [
"Error in on_chain_start callback: 'name'\n"
]
},
2023-03-01 06:19:11 +00:00
{
"name": "stdout",
"output_type": "stream",
"text": [
2023-06-15 15:18:50 +00:00
"\u001b[32;1m\u001b[1;3m\n",
"Invoking: `python_repl_ast` with `df[df['SibSp'] > 3]['PassengerId'].count()`\n",
2023-03-01 06:19:11 +00:00
"\n",
"\n",
2023-06-15 15:18:50 +00:00
"\u001b[0m\u001b[36;1m\u001b[1;3m30\u001b[0m\u001b[32;1m\u001b[1;3mThere are 30 people in the dataframe who have more than 3 siblings.\u001b[0m\n",
2023-03-01 06:19:11 +00:00
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
2023-06-15 15:18:50 +00:00
"'There are 30 people in the dataframe who have more than 3 siblings.'"
2023-03-01 06:19:11 +00:00
]
},
2023-06-15 15:18:50 +00:00
"execution_count": 5,
2023-03-01 06:19:11 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
2023-05-05 15:35:49 +00:00
"agent.run(\"how many people have more than 3 siblings\")"
2023-03-01 06:19:11 +00:00
]
},
{
"cell_type": "code",
2023-06-15 15:18:50 +00:00
"execution_count": 6,
2023-03-01 06:19:11 +00:00
"id": "964a09f7",
"metadata": {},
"outputs": [
2023-06-15 15:18:50 +00:00
{
"name": "stderr",
"output_type": "stream",
"text": [
"Error in on_chain_start callback: 'name'\n"
]
},
2023-03-01 06:19:11 +00:00
{
"name": "stdout",
"output_type": "stream",
"text": [
2023-06-15 15:18:50 +00:00
"\u001b[32;1m\u001b[1;3m\n",
"Invoking: `python_repl_ast` with `import pandas as pd\n",
"import math\n",
"\n",
"# Create a dataframe\n",
"data = {'Age': [22, 38, 26, 35, 35]}\n",
"df = pd.DataFrame(data)\n",
"\n",
"# Calculate the average age\n",
"average_age = df['Age'].mean()\n",
"\n",
"# Calculate the square root of the average age\n",
"square_root = math.sqrt(average_age)\n",
"\n",
"square_root`\n",
2023-03-01 06:19:11 +00:00
"\n",
"\n",
2023-06-15 15:18:50 +00:00
"\u001b[0m\u001b[36;1m\u001b[1;3m5.585696017507576\u001b[0m\u001b[32;1m\u001b[1;3mThe square root of the average age is approximately 5.59.\u001b[0m\n",
2023-03-01 06:19:11 +00:00
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
2023-06-15 15:18:50 +00:00
"'The square root of the average age is approximately 5.59.'"
2023-03-01 06:19:11 +00:00
]
},
2023-06-15 15:18:50 +00:00
"execution_count": 6,
2023-03-01 06:19:11 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"agent.run(\"whats the square root of the average age?\")"
]
},
2023-05-25 21:23:11 +00:00
{
"cell_type": "markdown",
"id": "09539c18",
"metadata": {},
"source": [
"### Multi CSV Example\n",
"\n",
"This next part shows how the agent can interact with multiple csv files passed in as a list."
]
},
{
"cell_type": "code",
2023-06-15 15:18:50 +00:00
"execution_count": 8,
2023-05-25 21:23:11 +00:00
"id": "15f11fbd",
"metadata": {},
"outputs": [
2023-06-15 15:18:50 +00:00
{
"name": "stderr",
"output_type": "stream",
"text": [
"Error in on_chain_start callback: 'name'\n"
]
},
2023-05-25 21:23:11 +00:00
{
"name": "stdout",
"output_type": "stream",
"text": [
2023-06-15 15:18:50 +00:00
"\u001b[32;1m\u001b[1;3m\n",
"Invoking: `python_repl_ast` with `df1['Age'].nunique() - df2['Age'].nunique()`\n",
2023-05-25 21:23:11 +00:00
"\n",
"\n",
2023-06-15 15:18:50 +00:00
"\u001b[0m\u001b[36;1m\u001b[1;3m-1\u001b[0m\u001b[32;1m\u001b[1;3mThere is 1 row in the age column that is different between the two dataframes.\u001b[0m\n",
2023-05-25 21:23:11 +00:00
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
2023-06-15 15:18:50 +00:00
"'There is 1 row in the age column that is different between the two dataframes.'"
2023-05-25 21:23:11 +00:00
]
},
2023-06-15 15:18:50 +00:00
"execution_count": 8,
2023-05-25 21:23:11 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
2023-06-16 18:52:56 +00:00
"agent = create_csv_agent(\n",
" ChatOpenAI(temperature=0, model=\"gpt-3.5-turbo-0613\"),\n",
" [\"titanic.csv\", \"titanic_age_fillna.csv\"],\n",
" verbose=True,\n",
" agent_type=AgentType.OPENAI_FUNCTIONS,\n",
")\n",
2023-06-15 15:18:50 +00:00
"agent.run(\"how many rows in the age column are different between the two dfs?\")"
2023-05-25 21:23:11 +00:00
]
},
2023-03-01 06:19:11 +00:00
{
"cell_type": "code",
"execution_count": null,
2023-05-25 21:23:11 +00:00
"id": "f2909808",
2023-03-01 06:19:11 +00:00
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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",
2023-08-24 06:17:47 +00:00
"version": "3.10.12"
2023-03-01 06:19:11 +00:00
}
},
"nbformat": 4,
"nbformat_minor": 5
}