Files
fckbot/rag/functions/plan_generation.py
2026-06-30 21:01:57 +00:00

73 lines
2.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
Генерация плана действий для сложных запросов.
Модель составляет пошаговый план, который затем может быть выполнен.
"""
import json
import logging
from typing import List, Dict, Any
from rag.services.giga_client import GigaClient
from rag.config_models import AppConfig
logger = logging.getLogger(__name__)
async def generate_plan(
query: str,
history: List[Dict[str, str]],
config: AppConfig,
giga: GigaClient,
prompts: Dict[str, str],
) -> List[Dict[str, Any]]:
"""
Генерирует план действий для сложного запроса.
Аргументы:
query: запрос пользователя.
history: история диалога (может использоваться для контекста).
config: конфигурация.
giga: клиент GigaChat.
prompts: словарь с промптами (ключ 'planning').
Возвращает:
List[Dict]: список шагов, каждый с полями:
step_description: str,
required_data: str (опционально),
expected_output: str (опционально).
В случае ошибки возвращает пустой список.
"""
prompt_template = prompts.get("planning")
if not prompt_template:
logger.error("Промпт для генерации плана не найден")
return []
context = ""
if history:
context = "\n".join([f"{msg['role']}: {msg['content']}" for msg in history[-3:]])
full_prompt = prompt_template.format(query=query, context=context)
try:
response = await giga.chat(
history=[],
query=full_prompt,
system_prompt=None,
file_id=None,
temperature=getattr(config, 'planning_temperature', 0.2),
)
data = json.loads(response.strip())
if isinstance(data, list):
for item in data:
if "step_description" not in item:
item["step_description"] = item.get("description", "Неизвестный шаг")
return data
else:
logger.error("План не является списком JSON")
return []
except json.JSONDecodeError as e:
logger.error(f"Ошибка парсинга JSON плана: {e}, ответ: {response[:200]}")
return []
except Exception as e:
logger.exception(f"Ошибка при генерации плана: {e}")
return []