File size: 1,551 Bytes
c1cbefd
 
 
e441606
c1cbefd
 
e441606
c1cbefd
 
 
 
e441606
 
 
 
 
c1cbefd
 
 
 
 
 
 
e441606
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1cbefd
e441606
 
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
import json
from typing import List, Dict, Optional, Union
from src.llm.base_provider import LLMProvider
from src.services.context_provider import context_provider

class EnhancedLLMProvider(LLMProvider):
    """Base provider with intelligent context enrichment"""
    
    def __init__(self, model_name: str, timeout: int = 30, max_retries: int = 3):
        super().__init__(model_name, timeout, max_retries)
        
    def _enrich_context_intelligently(self, conversation_history: List[Dict]) -> List[Dict]:
        """Add context only when it's actually relevant"""
        if not conversation_history:
            return conversation_history
        
        # Get the last user message to determine context needs
        last_user_message = ""
        for msg in reversed(conversation_history):
            if msg["role"] == "user":
                last_user_message = msg["content"]
                break
        
        # Get intelligent context
        context_string = context_provider.get_context_for_llm(
            last_user_message, 
            conversation_history
        )
        
        # Only add context if it's relevant
        if context_string:
            context_message = {
                "role": "system",
                "content": context_string
            }
            # Insert context at the beginning
            enriched_history = [context_message] + conversation_history
            return enriched_history
        
        # Return original history if no context needed
        return conversation_history