|
| 1 | +from langchain.chat_models import init_chat_model |
| 2 | + |
| 3 | +from piglets.policies import SemanticRules |
| 4 | +from piglets.types import ( |
| 5 | + ProfilingQueries, |
| 6 | + SemanticLinkingResult, |
| 7 | + Table, |
| 8 | +) |
| 9 | + |
| 10 | + |
| 11 | +class Profiler(): |
| 12 | + def __init__( |
| 13 | + self, |
| 14 | + model_name: str, |
| 15 | + model_provider: str = None, |
| 16 | + rules: SemanticRules | None = None, |
| 17 | + ): |
| 18 | + self.model_name = model_name |
| 19 | + self.model_provider = model_provider |
| 20 | + self.rules = rules or SemanticRules() |
| 21 | + |
| 22 | + def profile_table( |
| 23 | + self, |
| 24 | + natural_language_query: str, |
| 25 | + database_type: str | None, |
| 26 | + table: Table, |
| 27 | + semantic_linking_result: SemanticLinkingResult, |
| 28 | + ) -> ProfilingQueries: |
| 29 | + |
| 30 | + llm = init_chat_model(model=self.model_name, model_provider=self.model_provider) |
| 31 | + llm = llm.with_structured_output(ProfilingQueries) |
| 32 | + critical_rules = self.rules.critical_rules_to_string() |
| 33 | + sql_type = f"{database_type.upper()} SQL" if database_type else "SQL" |
| 34 | + semantic_table_functions_lower = {k.lower(): v for k, v in semantic_linking_result.table_functions.items()} |
| 35 | + semantic_table_function = semantic_table_functions_lower.get(table.name.lower(), "Unknown Role") |
| 36 | + |
| 37 | + # TODO: Include column descriptions here once Column supports them. |
| 38 | + DATA_PROFILE_QUERY_GENERATION_PROMPT = f""" |
| 39 | + *** TASK CONTEXT *** |
| 40 | + You are an agent exploring a database table to verify its |
| 41 | + relevance to a user question. |
| 42 | + You must not explore randomly. You must verify if this |
| 43 | + table fits its anticipated role. |
| 44 | + {critical_rules} |
| 45 | +
|
| 46 | + *** TARGET TABLE: {table.name} *** |
| 47 | + Columns: |
| 48 | + {table.columns_to_string()} |
| 49 | +
|
| 50 | + *** USER QUESTION *** |
| 51 | + {natural_language_query} |
| 52 | +
|
| 53 | + *** ANTICIPATED ROLE *** |
| 54 | + This table was identified as: {semantic_table_function}. Use this to |
| 55 | + guide your exploration. |
| 56 | +
|
| 57 | + *** YOUR MISSION *** |
| 58 | + Generate 3-8 {sql_type} queries to investigate. **Focus on |
| 59 | + understanding the table’s semantics and utility.** |
| 60 | + Each query must profile only the target table, {table.name}. |
| 61 | + Do not join to, reference, or infer data from any other table. |
| 62 | +
|
| 63 | + **Motivation for Exploration**: |
| 64 | + 1. **Semantic Alignment**: Check distinct values to understand what the column *means* versus what the query |
| 65 | + *needs*. (e.g., If column is ’type’, does it contain the specific |
| 66 | + categories? If ’status’, does it contain values like ’Active’ |
| 67 | + or code ’1’?) |
| 68 | + 2. **Granularity & Scope**: Verify the table’s grain (e.g., is |
| 69 | + it one row per Order or per Item?). This determines if it |
| 70 | + supports the required aggregations. |
| 71 | + 3. **Bridge/Connectivity**: If this looks like a linking table, |
| 72 | + verify the Foreign Keys are populated (not all NULL) to |
| 73 | + ensure it can actually serve as a bridge. |
| 74 | + 4. **Data Quality**: Are critical columns (targets for filters |
| 75 | + or answers) usable, or are they mostly NULL? |
| 76 | +
|
| 77 | + *** OUTPUT FORMAT *** |
| 78 | + Return structured data matching the ProfilingQueries schema: |
| 79 | + - exploratory_queries: a list of 3-8 objects. |
| 80 | + - Each object must contain: |
| 81 | + - motivation: a concise reason for running the profiling query. |
| 82 | + - query: the {sql_type} query string. |
| 83 | +
|
| 84 | + Do not wrap queries in markdown fences. Put the reason for each |
| 85 | + query in the motivation field, not in SQL comments. |
| 86 | + Generate your exploration queries: |
| 87 | + """ |
| 88 | + |
| 89 | + profiling_queries = llm.invoke(DATA_PROFILE_QUERY_GENERATION_PROMPT) |
| 90 | + |
| 91 | + return profiling_queries |
0 commit comments