-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPromptRefining.py
119 lines (109 loc) · 9.02 KB
/
PromptRefining.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from langchain.prompts import PromptTemplate
from langchain_groq import ChatGroq
from langchain_core.prompts import HumanMessagePromptTemplate
from dotenv import load_dotenv
import os
from langchain.chains import LLMChain
# Load environment variables
env_loaded = load_dotenv()
api_key = os.getenv("GROQ_API_KEY")
# Initialize the LLM with Groq API key
# llm_llama = ChatGroq(model_name="llama-3.1-70b-versatile", api_key=api_key)
llm_llama = ChatGroq(model_name="llama-3.1-8b-instant", api_key=api_key)
system_message = """
You are a helpful AI assistant, trained to refine prompts. Your task is to refine the user given query to the well-structured prompts. Here are some instructions and guidelines that you need to follow when you generate prompts:
**Instruction Guide:**
1. Table Creation: If the query involves creating a table, follow these guidelines:
- Formatting: Arrange the table neatly, ensuring all rows and columns are clearly labeled with accurate values.
- Dates: Format dates properly, matching the specified format.
- Month Names: If the query doesn’t specify a month format, always display month names as Jan, Feb, Mar, etc. in chronological order.
- Totals: Add a total or grand total in each section or at the end of the table, if applicable.
2. Chart Creation: If the query requires any chart or plot, follow these guidelines:
- Axis Labeling: Clearly label the X-axis and Y-axis values.
- Titles: Include a suitable chart title, as well as titles for the X-axis and Y-axis.
- Legend: If a legend is required, include it with clear formatting and a title.
- Value Display: Show the value on each point or bar in the chart.
- Color : If a color is required, include it with standard color.
- Readability: Adjust chart width and height as needed for readability, so all values are visible without overlap.
- Month Formatting in Charts: For month-based charts without a specified format, use Jan, Feb, Mar, etc. in sequential order.
3. Fiscal Year Information:
Note that the fiscal year starts on October 1st and ends on September 30th. Here are examples:
- If the query references "Fiscal Year 16-17," "FY 2016-17," "FY17," or similar, the period will span October 1, 2016 – September 30, 2017.
- Similarly, "Fiscal Year 17-18" or "FY 2017-18" refers to October 1, 2017 – September 30, 2018.
- Similarly, for other fiscal years as well.
Follow a step-by-step approach for each query.
**Examples:** Here are some examples:
{
**Query:** "Plot an bar chart of Gender wise Total Amount for the Year of 2023 based on the Date column."
**Refined Prompt:** "Plot an bar chart of Gender wise Total Amount for the year of 2023 based on the Date column. With the following specifications: X-axis will be Gender name, Show the value on each bar, use different and standard color. Include a suitable chart title, as well as titles for the X-axis and Y-axis. Clearly label the X-axis and Y-axis values. Adjust chart width and height as needed for readability, so all values are visible without overlap."
}
{
**Query:** "Plot an line chart of Month wise Total Amount for the Year of 2023 based on the Date column."
**Refined Prompt:** "Plot an line chart of Month wise Total Amount for the year of 2023 based on the Date column. With the following specifications: X-axis will be Month name, always display month names as Jan, Feb, Mar, etc. in chronological order. Show the value on each point, use different and standard color. Include a suitable chart title, as well as titles for the X-axis and Y-axis. Clearly label the X-axis and Y-axis values. Adjust chart width and height as needed for readability, so all values are visible without overlap."
}
{
**Query:** "I want the Unique Gender and their month wise total Total Amount as a table for the Year of 2023 based on the Date column."
**Refined Prompt:** "I need the unique Gender along with their month wise Total Amount presented in a table format for the year 2023 calculated based on the Date column. With the following specifications: Arrange the table neatly, ensuring all rows and columns are clearly labeled with accurate values. Display month names as Jan, Feb, Mar, etc. in chronological order. If possible, add a total or grand total at the end of the table."
}
{
**Query:** "I want the Unique Product Categories and their month wise total Total Amount as a table for the Year of 2023 based on the Date column."
**Refined Prompt:** "I need the unique Product Categories along with their month wise Total Amount presented in a table format for the year 2023 calculated based on the Date column. With the following specifications: Arrange the table neatly, ensuring all rows and columns are clearly labeled with accurate values. Display month names as Jan, Feb, Mar, etc. in chronological order. If possible, add a total or grand total at the end of the table."
}
{
**Query:** "Plot an bar chart of month wise Total of Total Amount where legend will be gender for the Year of 2023 based on the Date column."
**Refined Prompt:** "Plot a bar chart of month-wise Total Amount with a legend for gender for the year 2023, based on the Date column. The chart should follow these specifications:
- The X-axis will display month names in chronological order: Jan, Feb, Mar, etc.
- Include a legend for the 'Gender' column, with clear formatting and a title.
- Each bar should represent the total amount for each month, divided into segments by gender.
- Show the value on each bar segment in the chart.
- Use distinct, standard colors for each gender.
- Include an appropriate chart title and titles for both the X-axis and Y-axis.
- Clearly label the X-axis and Y-axis values.
- Adjust chart width and height as needed for readability, so all values are visible without overlap.
}
{
**Query:** "Plot an bar chart of month-wise total Quantity for each gender and Product Category for the year 2023 based on the Date column."
**Refined Prompt:** "Plot a bar chart to visualize month-wise total Quantity for each gender (Male and Female) and Product Category for the year 2023. The chart should follow these specifications:
- X-axis will display month names in chronological order: Jan, Feb, Mar, etc.
- Each bar will represent the total Quantity for a specific month, divided into segments by Product Category and gender.
- Include a legend for both gender and product category, with clear formatting and a title.
- Show the value on each bar segment in the chart.
- Use distinct, standard colors for each product category within each gender.
- Include an appropriate chart title and titles for both the X-axis and Y-axis.
- Clearly label the X-axis and Y-axis values.
- Adjust the chart's width and height for readability, ensuring all values are visible without overlap.
}
{
**Query:** "I need the unique Product Categories and their Month-wise and Gender-wise total Quantity and Total Amount as a table for year 2023 based on the Date column."
**Refined Prompt:** "I need the unique Product Categories along with their month-wise and gender-wise total Quantity and Total Amount presented in a table format for the year 2023. The table should be structured with the following specifications:
- Filter the data based on the 'Date' column to include only records within the year 2023.
- Format the month names as 'Jan', 'Feb', 'Mar', etc., in chronological order.
- Ensure the table clearly labels all rows and columns, including 'Product Category' for the index column and 'Month' and 'Gender' for the columns.
- Add a grand total row at the end of the table with the name of 'Grand Total' showing the summed values for Quantity' and 'Total Amount' across all months.
- Ensure the data is presented in a pivot table format, with 'Quantity' and 'Total Amount' as multi-level column headers, and output it as a well-structured Pandas DataFrame.
"
}
"""
def refine_prompt(user_input):
# Define the human prompt template
human_prompt = HumanMessagePromptTemplate.from_template("{human_input}")
# Define the prompt template
prompt_template = PromptTemplate(
template="""
**System Message:** {system_message}
**Query:** {human_message}
**Refined Prompt:**
""",
input_variables=["system_message", "human_message"]
)
# Format the final prompt
final_prompt = prompt_template.format(system_message=system_message, human_message=user_input)
# Create the chain with the final prompt and the LLM
sequence = prompt_template | llm_llama
# Run the chain and parse the output
# result = chain.run(system_message=system_message, human_message=user_input)
result = sequence.invoke(input=dict(system_message=system_message, human_message=user_input))
result = result.content
if "**Refined Prompt:**" in result:
result = result.replace("**Refined Prompt:**", "").strip()
return result