In the final instalment (for now) of our Charting in Retool with Plotly series, we want to share our final tip to beat them all: a little AI chatbot helper that can get all your next Plotly endeavors off the ground, really fast.
We have already built a super useful AI chatbot that helps our team solve Retool problems quickly, using Retool Vectors to gather together resources for more informed and contextualized AI responses. We pull information in from the Retool Docs, Community, and our own blog.
Since charts can be such useful tools in internal business software, it’s always advantageous to build them fast. We use this AI tool to expedite the initial setup of charts we aren’t familiar with, as well as to generate quick lines of code to add customization features that make these charts stand out.
Subscribe to The Toolbox for free to skip the tutorial and download the JSON at the end of this article ⬇️
Members will get guides, tutorials and internal tool tips in 1-2 emails every month.
How to use the Plotly chatbot
The Plotly AI helper is a simple chatbot in a drawer frame in Retool. We set it up as a module so the chatbot can be pulled into multiple applications and used all over your apps when building.
The user simply prompts the chatbot with the kind of charts they need and, using our system prompts and connected vectors, the chatbot returns the code and the chart itself to preview.
You can ask the chatbot for different chart types, colors, annotations and code snippets to improve your charts. You can make the prompts even more specific by checking the Plotly docs first and making sure you use the correct terms.
Users can then copy the data object and layout object to their clipboards and paste them directly into the respective parts of the chart component in their app. And it’s really as simple as that!
How did we build it?
We built this much like our original AI chatbot, starting first with the Vectors to contextualize the information. In this case, we set up a vector to scan the Plotly docs online - you can see how to to do this to set up the module in your own environment. We also set up a vector to scrape the Retool docs.
Inside our Ai query, we made sure to include a specific prompt which we tested and refined to get the perfect result each time:
“You are a plotly chart assistant that will return a JSON object with 2 keys: 'data' and 'layout' ALWAYS between <code> and </code> tags.
If data is not provided please generate data so the chart can be rendered.
Provide additional tips how to enhance chart if asked for.”
We then connected the query to our Plotly and Retool Docs.
Once we get a response, we need to parse the data and use this to present the preview of the chart, and separate the data and layout objects to be copied into the chart component. We pulled in a chart component to the chatbot to present the preview. This is what our data object looks like, as it parses out the response from the AI bot.
And the layout object, which parses for the layout object, and sets the background to transparent, as well as allowing for an error option:
Inside our chat window we have two buttons that appear to allow you to copy the objects to your clipboard.
{{JSON.stringify(JSON.parse(bold_tech_plotly_helper.data?.split("<code>")?.[1]?.split("</code>")?.[0]?.replaceAll("'",'"'))?.data)}}
{{JSON.stringify(JSON.parse(bold_tech_plotly_helper.data?.split("<code>")?.[1]?.split("</code>")?.[0]?.replaceAll("'",'"'))?.layout)}}
Our Should_hide variable checks if the AI query is fetching and shows the buttons and chart component once complete. This query also makes sure that the chart preview has an error value option if the AI query doesn't work properly.
To make sure they stay hidden until the AI query is complete, we added this value to each component in the ‘hidden’ value:
How to set it up in your environment
Setting up the Plotly AI helper in your own environment is as simple as downloading the typescript and importing it into your environment as a module. All you have to do to make sure it works is set up a Retool Vector to scan the Plotly docs by adding the URL ‘Plotly.com’. Like we did, you can also set up a Retool Docs vector that scans the Retool documentation too.
This will then crawl the domain for all URLs to formulate an accurate response.
Once that’s done, simply connect the Vectors in the AI query and you’re ready and raring to go!
Download the sample toolscript
The Toolbox newsletter members can download the JSON and import it directly into their Retool environment to test it out ⬇️⬇️⬇️