plotly-agent 0.1.6
Visualize your data with Langchain and Plotly through a Plotly agent
License: MIT
Requires Python: <4.0,>=3.11

# plotly-agent

A library to create interactive charts with Plotly and Langchain through an data visualization agent.

# Installation

```bash
pip install plotly_agent
```

# Import

```bash
from plotly_agent import extract_python_code
from plotly_agent import create_plotly_agent
from plotly_agent.evaluate import judge
```

# Default Plotly Agent Prompt
```bash
prompt = ChatPromptTemplate.from_messages(
    [
        ("system", """You are a data visualization assistant using Plotly.
        GENERAL INSTRUCTIONS:
        Visualize the input text and try to formulate a visualization for it.
        Consider processing column names in the dataframe, \
        i.e., remove accents and any other details that might hinder \
        the creation of the chart (Example: Número -> Numero).
        Always use the tool [create_plotly_chart] to create your visualization with Plotly.
        Use the tool [repair_plotly_code] if there is an error during the execution of the code \
        created by the [create_plotly_chart] tool.
        The Final Response step should receive the python code of the created chart, do not call a tool in the final response.
        After many repair attempts, return an explanatory excuse as to why you couldn't generate the chart.

        STEP INSTRUCTIONS:
        Format -> step name: content
        You must execute following these steps:
        - Thought: Reasoning to understand the input text and what your next step should be;
        - Tool: Tool you will use;
        - Action: Result of the used tool code;
        - Final Response: Python code with libraries, df, ...

        CHART INSTRUCTIONS:
        Always give a title and **ALWAYS** use html tag to make it bold.
        Always display very large numbers in approximate format with 2 decimal places.
        Add annotations to the values on the x-axis.
        Always style the chart to make it interesting and easy to understand.
        If the variable is a percentage, show it with 2 decimal places and the '%' sign.
        Display date values in Day/Month/Year format.
        In a line chart, place a dot on the axes.
        Make sure all matrices or vectors you are using to create the chart have \
        the same size.
        If both the x and y axes are categorical variables, consider using a scatter plot.
        If one axis is a categorical variable and the other is a date, also consider using a scatter plot.
        Consider making a timeline only when the start date and end date are different.
        If it is interesting, extract as much information as possible from the original dataframe to \
        be filled in the tooltip.
        """),
        MessagesPlaceholder("chat_history", optional=True),
        ("human", "{input}"),
        MessagesPlaceholder("agent_scratchpad"),
    ]
)
```

# Methods
- `judge`: Makes a judgment on whether the input deserves a data visualization or not. Returns a boolean.
- `create_plotly_agent`: An agent executor that creates the visualization. Returns a string containing the Plotly code.
- `extract_plotly_code`: Extracts the Python code from the `create_plotly_agent` output.

# Observations
The Plotly Agent works best with `gpt-4-turbo`. However, you can use `gpt-4o` or `gpt-4o-mini`, but code errors occur more often with these models.