llm-quant/app/llm/prompts.py
2025-10-05 16:28:53 +08:00

59 lines
2.2 KiB
Python

"""Prompt templates for natural language outputs."""
from __future__ import annotations
from typing import Dict, TYPE_CHECKING
from .templates import TemplateRegistry
if TYPE_CHECKING: # pragma: no cover
from app.utils.config import DepartmentSettings
from app.agents.departments import DepartmentContext
def plan_prompt(data: Dict) -> str:
"""Build a concise instruction prompt for the LLM."""
_ = data
return "你是一个投资助理,请根据提供的数据给出三条要点和两条风险提示。"
def department_prompt(
settings: "DepartmentSettings",
context: "DepartmentContext",
supplements: str = "",
) -> str:
"""Compose a structured prompt for department-level LLM ensemble."""
# Format data for template
feature_lines = "\n".join(
f"- {key}: {value}" for key, value in sorted(context.features.items())
)
market_lines = "\n".join(
f"- {key}: {value}" for key, value in sorted(context.market_snapshot.items())
)
scope_lines = "\n".join(f"- {item}" for item in settings.data_scope)
role_description = settings.description.strip()
role_instruction = settings.prompt.strip()
# Determine template ID based on department settings
template_id = f"{settings.code.lower()}_dept"
if not TemplateRegistry.get(template_id):
template_id = "department_base"
# Prepare template variables
template_vars = {
"title": settings.title,
"ts_code": context.ts_code,
"trade_date": context.trade_date,
"description": role_description or "未配置,默认沿用部门名称所代表的研究职责。",
"instruction": role_instruction or "在保持部门风格的前提下,结合可用数据做出审慎判断。",
"data_scope": scope_lines or "- 使用系统提供的全部上下文,必要时指出仍需的额外数据。",
"features": feature_lines or "- (无)",
"market_snapshot": market_lines or "- (无)",
"supplements": supplements.strip() or "- 当前无追加数据"
}
# Get template and format prompt
template = TemplateRegistry.get(template_id)
return template.format(template_vars)