|
| 1 | +cat <<'EOF' > lib/langchain_experimental/agents/agent_toolkits/matplotlib/README.md |
| 2 | +# 🧠 Matplotlib Agent Toolkit |
| 3 | + |
| 4 | +The **Matplotlib Agent Toolkit** extends LangChain with intelligent data visualization capabilities using **Matplotlib** and **Python REPL** tools. |
| 5 | +It enables agents to understand your data and **generate, execute, and explain plots** — all from natural language instructions. |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## 🚀 Overview |
| 10 | + |
| 11 | +This toolkit lets a language model: |
| 12 | +- Analyze one or multiple Pandas DataFrames |
| 13 | +- Generate valid Python + Matplotlib plotting code |
| 14 | +- Execute it safely in a sandboxed REPL |
| 15 | +- Save or display visualizations automatically |
| 16 | + |
| 17 | +It supports all major LangChain agent types, including: |
| 18 | +- `AgentType.ZERO_SHOT_REACT_DESCRIPTION` |
| 19 | +- `AgentType.OPENAI_FUNCTIONS` |
| 20 | +- `"openai-tools"` |
| 21 | +- `"tool-calling"` |
| 22 | + |
| 23 | +--- |
| 24 | + |
| 25 | +## ⚙️ Installation |
| 26 | + |
| 27 | +Clone the experimental repo and install dependencies: |
| 28 | + |
| 29 | +```bash |
| 30 | +git clone https://github.com/langchain-ai/langchain-experimental.git |
| 31 | +cd langchain-experimental |
| 32 | +pip install -e . |
| 33 | +``` |
| 34 | + |
| 35 | + |
| 36 | +Ensure the following are installed |
| 37 | +```bash |
| 38 | +pip install pandas matplotlib langchain python-dotenv |
| 39 | +``` |
| 40 | + |
| 41 | +## Example Usage: |
| 42 | +``` bash |
| 43 | +from langchain_openai import ChatOpenAI |
| 44 | +from langchain_experimental.agents.agent_toolkits.matplotlib.base import create_matplotlib_agent |
| 45 | +import pandas as pd |
| 46 | +from dotenv import load_dotenv |
| 47 | + |
| 48 | +# Load your OpenAI API key |
| 49 | +load_dotenv() |
| 50 | + |
| 51 | +# Load a sample dataset |
| 52 | +df = pd.read_csv("https://raw.githubusercontent.com/pandas-dev/pandas/main/doc/data/titanic.csv") |
| 53 | + |
| 54 | +# Initialize an LLM |
| 55 | +llm = ChatOpenAI(model="gpt-4o", temperature=0) |
| 56 | + |
| 57 | +# Create a Matplotlib-powered agent |
| 58 | +agent = create_matplotlib_agent( |
| 59 | + llm=llm, |
| 60 | + df=df, |
| 61 | + verbose=True, |
| 62 | + allow_dangerous_code=True, # ⚠️ Enables Python execution for plotting |
| 63 | +) |
| 64 | + |
| 65 | +# Natural language plotting query |
| 66 | +query = """ |
| 67 | +Create a scatter plot of Age vs Fare, color-coded by passenger class. |
| 68 | +Save the figure as 'titanic_scatter.png'. |
| 69 | +""" |
| 70 | + |
| 71 | +# Run the agent |
| 72 | +result = agent.invoke({"input": query}) |
| 73 | + |
| 74 | +print("Agent output:", result.get("output", "")) |
| 75 | +print("✅ Plot generated and saved as titanic_scatter.png") |
| 76 | + |
| 77 | +``` |
| 78 | +After Execution you will have a chart named titanic_scatter.png |
| 79 | + |
| 80 | + |
| 81 | +## Agent Creator: |
| 82 | + |
| 83 | +``` bash |
| 84 | + |
| 85 | +agent = create_matplotlib_agent( |
| 86 | + llm=llm, |
| 87 | + df=df, |
| 88 | + verbose=True, |
| 89 | + allow_dangerous_code=True, |
| 90 | +) |
| 91 | +``` |
| 92 | + |
| 93 | + |
| 94 | +## 🧩 Parameters |
| 95 | + |
| 96 | +| Parameter | Type | Description | |
| 97 | +|------------|------|-------------| |
| 98 | +| `llm` | `LanguageModelLike` | Any LLM supporting tool-calling (e.g. `ChatOpenAI`) | |
| 99 | +| `df` | `pd.DataFrame` or `List[pd.DataFrame]` | Dataset(s) for visualization | |
| 100 | +| `agent_type` | `AgentType` or `str` | Type of agent (`react`, `openai-functions`, etc.) | |
| 101 | +| `allow_dangerous_code` | `bool` | Must be `True` to enable Python REPL execution | |
| 102 | +| `include_df_in_prompt` | `bool` | Whether to embed sample DataFrame rows in the prompt | |
| 103 | +| `extra_tools` | `Sequence[BaseTool]` | Add additional LangChain tools if needed | |
| 104 | + |
| 105 | + |
| 106 | +## 🧠 How It Works |
| 107 | + |
| 108 | +Under the hood, `create_matplotlib_agent()`: |
| 109 | + |
| 110 | +- Builds a customized prompt based on your DataFrame(s) |
| 111 | +- Uses a Python REPL tool (`PythonAstREPLTool`) for safe code execution |
| 112 | +- Integrates Matplotlib and Pandas contexts for inline plotting |
| 113 | +- Returns an `AgentExecutor` ready to interpret natural language queries into plots |
| 114 | + |
| 115 | +--- |
| 116 | + |
| 117 | +## 🧪 Running Tests |
| 118 | + |
| 119 | +To verify functionality: |
| 120 | + |
| 121 | +```bash |
| 122 | +pytest tests/test_matplotlib_toolkit.py -v |
| 123 | + |
| 124 | + |
0 commit comments