-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
279 lines (230 loc) · 14.7 KB
/
models.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
from langchain_core.prompts import PromptTemplate
from langchain_community.utilities import SQLDatabase
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain.chains import LLMChain
#MODEL_NAME="gemini-2.0-flash-lite-preview-02-05"
MODEL_NAME="gemini-2.0-flash"
def init_database(
user: str,
password: str,
host: str,
port: str,
database: str,
db_type: str = "postgresql",
) -> SQLDatabase:
if db_type == "postgresql":
db_uri = f"postgresql+psycopg2://{user}:{password}@{host}:{port}/{database}"
elif db_type == "mysql":
db_uri = f"mysql+mysqldb://{user}:{password}@{host}:{port}/{database}"
elif db_type == "sqlite":
db_uri = f"sqlite:///{database}"
elif db_type == "oracle":
db_uri = f"oracle+cx_oracle://{user}:{password}@{host}:{port}/{database}"
elif db_type == "mssql":
db_uri = f"mssql+pyodbc://{user}:{password}@{host}:{port}/{database}"
return SQLDatabase.from_uri(db_uri)
def get_sql_chain(api_key):
template = PromptTemplate(
input_variables=["schema", "chat_history", "question", "db_type"],
template="""
# Role:
You are a SQL expert tasked with writing efficient queries to extract and organize data from a company's database. Your focus is on generating SQL queries that directly address user questions based on the provided database schema. You handle complex query requirements, including the use of Common Table Expressions (CTEs) and subqueries, ensuring optimal data extraction and visualization when needed.
# Responsibilities:
- Retrieve all necessary data required to answer user queries.
- Use CTEs and subqueries where necessary to handle complex logic.
- Ensure that the SQL query provides comprehensive and relevant data in one go, inferring useful additional information when appropriate.
- Prepare SQL queries optimized for visualization tasks if the question includes a graph or chart request.
- Rely strictly on the provided database schema and avoid external assumptions.
- If the database does not have the required data, politely inform the using a comment in the SQL query and and give some data that is available.
# Constraints:
- Output only the SQL query without any additional context, text, or enclosing syntax like backticks.
- Strictly base the SQL query on the database schema provided.
- If visualization is requested, ensure the query prepares data suitable for the graph type recommended.
# Input Format:
- Input is structured as a JSON with 'enhanced_question' and 'answer' fields.
- Example input:
{{
"enhanced_question": "I want to compare the revenue performance of different regions for the current quarter.",
"answer": {{
"Graph Type": "Pie chart or bar chart",
"Data Visualization": "If you want to show the proportion of total revenue contributed by each region, a pie chart works well with distinct color coding for each region. For a clearer comparison of exact revenue values, a horizontal bar chart is better, with regions on the y-axis and revenue on the x-axis. Use data labels or tooltips to display the exact revenue figures and percentages for better clarity."
}}
}}
# Output Format:
- A single SQL query without any additional context or formatting.
- Ensure efficiency and completeness in data extraction.
# Example Input/Output:
- Input:
{{
"enhanced_question": "I need to visualize the change in customer satisfaction scores over the last 12 months to understand any trends or patterns. What graph is most suitable, and how should the data be displayed?",
"graph": {{
"Graph Type": "Line graph",
"Data Visualization": "Plot customer satisfaction scores on a line graph with months on the x-axis and satisfaction scores on the y-axis. Use data points connected by smooth lines to show the overall trend. Adding markers at each data point can help highlight fluctuations, and including a moving average line can give additional insights into long-term trends. Ensure both axes are labeled clearly for better interpretation."
}}
}}
- Output:
SELECT region, SUM(revenue) AS total_revenue
FROM sales
WHERE quarter = 'current'
GROUP BY region
# Conversation History:
{chat_history}
# Your turn:
- Schema: {schema}
- Question: {question}
- DB Type: {db_type}
- SQL Query:
""",
)
# llm = ChatMistralAI(model="codestral-2405", mistral_api_key=MINSTRAL_API_KEY)
llm = ChatGoogleGenerativeAI(
model=MODEL_NAME, google_api_key=api_key, temperature=0
)
chain = LLMChain(
llm=llm,
prompt=template,
)
print("The SQL Model Ready...")
return chain
def get_response(api_key):
template = PromptTemplate(
input_variables=["schema", "chat_history", "query", "question", "response"],
template="""
### System Role Prompt:
You are a natural language response generator specialized in assisting users with questions about a company's database. Based on the provided table schema, question, SQL query, and SQL response, your task is to generate concise, informative answers that directly address the user's question.
- **Do not provide information beyond what is given.**
- **Do not highlight errors in the SQL query or its response.**
- **Notify the user if the conversation deviates from the given topic.**
### Response Generation Steps:
1. **Review the provided inputs**: table schema, users question, SQL query, and SQL response.
2. **Extract relevant information** from the SQL response to directly answer the user's question in first person.
3. **Craft a clear, helpful, and concise natural language response** in markdown based solely on the provided data.
4. Ensure the response is informative and aligned with the users question.
5. If the users question is off-topic or irrelevant to the data, **politely inform them** that the conversation has veered off course.
6. When using numerical data, **provide context** to make the response more meaningful
7. When providing counts or sums, **clarify the basis** of the calculation and have units which are relevant to the data and eay to understand.
### Example Input:
- **Table Schema:** Customers (CustomerID, Name, Age, Country)
- **Question:** How many customers are from Germany?
- **SQL Query:** SELECT COUNT(*) FROM Customers WHERE Country = 'Germany';
- **SQL Response:** 15
### Example Output:
The company has **15** customers from Germany. This number is based on filtering the customer database to count those located in Germany.
### Additional Notes:
- Always base your response on the explicit SQL response or what is directly implied by the data schema.
- Avoid speculation or providing additional, unrequested details.
- Generate responces in paragraph in markdowm format, ensuring clarity and relevance to the user's question.
- use features of mark down like tables, bold, italics, and lists to make the response more readable.
---
#### Input format:
- **Schema**: {schema}
- **Conversation History**: {chat_history}
- **SQL Query**: {query}
- **User Question**: {question}
- **SQL Response**: {response}
""",
)
llm = ChatGoogleGenerativeAI(
model=MODEL_NAME, google_api_key=api_key, temperature=0.1
)
chain = LLMChain(llm=llm, prompt=template)
print("The Explainer Model Ready...")
return chain
def generate_plotly_code(api_key) -> str:
template = PromptTemplate(
input_variables=["question", "sql", "df_metadata", "sample_data"],
template=""" **System Role:**
You are an expert Python assistant specialized in data visualization using Matplotlib. Your task is to generate Python code that charts data from a predefined pandas DataFrame named 'df' using insights from its metadata and sample data, based on a user’s question. You strictly output Python code without explanations, focusing on the appropriate chart type for the question asked.
**Task Description:**
You need to generate Python Matplotlib code to visualize results from a DataFrame 'df'. The user will provide you with the following inputs:
- Metadata about the structure of the DataFrame.
- A sample of the data in the DataFrame.
- A specific question asking for a particular type of visualization.
**Steps to Follow:**
1. Analyze the metadata and sample data to understand the structure of 'df'.
2. Determine the most appropriate chart type based on the user's question and the characteristics of the data.
3. Write the corresponding Python code to create the visualization using Matplotlib.
**Key Rules:**
- Assume that DataFrame 'df' is predefined and accessible, you do not need to defie it.
- If there is only one value in the DataFrame, use an Indicator chart.
- Always include `plt.show()` at the end to display the chart.
- The user question will guide the type of chart required.
- The sample data provided is just for context; do not use it directly in your code.
- Focus on clear and concise visualizations of the data.
- Provide only the Python code without explanations or formatting (like backticks).
- The sample data is for reference only and should not be used in the code.
- <important>The sample-data may not be representative of the entire dataset, it provides a glimpse of the data structure.</important>
---
**Contextual Information:**
<information>
The following details what type of graph is required: '{question}'
The following is a pandas DataFrame.
The DataFrame was produced using this query: {sql}
The following is information about the resulting pandas DataFrame 'df':
{df_metadata}
This is some sample data from the DataFrame: <sample-data>{sample_data}<sample-data>
</information>
""",
)
llm = ChatGoogleGenerativeAI(
model=MODEL_NAME, google_api_key=api_key, temperature=0
)
chain = LLMChain(llm=llm, prompt=template)
print("The Visualization Model Ready...")
return chain
def better_question(api_key):
template = PromptTemplate(
input_variables=["user_input", "chat_history", "tables", "documnet"],
template="""**Role**: You are an advanced context aware queation generating assistant that specializes in clarifying and improving user questions related to the data involved and graph types. Your role is to analyze user queries based on the current chat and historical context, and guide them towards the most effective visualization techniques.
---
**Task**: Clarify, improve, and enhance the user's questions considering previous interactions, chat history, and goals. Provide detailed advice on graph selection and data visualization. Additionally, table names will be provided, and you must analyze and select the most relevant tables for the visualization process, returning them in the JSON.
### Steps (Simplified):
1. **Analyze the Question**:
- Identify the user's main inquiry and check if it relates to prior questions(chat history) and get who the **pronouns** are refering to.
2. **Key Data Elements**:
- Review user input and context to understand the data and goal.
3. **Table Selection**:
- Choose the most relevant tables based on the goal.
4. **Graph Type**:
- Select a graph type (e.g., bar, line) based on the data and goal.
5. **Clarify the Question**:
- Rephrase the query for better clarity and actionable results.
6. **Describe Visualization**:
- Suggest how to present the data (axis labels, readability) and needed transformations.
### Output Format:
Provide your response in the form of a JSON object containing:
- `"enhanced_question"`: A clearer, rephrased version of the user's question in first-person consideer the chat history.
- `"graph"`: An object detailing the recommended graph type and visualization approach.
- `"useful_tables"`: A list of the most relevant table names selected from those provided and never leave it empty.
### Example:
**Original User Input**: "Which region performed better in terms of revenue?"
**Tables to Select From**: ["sales", "revenue", "region", "customer", "product", "inventory", "order", "shipment", "employee", "supplier"]
**Enhanced JSON Output**:
{{
"enhanced_question": "Compare the revenue performance of all regions and provide details on the top-performing region.",
"graph": {{
"Graph Type": "Bar chart or pie chart",
"Data Visualization": "Use a bar chart for accurate revenue comparison with regions on the y-axis and revenue on the x-axis. Alternatively, use a pie chart to represent proportions."
}},
"useful_tables": ["revenue", "region"]
}}
---
### Additional Notes:
- The JSON object should always be provided in the format shown in the example.
- Ensure that you consider the user's query in the context of the conversation history.
- Ensure that each suggestion focuses on helping the user gain actionable insights through clear visualization.
- Favor graphs like bar charts over tables, unless the user specifies otherwise.
- Select the table names from the provided tables only and never leave it empty
---
**User chat History**: `{chat_history}`
**Some documentation**: `{documnet}`
**User's Current Input**: `{user_input}`
**Tables to select from**:`{tables}`
""",
)
llm = ChatGoogleGenerativeAI(
model=MODEL_NAME, google_api_key=api_key, temperature=0.3
)
chain = LLMChain(llm=llm, prompt=template)
print("The Question Enhansor Model Ready...")
return chain