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

}
相关推荐
npupengsir5 天前
LUXICAL文档embedding模型介绍
embedding
前端.火鸡9 天前
Embedding:机器是怎么“理解“语义的
人工智能·embedding
ZealSinger9 天前
SpringAI的MCP改STREAMABLE怎么配
协议·springai·mcp·streamable
鱼宵10 天前
Spring AI RAG 问答助手:回答带引用,AI 不再睁眼说瞎话
java·人工智能·spring·springai
旋生万物10 天前
全息结构入门:Embedding、向量库、知识图谱的统一视角
人工智能·大模型·aigc·软件工程·知识图谱·embedding·后端开发
云和数据.ChenGuang12 天前
用 Claude Code 重构遗留系统:从评估到落地的完整指南
java·人工智能·spring boot·后端·重构·springai
HySpark13 天前
从5秒延迟到轻量级推理:eGeMAPS与Embedding原型库的情感识别实践
embedding·语音情感识别·egemaps·opensmile
浪子明X14 天前
LangChain 接入 Embedding 之前:先画清文本、向量与检索器的契约
人工智能·langchain·embedding
丈剑走天涯14 天前
rag ollama + qwen3+ embedding + chroma_db 回答content为空
embedding
MicrosoftReactor14 天前
技术速递|从 Jev 到你的笔记本电脑:使用 Mobius 在 ONNX 中构建“System One”决策模型
人工智能·ai·大模型·onnx