[Question]: `SimpleChatStore` encoding for `persist`.

[Question]: `SimpleChatStore` encoding for `persist`. 这个问题出现在 LlamaIndex 的 SimpleChatStore 将含波斯语/阿拉伯语等非 ASCII 文本持久化为 JSON 文件时,默认输出成了 \u0633\u0644\u062

快速结论:[Question]: `SimpleChatStore` encoding for `persist`. 这个问题出现在 LlamaIndex 的 SimpleChatStore 将含波斯语/阿拉伯语等非 ASCII 文本持久化为 JSON 文件时,默认输出成了 \u0633\u0644\u0627\u0645 这类转义字符,导致文件体积明显膨胀。优先排查从 SimpleChatStore.persistBaseComponent.to_json 这条序列化链路上,json.dumps 是否使用了 ensure_ascii=False

适用环境:LlamaIndex 的 SimpleChatStore 及相关序列化基类 BaseComponent;依赖 Python 标准库 json。Issue 中未提及操作系统、Python 版本、CUDA、显卡等具体环境,不要额外补充。

最快修复方案:暂无确认的一步修复方案。可优先尝试修改 BaseComponent.to_json,让 json.dumps 使用 ensure_ascii=False;只修改 SimpleChatStore.persist 并不能解决问题,因为 self.json() 在进入该方法前已经完成转义。

注意事项:Question 讨论中给出的修改方案属于建议性补丁,并未在 Issue 中明确标记为经过验证的正式修复。monkey-patch 需要覆盖 SimpleChatStoreBaseChatStoreBaseComponent 整条继承链;直接修改 BaseComponent 会影响所有依赖该基类序列化的组件,改动前需要评估对旧数据读取兼容性的影响。

问题场景

用户在使用 LlamaIndex 的 SimpleChatStore 保存聊天记忆时,聊天内容包含波斯语等非 ASCII 字符。调用 persist 后,JSON 文件中的 Unicode 字符被转义为 \uXXXX 形式,导致文件占用空间比预期大,担心中长期扩展性。

报错原文

[Question]: `SimpleChatStore` encoding for `persist`.

The issue is that while the contents are correctly parsed with `utf-8` encoding afterwards,
saving them to a json string occupies a lot of space as it uses unicode encoding by default.

For instance the word "سلام" which is 4 characters in persian/arabic,
is mapped to `\u0633\u0644\u0627\u0645`.

原因分析

可能原因:SimpleChatStorepersist 方法在写文件时调用 json.dumps(self.json()),而 self.json() 最终由 BaseComponent.to_json 生成。Python json.dumpsensure_ascii 参数默认是 True,因此所有非 ASCII 字符都会被转义成 \uXXXX。用户最初提出的补丁只给 persist 增加 encoding 参数并不足以解决问题,因为在调用 persist 之前,self.json() 返回的字符串已经完成了 Unicode 转义。

环境排查

celebrityanime
celebrityanime
文章: 18303

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注