-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathdata_analysis_agent.py
More file actions
362 lines (312 loc) · 15.1 KB
/
data_analysis_agent.py
File metadata and controls
362 lines (312 loc) · 15.1 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
# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os, io, re
import pandas as pd
import streamlit as st
from openai import OpenAI
import matplotlib.pyplot as plt
from typing import List, Dict, Any, Tuple
from dotenv import load_dotenv
load_dotenv()
# === Configuration ===
api_key = os.environ.get("NVIDIA_API_KEY")
client = OpenAI(
base_url="https://integrate.api.nvidia.com/v1",
api_key=api_key
)
# ------------------ QueryUnderstandingTool ---------------------------
def QueryUnderstandingTool(query: str) -> bool:
"""Return True if the query seems to request a visualisation based on keywords."""
# Use LLM to understand intent instead of keyword matching
messages = [
{"role": "system", "content": "detailed thinking off. You are an assistant that determines if a query is requesting a data visualization. Respond with only 'true' if the query is asking for a plot, chart, graph, or any visual representation of data. Otherwise, respond with 'false'."},
{"role": "user", "content": query}
]
response = client.chat.completions.create(
model="nvidia/llama-3.1-nemotron-ultra-253b-v1",
#model="gpt-4o-mini",
messages=messages,
temperature=0.1,
max_tokens=5 # We only need a short response
)
# Extract the response and convert to boolean
intent_response = response.choices[0].message.content.strip().lower()
return intent_response == "true"
# === CodeGeneration TOOLS ============================================
# ------------------ PlotCodeGeneratorTool ---------------------------
def PlotCodeGeneratorTool(cols: List[str], query: str) -> str:
"""Generate a prompt for the LLM to write pandas+matplotlib code for a plot based on the query and columns."""
return f"""
Given DataFrame `df` with columns: {', '.join(cols)}
Write Python code using pandas **and matplotlib** (as plt) to answer:
"{query}"
Rules
-----
1. Use pandas for data manipulation and matplotlib.pyplot (as plt) for plotting.
2. Assign the final result (DataFrame, Series, scalar *or* matplotlib Figure) to a variable named `result`.
3. Create only ONE relevant plot. Set `figsize=(6,4)`, add title/labels.
4. Return your answer inside a single markdown fence that starts with ```python and ends with ```.
"""
# ------------------ CodeWritingTool ---------------------------------
def CodeWritingTool(cols: List[str], query: str) -> str:
"""Generate a prompt for the LLM to write pandas-only code for a data query (no plotting)."""
return f"""
Given DataFrame `df` with columns: {', '.join(cols)}
Write Python code (pandas **only**, no plotting) to answer:
"{query}"
Rules
-----
1. Use pandas operations on `df` only.
2. Assign the final result to `result`.
3. Wrap the snippet in a single ```python code fence (no extra prose).
"""
# === CodeGenerationAgent ==============================================
def CodeGenerationAgent(query: str, df: pd.DataFrame):
"""Selects the appropriate code generation tool and gets code from the LLM for the user's query."""
should_plot = QueryUnderstandingTool(query)
prompt = PlotCodeGeneratorTool(df.columns.tolist(), query) if should_plot else CodeWritingTool(df.columns.tolist(), query)
messages = [
{"role": "system", "content": "detailed thinking off. You are a Python data-analysis expert who writes clean, efficient code. Solve the given problem with optimal pandas operations. Be concise and focused. Your response must contain ONLY a properly-closed ```python code block with no explanations before or after. Ensure your solution is correct, handles edge cases, and follows best practices for data analysis."},
{"role": "user", "content": prompt}
]
response = client.chat.completions.create(
model="nvidia/llama-3.1-nemotron-ultra-253b-v1",
#model="gpt-4o-mini",
messages=messages,
temperature=0.2,
max_tokens=1024
)
full_response = response.choices[0].message.content
code = extract_first_code_block(full_response)
return code, should_plot, ""
# === ExecutionAgent ====================================================
def ExecutionAgent(code: str, df: pd.DataFrame, should_plot: bool):
"""Executes the generated code in a controlled environment and returns the result or error message."""
env = {"pd": pd, "df": df}
if should_plot:
plt.rcParams["figure.dpi"] = 100 # Set default DPI for all figures
env["plt"] = plt
env["io"] = io
try:
exec(code, {}, env)
return env.get("result", None)
except Exception as exc:
return f"Error executing code: {exc}"
# === ReasoningCurator TOOL =========================================
def ReasoningCurator(query: str, result: Any) -> str:
"""Builds and returns the LLM prompt for reasoning about the result."""
is_error = isinstance(result, str) and result.startswith("Error executing code")
is_plot = isinstance(result, (plt.Figure, plt.Axes))
if is_error:
desc = result
elif is_plot:
title = ""
if isinstance(result, plt.Figure):
title = result._suptitle.get_text() if result._suptitle else ""
elif isinstance(result, plt.Axes):
title = result.get_title()
desc = f"[Plot Object: {title or 'Chart'}]"
else:
desc = str(result)[:300]
if is_plot:
prompt = f'''
The user asked: "{query}".
Below is a description of the plot result:
{desc}
Explain in 2–3 concise sentences what the chart shows (no code talk).'''
else:
prompt = f'''
The user asked: "{query}".
The result value is: {desc}
Explain in 2–3 concise sentences what this tells about the data (no mention of charts).'''
return prompt
# === ReasoningAgent (streaming) =========================================
def ReasoningAgent(query: str, result: Any):
"""Streams the LLM's reasoning about the result (plot or value) and extracts model 'thinking' and final explanation."""
prompt = ReasoningCurator(query, result)
is_error = isinstance(result, str) and result.startswith("Error executing code")
is_plot = isinstance(result, (plt.Figure, plt.Axes))
# Streaming LLM call
response = client.chat.completions.create(
model="nvidia/llama-3.1-nemotron-ultra-253b-v1",
#model="gpt-4o-mini",
messages=[
{"role": "system", "content": "detailed thinking on. You are an insightful data analyst."},
{"role": "user", "content": prompt}
],
temperature=0.2,
max_tokens=1024,
stream=True
)
# Stream and display thinking
thinking_placeholder = st.empty()
full_response = ""
thinking_content = ""
in_think = False
for chunk in response:
if chunk.choices[0].delta.content is not None:
token = chunk.choices[0].delta.content
full_response += token
# Simple state machine to extract <think>...</think> as it streams
if "<think>" in token:
in_think = True
token = token.split("<think>", 1)[1]
if "</think>" in token:
token = token.split("</think>", 1)[0]
in_think = False
if in_think or ("<think>" in full_response and not "</think>" in full_response):
thinking_content += token
thinking_placeholder.markdown(
f'<details class="thinking" open><summary>🤔 Model Thinking</summary><pre>{thinking_content}</pre></details>',
unsafe_allow_html=True
)
# After streaming, extract final reasoning (outside <think>...</think>)
cleaned = re.sub(r"<think>.*?</think>", "", full_response, flags=re.DOTALL).strip()
return thinking_content, cleaned
# === DataFrameSummary TOOL (pandas only) =========================================
def DataFrameSummaryTool(df: pd.DataFrame) -> str:
"""Generate a summary prompt string for the LLM based on the DataFrame."""
prompt = f"""
Given a dataset with {len(df)} rows and {len(df.columns)} columns:
Columns: {', '.join(df.columns)}
Data types: {df.dtypes.to_dict()}
Missing values: {df.isnull().sum().to_dict()}
Provide:
1. A brief description of what this dataset contains
2. 3-4 possible data analysis questions that could be explored
Keep it concise and focused."""
return prompt
# === DataInsightAgent (upload-time only) ===============================
def DataInsightAgent(df: pd.DataFrame) -> str:
"""Uses the LLM to generate a brief summary and possible questions for the uploaded dataset."""
prompt = DataFrameSummaryTool(df)
try:
response = client.chat.completions.create(
model="nvidia/llama-3.1-nemotron-ultra-253b-v1",
#model="gpt-4o-mini",
messages=[
{"role": "system", "content": "detailed thinking off. You are a data analyst providing brief, focused insights."},
{"role": "user", "content": prompt}
],
temperature=0.2,
max_tokens=512
)
return response.choices[0].message.content
except Exception as exc:
return f"Error generating dataset insights: {exc}"
# === Helpers ===========================================================
def extract_first_code_block(text: str) -> str:
"""Extracts the first Python code block from a markdown-formatted string."""
start = text.find("```python")
if start == -1:
return ""
start += len("```python")
end = text.find("```", start)
if end == -1:
return ""
return text[start:end].strip()
# === Main Streamlit App ===============================================
def main():
st.set_page_config(layout="wide")
if "plots" not in st.session_state:
st.session_state.plots = []
left, right = st.columns([3,7])
with left:
st.header("Data Analysis Agent")
st.markdown("<medium>Powered by <a href='https://build.nvidia.com/nvidia/llama-3_1-nemotron-ultra-253b-v1'>NVIDIA Llama-3.1-Nemotron-Ultra-253B-v1</a></medium>", unsafe_allow_html=True)
#file = st.file_uploader("Choose CSV", type=["csv"])
file = st.file_uploader("Choose CSV or Excel", type=["csv", "xlsx"])
if file:
if ("df" not in st.session_state) or (st.session_state.get("current_file") != file.name):
if file.name.endswith(".csv"):
st.session_state.df = pd.read_csv(file)
elif file.name.endswith(".xlsx"):
st.session_state.df = pd.read_excel(file)
else:
st.error("Unsupported file format.")
return
st.session_state.current_file = file.name
st.session_state.messages = []
with st.spinner("Generating dataset insights …"):
st.session_state.insights = DataInsightAgent(st.session_state.df)
st.dataframe(st.session_state.df.head())
st.markdown("### Dataset Insights")
st.markdown(st.session_state.insights)
else:
st.info("Upload a CSV to begin chatting with your data.")
with right:
st.header("Chat with your data")
if "messages" not in st.session_state:
st.session_state.messages = []
chat_container = st.container()
with chat_container:
for msg in st.session_state.messages:
with st.chat_message(msg["role"]):
st.markdown(msg["content"], unsafe_allow_html=True)
if msg.get("plot_index") is not None:
idx = msg["plot_index"]
if 0 <= idx < len(st.session_state.plots):
# Display plot at fixed size
st.pyplot(st.session_state.plots[idx], use_container_width=False)
if file: # only allow chat after upload
if user_q := st.chat_input("Ask about your data…"):
st.session_state.messages.append({"role": "user", "content": user_q})
with st.spinner("Working …"):
code, should_plot_flag, code_thinking = CodeGenerationAgent(user_q, st.session_state.df)
result_obj = ExecutionAgent(code, st.session_state.df, should_plot_flag)
raw_thinking, reasoning_txt = ReasoningAgent(user_q, result_obj)
reasoning_txt = reasoning_txt.replace("`", "")
# Build assistant response
is_plot = isinstance(result_obj, (plt.Figure, plt.Axes))
plot_idx = None
if is_plot:
fig = result_obj.figure if isinstance(result_obj, plt.Axes) else result_obj
st.session_state.plots.append(fig)
plot_idx = len(st.session_state.plots) - 1
header = "Here is the visualization you requested:"
elif isinstance(result_obj, (pd.DataFrame, pd.Series)):
header = f"Result: {len(result_obj)} rows" if isinstance(result_obj, pd.DataFrame) else "Result series"
else:
header = f"Result: {result_obj}"
# Show only reasoning thinking in Model Thinking (collapsed by default)
thinking_html = ""
if raw_thinking:
thinking_html = (
'<details class="thinking">'
'<summary>🧠 Reasoning</summary>'
f'<pre>{raw_thinking}</pre>'
'</details>'
)
# Show model explanation directly
explanation_html = reasoning_txt
# Code accordion with proper HTML <pre><code> syntax highlighting
code_html = (
'<details class="code">'
'<summary>View code</summary>'
'<pre><code class="language-python">'
f'{code}'
'</code></pre>'
'</details>'
)
# Combine thinking, explanation, and code accordion
assistant_msg = f"{thinking_html}{explanation_html}\n\n{code_html}"
st.session_state.messages.append({
"role": "assistant",
"content": assistant_msg,
"plot_index": plot_idx
})
st.rerun()
if __name__ == "__main__":
main()