|
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 |
|
|
|
|
|
last_user_message = "" |
|
for msg in reversed(conversation_history): |
|
if msg["role"] == "user": |
|
last_user_message = msg["content"] |
|
break |
|
|
|
|
|
context_string = context_provider.get_context_for_llm( |
|
last_user_message, |
|
conversation_history |
|
) |
|
|
|
|
|
if context_string: |
|
context_message = { |
|
"role": "system", |
|
"content": context_string |
|
} |
|
|
|
enriched_history = [context_message] + conversation_history |
|
return enriched_history |
|
|
|
|
|
return conversation_history |
|
|