Create plots with LLMs through OpenAI and langchain
DreamMyPlots is a friendly utility class for anyone doing data science, who want to turn numbers and data into seaborn/matplot plots quickly, through the power of Python's graph-making skills with any popular LLM prompts (through Langchain)to make even complex charts simple to create.
Here's how you can use it:
- Provide Data 📊: Input your DataFrame and what you'd like to visualize.
- AI Assists 🤖: It then uses AI to generate the Python plotting code based on your prompt.
- Visualize 🌟: Run the generated code to see your data transformed into a visual plot.
And this may:
- Saves Time ⏰: No need to write complex code from scratch. It's quicker to get your visuals ready.
- Boosts Creativity 🎨: Encourages you to experiment with different types of visuals without the heavy lifting.
- Simplifies Learning 📘: Makes it easier to jump into data visualization, even if you're new to data science.
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.rand(100), columns=['A'])
- 💻 Install :
pip install dream-my-plots
or through github tags,
pip install git+https://github.com/mustafah/dream-my-plots.git@1.0.0
# Sample .env file
DREAM_MY_PLOTS_LLM_API_KEY=your-key-here
- 📈 Create your dataframe :
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.rand(100, 3), columns=['A', 'B', 'C'])
- 🎨 Dream your plot :
from dream_my_plots import DreamMyPlots
DreamMyPlots(df, """
Make line plots for first two columns, and first 7 rows !
""")
📈 Create your dataframe :
import numpy as np
# Generating features for Class A & B with normal distribution
features_class_A = np.random.normal(70, 10, 100)
features_class_B = np.random.normal(80, 15, 100)
features = np.concatenate([features_class_A, features_class_B]) # Combining Class A and B features
classes = ['Class A'] * 100 + ['Class B'] * 100 # Creating labels for the classes
import pandas as pd
df = pd.DataFrame({'Feature': features, 'Class': classes}) # Creating DataFrame with features and class labels
🌟✍️ Send your prompt :
from dream_my_plots import DreamMyPlots
DreamMyPlots(df, """
📊 Make a plot contains 6 kinds of Distribution Plots (Categorical and Continuous Distribution plots)
🎨 Give a large title please, fit upto 4 subplots horizontally
""")
You use # character to comment any line in prompt, so it will be not sent to the LLM
from dream_my_plots import DreamMyPlots
DreamMyPlots(df, """
📊 Make a plot contains 6 kinds of Distribution Plots (Categorical and Continuous Distribution plots)
# 🎨 Give a large title please, fit upto 4 subplots horizontally
""")
Set an environment with value of DARK
or LIGHT
for the syntax higlight
DREAM_MY_PLOTS_THEME=DARK
or send it as a parameter in the class
from dream_my_plots import DreamMyPlots
DreamMyPlots(df, """
Make a scatter plot !
""", dark=True)
You can pass it directly instead of using .env files (not recommended)
from dream_my_plots import DreamMyPlots
DreamMyPlots(df, """
Make a histogram plot !
""", api_key="*****")
Try to rexecute your code, as it is or add more specifications to the prompt ... You are last error and generated code is automatically attached when you resend your prompts
from langchain.prompts.prompt import PromptTemplate
template = PromptTemplate(
input_variables=["df", "prompt", "previous_code", "previous_error"], template="your custom template content"
)
from dream_my_plots import DreamMyPlots
DreamMyPlots(df, """
Make a histogram plot !
""", prompt="*****", template)
It's based on langchain, you can use your custom llm provider
from langchain.chat_models import AzureChatOpenAI
from dream_my_plots import DreamMyPlots
DreamMyPlots(df, """
Make a histogram plot !
""", llm=AzureChatOpenAI(deployment_name="YOUR_DEPLOYMENT_NAME", openai_api_version="2023-05-15", openai_api_key=openai_api_key, ...))