File size: 11,873 Bytes
c399543
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Prompt Engine for SQL Generation
Constructs intelligent prompts for SQL generation using retrieved examples and best practices.
"""

import json
from typing import List, Dict, Any, Optional
from pathlib import Path
from loguru import logger

class PromptEngine:
    """Intelligent prompt construction for SQL generation."""
    
    def __init__(self, prompts_dir: str = "./prompts"):
        """
        Initialize the prompt engine.
        
        Args:
            prompts_dir: Directory containing prompt templates
        """
        self.prompts_dir = Path(prompts_dir)
        self.prompts_dir.mkdir(parents=True, exist_ok=True)
        
        # Load prompt templates
        self.templates = self._load_prompt_templates()
        
        # Default system prompt
        self.default_system_prompt = """You are an expert SQL developer. Your task is to convert natural language questions into accurate SQL queries.

Key Guidelines:
1. Always use the exact table column names provided
2. Generate standard SQL syntax (compatible with most databases)
3. Use appropriate JOINs when multiple tables are involved
4. Apply proper WHERE clauses for filtering
5. Use GROUP BY for aggregations when needed
6. Ensure queries are efficient and readable
7. Handle edge cases appropriately

Table Schema: {table_schema}

Retrieved Examples:
{examples}

Question: {question}

Generate the SQL query:"""
    
    def _load_prompt_templates(self) -> Dict[str, str]:
        """Load prompt templates from files."""
        templates = {}
        
        # Create default templates if they don't exist
        default_templates = {
            "sql_generation.txt": self._get_default_sql_prompt(),
            "few_shot_examples.txt": self._get_default_few_shot_prompt(),
            "error_correction.txt": self._get_default_error_correction_prompt()
        }
        
        for filename, content in default_templates.items():
            template_path = self.prompts_dir / filename
            if not template_path.exists():
                with open(template_path, 'w', encoding='utf-8') as f:
                    f.write(content)
                logger.info(f"Created default template: {filename}")
            
            # Load the template
            with open(template_path, 'r', encoding='utf-8') as f:
                templates[filename.replace('.txt', '')] = f.read()
        
        return templates
    
    def _get_default_sql_prompt(self) -> str:
        """Get default SQL generation prompt template."""
        return """You are an expert SQL developer. Convert the natural language question to SQL.

Table Schema: {table_schema}

Examples:
{examples}

Question: {question}

Generate SQL:"""
    
    def _get_default_few_shot_prompt(self) -> str:
        """Get default few-shot learning prompt template."""
        return """Given these examples, generate SQL for the new question:

Examples:
{examples}

New Question: {question}
Table Schema: {table_schema}

SQL Query:"""
    
    def _get_default_error_correction_prompt(self) -> str:
        """Get default error correction prompt template."""
        return """The following SQL query has an error. Please correct it:

Original Question: {question}
Table Schema: {table_schema}
Incorrect SQL: {incorrect_sql}
Error: {error_message}

