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

}
相关推荐
ShallWeL12 小时前
【机器学习】(30)—— 嵌入空间
人工智能·神经网络·机器学习·embedding
孙启超1 天前
【AI应用开发】 RAG篇(四):Prompt 工程与进阶技术
人工智能·llm·embedding·rag·向量化·chunking·文档切分
周航宇JoeZhou1 天前
JP3-2-3-MyItem智能业务
java·多智能体·rag·智能体·springai·mcp·skills
Cachel wood1 天前
hands-on-modern-rl:动手学强化学习 贝尔曼方程
开发语言·网络·python·学习·embedding
ShallWeL2 天前
【机器学习】(29)—— Embedding
人工智能·神经网络·机器学习·embedding
佛祖让我来巡山2 天前
SpringAI保姆级实战教程
spring ai·springai·springai教程·springai入门
周航宇JoeZhou3 天前
JP3-2-1-MyItem项目简介
java·ai·springboot·环境搭建·项目·管理·springai
Muscleheng3 天前
SpringBoot 集成 DeepSeek 实现 RAG 文档问答
java·spring boot·ai·springai
sugar__salt3 天前
向量数据库从零到实战:用 Milvus + Zilliz 构建 AI 日记语义检索系统
数据库·人工智能·embedding·milvus·rag·zilliz
橙臣程4 天前
深度学习11——Tokenization、embedding、位置编码
人工智能·深度学习·embedding