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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# DEPENDENCIES
from typing import List
from typing import Dict
from typing import Optional
from config.models import PromptType
from config.settings import get_settings
from config.models import ChunkWithScore
from config.logging_config import get_logger
from utils.error_handler import handle_errors
from generation.token_manager import TokenManager
from utils.error_handler import PromptBuildingError


# Setup Settings and Logging
settings = get_settings()
logger   = get_logger(__name__)


class PromptBuilder:
    """
    Intelligent prompt building for LLM generation: Constructs optimized prompts for different task types with proper context management and citation handling
    """
    def __init__(self, model_name: str = None):
        """
        Initialize prompt builder
        
        Arguments:
        ----------
            model_name { str } : Model name for token management
        """
        self.logger           = logger
        self.settings         = get_settings()
        self.model_name       = model_name or self.settings.OLLAMA_MODEL
        self.token_manager    = TokenManager(model_name)
        
        # Prompt templates for different tasks
        self.prompt_templates = {PromptType.QA             : {"system": self._get_qa_system_prompt(), "user": self._get_qa_user_template(), "max_context_ratio": 0.6},
                                 PromptType.SUMMARY        : {"system": self._get_summary_system_prompt(), "user": self._get_summary_user_template(), "max_context_ratio": 0.8,},
                                 PromptType.ANALYTICAL     : {"system": self._get_analytical_system_prompt(), "user": self._get_analytical_user_template(), "max_context_ratio": 0.7},
                                 PromptType.COMPARISON     : {"system": self._get_comparison_system_prompt(), "user": self._get_comparison_user_template(), "max_context_ratio": 0.5},
                                 PromptType.EXTRACTION     : {"system": self._get_extraction_system_prompt(), "user": self._get_extraction_user_template(), "max_context_ratio": 0.6},
                                 PromptType.CREATIVE       : {"system": self._get_creative_system_prompt(), "user": self._get_creative_user_template(), "max_context_ratio": 0.4},
                                 PromptType.CONVERSATIONAL : {"system": self._get_conversational_system_prompt(), "user": self._get_conversational_user_template(), "max_context_ratio": 0.5}
                                }
    

    def build_prompt(self, query: str, context: str, sources: List[ChunkWithScore], prompt_type: PromptType = PromptType.QA,  include_citations: bool = True,
                     max_completion_tokens: int = 1000) -> Dict[str, str]:
        """
        Build complete prompt for LLM generation
        
        Arguments:
        ----------
            query                      { str }   : User query
            
            context                    { str }   : Retrieved context
            
            sources                   { list }   : Source chunks
            
            prompt_type           { PromptType } : Type of prompt to build
            
            include_citations         { bool }   : Whether to include citation instructions
            
            max_completion_tokens     { int }    : Tokens to reserve for completion
        
        Returns:
        --------
                         { dict }                : Dictionary with 'system' and 'user' prompts
        """
        if not query or not context:
            raise PromptBuildingError("Query and context cannot be empty")
        
        try:
            # Get template for prompt type
            template          = self.prompt_templates.get(prompt_type, self.prompt_templates[PromptType.QA])
            
            # Optimize context to fit within token limits
            optimized_context = self._optimize_context(context               = context, 
                                                       template              = template, 
                                                       max_completion_tokens = max_completion_tokens,
                                                      )
            
            # Build system prompt
            system_prompt     = self._build_system_prompt(template          = template, 
                                                          include_citations = include_citations, 
                                                          prompt_type       = prompt_type,
                                                         )
            
            # Build user prompt
            user_prompt       = self._build_user_prompt(template    = template, 
                                                        query       = query, 
                                                        context     = optimized_context, 
                                                        sources     = sources, 
                                                        prompt_type = prompt_type,
                                                       )
            
            # Validate token usage
            self._validate_prompt_length(system_prompt         = system_prompt, 
                                         user_prompt           = user_prompt, 
                                         max_completion_tokens = max_completion_tokens,
                                        )
            
            self.logger.debug(f"Built {prompt_type.value} prompt: {self.token_manager.count_tokens(system_prompt + user_prompt)} tokens")
            
            return {"system"   : system_prompt,
                    "user"     : user_prompt,
                    "metadata" : {"prompt_type"    : prompt_type.value,
                                  "context_tokens" : self.token_manager.count_tokens(optimized_context),
                                  "total_tokens"   : self.token_manager.count_tokens(system_prompt + user_prompt),
                                  "sources_count"  : len(sources),
                                 }
                  }
        
        except Exception as e:
            self.logger.error(f"Prompt building failed: {repr(e)}")
            raise PromptBuildingError(f"Prompt building failed: {repr(e)}")
    

    def _optimize_context(self, context: str, template: Dict, max_completion_tokens: int) -> str:
        """
        Optimize context to fit within token limits
        
        Arguments:
        ----------
            context               { str }  : Context text to optimize

            template              { Dict } : Prompt template
            
            max_completion_tokens { int }  : Tokens to reserve for completion
        
        Returns:
        --------
                        { str }            : Optimized context
        """
        # Calculate tokens for system prompt
        system_tokens          = self.token_manager.count_tokens(template["system"])
        
        # Estimate user template tokens by removing placeholders
        user_template_clean    = template["user"]
        
        # Remove all known placeholders
        placeholders_to_remove = ["{query}", "{context}", "{sources_info}", "{focus}"]
        
        for placeholder in placeholders_to_remove:
            user_template_clean = user_template_clean.replace(placeholder, "")
        
        user_template_tokens = self.token_manager.count_tokens(user_template_clean)
        
        # Calculate available tokens for context - Reserve: system + user template + completion + buffer
        reserved_tokens      = system_tokens + user_template_tokens + max_completion_tokens + 100
        max_context_tokens   = self.token_manager.context_window - reserved_tokens
        
        # Ensure we don't exceed zero
        if (max_context_tokens <= 0):
            self.logger.warning(f"No tokens available for context. Reserved: {reserved_tokens}, Window: {self.token_manager.context_window}")
            return ""
        
        # Apply max context ratio from template config
        ratio_limit        = int(self.token_manager.context_window * template["max_context_ratio"])
        max_context_tokens = min(max_context_tokens, ratio_limit)
        
        # Log optimization details
        self.logger.debug(f"Context optimization: max_tokens={max_context_tokens}, ratio={template['max_context_ratio']}")
        
        # Truncate context to fit
        optimized_context  = self.token_manager.truncate_to_fit(context, max_context_tokens, strategy="end")
        
        # Calculate reduction percentage
        original_tokens    = self.token_manager.count_tokens(context)
        optimized_tokens   = self.token_manager.count_tokens(optimized_context)
        
        if (original_tokens > optimized_tokens):
            reduction_pct = ((original_tokens - optimized_tokens) / original_tokens) * 100
            
            self.logger.info(f"Context reduced by {reduction_pct:.1f}% ({original_tokens}{optimized_tokens} tokens)")
        
        return optimized_context
    

    def _build_system_prompt(self, template: Dict, include_citations: bool, prompt_type: PromptType) -> str:
        """
        Build system prompt
        """
        system_prompt = template["system"]
        
        # Add citation instructions if needed
        if (include_citations and (prompt_type != PromptType.CREATIVE)):
            citation_instructions = self._get_citation_instructions()
            system_prompt        += "\n\n" + citation_instructions
        
        return system_prompt
    

    def _build_user_prompt(self, template: Dict, query: str, context: str, sources: List[ChunkWithScore], prompt_type: PromptType) -> str:
        """
        Build user prompt
        """
        # Format sources information
        sources_info = self._format_sources_info(sources) if sources else ""
        
        # Build user prompt using template
        user_prompt  = template["user"].format(query        = query,
                                               context      = context,
                                               sources_info = sources_info,
                                             )
        
        # Add task-specific formatting
        if (prompt_type == PromptType.COMPARISON):
            user_prompt = self._enhance_comparison_prompt(user_prompt = user_prompt, 
                                                          sources     = sources,
                                                         )

        elif (prompt_type == PromptType.ANALYTICAL):
            user_prompt = self._enhance_analytical_prompt(user_prompt = user_prompt, 
                                                          sources     = sources,
                                                         )
        
        return user_prompt
    

    def _format_sources_info(self, sources: List[ChunkWithScore]) -> str:
        """
        Format sources information for the prompt
        """
        if not sources:
            return ""
        
        sources_list = list()

        for i, source in enumerate(sources, 1):
            chunk       = source.chunk
            source_info = f"Source [{i}]:"
            
            if hasattr(chunk, 'metadata') and 'filename' in chunk.metadata:
                source_info += f" {chunk.metadata['filename']}"
            
            if chunk.page_number:
                source_info += f" (page {chunk.page_number})"
            
            if chunk.section_title:
                source_info += f" - {chunk.section_title}"
            
            sources_list.append(source_info)
        
        return "Available sources:\n" + "\n".join(sources_list)
    

    def _enhance_comparison_prompt(self, user_prompt: str, sources: List[ChunkWithScore]) -> str:
        """
        Enhance prompt for comparison tasks
        
        Arguments:
        ----------
            user_prompt { str }  : Base user prompt

            sources     { list } : Source chunks
        
        Returns:
        --------
                { str }          : Enhanced prompt
        """
        if (len(sources) < 2):
            return user_prompt
        
        enhancement  = "\n\nPlease compare and contrast the information from different sources. "
        enhancement += "Highlight agreements, disagreements, and complementary information. "
        enhancement += "If sources conflict, present both perspectives clearly."
        
        return user_prompt + enhancement
    

    def _enhance_analytical_prompt(self, user_prompt: str, sources: List[ChunkWithScore]) -> str:
        """
        Enhance prompt for analytical tasks - FIXED SIGNATURE
        
        Arguments:
        ----------
            user_prompt { str }  : Base user prompt

            sources     { list } : Source chunks
        
        Returns:
        --------
                { str }          : Enhanced prompt
        """
        enhancement  = "\n\nProvide analytical insights by:"
        enhancement += "\n1. Identifying patterns and relationships in the information"
        enhancement += "\n2. Analyzing implications and consequences"
        enhancement += "\n3. Evaluating the strength of evidence from different sources"
        enhancement += "\n4. Drawing well-reasoned conclusions"
        
        return user_prompt + enhancement
    

    def _validate_prompt_length(self, system_prompt: str, user_prompt: str, max_completion_tokens: int):
        """
        Validate that prompt fits within context window
        
        Arguments:
        ----------
            system_prompt         { str } : System prompt

            user_prompt           { str } : User prompt
            
            max_completion_tokens { int } : Tokens needed for completion
        
        Raises:
        -------
            PromptBuildingError           : If prompt exceeds context window
        """
        system_tokens  = self.token_manager.count_tokens(system_prompt)
        user_tokens    = self.token_manager.count_tokens(user_prompt)
        total_tokens   = system_tokens + user_tokens
        total_required = total_tokens + max_completion_tokens
        
        if (total_required > self.token_manager.context_window):
            error_msg = (f"Prompt exceeds context window:\n"
                         f"- System prompt: {system_tokens} tokens\n"
                         f"- User prompt: {user_tokens} tokens\n"
                         f"- Completion reserve: {max_completion_tokens} tokens\n"
                         f"- Total required: {total_required} tokens\n"
                         f"- Context window: {self.token_manager.context_window} tokens\n"
                         f"- Overflow: {total_required - self.token_manager.context_window} tokens"
                        )
            self.logger.error(error_msg)

            raise PromptBuildingError(error_msg)
        
        # Log successful validation
        utilization = (total_required / self.token_manager.context_window) * 100

        self.logger.debug(f"Prompt validation passed: {total_required}/{self.token_manager.context_window} tokens ({utilization:.1f}% utilization)")


    def _get_citation_instructions(self) -> str:
        """
        Get citation instructions for system prompt
        """
        return ("CITATION INSTRUCTIONS:\n"
                "1. Always cite your sources using [number] notation\n"
                "2. Use the citation number that corresponds to the source in the context\n"
                "3. Cite sources for all factual claims and specific information\n"
                "4. If information appears in multiple sources, cite the most relevant one\n"
                "5. Do not make up information not present in the provided context\n"
                "6. If the context doesn't contain the answer, explicitly state this"
               )
    

    # Template methods for different prompt types
    def _get_qa_system_prompt(self) -> str:
        return ("You are a precise and helpful AI assistant that answers questions based solely on the provided context.\n\n"
                "Core Principles:\n"
                "1. ONLY use information from the provided context\n"
                "2. Be concise but complete - don't omit important details\n"
                "3. Structure complex answers clearly\n"
                "4. If information is ambiguous or conflicting, acknowledge this\n"
                "5. Never make up or infer information not present in the context"
               )
    

    def _get_qa_user_template(self) -> str:
        return ("Context Information:\n{context}\n\n"
                "{sources_info}\n\n"
                "Question: {query}\n\n"
                "Instructions: Answer the question using ONLY the information provided in the context above. "
                "Cite your sources using [number] notation. If the context doesn't contain enough information "
                "to answer fully, state this clearly.\n\n"
                "Answer:"
               )
    

    def _get_summary_system_prompt(self) -> str:
        return ("You are a thorough AI assistant that provides comprehensive summaries based on the provided context.\n\n"
                "Summary Guidelines:\n"
                "1. Capture all key points and main ideas\n"
                "2. Maintain the original meaning and intent\n"
                "3. Organize information logically\n"
                "4. Be comprehensive but concise\n"
                "5. Highlight important findings and conclusions"
               )
    

    def _get_summary_user_template(self) -> str:
        return ("Content to summarize:\n{context}\n\n"
                "Please provide a comprehensive summary that captures all key points and main ideas. "
                "Organize the summary logically and ensure it reflects the original content accurately.\n\n"
                "Summary:"
               )
    

    def _get_analytical_system_prompt(self) -> str:
        return ("You are an analytical AI assistant that provides insights based on the provided context.\n\n"
                "Analytical Guidelines:\n"
                "1. Analyze patterns and connections in the information\n"
                "2. Compare different perspectives if multiple sources exist\n"
                "3. Highlight key findings and implications\n"
                "4. Identify gaps or limitations in the available information\n"
                "5. Provide well-reasoned analysis and conclusions"
               )

    
    def _get_analytical_user_template(self) -> str:
        return ("Context for analysis:\n{context}\n\n"
                "{sources_info}\n\n"
                "Analytical task: {query}\n\n"
                "Please provide a detailed analysis based on the context above. "
                "Identify patterns, relationships, and implications. "
                "Cite sources for all analytical claims.\n\n"
                "Analysis:"
               )

    
    def _get_comparison_system_prompt(self) -> str:
        return ("You are an AI assistant that compares information across multiple sources.\n\n"
                "Comparison Guidelines:\n"
                "1. Identify similarities and differences between sources\n"
                "2. Note if sources agree or disagree on specific points\n"
                "3. Highlight complementary information\n"
                "4. Present conflicting perspectives fairly\n"
                "5. Draw conclusions about the overall consensus or disagreement"
               )

    
    def _get_comparison_user_template(self) -> str:
        return ("Context from multiple sources:\n{context}\n\n"
                "{sources_info}\n\n"
                "Comparison task: {query}\n\n"
                "Please compare how different sources address this topic. "
                "Identify agreements, disagreements, and complementary information. "
                "Cite specific sources for each point of comparison.\n\n"
                "Comparison:"
               )

    
    def _get_extraction_system_prompt(self) -> str:
        return ("You are a precise AI assistant that extracts specific information from context.\n\n"
                "Extraction Guidelines:\n"
                "1. Extract only the requested information\n"
                "2. Be thorough and complete\n"
                "3. Maintain accuracy and precision\n"
                "4. Organize extracted information clearly\n"
                "5. Cite sources for all extracted information"
               )

    
    def _get_extraction_user_template(self) -> str:
        return ("Context:\n{context}\n\n"
                "{sources_info}\n\n"
                "Extraction task: {query}\n\n"
                "Please extract the requested information from the context above. "
                "Be thorough and precise. Cite sources for all extracted information.\n\n"
                "Extracted Information:"
               )

    
    def _get_creative_system_prompt(self) -> str:
        return ("You are a creative AI assistant that generates content based on provided context.\n\n"
                "Creative Guidelines:\n"
                "1. Use the context as inspiration and foundation\n"
                "2. Be creative and engaging\n"
                "3. Maintain coherence with the source material\n"
                "4. You may extrapolate and build upon the provided information\n"
                "5. Clearly distinguish between source-based content and creative additions"
               )

    
    def _get_creative_user_template(self) -> str:
        return ("Context and inspiration:\n{context}\n\n"
                "Creative task: {query}\n\n"
                "Please create content based on the context above. "
                "You may use the context as inspiration and build upon it creatively. "
                "If you add information beyond what's in the context, make this clear.\n\n"
                "Creative Response:"
               )

    
    def _get_conversational_system_prompt(self) -> str:
        return ("You are a helpful and engaging conversational AI assistant.\n\n"
                "Conversational Guidelines:\n"
                "1. Be natural and engaging in conversation\n"
                "2. Use the provided context to inform your responses\n"
                "3. Maintain a friendly and helpful tone\n"
                "4. Ask clarifying questions when needed\n"
                "5. Cite sources when providing specific information"
               )

    
    def _get_conversational_user_template(self) -> str:
        return ("Context for our conversation:\n{context}\n\n"
                "Current message: {query}\n\n"
                "Please respond naturally and helpfully based on the context above. "
                "If providing specific information, cite your sources.\n\n"
                "Response:"
               )


# Global prompt builder instance
_prompt_builder = None


def get_prompt_builder(model_name: str = None) -> PromptBuilder:
    """
    Get global prompt builder instance (singleton)
    
    Arguments:
    ----------
        model_name { str } : Model name for token management
    
    Returns:
    --------
        { PromptBuilder }  : PromptBuilder instance
    """
    global _prompt_builder
    
    if _prompt_builder is None or (model_name and _prompt_builder.model_name != model_name):
        _prompt_builder = PromptBuilder(model_name)
    
    return _prompt_builder


@handle_errors(error_type = PromptBuildingError, log_error = True, reraise = False)
def build_qa_prompt(query: str, context: str, sources: List[ChunkWithScore], **kwargs) -> Dict[str, str]:
    """
    Convenience function for building QA prompts
    
    Arguments:
    ----------
        query   { str }  : User query
        
        context { str }  : Retrieved context
        
        sources { list } : Source chunks
        
        **kwargs         : Additional prompt building arguments
    
    Returns:
    --------
             { dict }    : Dictionary with system and user prompts
    """
    builder = get_prompt_builder()
    
    return builder.build_prompt(query, context, sources, PromptType.QA, **kwargs)