Skip to main content

plotly-agent

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

Installation

pip install plotly_agent

Import

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

Default Plotly Agent Prompt

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.

Metadata

Release files for plotly-agent 0.1.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for plotly-agent 0.1.6
File Size Uploaded
plotly_agent-0.1.6.tar.gz 5.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for plotly-agent 0.1.6
File Interpreter ABI Platform
plotly_agent-0.1.6-py3-none-any.whl Python 3 none any Details

Total release size: 13.3 kB

Release files / plotly_agent-0.1.6.tar.gz

Download URL plotly_agent-0.1.6.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
6aff0f63a04a0f1284c8fc6fe55ab4f50c66f31f3e0d3c37494685c60cd95b3e
BLAKE2b-256 checksum
How to use checksums
5fd2cdb4a894b96a74f2493cf11a54389eea5801a7854124798fc0f75f9c3297
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.5 Windows/11

Release files / plotly_agent-0.1.6-py3-none-any.whl

Download URL plotly_agent-0.1.6-py3-none-any.whl
Size 7.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ee44b154bf0baf03401a0e59ffc832df2d5b3e651856f77c7bf796f241f51a10
BLAKE2b-256 checksum
How to use checksums
d14e5147e2181d66ec893a5c9360daa4df1d667e6ee0b6d773e7e8762ebf2c6a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.5 Windows/11

Release history Release notifications | RSS feed

This release

0.1.6 This release

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

1 release file

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page