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

}
相关推荐
做cv的小昊1 天前
【大模型算法自学笔记07】NLP基础知识(1.7 Embedding)
人工智能·笔记·算法·计算机视觉·自然语言处理·transformer·embedding
派勤电子1 天前
ai盒子算子兼容矩阵:五家平台的支持差异表
onnx·npu·ai盒子·算子兼容·模型移植
Forget the Dream1 天前
Spring AI 2.0 系列(四):Starter 与 OpenAI-compatible:一个不绑死厂商的模型环境
java·人工智能·spring·springai
电科_银尘2 天前
【大模型算法与工程 51】检索基础:Embedding、向量索引与BM25
算法·机器学习·embedding
长不大的杨瑞杰2 天前
Spring AI 企业级 RAG 实战:检索、重排、结构化输出与提示注入防护
java·人工智能·spring·llm·rag·springai
Forget the Dream15 天前
Spring AI 2.0 系列(二):第一个 ChatClient——从注入到一次完整调用
java·人工智能·spring boot·ai·springai
Forget the Dream15 天前
Spring AI 2.0 系列(三):ChatClient 与 ChatModel:Spring AI 2.0 的两层职责
java·人工智能·spring·springai
电科_银尘16 天前
【大模型算法与工程 14】Tokenizer与Embedding:文本如何变成模型输入
算法·embedding
LiaoWL12316 天前
从零搭一个 RAG 知识库(二):换上语义 Embedding——BGE 实测与阈值重校准
python·大模型·numpy·embedding·向量检索·rag
装不满的克莱因瓶19 天前
【从零搭建】AI大模型之ReactAgent智能体对接MCP实战
java·人工智能·ai·springai·mcp·springaialibaba