Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

将AI返回内容中的markdown代码块转换为普通json #160

Merged
merged 5 commits into from
Jul 24, 2024

Conversation

yanyao2333
Copy link
Contributor

在生成记忆时,部分LLM有概率返回markdown代码块格式而非单纯地json格式,导致无法解析并创建记忆.

这个pr尝试移除掉代码块标记,并在生成记忆错误时打印log

@idootop
Copy link
Owner

idootop commented Jul 23, 2024

多谢 PR,这里没有严格校验 AI 输出的内容格式,考虑到 LLM 返回的格式可能多种多样,解析失败默认按异常处理。

Copy link
Owner

@idootop idootop left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️ 感谢 PR,你的 fix 很有效,但仍有一些可优化的空间,暂时就先不合并了哈。

这个问题的本质是怎样尽可能多的兼容 LLM 输出的各种异常 case,

也欢迎你继续提出一些建议和改进方法,咱们一起讨论。

@yanyao2333
Copy link
Contributor Author

感谢您的 code review!

  1. 当时修改这部分代码时只想着针对这一种 case, 代码量较少, 所以没做过多工程和代码风格方面的考量, 这确实是我的问题.
  2. 大部分指令服从性好的 LLM 都能够输出正确的 json 格式, 少部分模型会稳定输出 markdown code block, 这导致使用这种模型时记忆完全不可用, 所以考虑通过这种方式来纠正这种情况.
  3. 我觉得再通过优化 prompt (eg. 增加样本, 完善要求) 去适应其他的极端情况并不划算, 因为 LLM 的输出本身就具有随机性, 对于其他的更极端的偶然情况, 直接丢弃就可以.
  4. 通过正则表达式来处理 json 确实不错, 可以考虑换用这种方式.

另外这个 pr 用不用 close? 还是我继续优化一下?

@idootop
Copy link
Owner

idootop commented Jul 23, 2024

了解你说的场景了,我觉得还是有必要兼容下的。

如果你能换用正则匹配,并封装成一个统一的解析方法的话,我可以重新考虑合并你的这个 PR。一个 python 的例子:

图片

@yanyao2333
Copy link
Contributor Author

这次修改我把jsonDecode jsonEncode放到了/utils/parse, 并且将清洗json代码封装为一个函数

@idootop
Copy link
Owner

idootop commented Jul 24, 2024

已合并

@idootop idootop closed this Jul 24, 2024
@idootop idootop reopened this Jul 24, 2024
@idootop idootop merged commit 5d455e2 into idootop:main Jul 24, 2024
@yanyao2333 yanyao2333 deleted the fix-code-block-format branch July 24, 2024 07:32
@bwnjnOEI
Copy link

bwnjnOEI commented Jan 1, 2025

遇到返回md的内容就没语音了
image

image

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.

3 participants