Repository navigation
fix(analysis): 更稳健地从 LLM 输出提取 JSON,并对截断输出用 json-repair 兜底 - #4
Merged
Merged
Conversation
llm.py 与 judge.py 各写了一份相同的 _extract_json(find("{")/rfind("}")),
现合并为 goodprice/analysis/json_extract.py。新实现先用正则剥离 ``` 围栏,
再用括号配平扫描取第一个完整对象:能正确跳过字符串内的括号与转义,在输出
含多个对象时也能取到正确范围,并容忍尾随逗号。
验证:新增 tests/test_json_extract.py;与 tests/test_llm.py、tests/test_judge.py
一并运行 43 passed。
LLM 输出被 max_tokens 截断时会得到不完整 JSON,纯括号配平无法解析。标准解析 全部失败后,若安装了 json-repair 则交给它修复;未安装时行为不变(仍抛 ValueError)。json-repair 对纯文本返回空串,不会把解释文字强行解析成对象。 同步更新 uv.lock / requirements.lock(上游 CI 会跑 uv lock --check + --locked)。 验证:uv lock --check 通过;新增「截断 JSON 兜底」与「纯文本不误判」用例, test_json_extract / test_llm / test_judge 共 45 passed。
Owner
|
感谢你这次的完善!统一 JSON 提取逻辑并补充截断输出兜底,确实提升了 LLM 输出异常时的稳定性,新增测试也很完整。我已检查,改动没有发现问题,感谢贡献。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
goodprice/analysis/llm.py与judge.py各有一份_extract_json,用find("{")/rfind("}")截取 JSON。当 LLM 输出里出现多个对象、或字符串内部含大括号时会取错范围;输出被 max_tokens 截断时直接失败。改动
goodprice/analysis/json_extract.py,合并两处重复实现:json-repair则修复截断/畸形 JSON;未安装时行为不变(仍抛ValueError)。pyproject.toml增加json-repair>=0.30,并同步uv.lock/requirements.lock。验证
uv lock --check通过tests/test_json_extract.py(围栏、前后夹文字、字符串内大括号、多对象、尾随逗号、截断兜底、纯文本不误判)uv run --locked pytest -q→ 217 passed