关于 InMemoryEmbeddingStore 读取文件报错问题

问题

使用了 LangChain4j 的 InMemoryEmbeddingStore,并使用了serializeToFile 将数据持久化到了文件;再重新从文件构建 InMemoryEmbeddingStore 时,则报错:

Exception in thread "Thread-3" java.lang.RuntimeException: com.fasterxml.jackson.databind.JsonMappingException: Unrecognized character escape '鏂' (code 37826 / 0x93c2)
    at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 1, column: 23117814] (through reference chain: dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore["entries"]->java.util.concurrent.CopyOnWriteArrayList[3110]->dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore$Entry["embedded"]->dev.langchain4j.data.segment.TextSegment["text"])
    at dev.langchain4j.store.embedding.inmemory.JacksonInMemoryEmbeddingStoreJsonCodec.fromJson(JacksonInMemoryEmbeddingStoreJsonCodec.java:35)

然而,我直接在 idea 中运行又不报错,只有运行通过 mvn clean package 生成的 jar 包才报错。

解决

排查了好久,最终发现是编码问题导致。

java -Dfile.encoding=UTF-8 -jar your-app.jar

运行时指定文件编码即可解决。

核心原因

查看 InMemoryEmbeddingStore#fromFile(Path) 方法:

    public static InMemoryEmbeddingStore<TextSegment> fromFile(Path filePath) {
        try {
            String json = new String(Files.readAllBytes(filePath));
            return fromJson(json);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

发现其使用了默认的 String 构造方法,具体实现为:

    public String(byte[] bytes) {
        this(Charset.defaultCharset(), bytes, 0, bytes.length);
    }

这里就用到了 Charset.defaultCharset(),因此当在 idea 中运行时序列化的文件,再在 mvn clean package 生成的 jar 包中读取该文件,可能导致出错,统一使用 utf-8 编码即可。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容