yf176 commited on
Commit ·
053707d
1
Parent(s): 864d4c9
fix(trend): reduce prompt and improve JSON retry
Browse files
src/polyhedra/agents/quant/io_registry.py
CHANGED
|
@@ -214,7 +214,21 @@ def _project_options_summary(inputs: BaseModel) -> dict[str, Any]:
|
|
| 214 |
return projected
|
| 215 |
|
| 216 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
_PROMPT_PROJECTORS = {
|
|
|
|
| 218 |
"MacroContextWatcher": _project_options_summary,
|
| 219 |
"UnusualEventDetector": _project_options_summary,
|
| 220 |
"EventRouter": _project_options_summary,
|
|
|
|
| 214 |
return projected
|
| 215 |
|
| 216 |
|
| 217 |
+
def _project_trend_input(inputs: BaseModel) -> dict[str, Any]:
|
| 218 |
+
"""Remove nontechnical records that TrendAnalyst never reasons over."""
|
| 219 |
+
projected = inputs.model_dump(mode="json")
|
| 220 |
+
market_data = projected.get("dependencies", {}).get("market_data", [])
|
| 221 |
+
for market in market_data:
|
| 222 |
+
omitted = {}
|
| 223 |
+
for collection in ("news", "earnings"):
|
| 224 |
+
records = market.pop(collection, [])
|
| 225 |
+
omitted[collection] = len(records)
|
| 226 |
+
market["omitted_nontechnical_records"] = omitted
|
| 227 |
+
return projected
|
| 228 |
+
|
| 229 |
+
|
| 230 |
_PROMPT_PROJECTORS = {
|
| 231 |
+
"TrendAnalyst": _project_trend_input,
|
| 232 |
"MacroContextWatcher": _project_options_summary,
|
| 233 |
"UnusualEventDetector": _project_options_summary,
|
| 234 |
"EventRouter": _project_options_summary,
|
src/polyhedra/services/llm_service.py
CHANGED
|
@@ -751,6 +751,11 @@ class LLMService:
|
|
| 751 |
f"{sanitize_error_text(error['msg'], limit=160)}"
|
| 752 |
for error in exc.errors(include_input=False, include_context=False, include_url=False)
|
| 753 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 754 |
except ValueError:
|
| 755 |
errors = ("expected one valid JSON object with unique keys and finite numbers",)
|
| 756 |
else:
|
|
|
|
| 751 |
f"{sanitize_error_text(error['msg'], limit=160)}"
|
| 752 |
for error in exc.errors(include_input=False, include_context=False, include_url=False)
|
| 753 |
)
|
| 754 |
+
except json.JSONDecodeError as exc:
|
| 755 |
+
errors = (
|
| 756 |
+
f"invalid JSON at line {exc.lineno}, column {exc.colno}: "
|
| 757 |
+
f"{sanitize_error_text(exc.msg, limit=120)}",
|
| 758 |
+
)
|
| 759 |
except ValueError:
|
| 760 |
errors = ("expected one valid JSON object with unique keys and finite numbers",)
|
| 761 |
else:
|
tests/test_quant_io_registry.py
CHANGED
|
@@ -11,7 +11,8 @@ from polyhedra.agents.quant.asset_contracts import (
|
|
| 11 |
)
|
| 12 |
from polyhedra.agents.quant.decision_contracts import QuantStrategistLLMPayload
|
| 13 |
from polyhedra.agents.quant.io_registry import (
|
| 14 |
-
QUANT_IO_REGISTRY, _project_options_summary,
|
|
|
|
| 15 |
)
|
| 16 |
from polyhedra.agents.quant.market_data_contracts import DataFetcherInput
|
| 17 |
from polyhedra.agents.quant.product_contracts import PublisherPayload
|
|
@@ -159,6 +160,28 @@ def test_downstream_options_projection_omits_only_raw_contract_rows():
|
|
| 159 |
assert source.dependencies["options_flow"]["payload"]["ticker_results"][0]["contracts"]
|
| 160 |
|
| 161 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
def test_specialized_options_projection_reads_optional_context():
|
| 163 |
source = ProjectionInput(dependencies={}, optional_context={"options_flow": {
|
| 164 |
"artifact_id": "options-1",
|
|
|
|
| 11 |
)
|
| 12 |
from polyhedra.agents.quant.decision_contracts import QuantStrategistLLMPayload
|
| 13 |
from polyhedra.agents.quant.io_registry import (
|
| 14 |
+
QUANT_IO_REGISTRY, _project_options_summary, _project_trend_input,
|
| 15 |
+
quant_artifact_schema, quant_io_binding,
|
| 16 |
)
|
| 17 |
from polyhedra.agents.quant.market_data_contracts import DataFetcherInput
|
| 18 |
from polyhedra.agents.quant.product_contracts import PublisherPayload
|
|
|
|
| 160 |
assert source.dependencies["options_flow"]["payload"]["ticker_results"][0]["contracts"]
|
| 161 |
|
| 162 |
|
| 163 |
+
def test_trend_projection_omits_nontechnical_records_only():
|
| 164 |
+
source = ProjectionInput(dependencies={"market_data": [{
|
| 165 |
+
"ticker": "SPY",
|
| 166 |
+
"ohlcv": [{"close": 650.0}],
|
| 167 |
+
"indicators": {"points": [{"rsi_14": 58.0}]},
|
| 168 |
+
"news": [{"headline": "Not used for technical analysis"}],
|
| 169 |
+
"earnings": [{"fiscal_period": "Q3"}],
|
| 170 |
+
"provenance": [{"component": "OHLCV"}],
|
| 171 |
+
"quality_issues": [],
|
| 172 |
+
}]})
|
| 173 |
+
|
| 174 |
+
projected = _project_trend_input(source)
|
| 175 |
+
market = projected["dependencies"]["market_data"][0]
|
| 176 |
+
|
| 177 |
+
assert "news" not in market
|
| 178 |
+
assert "earnings" not in market
|
| 179 |
+
assert market["omitted_nontechnical_records"] == {"news": 1, "earnings": 1}
|
| 180 |
+
assert market["ohlcv"] == [{"close": 650.0}]
|
| 181 |
+
assert market["indicators"] == {"points": [{"rsi_14": 58.0}]}
|
| 182 |
+
assert source.dependencies["market_data"][0]["news"]
|
| 183 |
+
|
| 184 |
+
|
| 185 |
def test_specialized_options_projection_reads_optional_context():
|
| 186 |
source = ProjectionInput(dependencies={}, optional_context={"options_flow": {
|
| 187 |
"artifact_id": "options-1",
|
tests/test_services/test_structured_completion.py
CHANGED
|
@@ -49,6 +49,18 @@ async def test_correction_accounts_for_both_attempts(service):
|
|
| 49 |
assert "Input should be less than or equal to 1" in service.complete.call_args.args[0]
|
| 50 |
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
@pytest.mark.parametrize("text", [
|
| 53 |
"# Analysis\nSPY looks bullish", f"```json\n{VALID}\n```", "{}", "[]",
|
| 54 |
VALID.replace('"SPY"', '"QQQ"'), VALID.replace("0.5", '"0.5"'),
|
|
|
|
| 49 |
assert "Input should be less than or equal to 1" in service.complete.call_args.args[0]
|
| 50 |
|
| 51 |
|
| 52 |
+
async def test_json_parse_error_gives_correction_location_without_response_text(service):
|
| 53 |
+
malformed = "```json\n" + VALID + "\n```"
|
| 54 |
+
service.complete.side_effect = [(malformed, 10, 20), (VALID, 30, 40)]
|
| 55 |
+
|
| 56 |
+
result = await service.complete_structured("Evidence", Finding)
|
| 57 |
+
|
| 58 |
+
assert result.attempts == 2
|
| 59 |
+
correction = service.complete.call_args.args[0]
|
| 60 |
+
assert "invalid JSON at line 1, column 1" in correction
|
| 61 |
+
assert "```json" not in correction
|
| 62 |
+
|
| 63 |
+
|
| 64 |
@pytest.mark.parametrize("text", [
|
| 65 |
"# Analysis\nSPY looks bullish", f"```json\n{VALID}\n```", "{}", "[]",
|
| 66 |
VALID.replace('"SPY"', '"QQQ"'), VALID.replace("0.5", '"0.5"'),
|