-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDataAnalyzer.py
More file actions
370 lines (301 loc) · 15 KB
/
Copy pathDataAnalyzer.py
File metadata and controls
370 lines (301 loc) · 15 KB
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import pandas as pd
from typing import Dict, List, Tuple
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain, SequentialChain
from OprFuncs import *
#from langchain.schema.runnable import RunnableSequence
from langchain_core.messages import HumanMessage, AIMessage
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
#from langchain.agents import AgentExecutor, Tool, create_react_agent
#from langchain import hub
import re
#from modelEXT.PygalCodeComponents import PygalCodeComponents
#from langchain.output_parsers import PydanticOutputParser
from DatabaseManager import DatabaseManager
from langchain_experimental.agents import create_pandas_dataframe_agent
class DataAnalyzer:
def __init__(self,dataframe,llm,user_id=None):
self.dataframe = dataframe
self.llm = llm
self.data_info = data_infer(dataframe)
self.data_description = data_describer(dataframe)
self.data_sample = dataframe.head().to_string()
self.data_cols = ", ".join(dataframe.columns)
self.db = DatabaseManager()
self.report_id = None
self.memory = []
self.user_id = user_id
def analysis_data(self):
data_info = self.data_info
data_sample = self.data_sample
data_description = self.data_description
analysis_template = '''
You are a world-class data analyst.
You are provided with:
1. Dataset metadata: {data_info}
2. Dataset sample: {data_sample}
3. Dataset summary: {data_description}
Your task is to provide a clear, comprehensive, and insightful **summary of the actual dataset provided**.
- **Do not invent or assume any additional data.**
- **Do not generate code , hypothetical examples, or reference data not present.**
- Focus only on the real data and its characteristics.
- Summarize the most important findings, patterns, and statistics present in the dataset.
- Highlight any interesting trends, outliers, or relationships you observe.
- If the data is limited, mention this and only summarize what is actually present.
Respond with a professional, business-oriented summary suitable for decision-makers.
'''
analysis_prompt = PromptTemplate(
input_variables=["data_info", "data_sample", "data_description"],
template=analysis_template
)
analysis_chain = analysis_prompt | self.llm
formatted_analysis_prompt = analysis_template.format(data_info=data_info,data_sample=data_sample,
data_description=data_description)
self.analysis = self.llm.invoke(formatted_analysis_prompt)
self.memory.append(HumanMessage(content=formatted_analysis_prompt))
self.memory.append(AIMessage(content=self.analysis))
self.db.saveMemory(reportID=self.report_id,
llm=self.db.llm_id_by_name(self.llm.model),
prompet=formatted_analysis_prompt,
response=self.analysis,
chat=False)
return self.analysis
def questions_gen(self, num):
data_info = self.data_info
data_sample = self.data_sample
data_description = self.data_description
question_prompt = f"""
You are a senior data analyst hired by a company to extract meaningful, high-level, and actionable business insights from the following dataset.
Your job is to generate advanced **strategic questions** that:
- Are deeply rooted in the data structure and semantics.
- Reflect important **business objectives**, patterns, risks, or growth opportunities.
- Are **strong, insightful, and relevant** to decision-makers like company owners or managers.
- Can be **easily visualized** using bar charts, line plots, histograms, scatter plots, or pie charts.
**DO NOT generate general or surface-level questions. Instead, focus on questions that:**
- Quantify change over time or between groups.
- Explore distribution, frequency, or correlation.
- Investigate trends, seasonality, or anomalies.
- Provide guidance for optimizing business performance or identifying risks.
You MUST generate exactly {num} chartable, insightful questions.
### INPUTS:
1. Dataset Overview: {data_info}
2. Dataset Sample: {data_sample}
3. Data Summary: {data_description}
### OUTPUT FORMAT:
Write {num} powerful analytical questions that:
- Could be visualized with a chart.
- Have clear business relevance.
- Reflect advanced reasoning.
Each question should be written on a separate line.
"""
question_template = PromptTemplate(
input_variables=["num", "data_info", "data_sample", "data_description"],
template=question_prompt
)
question_chain = question_template | self.llm
try:
generated_questions = question_chain.invoke({
"num": num,
"data_info": data_info,
"data_sample": data_sample,
"data_description": data_description
})
# Ensure the response is properly encoded
if isinstance(generated_questions, str):
generated_questions = generated_questions.encode('utf-8', 'replace').decode('utf-8')
print("Raw LLM Output:", repr(generated_questions))
if not generated_questions.strip():
print("Warning: LLM did not generate any questions.")
return []
# Use the improved extraction function
questions_list = extract_questions(generated_questions)
print("Extracted Questions List:", questions_list)
# Trim or handle missing questions
if len(questions_list) > num:
questions_list = questions_list[:num]
elif len(questions_list) < num:
print(f"Warning: Expected {num} questions, but got {len(questions_list)}")
# Store in memory
formatted_question_prompt = question_template.format(
num=num,
data_info=data_info,
data_sample=data_sample,
data_description=data_description
)
self.memory.append(HumanMessage(content=formatted_question_prompt))
self.memory.append(AIMessage(content="\n".join(questions_list)))
self.db.saveMemory(reportID=self.report_id,
llm=self.db.llm_id_by_name(self.llm.model),
prompet=formatted_question_prompt,
response="\n".join(questions_list),
chat=False)
return questions_list
except Exception as e:
print(f"Error generating questions: {str(e)}")
return []
def chat(self,question):
prompt_template = ChatPromptTemplate.from_messages(
[
(
"system",
"You are a data analyst.",
),
MessagesPlaceholder(variable_name="memory"),
("human", "{input}"),
]
)
chain = prompt_template | self.llm
response = chain.invoke({"input": question, "memory":self.memory})
self.db.saveMemory(reportID=self.report_id,
llm=self.db.llm_id_by_name(self.llm.model),
prompet=question,
response=response,
chat=True)
self.memory.append(HumanMessage(content=question))
self.memory.append(AIMessage(content=response))
return response
def select_chart_type(self, question: str) -> str:
self.chart_type_prompt = ChatPromptTemplate.from_messages([
("system", """You are an expert at selecting chart types for data visualization. Strictly follow these rules:
1. CHART SELECTION GUIDE:
- For comparing categories: Bar
- For trends over time: Line
- For parts of a whole: Pie (few categories)
- For relationships: Scatter
- For the distribution of a numirecal variable: Histogram
3. OUTPUT FORMAT (EXACTLY):
chart_type: [Bar|Line|Pie|Scatter|Histogram]
Data Description: {data_description}
Available Columns: {columns}
Sample Data: {sample_data}
Question: {question}
Respond ONLY with:
chart_type: [chart_type]""")
])
"""Select only the chart type based on the question and data."""
self.llm.temperature = 0.3
chain = self.chart_type_prompt | self.llm
response = chain.invoke({
"data_description": self.data_description,
"columns": self.data_cols,
"sample_data": self.data_sample,
"question": question
})
self.llm.temperature = 0.7
# Parse response
chart_match = re.search(r'chart_type:\s*([a-zA-Z]+)', response, re.IGNORECASE)
chart_type = chart_match.group(1) if chart_match else None
# Validate
allowed_charts = {
'Bar', 'Line', 'Histogram',
'Pie', 'Scatter'
}
return chart_type if chart_type in allowed_charts else 'Bar'
def select_columns(self, question: str) -> List[str]:
self.columns_prompt = ChatPromptTemplate.from_messages([
("system", """You are an expert at selecting relevant columns for data visualization. Strictly follow:
1. COLUMN SELECTION RULES:
- Focus on columns mentioned in the question
- What is being measured (numerical columns)
- What is being compared/grouped by (categorical columns)
- Any time dimensions for trends
- Never suggest columns not in Available Columns
2. OUTPUT FORMAT (EXACTLY):
columns: [exact_column_name1, exact_column_name2]
Data Description: {data_description}
Available Columns: {columns}
Sample Data: {sample_data}
Question: {question}
Respond ONLY with:
columns: [column1, column2]""")
])
""""Select only the relevant columns based on the question and data."""
self.llm.temperature = 0.3
chain = self.columns_prompt | self.llm
response = chain.invoke({
"data_description": self.data_description,
"columns":self.data_cols,
"sample_data": self.data_sample,
"question": question
})
self.llm.temperature = 0.7
# Parse response
cols_match = re.search(r'columns:\s*\[([^\]]+)\]', response)
if cols_match:
columns = [col.strip() for col in cols_match.group(1).split(',')]
else:
# Fallback parsing
cols_line = next((line for line in response.split('\n') if line.startswith('columns:')), '')
columns = [col.strip() for col in cols_line.replace('columns:', '').split(',') if col.strip()]
# Validate columns exist in data
available_cols = self.dataframe.columns.tolist()
return [col for col in columns if col in available_cols]
def get_chart_recommendation(self, question: str) -> Tuple[str, List[str]]:
"""Combined recommendation (maintaining original interface)"""
chart_type = self.select_chart_type(question)
columns = self.select_columns(question)
return chart_type, columns
def generate_recommendations(self, num_recommendations: int = 5):
data_info = self.data_info
data_sample = self.data_sample
data_description = self.data_description
analysis = self.analysis # التحليل الذي تم عمله سابقاً
recommendation_prompt = '''
You are a world-class business consultant and data analyst.
You have analyzed the following:
- Dataset metadata: {data_info}
- Dataset sample: {data_sample}
- Dataset summary: {data_description}
- Detailed business analysis: {analysis}
Based on your deep understanding of the data and analysis:
Your task is to generate {num_recommendations} highly actionable, strategic recommendations for the business.
Your recommendations must:
- Be directly based on the analysis and insights.
- Address clear business actions (e.g., optimize processes, launch new products, reduce risks, target specific segments, etc.)
- Be specific, impactful, and feasible.
- Cover both short-term quick wins and long-term strategic moves.
- Include estimated expected outcome in percentage (%) where appropriate.
- Include any potential risks or challenges for each recommendation.
- Reference relevant metrics or insights from the analysis if possible.
- Use professional, executive-level language.
- Add an appropriate emoji based on risk level:
- ✅ for Low risk
- ⚠️ for Medium risk
- ❗for High risk
Output Format:
### 📋 Full Recommendation Details
1. **[Recommendation Title]** [Emoji]
- **Details:** Explain clearly what should be done and why.
- **Expected Impact:** [e.g., Increase attendance by 10%]
- **Metrics Reference:** [Reference specific metric if available, e.g., matches with <50% attendance]
- **Potential Risks:** [Possible challenges or risks involved]
- **Timeline:** [Short-term or Long-term]
Repeat similarly for each recommendation.
'''
rec_template = PromptTemplate(
input_variables=["data_info", "data_sample", "data_description", "analysis", "num_recommendations"],
template=recommendation_prompt
)
rec_chain = LLMChain(llm=self.llm, prompt=rec_template)
rec_response = rec_chain.run(
data_info=data_info,
data_sample=data_sample,
data_description=data_description,
analysis=analysis,
num_recommendations=num_recommendations
)
formatted_rec_prompt = recommendation_prompt.format(
data_info=data_info,
data_sample=data_sample,
data_description=data_description,
analysis=analysis,
num_recommendations=num_recommendations
)
self.memory.append(HumanMessage(content=formatted_rec_prompt))
self.memory.append(AIMessage(content=rec_response))
self.db.saveMemory(reportID=self.report_id,
llm=self.db.llm_id_by_name(self.llm.model),
prompet=formatted_rec_prompt,
response=rec_response,
chat=False)
return rec_response