SpringAI 内嵌模型 ONNX
- [1 下载](#1 下载)
- [2 依赖](#2 依赖)
- [3 配置](#3 配置)
- [4 代码](#4 代码)
- [5 测试](#5 测试)
1 下载
2 依赖
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.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xu</groupId>
<artifactId>spring-onnx</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-onnx</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>25</java.version>
<spring-ai.version>1.0.3</spring-ai.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-transformers</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.41</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<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>
3 配置
yml
servlet:
port: 8080
context-path: /
spring:
application:
name: spring-onnx
ai:
embedding:
transformer:
onnx:
modelUri: classpath:/onnx/all-MiniLM-L6-v2/model.onnx
modelOutputName: last_hidden_state
gpuDeviceId: -1
tokenizer:
uri: classpath:/onnx/all-MiniLM-L6-v2/tokenizer.json
cache:
enabled: true
directory: cache/onnx/all-MiniLM-L6-v2/
4 代码
java
package com.xu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class OnnxApplication {
static void main(String[] args) {
SpringApplication.run(OnnxApplication.class, args);
}
}
java
package com.xu.controller;
import lombok.AllArgsConstructor;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
@AllArgsConstructor
@RequestMapping("/embedding")
public class OnnxController {
private final EmbeddingModel model;
/**
* 文本转向量
*
* @param content 聊天内容
* @return 聊天结果
*/
@RequestMapping("/onnx/vector")
public Object vector(String content) {
Map<String, Object> map = new HashMap<>();
var vector = model.embed(content);
map.put("content", content);
map.put("model", model.getClass().getName());
map.put("modelName", model.getClass().getSimpleName());
map.put("modelType", model.getClass().getTypeName());
map.put("modelPackage", model.getClass().getPackage().getName());
map.put("modelVersion", model.getClass().getPackage().getImplementationVersion());
map.put("modelAuthor", model.getClass().getPackage().getImplementationVendor());
map.put("modelDescription", model.getClass().getPackage().getImplementationTitle());
map.put("modelUrl", model.getClass().getPackage().getSpecificationVendor());
map.put("modelLicense", model.getClass().getPackage().getSpecificationTitle());
map.put("modelCopyright", model.getClass().getPackage().getSpecificationVersion());
map.put("modelStatus", model.getClass().getPackage().getImplementationVendor());
map.put("length", vector.length);
map.put("vector", vector);
return map;
}
}
5 测试
java
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.5.6)
2025-10-23T21:50:10.732+08:00 INFO 21596 --- [spring-onnx] [ restartedMain] com.xu.OnnxApplication : Starting OnnxApplication using Java 25 with PID 21596 (D:\SourceCode\JavaLearn\spring-onnx\target\classes started by xuhya in D:\SourceCode\JavaLearn\spring-onnx)
2025-10-23T21:50:10.736+08:00 INFO 21596 --- [spring-onnx] [ restartedMain] com.xu.OnnxApplication : No active profile set, falling back to 1 default profile: "default"
2025-10-23T21:50:10.801+08:00 INFO 21596 --- [spring-onnx] [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2025-10-23T21:50:10.801+08:00 INFO 21596 --- [spring-onnx] [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2025-10-23T21:50:11.911+08:00 INFO 21596 --- [spring-onnx] [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port 8080 (http)
2025-10-23T21:50:11.931+08:00 INFO 21596 --- [spring-onnx] [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2025-10-23T21:50:11.931+08:00 INFO 21596 --- [spring-onnx] [ restartedMain] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.46]
2025-10-23T21:50:11.989+08:00 INFO 21596 --- [spring-onnx] [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2025-10-23T21:50:11.990+08:00 INFO 21596 --- [spring-onnx] [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1187 ms
2025-10-23T21:50:12.105+08:00 INFO 21596 --- [spring-onnx] [ restartedMain] o.s.a.transformers.ResourceCacheService : The class path resource [onnx/all-MiniLM-L6-v2/tokenizer.json] resource with URI schema [file] is excluded from caching
2025-10-23T21:50:12.318+08:00 INFO 21596 --- [spring-onnx] [ restartedMain] ai.djl.util.Platform : Found matching platform from: jar:file:/E:/Maven/ai/djl/huggingface/tokenizers/0.32.0/tokenizers-0.32.0.jar!/native/lib/tokenizers.properties
2025-10-23T21:50:12.653+08:00 INFO 21596 --- [spring-onnx] [ restartedMain] o.s.a.transformers.ResourceCacheService : The class path resource [onnx/all-MiniLM-L6-v2/model.onnx] resource with URI schema [file] is excluded from caching
2025-10-23T21:50:13.030+08:00 INFO 21596 --- [spring-onnx] [ restartedMain] o.s.a.t.TransformersEmbeddingModel : Model input names: input_ids, attention_mask, token_type_ids
2025-10-23T21:50:13.030+08:00 INFO 21596 --- [spring-onnx] [ restartedMain] o.s.a.t.TransformersEmbeddingModel : Model output names: last_hidden_state
2025-10-23T21:50:13.511+08:00 INFO 21596 --- [spring-onnx] [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2025-10-23T21:50:13.546+08:00 INFO 21596 --- [spring-onnx] [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path '/'
2025-10-23T21:50:13.556+08:00 INFO 21596 --- [spring-onnx] [ restartedMain] com.xu.OnnxApplication : Started OnnxApplication in 3.394 seconds (process running for 3.953)

java
{
"modelPackage": "org.springframework.ai.transformers",
"modelVersion": "1.0.3",
"modelStatus": null,
"length": 384,
"modelAuthor": null,
"modelType": "org.springframework.ai.transformers.TransformersEmbeddingModel",
"content": "你好",
"modelCopyright": null,
"modelDescription": "spring-ai-transformers",
"modelName": "TransformersEmbeddingModel",
"model": "org.springframework.ai.transformers.TransformersEmbeddingModel",
"vector": [
0.02343683,
0.08762656,
0.46727753,
0.006662041,
0.08734512,
-0.0237237,
0.6776009,
0.08279213,
0.19696343,
-0.2474854,
0.48936588,
-0.50837773,
0.5859534,
0.0016979873,
-0.28368798,
-0.40596563,
0.1884622,
-0.02893056,
-0.18570054,
0.07782484,
-0.3777804,
0.26379645,
0.31509066,
0.13158788,
-0.41920042,
0.23772626,
-0.258385,
0.10639739,
0.299919,
-0.2270475,
0.05660044,
0.050097413,
-0.07569961,
-0.5688242,
0.2252784,
0.14336713,
-0.09314251,
-0.1857754,
-0.3260218,
0.2012273,
-0.39426142,
-0.4168207,
0.39458168,
-0.5235817,
0.2067312,
0.18824029,
-0.33371818,
-0.12888348,
0.25557804,
-0.083815955,
-0.06923346,
1.7691031E-4,
-0.2905802,
0.24975006,
0.29135913,
0.11453216,
-0.011198841,
-0.2813206,
-0.24109977,
-0.5403303,
-0.097307846,
0.19215679,
0.13671952,
0.004049033,
-0.16162445,
0.049333755,
0.07281785,
-0.05952841,
0.34301934,
0.18180086,
0.14120728,
-0.08963088,
0.12585503,
-0.35094154,
-0.006964743,
0.010616228,
0.76091987,
0.3534711,
0.03441772,
0.43357548,
-0.12754633,
-0.026704151,
-0.13369814,
0.3868227,
-0.69556093,
-0.05773553,
-0.46564227,
0.2115674,
0.11294371,
0.30452234,
-0.16527346,
0.047342196,
0.043399025,
-0.0021228753,
-0.8792201,
0.102754846,
0.20576744,
-0.5005492,
-0.49639094,
0.6637553,
0.27751437,
0.19987479,
0.28327656,
-0.16615942,
-0.36877495,
-0.19726619,
0.06972954,
0.048923187,
0.2869923,
0.040557604,
-0.24672648,
-0.68537754,
-0.094700076,
-0.33511552,
0.31731382,
0.3132615,
-0.048334103,
0.18854639,
-0.42317948,
0.18848293,
0.33576748,
0.27275294,
-0.14877044,
-0.24084699,
-0.32112318,
-0.44178855,
-0.06404343,
-1.7762377E-33,
-0.44600853,
0.1611438,
-0.14676858,
-0.34425494,
-0.3532662,
0.53610724,
-0.10688757,
0.06694295,
-0.27119362,
0.025012277,
-0.041192394,
0.21542323,
-0.28310975,
-0.39334315,
-0.08262417,
-0.04368668,
-0.22893336,
0.05886013,
0.49458933,
0.021131746,
-0.06713415,
-0.48626983,
3.0223653E-4,
0.12450987,
2.702158E-4,
-0.39675117,
0.12332257,
-0.17871189,
-0.448998,
0.056852646,
-0.029862035,
0.05339087,
-0.07675255,
0.20126203,
-0.3195867,
-0.2423491,
0.5379797,
0.28294447,
-0.10426624,
0.37720814,
0.21029016,
-0.2662666,
-0.7171446,
-0.49032113,
0.2827458,
0.26780257,
-0.09694694,
-0.5018139,
-0.1177226,
-0.0050436854,
0.06094081,
0.34807912,
-0.64833754,
0.5589546,
0.48746812,
-0.056296237,
0.39923164,
-0.12987405,
-0.3695327,
-0.26740387,
0.06680949,
-0.36498117,
-0.15486097,
-0.22323893,
-0.5497947,
0.016928513,
0.24453883,
0.0425144,
0.12597032,
-0.28333813,
-0.37821695,
0.21431178,
0.44229633,
0.32369018,
-0.38414,
-0.27648276,
-0.23844285,
0.057112947,
0.4204495,
-0.095233604,
0.2369657,
-0.14185676,
0.19784412,
-0.53305143,
0.40239045,
0.28219426,
0.028366677,
-0.6645464,
-0.056536026,
-0.24196622,
-0.6879737,
-0.42562604,
0.6032052,
0.16719256,
-0.37094754,
-6.9693774E-33,
0.03334776,
0.28022447,
0.38790628,
-0.32675046,
-0.07047665,
-0.3296865,
0.2195594,
0.54308283,
0.0716847,
0.37615535,
0.31447393,
-0.07753472,
-0.25349307,
0.28505346,
0.09891618,
0.4205811,
0.3716994,
0.3685578,
0.003856957,
0.12483545,
0.007776603,
-0.59751886,
-0.44894615,
0.16061682,
-0.08751269,
0.47611332,
0.14247757,
-8.789301E-4,
-0.10702883,
0.13482541,
-0.056214813,
0.11572923,
-0.15180114,
0.085722685,
-0.25033408,
-0.18144892,
-0.36725488,
0.06579246,
-0.21438788,
0.43983305,
-0.04696831,
-0.066859365,
0.44310692,
0.5884305,
0.03468699,
0.27791,
-0.24718562,
0.054443184,
0.051003046,
-0.09252158,
-0.1189761,
-0.30506495,
0.066067204,
-0.13125275,
0.09540975,
0.15626487,
0.07162977,
0.113467336,
0.057863533,
0.09977088,
-0.33089894,
0.056722566,
-0.13639916,
0.380983,
-0.025380611,
-0.047991037,
-0.17019905,
-0.29330885,
0.019013897,
-0.2413308,
0.75331867,
0.12772845,
-0.6135099,
0.0024178368,
-0.4514789,
0.32686836,
-0.016809352,
0.37733072,
0.13927627,
0.21178877,
-0.044724572,
0.25466985,
0.15880512,
-0.21384417,
-0.17937246,
-0.57107306,
0.16716002,
0.13885054,
-0.12236281,
-0.08600644,
0.5180234,
0.4947607,
0.085024156,
-0.45527482,
0.044204026,
-9.027177E-8,
-0.069826305,
-0.23688585,
0.37564105,
-0.17352962,
0.29182756,
0.33645248,
-0.49530286,
0.26038873,
0.64728296,
-0.040468782,
0.56833476,
0.24429092,
-0.13851134,
0.6352078,
0.08770267,
0.051881906,
0.124625295,
0.5220107,
0.15340744,
-0.48510516,
0.49753255,
-0.201727,
-0.07491118,
-0.028699867,
-0.3118285,
0.38222334,
-0.5868065,
0.2980773,
-0.38717964,
-0.19433047,
0.011136763,
-0.35680646,
0.009474777,
0.034545265,
-0.13141194,
-0.32497257,
-0.11836141,
0.29064637,
0.027543083,
0.14105615,
-0.14284812,
0.035636846,
0.4313984,
-0.027813286,
0.12126367,
-0.24476744,
0.3799036,
0.43196177,
-0.20432082,
-0.17484045,
-0.09188651,
0.4474511,
0.21525286,
0.30090797,
0.29460397,
0.32016733,
0.08721432,
0.25081924,
0.14783166,
0.10748446,
0.41511208,
0.07636167,
0.062491305,
0.009732217
],
"modelUrl": null,
"modelLicense": null
}
