前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >langchain4j+ONNX小试牛刀

langchain4j+ONNX小试牛刀

原创
作者头像
code4it
发布2025-03-09 15:34:40
发布2025-03-09 15:34:40
600
代码可运行
举报
文章被收录于专栏:码匠的流水账码匠的流水账
运行总次数:0
代码可运行

本文主要研究一下langchain4j结合ONNX进行得分重排

步骤

pom.xml

代码语言:javascript
代码运行次数:0
复制
<dependency>
    <groupId>dev.langchain4j</groupId>
    <artifactId>langchain4j-onnx-scoring</artifactId>
    <version>1.0.0-beta1</version>
</dependency>

下载模型

代码语言:javascript
代码运行次数:0
复制
wget https://hf-mirror.com/Xenova/ms-marco-MiniLM-L-6-v2/resolve/main/onnx/model_quantized.onnx?download=true
wget https://hf-mirror.com/Xenova/ms-marco-MiniLM-L-6-v2/resolve/main/tokenizer.json?download=true

example

代码语言:javascript
代码运行次数:0
复制
public class ONNXTest {

    /**
     * wget https://hf-mirror.com/Xenova/ms-marco-MiniLM-L-6-v2/resolve/main/onnx/model_quantized.onnx?download=true
     * wget https://hf-mirror.com/Xenova/ms-marco-MiniLM-L-6-v2/resolve/main/tokenizer.json?download=true
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        // To check the modelMaxLength parameter, refer to the model configuration file at  https://huggingface.co/Xenova/ms-marco-MiniLM-L-6-v2/resolve/main/tokenizer_config.json
        String pathToModel = System.getProperty("user.home")+"/model_quantized.onnx";
        String pathToTokenizer = System.getProperty("user.home")+ "/tokenizer.json";
        OnnxScoringModel model = new OnnxScoringModel(pathToModel, new OrtSession.SessionOptions(), pathToTokenizer, 512, false);
        List<TextSegment> segments = new ArrayList<>();
        segments.add(TextSegment.from("Berlin has a population of 3,520,031 registered inhabitants in an area of 891.82 square kilometers."));
        segments.add(TextSegment.from("New York City is famous for the Metropolitan Museum of Art."));

        String query = "How many people live in Berlin?";

        // when
        Response<List<Double>> response = model.scoreAll(segments, query);

        // then
        List<Double> scores = response.content();
        System.out.println("score1:" + scores.get(0));
        System.out.println("score2:" + scores.get(1));
        System.out.println("token count:" + response.tokenUsage().totalTokenCount());
        System.out.println("finish reason:" + response.finishReason());
    }
}

输出如下:

代码语言:javascript
代码运行次数:0
复制
score1:8.663132667541504
score2:-11.245542526245117
token count:50
finish reason:null

小结

langchain4j提供了langchain4j-onnx-scoring用于通过ONNX runtime来本地运行scoring (reranking) model。通过OnnxScoringModel的scoreAll方法可以得到文档的评分。

doc

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 步骤
    • pom.xml
    • 下载模型
    • example
  • 小结
  • doc
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档