当前位置: 首页 > article >正文

langchain4j+ONNX小试牛刀

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

步骤

pom.xml

<dependency>
    <groupId>dev.langchain4j</groupId>
    <artifactId>langchain4j-onnx-scoring</artifactId>
    <version>1.0.0-beta1</version>
</dependency>

下载模型

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

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());
    }
}

输出如下:

score1:8.663132667541504
score2:-11.245542526245117
token count:50
finish reason:null

小结

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

doc

  • scoring-reranking-models

http://www.kler.cn/a/579456.html

相关文章:

  • STM32如何精准控制步进电机?
  • 解决CentOS 8.5被恶意扫描的问题
  • Ubuntu切换lowlatency内核
  • LeetCode1871 跳跃游戏VII
  • css错峰布局/瀑布流样式(类似于快手样式)
  • pyqt联合designer的运用和设置
  • 【人工智能】卷积神经网络的奥秘:深度学习的视觉革命
  • Github 2025-03-09 php开源项目日报Top10
  • Android MXPlayer-v1.86.0-wushidi专业版[原团队最后一个版本]
  • 视觉-语言模型-出发点CLIP--(精读论文)
  • 解决:Word 保存文档失败,重启电脑后,Word 在试图打开文件时遇到错误
  • 【c语言概述、数据类型、运算符与表达式精选题】
  • ROS云课基础题库-01C++案例-甜甜圈
  • C++ 学生成绩管理系统
  • Docker Desktop 安装与使用详解
  • 牛客python蓝桥杯11-32(自用)
  • 全域网络安全防御 健全网络安全防护体系
  • 数据科学如何助力游戏增长
  • 手写LLM(2): 模型训练及代码详解
  • Mybatis Generator 使用手册