AI-Life-Coach-Streamlit2 / src /llm /enhanced_provider.py
rdune71's picture
self healing/weather update
e441606
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