Skip to content

fix: 自动提取 LLM 返回内容中的 JSON,避免 pydantic 校验失败#37

Open
chinaguole wants to merge 1 commit into
protectai:mainfrom
chinaguole:prog.le
Open

fix: 自动提取 LLM 返回内容中的 JSON,避免 pydantic 校验失败#37
chinaguole wants to merge 1 commit into
protectai:mainfrom
chinaguole:prog.le

Conversation

@chinaguole

Copy link
Copy Markdown

变更内容

  • 在 LLMs.py 的 _validate_response 方法中,增加了自动提取 JSON 的逻辑。
  • 解决了 LLM 返回内容包含非 JSON 部分(如 标签等)时,pydantic 校验失败的问题。

变更原因

  • 某些 LLM 返回内容可能包含额外的注释、标签或 markdown,导致 pydantic 的 model_validate_json 解析失败。
  • 通过正则提取第一个合法 JSON,有效提升了兼容性和健壮性。

测试说明

  • 本地测试通过,LLM 返回内容包含非 JSON 部分时,依然可以正常解析和校验。

如有需要可进一步完善单元测试。

## 变更内容

- 在 LLMs.py 的 _validate_response 方法中,增加了自动提取 JSON 的逻辑。
- 解决了 LLM 返回内容包含非 JSON 部分(如 <think> 标签等)时,pydantic 校验失败的问题。

## 变更原因

- 某些 LLM 返回内容可能包含额外的注释、标签或 markdown,导致 pydantic 的 model_validate_json 解析失败。
- 通过正则提取第一个合法 JSON,有效提升了兼容性和健壮性。

## 测试说明

- 本地测试通过,LLM 返回内容包含非 JSON 部分时,依然可以正常解析和校验。

---

如有需要可进一步完善单元测试。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant