SpringAi 加载 ONNX Embedding

SpringAi 加载 ONNX Embedding

  • [1 依赖](#1 依赖)
  • [2 配置](#2 配置)
  • [3 Spring托管](#3 Spring托管)
  • [4 本地加载](#4 本地加载)
  • [5 全部代码](#5 全部代码)

1 依赖

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>3.5.9</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<groupId>com.xu</groupId>
	<artifactId>spring-openai-onnx</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>spring-openai-onnx</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>25</java.version>
		<spring-ai.version>1.1.2</spring-ai.version>
	</properties>

	<dependencies>

		<!-- SpringBoot 前端请求 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<!-- transformers 模型 -->
		<dependency>
			<groupId>org.springframework.ai</groupId>
			<artifactId>spring-ai-starter-model-transformers</artifactId>
		</dependency>

		<!-- hutool -->
		<dependency>
			<groupId>cn.hutool</groupId>
			<artifactId>hutool-all</artifactId>
			<version>5.8.42</version>
		</dependency>

		<!-- devtools -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>

		<!-- lombok -->
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>

		<!-- test -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.ai</groupId>
				<artifactId>spring-ai-bom</artifactId>
				<version>${spring-ai.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<annotationProcessorPaths>
						<path>
							<groupId>org.projectlombok</groupId>
							<artifactId>lombok</artifactId>
						</path>
					</annotationProcessorPaths>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<excludes>
						<exclude>
							<groupId>org.projectlombok</groupId>
							<artifactId>lombok</artifactId>
						</exclude>
					</excludes>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>

2 配置

yml 复制代码
spring:
  application:
    name: spring-openai-onnx
  ai:
    embedding:
      transformer:
        onnx:
          model-Uri: classpath:onnx/bge-small-zh-v1.5.onnx
          model-output-name: last_hidden_state
        tokenizer:
          uri: classpath:onnx/bge-small-zh-v1.5-tokenizer.json

3 Spring托管

java 复制代码
@Resource
private EmbeddingModel embeddingModel;

@Test
void spring() {
    String text = "测试一下内嵌模型";
    long t1 = System.currentTimeMillis();
    float[] embed = embeddingModel.embed(text);
    long t2 = System.currentTimeMillis();
    log.info("用时:{}", (t2 - t1));
    log.info("文本:{}", text);
    log.info("embed:{}", JSONUtil.toJsonPrettyStr(embed));
}

4 本地加载

java 复制代码
@Test
void local() throws Exception {
    TransformersEmbeddingModel model = new TransformersEmbeddingModel();
    model.setTokenizerResource("classpath:onnx/bge-small-zh-v1.5-tokenizer.json");
    model.setModelResource("classpath:onnx/bge-small-zh-v1.5.onnx");
    //model.setResourceCacheDirectory("classpath:onnx/cache");
    model.setTokenizerOptions(Map.of("padding", "true"));
    model.setModelOutputName("last_hidden_state");
    model.afterPropertiesSet();

    String text = "测试一下内嵌模型";
    long t1 = System.currentTimeMillis();
    float[] embed = model.embed(text);
    long t2 = System.currentTimeMillis();
    log.info("用时:{}", (t2 - t1));
    log.info("文本:{}", text);
    log.info("embed:{}", JSONUtil.toJsonPrettyStr(embed));
}

5 全部代码

java 复制代码
package com.xu;

import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.transformers.TransformersEmbeddingModel;
import org.springframework.boot.test.context.SpringBootTest;

import org.junit.jupiter.api.Test;

import cn.hutool.json.JSONUtil;

import java.util.Map;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@SpringBootTest
public class OnnxTest {

    @Resource
    private EmbeddingModel embeddingModel;

    @Test
    void spring() {
        String text = "测试一下内嵌模型";
        long t1 = System.currentTimeMillis();
        float[] embed = embeddingModel.embed(text);
        long t2 = System.currentTimeMillis();
        log.info("用时:{}", (t2 - t1));
        log.info("文本:{}", text);
        log.info("embed:{}", JSONUtil.toJsonPrettyStr(embed));
    }

    @Test
    void local() throws Exception {
        TransformersEmbeddingModel model = new TransformersEmbeddingModel();
        model.setTokenizerResource("classpath:onnx/bge-small-zh-v1.5-tokenizer.json");
        model.setModelResource("classpath:onnx/bge-small-zh-v1.5.onnx");
        //model.setResourceCacheDirectory("classpath:onnx/cache");
        model.setTokenizerOptions(Map.of("padding", "true"));
        model.setModelOutputName("last_hidden_state");
        model.afterPropertiesSet();

        String text = "测试一下内嵌模型";
        long t1 = System.currentTimeMillis();
        float[] embed = model.embed(text);
        long t2 = System.currentTimeMillis();
        log.info("用时:{}", (t2 - t1));
        log.info("文本:{}", text);
        log.info("embed:{}", JSONUtil.toJsonPrettyStr(embed));
    }

}
相关推荐
深色風信子13 小时前
SpringAI Rag 文件读取
rag·springai·springai rag
二进制_博客1 天前
SpringAI智能助手案例
大模型·springai
Haooog2 天前
Spring AI 与 LangChain4j 对比
人工智能·大模型·springai·langchain4j
abcd_zjq3 天前
VS2022+QT6.9配置ONNXruntime GPU、CUDA、cuDNN(附官网下载链接)(GPU开启代码示例)
qt·visual studio·cuda·onnx
、BeYourself3 天前
项目案例-构建 AI 驱动的文档搜索系统-2
java·人工智能·springai·项目案例
Lkygo4 天前
Embedding 和 Reranker 模型
人工智能·embedding·vllm·sglang
love3981467795 天前
Embedding,rerank,lora区别
embedding
CodeCaptain6 天前
huggingface.co下载Qwen3-Embedding模型的步骤
经验分享·embedding·dify
liuc03176 天前
调用embedding生成向量并存储到milvus中,进行查询
embedding·milvus