Update 3 files

- /rag/intent_router.py
- /rag/query_processor.py
- /rag/history_manager.py
This commit is contained in:
Markov Andrey
2026-06-30 16:15:00 +00:00
parent 9d351d037b
commit fbfe178067
3 changed files with 118 additions and 118 deletions

View File

@@ -11,13 +11,13 @@
"""
import logging
from typing import List, Dict, Optional, Any
from typing import List, Dict, Optional, Any, Tuple
from rag.services.postgres_service import PostgresService
from rag.services.giga_client import GigaClient
from rag.utils.text_utils import count_tokens
from rag.functions.hierarchical_summarize import hierarchical_summarize
from rag.config_models import AppConfig
from core.services.postgres_service import PostgresService
from core.services.giga_client import GigaClient
from core.utils.text_utils import count_tokens
from core.functions.hierarchical_summarize import hierarchical_summarize
from core.config_models import AppConfig
logger = logging.getLogger(__name__)
@@ -33,7 +33,7 @@ class HistoryManager:
giga: GigaClient,
config: AppConfig,
default_prompts: Optional[Dict[str, str]] = None
):
) -> None:
"""
Инициализация менеджера истории.
@@ -43,10 +43,14 @@ class HistoryManager:
config: объект конфигурации (AppConfig)
default_prompts: словарь промптов по умолчанию (нужен для hierarchical_summary)
"""
self.db = db
self.giga = giga
self.config = config
self.default_prompts = default_prompts or {}
self.db: PostgresService = db
self.giga: GigaClient = giga
self.config: AppConfig = config
self.default_prompts: Dict[str, str] = default_prompts or {}
# ------------------------------------------------------------------
# Получение и форматирование истории
# ------------------------------------------------------------------
async def get_history(
self,
@@ -65,7 +69,7 @@ class HistoryManager:
Возвращает:
List[Dict[str, str]]: список сообщений с ключами 'role' и 'content'
"""
raw_history = await self.db.get_history(user_jid, room_jid, limit=limit)
raw_history: List[Dict[str, Any]] = await self.db.get_history(user_jid, room_jid, limit=limit)
# Преобразуем в формат, ожидаемый LLM
return [
{"role": rec['role'], "content": rec['content']}
@@ -95,8 +99,8 @@ class HistoryManager:
return []
# Подсчитываем общее количество токенов в истории
history_text = "\n".join([f"{msg['role']}: {msg['content']}" for msg in history])
total_tokens = count_tokens(history_text)
history_text: str = "\n".join([f"{msg['role']}: {msg['content']}" for msg in history])
total_tokens: int = count_tokens(history_text)
if total_tokens <= max_tokens:
logger.debug(f"История укладывается в {max_tokens} токенов (фактически {total_tokens})")
@@ -127,14 +131,14 @@ class HistoryManager:
)
# Параметры сжатия из конфига
summarization_config = self.config.summarization
target_tokens = summarization_config.target_tokens_after_summary
chunk_size = summarization_config.chunk_size_tokens
max_depth = summarization_config.max_depth
temperature = self.config.ai.temperature
summarization_config: Dict[str, Any] = getattr(self.config, 'summarization', {})
target_tokens: int = summarization_config.get('target_tokens_after_summary', max_tokens)
chunk_size: int = summarization_config.get('chunk_size_tokens', 500)
max_depth: int = summarization_config.get('max_depth', 2)
temperature: float = getattr(self.config, 'ai_temperature', 0.1)
try:
compressed_text = await hierarchical_summarize(
compressed_text: str = await hierarchical_summarize(
text=history_text,
giga=self.giga,
prompt_template=prompt_template,
@@ -153,10 +157,10 @@ class HistoryManager:
except Exception as e:
logger.error(f"Ошибка при сжатии истории: {e}, выполняем простую обрезку")
# Fallback: простая обрезка по токенам
truncated_history = []
total = 0
truncated_history: List[Dict[str, str]] = []
total: int = 0
for msg in reversed(history):
tokens = count_tokens(msg['content'])
tokens: int = count_tokens(msg['content'])
if total + tokens <= max_tokens:
truncated_history.append(msg)
total += tokens