Skip to main content

Visualize your data with Langchain and Plotly through a Plotly agent

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

plotly_agent-0.1.5.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

plotly_agent-0.1.5-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file plotly_agent-0.1.5.tar.gz.

File metadata

  • Download URL: plotly_agent-0.1.5.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.5 Windows/11

File hashes

Hashes for plotly_agent-0.1.5.tar.gz
Algorithm Hash digest
SHA256 0faa8674720effa9d0916c99fb953ca65f72b7e24129c983b78f7ca177b303af
MD5 0dfdfce9a01f3ca52a3728ae631bcc87
BLAKE2b-256 d58df1e8fa4286cf46a6d94a28c082fee7ec116c2cbd870c84fdeced68e74803

See more details on using hashes here.

File details

Details for the file plotly_agent-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: plotly_agent-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.5 Windows/11

File hashes

Hashes for plotly_agent-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 f2e53f8299fd4fcec74826789e6145412402065a5be5c9b153a845c795e46a0b
MD5 0e2bf27b7e658a1fb74355ceb327e89c
BLAKE2b-256 906feda32b198f67683d4fa0dd118e92700783f3bd279b6a2a0ed3d745c1aee0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page