jashdoshi77 commited on
Commit
7c2121a
·
1 Parent(s): 8c6e5a2

made the chatbot smarter

Browse files
Files changed (1) hide show
  1. ai/signatures.py +17 -6
ai/signatures.py CHANGED
@@ -61,10 +61,21 @@ class SQLGeneration(dspy.Signature):
61
  The query must be syntactically correct and only reference existing
62
  tables and columns from the schema.
63
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  BUSINESS RULES:
65
- - Include status/state filters from the query plan for accurate metrics
66
- - Use appropriate aggregation functions
67
- - Ensure the query respects business logic (e.g., only closed orders for revenue)
68
 
69
  CRITICAL: Output ONLY the raw SQL. No markdown, no explanation, no comments."""
70
 
@@ -73,9 +84,9 @@ class SQLGeneration(dspy.Signature):
73
  query_plan = dspy.InputField(desc="Detailed logical query plan")
74
 
75
  sql_query = dspy.OutputField(
76
- desc="A valid PostgreSQL SELECT query. Output ONLY the raw SQL code. "
77
- "Do NOT include any explanation, comments, markdown, or text before or after the SQL. "
78
- "Do NOT wrap in code fences. Just the pure SQL statement."
79
  )
80
 
81
 
 
61
  The query must be syntactically correct and only reference existing
62
  tables and columns from the schema.
63
 
64
+ SIMPLICITY RULES (MUST FOLLOW):
65
+ - If a pre-computed total/summary column exists (e.g. total_amount, grand_total,
66
+ total_price, net_amount), SELECT THAT COLUMN DIRECTLY. NEVER reconstruct it
67
+ by adding component columns (e.g. gold_amount + diamond_amount) — that will give
68
+ wrong answers because it ignores labour, taxes, and other components.
69
+ - For single-record lookups (e.g. "total amount of PO12345"), write:
70
+ SELECT total_amount FROM <table> WHERE po_id = 'PO12345'
71
+ NOT a multi-table join with SUM of parts.
72
+ - Only JOIN tables if the required column does not exist in the primary table.
73
+ - Only use aggregation (SUM, COUNT, AVG, etc.) when the question genuinely asks
74
+ for an aggregate across multiple rows.
75
+
76
  BUSINESS RULES:
77
+ - Include status/state filters from the query plan for accurate metrics.
78
+ - Ensure the query respects business logic (e.g., only closed orders for revenue).
 
79
 
80
  CRITICAL: Output ONLY the raw SQL. No markdown, no explanation, no comments."""
81
 
 
84
  query_plan = dspy.InputField(desc="Detailed logical query plan")
85
 
86
  sql_query = dspy.OutputField(
87
+ desc="The SIMPLEST valid PostgreSQL SELECT query that correctly answers the question. "
88
+ "Use pre-computed total columns when available. Avoid unnecessary joins and aggregations. "
89
+ "Output ONLY the raw SQL code no markdown, no explanation, no code fences."
90
  )
91
 
92