Please help with custom prompts! #501
Replies: 2 comments 9 replies
-
Hi @ronaldsholt, thanks a lot for reporting. I've just released a new version of the custom prompts, as the previous implementation had become too chaotic. Here's a working example. Hope it helps. import pandas as pd
from pandasai import SmartDataframe
from pandasai.prompts import Prompt
# # Custom Prompts
class CustomAnalysisPrompt(Prompt):
text = """
You are provided with a dataset that contains sales data by brand across various regions. Here's the metadata for the given pandas DataFrames:
{dataframes}
Given this data, please follow these steps:
0. Acknowledge the user's query and provide context for the analysis.
1. **Data Analysis**: < custom instructions >
2. **Opportunity Identification**: < custom instructions >
3. **Reasoning**: < custom instructions >
4. **Recommendations**: < custom instructions >
5. **Output**: Return a dictionary with:
- type (possible values: "text", "number", "dataframe", "plot")
- value (can be a string, a dataframe, or the path of the plot, NOT a dictionary)
Example: {{ "type": "text", "value": < custom instructions > }}
``python
def analyze_data(dfs: list[pd.DataFrame]) -> dict:
# Code goes here (do not add comments)
# Declare a result variable
result = analyze_data(dfs)
``
Using the provided dataframes (`dfs`), update the Python code based on the user's query:
{conversation}
# Updated code:
# """
df = pd.DataFrame({
"brand": ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"],
"region": ["North America", "North America", "North America", "North America", "North America", "Europe", "Europe", "Europe", "Europe", "Europe"],
"sales": [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]
})
# Creating SmartDataFrames
sdf_sales_continent = SmartDataframe(
df,
name="df",
config={
"custom_prompts": {
"generate_python_code": CustomAnalysisPrompt(),
}
}
)
res = sdf_sales_continent.chat("What is the average sales by continent?") If you have any questions, just ping me. |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Hi All - I need help implementing a custom prompt. If someone could explain with a better example of what is in the docs it would be super helpful!
Here is what I'm doing now:
I can't get this to work (seemingly, since it doesn't throw any errors but only returns standard responses), so I am wondering if there are any tricks here or if I am missing something. Thank you!!!
Beta Was this translation helpful? Give feedback.
All reactions