Spaces:
Sleeping
Sleeping
File size: 26,141 Bytes
472e1d4 9016439 472e1d4 9016439 472e1d4 9016439 472e1d4 |
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 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 |
import sqlite3
import pandas as pd
import re
from agents.llms import LLM
from agents.tools import PlotSQLTool
from .states import SQLAgentState
from utils.consts import DB_PATH
def choose_visualization(state: SQLAgentState) -> SQLAgentState:
"""Use LLM to suggest a suitable chart type for the SQL result."""
question = state['question']
sql_query = state['sql_query']
sql_result = state['sql_result']
# Convert sql_result DataFrame to markdown or string preview (or sample rows)
if sql_result is not None:
if hasattr(sql_result, 'head'):
preview = sql_result.head(5).to_markdown(index=False)
else:
preview = str(sql_result)
else:
preview = "No results"
prompt = f'''
You are an AI assistant that recommends appropriate data visualizations. Based on the user's question, SQL query, and query results, suggest the most suitable type of graph or chart to visualize the data. If no visualization is appropriate, indicate that.
Available chart types and their use cases:
- Bar Graphs: Best for comparing categorical data or showing changes over time when categories are discrete and the number of categories is more than 2.
- Horizontal Bar Graphs: Best for comparing categorical data or showing changes over time when the number of categories is small or the disparity between categories is large.
- Scatter Plots: Useful for identifying relationships or correlations between two numerical variables or plotting distributions of data. Best used when both x axis and y axis are continuous.
- Pie Charts: Ideal for showing proportions or percentages within a whole.
- Line Graphs: Best for showing trends and distributions over time. Best used when both x axis and y axis are continuous or time-based.
Provide your response in the following format:
Recommended Visualization: [Chart type or "None"]. ONLY use the following names: bar, horizontal_bar, line, pie, scatter, none
Reason: [Brief explanation for your recommendation]
User question: {question}
SQL query: {sql_query}
Query results: {preview}
Recommend a visualization:
'''
llm = LLM()
response = llm.generate(prompt)
lines = response.split('\n')
visualization = 'none'
reason = ''
for line in lines:
if line.lower().startswith('recommended visualization:'):
visualization = line.split(':', 1)[1].strip()
elif line.lower().startswith('reason:'):
reason = line.split(':', 1)[1].strip()
state['visualization'] = visualization
state['visualization_reason'] = reason
state['step'] = 'choose_visualization'
return state
def format_data_for_visualization(state: SQLAgentState) -> SQLAgentState:
"""
Format the data for the chosen visualization type.
Hỗ trợ line, bar, scatter, grouped bar, fallback LLM cho các visualization khác.
"""
import json
import pandas as pd
llm = LLM()
visualization = state.get('visualization', 'none')
sql_result = state.get('sql_result')
question = state.get('question')
sql_query = state.get('sql_query')
# Convert DataFrame to list of lists for processing
if sql_result is not None and hasattr(sql_result, 'values'):
data = sql_result.values.tolist()
columns = list(sql_result.columns)
elif isinstance(sql_result, list):
data = sql_result
columns = []
else:
state['formatted_data_for_visualization'] = None
return state
def _format_line_data(data, question):
if len(data[0]) == 2:
x_values = [str(row[0]) for row in data]
y_values = [float(row[1]) for row in data]
prompt = f"""
You are a data labeling expert. Given a question and some data, provide a concise and relevant label for the data series.
Question: {question}
Data (first few rows): {data[:2]}
Provide a concise label for this y axis.
"""
label = llm.generate(prompt).strip()
formatted_data = {
"xValues": x_values,
"yValues": [
{
"data": y_values,
"label": label
}
]
}
return formatted_data
elif len(data[0]) == 3:
data_by_label = {}
x_values = []
labels = list(set(item2 for item1, item2, item3 in data if isinstance(item2, str) and not item2.replace(".", "").isdigit() and "/" not in item2))
if not labels:
labels = list(set(item1 for item1, item2, item3 in data if isinstance(item1, str) and not item1.replace(".", "").isdigit() and "/" not in item1))
for item1, item2, item3 in data:
if isinstance(item1, str) and not item1.replace(".", "").isdigit() and "/" not in item1:
label, x, y = item1, item2, item3
else:
x, label, y = item1, item2, item3
if str(x) not in x_values:
x_values.append(str(x))
if label not in data_by_label:
data_by_label[label] = []
data_by_label[label].append(float(y))
for other_label in labels:
if other_label != label:
if other_label not in data_by_label:
data_by_label[other_label] = []
data_by_label[other_label].append(None)
y_values = [
{
"data": data,
"label": label
}
for label, data in data_by_label.items()
]
formatted_data = {
"xValues": x_values,
"yValues": y_values,
"yAxisLabel": ""
}
prompt = f"""
You are a data labeling expert. Given a question and some data, provide a concise and relevant label for the y-axis.
Question: {question}
Data (first few rows): {data[:2]}
Provide a concise label for the y-axis.
"""
y_axis_label = llm.generate(prompt).strip()
formatted_data["yAxisLabel"] = y_axis_label
return formatted_data
return None
def _format_scatter_data(data):
formatted_data = {"series": []}
if len(data[0]) == 2:
formatted_data["series"].append({
"data": [
{"x": float(x), "y": float(y), "id": i+1}
for i, (x, y) in enumerate(data)
],
"label": "Data Points"
})
elif len(data[0]) == 3:
entities = {}
for item1, item2, item3 in data:
if isinstance(item1, str) and not item1.replace(".", "").isdigit() and "/" not in item1:
label, x, y = item1, item2, item3
else:
x, label, y = item1, item2, item3
if label not in entities:
entities[label] = []
entities[label].append({"x": float(x), "y": float(y), "id": len(entities[label])+1})
for label, d in entities.items():
formatted_data["series"].append({
"data": d,
"label": label
})
else:
raise ValueError("Unexpected data format in results")
return formatted_data
def _format_bar_data(data, question):
if len(data[0]) == 2:
labels = [str(row[0]) for row in data]
values = [float(row[1]) for row in data]
prompt = f"""
You are a data labeling expert. Given a question and some data, provide a concise and relevant label for the data series.
Question: {question}
Data (first few rows): {data[:2]}
Provide a concise label for this y axis.
"""
label = llm.generate(prompt).strip()
y_values = [{"data": values, "label": label}]
elif len(data[0]) == 3:
categories = set(row[1] for row in data)
labels = list(categories)
entities = set(row[0] for row in data)
y_values = []
for entity in entities:
entity_data = [float(row[2]) for row in data if row[0] == entity]
y_values.append({"data": entity_data, "label": str(entity)})
else:
raise ValueError("Unexpected data format in results")
formatted_data = {
"labels": labels,
"values": y_values
}
return formatted_data
def _format_other_visualizations(visualization, question, sql_query, data):
# Fallback: use LLM to format data
prompt = f"""
You are a Data expert who formats data according to the required needs. You are given the question asked by the user, its sql query, the result of the query and the format you need to format it in.
For the given question: {question}\n\nSQL query: {sql_query}\n\nResult: {data}\n\nFormat this data for visualization type: {visualization}. Just give the json string. Do not format it.
"""
response = llm.generate(prompt)
try:
formatted_data_for_visualization = json.loads(response)
return formatted_data_for_visualization
except json.JSONDecodeError:
return {"error": "Failed to format data for visualization", "raw_response": response}
visualization_map = {
"none": lambda data: None,
"scatter": lambda data: _format_scatter_data(data),
"bar": lambda data, question: _format_bar_data(data, question),
"horizontal_bar": lambda data, question: _format_bar_data(data, question),
"line": lambda data, question: _format_line_data(data, question)
}
try:
state["formatted_data_for_visualization"] = visualization_map[visualization](data, question)
except (KeyError, Exception):
state["formatted_data_for_visualization"] = _format_other_visualizations(visualization, question, sql_query, data)
state['step'] = 'format_data_for_visualization'
return state
def render_visualization(state: SQLAgentState) -> SQLAgentState:
"""
Render the visualization from formatted data.
Output: path to saved image file.
"""
import matplotlib.pyplot as plt
import os
from io import BytesIO
import uuid
data = state.get("formatted_data_for_visualization")
visualization = state.get("visualization", "none")
if not data:
state["visualization_output"] = None
return state
output_dir = "output/plots"
os.makedirs(output_dir, exist_ok=True)
def save_fig(fig):
file_path = os.path.join(output_dir, f"visualization_{uuid.uuid4().hex[:8]}.png")
fig.savefig(file_path, format="png", bbox_inches="tight")
plt.close(fig)
return file_path
def render_line(data):
fig, ax = plt.subplots()
x = data["xValues"]
for series in data["yValues"]:
ax.plot(x, series["data"], label=series["label"])
ax.set_xlabel("X")
ax.set_ylabel(data.get("yAxisLabel", "Y"))
ax.legend()
return save_fig(fig)
def render_bar(data, horizontal=False):
fig, ax = plt.subplots()
labels = data["labels"]
n_series = len(data["values"])
width = 0.8 / n_series
x_indexes = list(range(len(labels)))
for i, series in enumerate(data["values"]):
offset = (i - n_series / 2) * width + width / 2
if horizontal:
ax.barh(
[x + offset for x in x_indexes],
series["data"],
height=width,
label=series["label"]
)
ax.set_yticks(x_indexes)
ax.set_yticklabels(labels)
else:
ax.bar(
[x + offset for x in x_indexes],
series["data"],
width=width,
label=series["label"]
)
ax.set_xticks(x_indexes)
ax.set_xticklabels(labels, rotation=45, ha='right')
ax.legend()
return save_fig(fig)
def render_scatter(data):
fig, ax = plt.subplots()
for series in data["series"]:
xs = [point["x"] for point in series["data"]]
ys = [point["y"] for point in series["data"]]
ax.scatter(xs, ys, label=series["label"])
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.legend()
return save_fig(fig)
try:
if visualization == "line":
image_path = render_line(data)
elif visualization == "bar":
image_path = render_bar(data, horizontal=False)
elif visualization == "horizontal_bar":
image_path = render_bar(data, horizontal=True)
elif visualization == "scatter":
image_path = render_scatter(data)
else:
state["visualization_output"] = None
return state
state["visualization_output"] = image_path
except Exception as e:
state["visualization_output"] = None
state["error"] = f"Failed to render visualization: {str(e)}"
state["step"] = "render_visualization"
return state
def render_visualization(state: SQLAgentState) -> SQLAgentState:
"""
Render the visualization from formatted data.
Output: path to saved image file.
"""
import matplotlib.pyplot as plt
import os
import uuid
from typing import Dict, Any, Optional
def save_fig(fig: plt.Figure) -> str:
"""Save figure to file and return the file path."""
try:
output_dir = "output/plots"
os.makedirs(output_dir, exist_ok=True)
file_path = os.path.join(output_dir, f"visualization_{uuid.uuid4().hex[:8]}.png")
fig.savefig(file_path, format="png", bbox_inches="tight", dpi=100)
plt.close(fig)
return file_path
except Exception as e:
print(f"Error saving figure: {e}")
return ""
def validate_data(data: Dict[str, Any], required_keys: list) -> bool:
"""Validate that data contains all required keys and has valid values."""
if not all(key in data for key in required_keys):
return False
# Check if there's actual data to plot
if "values" in data and not data["values"]:
return False
if "yValues" in data and not data["yValues"]:
return False
return True
def render_line(data: Dict[str, Any]) -> Optional[str]:
"""Render line chart."""
required_keys = ["xValues", "yValues"]
if not validate_data(data, required_keys):
return None
try:
fig, ax = plt.subplots(figsize=(10, 6))
x = data["xValues"]
for series in data["yValues"]:
if len(x) == len(series["data"]):
ax.plot(x, series["data"], label=series.get("label", ""), marker='o')
ax.set_xlabel(data.get("xAxisLabel", "X"))
ax.set_ylabel(data.get("yAxisLabel", "Y"))
ax.set_title(data.get("title", ""))
if any(series.get("label") for series in data["yValues"]):
ax.legend()
plt.tight_layout()
return save_fig(fig)
except Exception as e:
print(f"Error rendering line chart: {e}")
return None
def render_bar(data: Dict[str, Any], horizontal: bool = False) -> Optional[str]:
"""Render bar chart (vertical or horizontal)."""
required_keys = ["labels", "values"]
if not validate_data(data, required_keys) or not data["values"]:
return None
try:
fig, ax = plt.subplots(figsize=(10, 6))
labels = data["labels"]
n_series = len(data["values"])
width = 0.8 / max(1, n_series) # Prevent division by zero
x_indexes = list(range(len(labels)))
for i, series in enumerate(data["values"]):
if not series["data"]: # Skip empty series
continue
offset = (i - n_series / 2) * width + width / 2
if horizontal:
ax.barh(
[x + offset for x in x_indexes],
series["data"],
height=width,
label=series.get("label", f"Series {i+1}")
)
ax.set_yticks(x_indexes)
ax.set_yticklabels(labels)
ax.set_xlabel(data.get("xAxisLabel", "Value"))
ax.set_ylabel(data.get("yAxisLabel", "Category"))
else:
ax.bar(
[x + offset for x in x_indexes],
series["data"],
width=width,
label=series.get("label", f"Series {i+1}")
)
ax.set_xticks(x_indexes)
ax.set_xticklabels(labels, rotation=45, ha='right')
ax.set_xlabel(data.get("xAxisLabel", "Category"))
ax.set_ylabel(data.get("yAxisLabel", "Value"))
if any(series.get("label") for series in data["values"]):
ax.legend()
ax.set_title(data.get("title", ""))
plt.tight_layout()
return save_fig(fig)
except Exception as e:
print(f"Error rendering {'horizontal ' if horizontal else ''}bar chart: {e}")
return None
def render_scatter(data: Dict[str, Any]) -> Optional[str]:
"""Render scatter plot."""
required_keys = ["series"]
if not validate_data(data, required_keys):
return None
try:
fig, ax = plt.subplots(figsize=(10, 6))
for series in data["series"]:
if not series.get("data"):
continue
xs = [point.get("x", 0) for point in series["data"]]
ys = [point.get("y", 0) for point in series["data"]]
if len(xs) == len(ys):
ax.scatter(
xs,
ys,
label=series.get("label"),
alpha=0.6,
edgecolors='w'
)
ax.set_xlabel(data.get("xAxisLabel", "X"))
ax.set_ylabel(data.get("yAxisLabel", "Y"))
ax.set_title(data.get("title", ""))
if any(series.get("label") for series in data["series"]):
ax.legend()
plt.tight_layout()
return save_fig(fig)
except Exception as e:
print(f"Error rendering scatter plot: {e}")
return None
# Main function logic
data = state.get("formatted_data_for_visualization")
visualization = state.get("visualization", "none")
state["visualization_output"] = None
if not data or visualization == "none":
return state
try:
renderers = {
"line": lambda: render_line(data),
"bar": lambda: render_bar(data, horizontal=False),
"horizontal_bar": lambda: render_bar(data, horizontal=True),
"scatter": lambda: render_scatter(data)
}
if visualization in renderers:
image_path = renderers[visualization]()
if image_path and os.path.exists(image_path):
state["visualization_output"] = image_path
else:
state["error"] = "Failed to generate visualization: No valid data to display"
else:
state["error"] = f"Unsupported visualization type: {visualization}"
except Exception as e:
state["error"] = f"Error in visualization: {str(e)}"
print(f"Visualization error: {e}")
state["step"] = "render_visualization"
return state
def finalize_output(state: SQLAgentState) -> SQLAgentState:
"""
Node hợp nhất kết quả cuối cùng (answer, visualization_output, error, ...).
Hiện tại chỉ trả về state, có thể mở rộng xử lý sau.
"""
state['step'] = 'finalize_output'
return state
# def ingest(state: SQLAgentState) -> SQLAgentState:
# """Populate state.tables with list of tables in the DB."""
# db_info = state['db_info']
# conn = sqlite3.connect(DB_PATH)
# try:
# db_info['tables'] = [row[0] for row in conn.execute(
# "SELECT name FROM sqlite_master WHERE type='table';"
# )]
# # Populate columns for each table
# columns = {}
# for table in db_info['tables']:
# col_rows = conn.execute(f'PRAGMA table_info("{table}")').fetchall()
# columns[table] = [r[1] for r in col_rows]
# db_info['columns'] = columns
# state.db_info = db_info
# finally:
# conn.close()
# return state
from agents.safe_guardrails import OffTopicValidator
from guardrails import Guard
def detect_off_topic(state: SQLAgentState) -> SQLAgentState:
"""Check if the input question is off-topic."""
question = state['question']
validator = Guard().use(
OffTopicValidator,
on_fail="fix"
)
metadata = {
"topic": "Database Queries",
"additional_context": "Only accept queries related to the data on Database/CSV"
# "additional_context": "The database is about ecommerce products with tables: products, laptops, phones, tablets, promotions, category"
}
validation_result = validator.validate(question, metadata=metadata)
if validation_result.validated_output == "OFF_TOPIC":
state['error'] = True
else:
state['error'] = False
state['step'] = 'detect_off_topic'
state['off_topic'] = validation_result.validated_output
print(state)
return state
def get_db_info(state: SQLAgentState) -> SQLAgentState:
"""Get database information."""
db_info = state['db_info']
conn = sqlite3.connect(DB_PATH)
try:
db_info['tables'] = [row[0] for row in conn.execute(
"SELECT name FROM sqlite_master WHERE type='table';"
)]
# Populate columns for each table
columns = {}
for table in db_info['tables']:
col_rows = conn.execute(f'PRAGMA table_info("{table}")').fetchall()
columns[table] = [r[1] for r in col_rows]
db_info['columns'] = columns
schema = "; ".join(f"{t}({', '.join(db_info['columns'][t])})" for t in db_info['tables'])
db_info['schema'] = schema
finally:
conn.close()
state['step'] = 'get_db_info'
return state
def generate_sql(state: SQLAgentState) -> SQLAgentState:
"""Use LLM to translate user_query into SQL."""
llm = LLM()
# Include detailed schema with columns
schema = state['db_info']['schema']
prompt = (
f"Given this database schema: {schema}, "
f"write an SQL query to: {state['question']}. "
"Respond with only the SQL enclosed in triple backticks."
)
raw = llm.generate(prompt)
# print('raw', raw)
lines = raw.splitlines()
if lines and lines[0].strip().startswith("```"):
lines = lines[1:]
if lines and lines[-1].strip().startswith("```"):
lines = lines[:-1]
state['sql_query'] = "\n".join(lines).strip()
state['step'] = 'generate_sql'
return state
def execute_sql(state: SQLAgentState) -> SQLAgentState:
"""Run the SQL in state.sql and store result DataFrame."""
sql_query = state['sql_query']
conn = sqlite3.connect(DB_PATH)
try:
state['sql_result'] = pd.read_sql_query(sql_query, conn)
except Exception as e:
state['error'] = str(e)
finally:
conn.close()
state['step'] = 'execute_sql'
return state
def generate_answer(state: SQLAgentState) -> SQLAgentState:
"""Generate answer using LLM based on SQL result."""
llm = LLM()
if state['sql_result'] is not None and not state['sql_result'].empty:
result_str = state['sql_result'].to_string(index=False)
prompt = (
f"Given the question: {state['question']},\n"
f"SQL Query: {state['sql_query']},\n"
f"and the following SQL query result: {result_str},\n"
"provide a concise answer:"
)
state['answer'] = llm.generate(prompt)
else:
state['error'] = state['error'] or "No results found."
if state["off_topic"] == "OFF_TOPIC":
state['error'] = "The question is off-topic."
# state["answer"] = "Sorry, I can't assist you with that request."
state["answer"] = "Sorry, I can only help you with questions about the data! What information would you like to explore from the data?"
state['step'] = 'generate_answer'
return state
def optional_plot(state: SQLAgentState) -> SQLAgentState:
"""If user_query requests plotting, generate plot and set state.plot_path."""
if any(k in state['question'].lower() for k in ['plot', 'vẽ', 'biểu đồ']):
tool = PlotSQLTool()
md = tool._run(state['sql_query'])
m = re.search(r'!\[.*\]\((.*?)\)', md)
if m:
state['plot_path'] = m.group(1)
else:
state['error'] = state['error'] or 'Plot generation failed'
return state
def format_response(state: SQLAgentState) -> SQLAgentState:
"""Build markdown response including SQL, table preview, and plot."""
parts = []
if state['sql_query']:
parts.append(f"```sql\n{state['sql_query']}\n```")
if state['sql_result'] is not None:
parts.append(state['sql_result'].to_markdown(index=False))
if state['plot_path']:
parts.append(f"")
if state['error']:
parts.append(f"**Error**: {state['error']}")
state['response_md'] = "\n\n".join(parts)
return state
|