refactor(factorminer): 将 LLM Prompt 和解析器改造为直接输出本地 DSL
- DSL 规范改为 snake_case、中缀运算符,示例同步替换 - 移除 ExpressionTree 依赖,改为括号匹配等基础校验 - retry prompt 适配本地 DSL 规则
This commit is contained in:
@@ -117,7 +117,9 @@ class FactorGenerator:
|
||||
max_tokens=self.max_tokens,
|
||||
)
|
||||
elapsed = time.monotonic() - t0
|
||||
logger.info("LLM response received in %.1fs (%d chars)", elapsed, len(raw_output))
|
||||
logger.info(
|
||||
"LLM response received in %.1fs (%d chars)", elapsed, len(raw_output)
|
||||
)
|
||||
|
||||
# 3. Parse output
|
||||
candidates, failed_lines = parse_llm_output(raw_output)
|
||||
@@ -198,14 +200,15 @@ class FactorGenerator:
|
||||
|
||||
repair_prompt = (
|
||||
"The following factor formulas failed to parse. "
|
||||
"Fix each one so it uses ONLY valid operators and features "
|
||||
"Fix each one so it uses ONLY valid local DSL operators and features "
|
||||
"from the library. Return them in the same numbered format:\n"
|
||||
"<number>. <name>: <corrected_formula>\n\n"
|
||||
"Broken formulas:\n"
|
||||
+ "\n".join(f" {i+1}. {f}" for i, f in enumerate(failed))
|
||||
+ "\n".join(f" {i + 1}. {f}" for i, f in enumerate(failed))
|
||||
+ "\n\nFix all syntax errors, unknown operators, and invalid "
|
||||
"feature names. Every formula must be a valid nested function "
|
||||
"call using only operators from the library."
|
||||
"feature names. Use snake_case functions (e.g., ts_mean, cs_rank), "
|
||||
"infix operators (+, -, *, /, >, <), and raw features without $ prefix. "
|
||||
"Every formula must be valid in the local DSL."
|
||||
)
|
||||
|
||||
try:
|
||||
|
||||
@@ -1,20 +1,16 @@
|
||||
"""Parse LLM output into structured CandidateFactor objects.
|
||||
|
||||
Handles various output formats from LLMs: numbered lists, JSON,
|
||||
markdown code blocks, and raw text. Validates each formula against
|
||||
the expression tree parser.
|
||||
markdown code blocks, and raw text. Validates each formula with
|
||||
basic string checks; no FactorMiner-specific parsing is performed.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import re
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Dict, List, Optional, Tuple
|
||||
|
||||
from src.factorminer.core.expression_tree import ExpressionTree
|
||||
from src.factorminer.core.parser import parse, try_parse
|
||||
from src.factorminer.core.types import OperatorType, OPERATOR_REGISTRY
|
||||
from dataclasses import dataclass
|
||||
from typing import List, Optional, Tuple
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -29,49 +25,44 @@ class CandidateFactor:
|
||||
Descriptive snake_case name.
|
||||
formula : str
|
||||
DSL formula string.
|
||||
expression_tree : ExpressionTree or None
|
||||
Parsed expression tree (None if parsing failed).
|
||||
category : str
|
||||
Inferred category based on outermost operators.
|
||||
parse_error : str
|
||||
Error message if formula failed to parse.
|
||||
Error message if formula failed basic validation.
|
||||
"""
|
||||
|
||||
name: str
|
||||
formula: str
|
||||
expression_tree: Optional[ExpressionTree] = None
|
||||
category: str = "unknown"
|
||||
parse_error: str = ""
|
||||
|
||||
@property
|
||||
def is_valid(self) -> bool:
|
||||
return self.expression_tree is not None
|
||||
return not self.parse_error and bool(self.formula.strip())
|
||||
|
||||
|
||||
def _infer_category(formula: str) -> str:
|
||||
"""Infer a rough category from the outermost operators in the formula."""
|
||||
lower = formula.lower()
|
||||
# Check for cross-sectional operators at the top
|
||||
if any(op in formula for op in ("CsRank", "CsZScore", "CsDemean", "CsScale", "CsNeutralize", "CsQuantile")):
|
||||
# Look deeper for sub-category
|
||||
if any(op in formula for op in ("Corr", "Cov", "Beta", "Resid")):
|
||||
if any(
|
||||
op in formula
|
||||
for op in ("cs_rank", "cs_zscore", "cs_demean", "cs_neutralize", "cs_winsorize")
|
||||
):
|
||||
if any(op in formula for op in ("ts_corr", "ts_cov")):
|
||||
return "cross_sectional_regression"
|
||||
if any(op in formula for op in ("Delta", "Delay", "Return", "LogReturn")):
|
||||
if any(op in formula for op in ("ts_delta", "ts_delay", "ts_pct_change")):
|
||||
return "cross_sectional_momentum"
|
||||
if any(op in formula for op in ("Std", "Var", "Skew", "Kurt")):
|
||||
if any(op in formula for op in ("ts_std", "ts_var", "ts_skew", "ts_kurt")):
|
||||
return "cross_sectional_volatility"
|
||||
if any(op in formula for op in ("Mean", "Sum", "EMA", "SMA", "WMA", "DEMA", "HMA", "KAMA")):
|
||||
if any(op in formula for op in ("ts_mean", "ts_sum", "ts_ema", "ts_wma")):
|
||||
return "cross_sectional_smoothing"
|
||||
if any(op in formula for op in ("TsLinReg", "TsLinRegSlope")):
|
||||
return "cross_sectional_trend"
|
||||
return "cross_sectional"
|
||||
if any(op in formula for op in ("Corr", "Cov", "Beta", "Resid")):
|
||||
if any(op in formula for op in ("ts_corr", "ts_cov")):
|
||||
return "regression"
|
||||
if any(op in formula for op in ("Delta", "Delay", "Return", "LogReturn")):
|
||||
if any(op in formula for op in ("ts_delta", "ts_delay", "ts_pct_change")):
|
||||
return "momentum"
|
||||
if any(op in formula for op in ("Std", "Var", "Skew", "Kurt")):
|
||||
if any(op in formula for op in ("ts_std", "ts_var", "ts_skew", "ts_kurt")):
|
||||
return "volatility"
|
||||
if any(op in formula for op in ("IfElse", "Greater", "Less")):
|
||||
if any(op in formula for op in ("if_", "where", ">", "<")):
|
||||
return "conditional"
|
||||
return "general"
|
||||
|
||||
@@ -82,28 +73,26 @@ def _infer_category(formula: str) -> str:
|
||||
|
||||
# Pattern: "1. name: formula" or "1) name: formula"
|
||||
_NUMBERED_PATTERN = re.compile(
|
||||
r"^\s*\d+[\.\)]\s*" # numbered prefix
|
||||
r"^\s*\d+[\.\)]\s*" # numbered prefix
|
||||
r"([a-zA-Z_][a-zA-Z0-9_]*)" # factor name
|
||||
r"\s*:\s*" # colon separator
|
||||
r"(.+)$" # formula
|
||||
r"\s*:\s*" # colon separator
|
||||
r"(.+)$" # formula
|
||||
)
|
||||
|
||||
# Pattern: "name: formula" (no number)
|
||||
_PLAIN_PATTERN = re.compile(
|
||||
r"^\s*([a-zA-Z_][a-zA-Z0-9_]*)" # factor name
|
||||
r"\s*:\s*" # colon separator
|
||||
r"(.+)$" # formula
|
||||
r"\s*:\s*" # colon separator
|
||||
r"(.+)$" # formula
|
||||
)
|
||||
|
||||
# Pattern: just a formula starting with an operator
|
||||
# Pattern: just a formula starting with a function call, unary minus, or number
|
||||
_FORMULA_ONLY_PATTERN = re.compile(
|
||||
r"^\s*([A-Z][a-zA-Z]*\(.+\))\s*$"
|
||||
r"^\s*([a-zA-Z_][a-zA-Z0-9_]*\s*\(.*\)|-.*|\d.*)\s*$"
|
||||
)
|
||||
|
||||
# Pattern: JSON-like {"name": "...", "formula": "..."}
|
||||
_JSON_PATTERN = re.compile(
|
||||
r'"name"\s*:\s*"([^"]+)"\s*,\s*"formula"\s*:\s*"([^"]+)"'
|
||||
)
|
||||
_JSON_PATTERN = re.compile(r'"name"\s*:\s*"([^"]+)"\s*,\s*"formula"\s*:\s*"([^"]+)"')
|
||||
|
||||
|
||||
def _strip_markdown(text: str) -> str:
|
||||
@@ -152,6 +141,8 @@ def parse_llm_output(raw_text: str) -> Tuple[List[CandidateFactor], List[str]]:
|
||||
json_matches = _JSON_PATTERN.findall(text)
|
||||
if json_matches:
|
||||
for name, formula in json_matches:
|
||||
if name is None or formula is None:
|
||||
continue
|
||||
formula = _clean_formula(formula)
|
||||
candidate = _try_build_candidate(name, formula)
|
||||
if candidate.name not in seen_names:
|
||||
@@ -184,6 +175,7 @@ def parse_llm_output(raw_text: str) -> Tuple[List[CandidateFactor], List[str]]:
|
||||
m = _FORMULA_ONLY_PATTERN.match(line)
|
||||
if m:
|
||||
formula = m.group(1)
|
||||
assert formula is not None
|
||||
# Generate name from formula
|
||||
name = _generate_name_from_formula(formula, len(candidates))
|
||||
|
||||
@@ -222,38 +214,29 @@ def parse_llm_output(raw_text: str) -> Tuple[List[CandidateFactor], List[str]]:
|
||||
|
||||
|
||||
def _try_build_candidate(name: str, formula: str) -> CandidateFactor:
|
||||
"""Attempt to parse a formula and build a CandidateFactor."""
|
||||
tree = try_parse(formula)
|
||||
if tree is not None:
|
||||
category = _infer_category(formula)
|
||||
return CandidateFactor(
|
||||
name=name,
|
||||
formula=tree.to_string(), # canonicalize
|
||||
expression_tree=tree,
|
||||
category=category,
|
||||
)
|
||||
else:
|
||||
# Try to get a useful error message
|
||||
error_msg = ""
|
||||
try:
|
||||
parse(formula)
|
||||
except (SyntaxError, KeyError, ValueError) as e:
|
||||
error_msg = str(e)
|
||||
|
||||
"""Attempt to validate a formula and build a CandidateFactor."""
|
||||
# Basic validation: parenthesis balance
|
||||
if formula.count("(") != formula.count(")"):
|
||||
return CandidateFactor(
|
||||
name=name,
|
||||
formula=formula,
|
||||
expression_tree=None,
|
||||
category="unknown",
|
||||
parse_error=error_msg,
|
||||
parse_error="括号不匹配",
|
||||
)
|
||||
category = _infer_category(formula)
|
||||
return CandidateFactor(name=name, formula=formula, category=category)
|
||||
|
||||
|
||||
def _generate_name_from_formula(formula: str, index: int) -> str:
|
||||
"""Generate a descriptive name from a formula."""
|
||||
# Extract the outermost operator
|
||||
m = re.match(r"([A-Z][a-zA-Z]*)\(", formula)
|
||||
# Extract the outermost operator (snake_case)
|
||||
m = re.match(r"([a-zA-Z_][a-zA-Z0-9_]*)\s*\(", formula)
|
||||
if m:
|
||||
outer_op = m.group(1).lower()
|
||||
return f"{outer_op}_factor_{index + 1}"
|
||||
# Handle unary minus
|
||||
m = re.match(r"-([a-zA-Z_][a-zA-Z0-9_]*)\s*\(", formula)
|
||||
if m:
|
||||
outer_op = m.group(1).lower()
|
||||
return f"neg_{outer_op}_factor_{index + 1}"
|
||||
return f"factor_{index + 1}"
|
||||
|
||||
@@ -9,68 +9,81 @@ from __future__ import annotations
|
||||
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
from src.factorminer.core.types import (
|
||||
FEATURES,
|
||||
OPERATOR_REGISTRY,
|
||||
OperatorSpec,
|
||||
OperatorType,
|
||||
)
|
||||
|
||||
LOCAL_OPERATOR_TABLE = {
|
||||
"ARITHMETIC": [
|
||||
("+", "二元", "x + y"),
|
||||
("-", "二元/一元", "x - y 或 -x"),
|
||||
("*", "二元", "x * y"),
|
||||
("/", "二元", "x / y"),
|
||||
("**", "二元", "x ** y (幂运算)"),
|
||||
(">", "二元", "x > y (条件判断,返回 0/1)"),
|
||||
("<", "二元", "x < y (条件判断,返回 0/1)"),
|
||||
("abs(x)", "一元", "绝对值"),
|
||||
("sign(x)", "一元", "符号函数"),
|
||||
("max_(x, y)", "二元", "逐元素最大值"),
|
||||
("min_(x, y)", "二元", "逐元素最小值"),
|
||||
("clip(x, lower, upper)", "一元带参", "截断"),
|
||||
("log(x)", "一元", "自然对数"),
|
||||
("sqrt(x)", "一元", "平方根"),
|
||||
("exp(x)", "一元", "指数函数"),
|
||||
],
|
||||
"TIMESERIES": [
|
||||
("ts_mean(x, window)", "一元+窗口", "滚动均值"),
|
||||
("ts_std(x, window)", "一元+窗口", "滚动标准差"),
|
||||
("ts_var(x, window)", "一元+窗口", "滚动方差"),
|
||||
("ts_max(x, window)", "一元+窗口", "滚动最大值"),
|
||||
("ts_min(x, window)", "一元+窗口", "滚动最小值"),
|
||||
("ts_sum(x, window)", "一元+窗口", "滚动求和"),
|
||||
("ts_delay(x, periods)", "一元+周期", "滞后 N 期"),
|
||||
("ts_delta(x, periods)", "一元+周期", "差分 N 期"),
|
||||
("ts_corr(x, y, window)", "二元+窗口", "滚动相关系数"),
|
||||
("ts_cov(x, y, window)", "二元+窗口", "滚动协方差"),
|
||||
("ts_pct_change(x, periods)", "一元+周期", "N 期百分比变化"),
|
||||
("ts_ema(x, window)", "一元+窗口", "指数移动平均"),
|
||||
("ts_wma(x, window)", "一元+窗口", "加权移动平均"),
|
||||
("ts_skew(x, window)", "一元+窗口", "滚动偏度"),
|
||||
("ts_kurt(x, window)", "一元+窗口", "滚动峰度"),
|
||||
("ts_rank(x, window)", "一元+窗口", "滚动分位排名"),
|
||||
],
|
||||
"CROSS_SECTIONAL": [
|
||||
("cs_rank(x)", "一元", "截面排名(分位数)"),
|
||||
("cs_zscore(x)", "一元", "截面 Z-Score 标准化"),
|
||||
("cs_demean(x)", "一元", "截面去均值"),
|
||||
("cs_neutralize(x, group)", "一元", "行业/市值中性化"),
|
||||
("cs_winsorize(x, lower, upper)", "一元", "截面缩尾处理"),
|
||||
],
|
||||
"CONDITIONAL": [
|
||||
("if_(condition, true_val, false_val)", "三元", "条件选择"),
|
||||
("where(condition, true_val, false_val)", "三元", "if_ 的别名"),
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
def _format_operator_table() -> str:
|
||||
"""Build a human-readable operator reference table grouped by category."""
|
||||
grouped: Dict[str, List[OperatorSpec]] = {}
|
||||
for spec in OPERATOR_REGISTRY.values():
|
||||
cat = spec.category.name
|
||||
grouped.setdefault(cat, []).append(spec)
|
||||
|
||||
lines: List[str] = []
|
||||
for cat_name in [
|
||||
"ARITHMETIC",
|
||||
"STATISTICAL",
|
||||
"TIMESERIES",
|
||||
"SMOOTHING",
|
||||
"CROSS_SECTIONAL",
|
||||
"REGRESSION",
|
||||
"LOGICAL",
|
||||
"AUTO_INVENTED",
|
||||
]:
|
||||
specs = grouped.get(cat_name, [])
|
||||
if not specs:
|
||||
continue
|
||||
lines = []
|
||||
for cat_name, ops in LOCAL_OPERATOR_TABLE.items():
|
||||
lines.append(f"\n### {cat_name} operators")
|
||||
for spec in sorted(specs, key=lambda s: s.name):
|
||||
params_str = ""
|
||||
if spec.param_names:
|
||||
parts = []
|
||||
for pname in spec.param_names:
|
||||
default = spec.param_defaults.get(pname, "")
|
||||
lo, hi = spec.param_ranges.get(pname, (None, None))
|
||||
range_str = f"[{lo}-{hi}]" if lo is not None else ""
|
||||
parts.append(f"{pname}={default}{range_str}")
|
||||
params_str = f" params: {', '.join(parts)}"
|
||||
arity_args = ", ".join([f"expr{i+1}" for i in range(spec.arity)])
|
||||
if spec.param_names:
|
||||
arity_args += ", " + ", ".join(spec.param_names)
|
||||
lines.append(f"- {spec.name}({arity_args}): {spec.description}{params_str}")
|
||||
for op_sig, arity, desc in ops:
|
||||
lines.append(f"- {op_sig}: {desc} ({arity})")
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def _format_feature_list() -> str:
|
||||
"""Build a description of available raw features."""
|
||||
descriptions = {
|
||||
"$open": "opening price",
|
||||
"$high": "highest price in the bar",
|
||||
"$low": "lowest price in the bar",
|
||||
"$close": "closing price",
|
||||
"$volume": "trading volume (shares)",
|
||||
"$amt": "trading amount (currency value)",
|
||||
"$vwap": "volume-weighted average price",
|
||||
"$returns": "close-to-close returns",
|
||||
"open": "开盘价",
|
||||
"high": "最高价",
|
||||
"low": "最低价",
|
||||
"close": "收盘价",
|
||||
"vol": "成交量(股数)",
|
||||
"amount": "成交额(金额)",
|
||||
"vwap": "可用 amount / vol 计算",
|
||||
"returns": "可用 close / ts_delay(close, 1) - 1 计算",
|
||||
}
|
||||
lines = []
|
||||
for feat in FEATURES:
|
||||
desc = descriptions.get(feat, "")
|
||||
for feat, desc in descriptions.items():
|
||||
lines.append(f" {feat}: {desc}")
|
||||
return "\n".join(lines)
|
||||
|
||||
@@ -81,7 +94,7 @@ def _format_feature_list() -> str:
|
||||
|
||||
SYSTEM_PROMPT = f"""You are a quantitative researcher mining formulaic alpha factors for stock selection.
|
||||
|
||||
Your goal is to generate novel, predictive factor expressions using a tree-structured domain-specific language (DSL). Each factor is a composition of operators applied to raw market features.
|
||||
Your goal is to generate novel, predictive factor expressions using the local ProStock DSL. Each factor is a composition of operators applied to raw market features.
|
||||
|
||||
## RAW FEATURES (leaf nodes)
|
||||
{_format_feature_list()}
|
||||
@@ -90,40 +103,37 @@ Your goal is to generate novel, predictive factor expressions using a tree-struc
|
||||
{_format_operator_table()}
|
||||
|
||||
## EXPRESSION SYNTAX RULES
|
||||
1. Every expression is a nested function call: Operator(args...)
|
||||
2. Leaf nodes are raw features ($close, $volume, etc.) or numeric constants.
|
||||
3. Operators are called by name with expression arguments first, then numeric parameters:
|
||||
- Mean($close, 20) = 20-day rolling mean of $close
|
||||
- Corr($close, $volume, 10) = 10-day rolling correlation of close and volume
|
||||
- IfElse(Greater($returns, 0), $volume, Neg($volume)) = conditional
|
||||
4. No infix operators; use Add(x,y) instead of x+y, Sub(x,y) instead of x-y, etc.
|
||||
5. Parameters like window sizes are trailing numeric arguments after expression children.
|
||||
6. Valid window sizes are integers; check each operator's parameter ranges above.
|
||||
7. Cross-sectional operators (CsRank, CsZScore, CsDemean, CsScale, CsNeutralize) operate across all stocks at each time step -- they are crucial for making factors comparable.
|
||||
1. Expressions use Python-style infix operators: +, -, *, /, **, >, <
|
||||
2. Function calls use snake_case names with comma-separated arguments: ts_mean(close, 20)
|
||||
3. Window sizes and periods are numeric arguments placed last in function calls.
|
||||
4. Valid window sizes are integers, typically in range [2, 250].
|
||||
5. Cross-sectional operators (cs_rank, cs_zscore, cs_demean) operate across all stocks at each time step -- they are crucial for making factors comparable.
|
||||
6. Do NOT use $ prefix for features. Use `close`, `vol`, `amount`, etc. directly.
|
||||
7. `vwap` is not a raw feature; use `amount / vol` if you need it.
|
||||
8. `returns` is not a raw feature; use `close / ts_delay(close, 1) - 1` if you need returns.
|
||||
|
||||
## EXAMPLES OF WELL-FORMED FACTORS
|
||||
- Neg(CsRank(Delta($close, 5)))
|
||||
- -cs_rank(ts_delta(close, 5))
|
||||
Short-term reversal: rank of 5-day price change, negated.
|
||||
- CsZScore(Div(Sub($volume, Mean($volume, 20)), Std($volume, 20)))
|
||||
- cs_zscore((vol - ts_mean(vol, 20)) / ts_std(vol, 20))
|
||||
Volume surprise: standardized deviation from 20-day mean volume.
|
||||
- CsRank(Div(Sub($close, $vwap), $vwap))
|
||||
- cs_rank((close - amount / vol) / (amount / vol))
|
||||
Intraday deviation from VWAP, cross-sectionally ranked.
|
||||
- Neg(Corr($volume, $close, 10))
|
||||
- -ts_corr(vol, close, 10)
|
||||
Negative price-volume correlation over 10 days.
|
||||
- CsRank(TsLinRegSlope($volume, 20))
|
||||
Trend in trading volume over 20 days, ranked.
|
||||
- IfElse(Greater($returns, 0), Std($returns, 10), Neg(Std($returns, 10)))
|
||||
- if_(close / ts_delay(close, 1) - 1 > 0, ts_std(close / ts_delay(close, 1) - 1, 10), -ts_std(close / ts_delay(close, 1) - 1, 10))
|
||||
Conditional volatility: positive for up-moves, negative for down-moves.
|
||||
- CsRank(Div(Sub($close, TsMin($low, 20)), Sub(TsMax($high, 20), TsMin($low, 20))))
|
||||
- cs_rank((close - ts_min(low, 20)) / (ts_max(high, 20) - ts_min(low, 20)))
|
||||
Position within 20-day price range, ranked.
|
||||
|
||||
## KEY PRINCIPLES FOR HIGH-QUALITY FACTORS
|
||||
- Always wrap the outermost expression with a cross-sectional operator (CsRank, CsZScore) for comparability.
|
||||
- Always wrap the outermost expression with a cross-sectional operator (cs_rank, cs_zscore) for comparability.
|
||||
- Combine DIFFERENT operator types for novelty (e.g., time-series + cross-sectional + arithmetic).
|
||||
- Use diverse window sizes; avoid always defaulting to 10.
|
||||
- Explore uncommon feature combinations ($amt, $vwap are underused).
|
||||
- Explore uncommon feature combinations (amount, amount/vol are underused).
|
||||
- Factors with depth 3-7 tend to be best: deep enough to capture non-trivial patterns but not so deep they overfit.
|
||||
- Prefer economically meaningful combinations over random nesting.
|
||||
- IMPORTANT: Avoid operators that are NOT listed above (e.g., Decay, TsLinRegSlope, HMA, DEMA, Resid). If you use them, the factor will be rejected.
|
||||
"""
|
||||
|
||||
|
||||
@@ -131,6 +141,7 @@ Your goal is to generate novel, predictive factor expressions using a tree-struc
|
||||
# PromptBuilder
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def normalize_factor_references(entries: Optional[List[Any]]) -> List[str]:
|
||||
"""Convert mixed factor metadata into prompt-safe string references."""
|
||||
if not entries:
|
||||
@@ -220,13 +231,10 @@ class PromptBuilder:
|
||||
lib_size = library_state.get("size", 0)
|
||||
target = library_state.get("target_size", 110)
|
||||
sections.append(
|
||||
f"\n## CURRENT LIBRARY STATUS\n"
|
||||
f"Library size: {lib_size} / {target} factors."
|
||||
f"\n## CURRENT LIBRARY STATUS\nLibrary size: {lib_size} / {target} factors."
|
||||
)
|
||||
|
||||
recent = normalize_factor_references(
|
||||
library_state.get("recent_admissions", [])
|
||||
)
|
||||
recent = normalize_factor_references(library_state.get("recent_admissions", []))
|
||||
if recent:
|
||||
sections.append(
|
||||
"Recently admitted factors:\n"
|
||||
@@ -235,10 +243,10 @@ class PromptBuilder:
|
||||
|
||||
saturation = library_state.get("domain_saturation", {})
|
||||
if saturation:
|
||||
sat_lines = [f" {domain}: {pct:.0%} saturated" for domain, pct in saturation.items()]
|
||||
sections.append(
|
||||
"Domain saturation:\n" + "\n".join(sat_lines)
|
||||
)
|
||||
sat_lines = [
|
||||
f" {domain}: {pct:.0%} saturated" for domain, pct in saturation.items()
|
||||
]
|
||||
sections.append("Domain saturation:\n" + "\n".join(sat_lines))
|
||||
|
||||
# --- Memory signal: recommended directions ---
|
||||
rec_dirs = memory_signal.get("recommended_directions", [])
|
||||
@@ -266,10 +274,7 @@ class PromptBuilder:
|
||||
|
||||
helix_prompt_text = memory_signal.get("prompt_text", "").strip()
|
||||
if helix_prompt_text:
|
||||
sections.append(
|
||||
"\n## HELIX RETRIEVAL SUMMARY\n"
|
||||
f"{helix_prompt_text}"
|
||||
)
|
||||
sections.append(f"\n## HELIX RETRIEVAL SUMMARY\n{helix_prompt_text}")
|
||||
|
||||
complementary_patterns = memory_signal.get("complementary_patterns", [])
|
||||
if complementary_patterns:
|
||||
@@ -330,8 +335,8 @@ class PromptBuilder:
|
||||
f"Output exactly {batch_size} factors, one per line.\n"
|
||||
f"Format each line as: <number>. <factor_name>: <formula>\n"
|
||||
f"Example:\n"
|
||||
f"1. momentum_reversal: Neg(CsRank(Delta($close, 5)))\n"
|
||||
f"2. volume_surprise: CsZScore(Div(Sub($volume, Mean($volume, 20)), Std($volume, 20)))\n"
|
||||
f"1. momentum_reversal: -cs_rank(ts_delta(close, 5))\n"
|
||||
f"2. volume_surprise: cs_zscore((vol - ts_mean(vol, 20)) / ts_std(vol, 20))\n"
|
||||
f"\nRules:\n"
|
||||
f"- factor_name: lowercase_with_underscores, descriptive, unique\n"
|
||||
f"- formula: valid DSL expression using ONLY operators and features listed above\n"
|
||||
@@ -346,6 +351,7 @@ class PromptBuilder:
|
||||
# New specialist/critic/debate prompt builder functions
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def build_specialist_prompt(
|
||||
specialist_name: str,
|
||||
specialist_domain: str,
|
||||
@@ -413,16 +419,12 @@ def build_specialist_prompt(
|
||||
|
||||
# Regime context
|
||||
if regime_context:
|
||||
sections.append(
|
||||
f"\n## CURRENT MARKET REGIME\n{regime_context}"
|
||||
)
|
||||
sections.append(f"\n## CURRENT MARKET REGIME\n{regime_context}")
|
||||
|
||||
# Library state
|
||||
lib_size = library_diagnostics.get("size", 0)
|
||||
target = library_diagnostics.get("target_size", 110)
|
||||
sections.append(
|
||||
f"\n## LIBRARY STATUS\nCurrent: {lib_size}/{target} factors."
|
||||
)
|
||||
sections.append(f"\n## LIBRARY STATUS\nCurrent: {lib_size}/{target} factors.")
|
||||
|
||||
recent = normalize_factor_references(
|
||||
library_diagnostics.get("recent_admissions", [])
|
||||
@@ -435,31 +437,26 @@ def build_specialist_prompt(
|
||||
|
||||
saturation = library_diagnostics.get("domain_saturation", {})
|
||||
if saturation:
|
||||
sat_lines = [
|
||||
f" {d}: {p:.0%} saturated" for d, p in saturation.items()
|
||||
]
|
||||
sat_lines = [f" {d}: {p:.0%} saturated" for d, p in saturation.items()]
|
||||
sections.append("Domain saturation:\n" + "\n".join(sat_lines))
|
||||
|
||||
# Memory signal injections
|
||||
rec_dirs = memory_signal.get("recommended_directions", [])
|
||||
if rec_dirs:
|
||||
sections.append(
|
||||
"\n## RECOMMENDED DIRECTIONS\n"
|
||||
+ "\n".join(f" * {d}" for d in rec_dirs)
|
||||
"\n## RECOMMENDED DIRECTIONS\n" + "\n".join(f" * {d}" for d in rec_dirs)
|
||||
)
|
||||
|
||||
forbidden = memory_signal.get("forbidden_directions", [])
|
||||
if forbidden:
|
||||
sections.append(
|
||||
"\n## FORBIDDEN DIRECTIONS\n"
|
||||
+ "\n".join(f" X {d}" for d in forbidden)
|
||||
"\n## FORBIDDEN DIRECTIONS\n" + "\n".join(f" X {d}" for d in forbidden)
|
||||
)
|
||||
|
||||
insights = memory_signal.get("strategic_insights", [])
|
||||
if insights:
|
||||
sections.append(
|
||||
"\n## STRATEGIC INSIGHTS\n"
|
||||
+ "\n".join(f" - {ins}" for ins in insights)
|
||||
"\n## STRATEGIC INSIGHTS\n" + "\n".join(f" - {ins}" for ins in insights)
|
||||
)
|
||||
|
||||
helix_text = memory_signal.get("prompt_text", "").strip()
|
||||
@@ -476,8 +473,7 @@ def build_specialist_prompt(
|
||||
warn = memory_signal.get("conflict_warnings", [])
|
||||
if warn:
|
||||
sections.append(
|
||||
"\n## SATURATION WARNINGS\n"
|
||||
+ "\n".join(f" ! {w}" for w in warn)
|
||||
"\n## SATURATION WARNINGS\n" + "\n".join(f" ! {w}" for w in warn)
|
||||
)
|
||||
|
||||
gaps = memory_signal.get("semantic_gaps", [])
|
||||
@@ -508,8 +504,7 @@ def build_specialist_prompt(
|
||||
# Avoid patterns
|
||||
if avoid_patterns:
|
||||
sections.append(
|
||||
"\n## PATTERNS TO AVOID\n"
|
||||
+ "\n".join(f" X {av}" for av in avoid_patterns)
|
||||
"\n## PATTERNS TO AVOID\n" + "\n".join(f" X {av}" for av in avoid_patterns)
|
||||
)
|
||||
|
||||
# Few-shot patterns from memory
|
||||
@@ -526,7 +521,7 @@ def build_specialist_prompt(
|
||||
f"\n## OUTPUT FORMAT\n"
|
||||
f"Generate exactly {n_proposals} novel factor candidates.\n"
|
||||
f"Format: <number>. <factor_name>: <formula>\n"
|
||||
f"Example: 1. momentum_reversal: Neg(CsRank(Delta($close, 5)))\n"
|
||||
f"Example: 1. momentum_reversal: -cs_rank(ts_delta(close, 5))\n"
|
||||
f"Rules:\n"
|
||||
f"- factor_name: lowercase_with_underscores, unique, descriptive\n"
|
||||
f"- formula: valid DSL expression only\n"
|
||||
@@ -581,16 +576,16 @@ def build_critic_scoring_prompt(
|
||||
)
|
||||
|
||||
if memory_signal:
|
||||
sections.append(f"\n## MEMORY CONTEXT (success patterns)\n{memory_signal[:600]}")
|
||||
sections.append(
|
||||
f"\n## MEMORY CONTEXT (success patterns)\n{memory_signal[:600]}"
|
||||
)
|
||||
|
||||
sections.append("\n## CANDIDATES")
|
||||
for c in candidates:
|
||||
name = c.get("name", "unknown")
|
||||
formula = c.get("formula", "")
|
||||
specialist = c.get("specialist", "unknown")
|
||||
sections.append(
|
||||
f" [{specialist}] {name}: {formula}"
|
||||
)
|
||||
sections.append(f" [{specialist}] {name}: {formula}")
|
||||
|
||||
sections.append(
|
||||
"\n## SCORING CRITERIA\n"
|
||||
@@ -647,7 +642,7 @@ def build_debate_synthesis_prompt(
|
||||
all_proposals,
|
||||
key=lambda p: score_map.get(p.get("name", ""), 0.0),
|
||||
reverse=True,
|
||||
)[:top_k * 2] # take 2x top_k for synthesis
|
||||
)[: top_k * 2] # take 2x top_k for synthesis
|
||||
|
||||
sections: List[str] = []
|
||||
sections.append(
|
||||
@@ -664,9 +659,7 @@ def build_debate_synthesis_prompt(
|
||||
formula = p.get("formula", "?")
|
||||
specialist = p.get("specialist", "?")
|
||||
score = score_map.get(name, 0.5)
|
||||
sections.append(
|
||||
f" [{specialist}, score={score:.2f}] {name}: {formula}"
|
||||
)
|
||||
sections.append(f" [{specialist}, score={score:.2f}] {name}: {formula}")
|
||||
|
||||
sections.append(
|
||||
f"\n## SELECTION CRITERIA\n"
|
||||
|
||||
Reference in New Issue
Block a user