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

}
相关推荐
Lee_jerome1 天前
从 PyTorch 权重到 RK3588 板端推理:ResNet18 二分类模型完整部署教程
pytorch·边缘计算·rk3588·模型部署·onnx·resnet18·int8量化
屋昂仼2 天前
企业级 RAG 概念篇:切块(Chunking)、 翻译(Embedding)和向量数据库、混合检索与 Rerank 重排序、权限前置与性能优化
embedding
莫逸风5 天前
【AgentScope 2.0】10-总结回顾详解
java·ai·agent·springai·agentscope
人道领域6 天前
【0-1的agent进阶篇】RAG:从Embedding到检索增强生成的底层逻辑
java·embedding·agent·rag
番茄炒鸡蛋加糖6 天前
主流 AI 框架 + RAG 落地实战
人工智能·rag·springai
小白说大模型8 天前
OpenClaw 测试策略实战:AI Agent 自动化测试体系搭建与落地
人工智能·重构·开源·prompt·embedding
Joy T9 天前
Agent 开源项目全景解析(下):LlamaIndex、Dify、FastGPT 与真实工程选型
langchain·开源·框架·agent·springai·langgraph·mcp
DBA小马哥9 天前
向量数据库入门到进阶:Embedding、ANN算法与RAG落地的关键术语
数据库·算法·embedding
小田学Python9 天前
RAG和Embedding技术解析:从理论到实践的探索
大模型·embedding·知识库·向量检索·rag
小白说大模型10 天前
Prompt 工程的数学直觉:Embedding 空间中的 Prompt 优化的底层原理
人工智能·mysql·prompt·embedding