Corrected SQL:"""
    
    def construct_sql_prompt(self, 
                           question: str, 
                           table_headers: List[str], 
                           retrieved_examples: List[Dict[str, Any]],
                           prompt_type: str = "sql_generation") -> str:
        """
        Construct a prompt for SQL generation.
        
        Args:
            question: Natural language question
            table_headers: List of table column names
            retrieved_examples: List of retrieved relevant examples
            prompt_type: Type of prompt to use
            
        Returns:
            Constructed prompt string
        """
        # Format table schema
        table_schema = self._format_table_schema(table_headers)
        
        # Format examples
        examples_text = self._format_examples(retrieved_examples)
        
        # Get template
        template = self.templates.get(prompt_type, self.templates["sql_generation"])
        
        # Fill template
        prompt = template.format(
            question=question,
            table_schema=table_schema,
            examples=examples_text
        )
        
        return prompt
    
    def construct_enhanced_prompt(self, 
                                question: str, 
                                table_headers: List[str], 
                                retrieved_examples: List[Dict[str, Any]],
                                additional_context: Optional[Dict[str, Any]] = None) -> str:
        """
        Construct an enhanced prompt with additional context and examples.
        
        Args:
            question: Natural language question
            table_headers: List of table column names
            retrieved_examples: List of retrieved relevant examples
            additional_context: Additional context information
            
        Returns:
            Enhanced prompt string
        """
        # Start with system prompt
        prompt_parts = [self.default_system_prompt]
        
        # Add table schema
        table_schema = self._format_table_schema(table_headers)
        prompt_parts.append(f"Table Schema: {table_schema}\n")
        
        # Add retrieved examples with relevance scores
        if retrieved_examples:
            prompt_parts.append("Relevant Examples (ordered by relevance):")
            for i, example in enumerate(retrieved_examples[:3], 1):  # Top 3 examples
                relevance = example.get("final_score", example.get("similarity_score", 0))
                prompt_parts.append(f"\nExample {i} (Relevance: {relevance:.2f}):")
                prompt_parts.append(f"Question: {example['question']}")
                prompt_parts.append(f"SQL: {example['sql']}")
                prompt_parts.append(f"Table: {example['table_headers']}")
        
        # Add additional context if provided
        if additional_context:
            prompt_parts.append("\nAdditional Context:")
            for key, value in additional_context.items():
                prompt_parts.append(f"{key}: {value}")
        
        # Add the current question
        prompt_parts.append(f"\nCurrent Question: {question}")
        prompt_parts.append("\nGenerate the SQL query:")
        
        return "\n".join(prompt_parts)
    
    def construct_few_shot_prompt(self, 
                                 question: str, 
                                 table_headers: List[str], 
                                 examples: List[Dict[str, Any]]) -> str:
        """
        Construct a few-shot learning prompt.
        
        Args:
            question: Natural language question
            table_headers: List of table column names
            examples: List of examples for few-shot learning
            
        Returns:
            Few-shot prompt string
        """
        template = self.templates["few_shot_examples"]
        
        # Format examples in a structured way
        examples_text = ""
        for i, example in enumerate(examples[:5], 1):  # Use top 5 examples
            examples_text += f"\n--- Example {i} ---\n"
            examples_text += f"Question: {example['question']}\n"
            examples_text += f"Table: {example['table_headers']}\n"
            examples_text += f"SQL: {example['sql']}\n"
        
        table_schema = self._format_table_schema(table_headers)
        
        return template.format(
            examples=examples_text,
            question=question,
            table_schema=table_schema
        )
    
    def construct_error_correction_prompt(self, 
                                        question: str, 
                                        table_headers: List[str], 
                                        incorrect_sql: str, 
                                        error_message: str) -> str:
        """
        Construct a prompt for error correction.
        
        Args:
            question: Natural language question
            table_headers: List of table column names
            incorrect_sql: The incorrect SQL query
            error_message: Error message or description
            
        Returns:
            Error correction prompt string
        """
        template = self.templates["error_correction"]
        table_schema = self._format_table_schema(table_headers)
        
        return template.format(
            question=question,
            table_schema=table_schema,
            incorrect_sql=incorrect_sql,
            error_message=error_message
        )
    
    def _format_table_schema(self, table_headers: List[str]) -> str:
        """Format table headers into a readable schema."""
        if not table_headers:
            return "No table schema provided"
        
        # Group headers by type for better readability
        schema_parts = []
        
        # Primary keys and IDs
        pk_headers = [h for h in table_headers if 'id' in h.lower() or 'key' in h.lower()]
        if pk_headers:
            schema_parts.append(f"Primary Keys: {', '.join(pk_headers)}")
        
        # Text fields
        text_headers = [h for h in table_headers if any(word in h.lower() for word in ['name', 'title', 'description', 'text'])]
        if text_headers:
            schema_parts.append(f"Text Fields: {', '.join(text_headers)}")
        
        # Numeric fields
        numeric_headers = [h for h in table_headers if any(word in h.lower() for word in ['age', 'count', 'price', 'salary', 'amount', 'number'])]
        if numeric_headers:
            schema_parts.append(f"Numeric Fields: {', '.join(numeric_headers)}")
        
        # Date fields
        date_headers = [h for h in table_headers if any(word in h.lower() for word in ['date', 'time', 'created', 'updated', 'birth'])]
        if date_headers:
            schema_parts.append(f"Date Fields: {', '.join(date_headers)}")
        
        # Boolean fields
        bool_headers = [h for h in table_headers if any(word in h.lower() for word in ['is_', 'has_', 'active', 'enabled', 'status'])]
        if bool_headers:
            schema_parts.append(f"Boolean Fields: {', '.join(bool_headers)}")
        
        # Other fields
        other_headers = [h for h in table_headers if h not in pk_headers + text_headers + numeric_headers + date_headers + bool_headers]
        if other_headers:
            schema_parts.append(f"Other Fields: {', '.join(other_headers)}")
        
        return "\n".join(schema_parts)
    
    def _format_examples(self, examples: List[Dict[str, Any]]) -> str:
        """Format retrieved examples for prompt inclusion."""
        if not examples:
            return "No relevant examples found."
        
        formatted_examples = []
        for i, example in enumerate(examples[:3], 1):  # Use top 3 examples
            relevance = example.get("final_score", example.get("similarity_score", 0))
            formatted_examples.append(f"Example {i} (Relevance: {relevance:.2f}):")
            formatted_examples.append(f"  Question: {example['question']}")
            formatted_examples.append(f"  SQL: {example['sql']}")
            formatted_examples.append(f"  Table: {example['table_headers']}")
        
        return "\n".join(formatted_examples)
    
    def get_prompt_statistics(self) -> Dict[str, Any]:
        """Get statistics about the prompt engine."""
        return {
            "available_templates": list(self.templates.keys()),
            "prompts_directory": str(self.prompts_dir),
            "template_count": len(self.templates)
        }