1、pom文件
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
</dependency>
</dependencies>
2、ollama 需又向量模型,application.yml 内容如下
server:
port: 10005
spring:
application:
name: spring-ai-alibaba-ollama-chat-model-example
ai:
ollama:
base-url: http://localhost:11434
chat:
model: llama3.2:1b
embedding:
model: bge-m3
3、测试代码如下
package com.alibaba.cloud.ai.example.chat.ollama.controller;
import org.springframework.ai.embedding.Embedding;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.embedding.EmbeddingResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
@RestController
public class EmbeddingController {
private final EmbeddingModel embeddingModel;
@Autowired
public EmbeddingController(EmbeddingModel embeddingModel) {
this.embeddingModel = embeddingModel;
}
@GetMapping("/ai/embedding")
public Map embed(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
EmbeddingResponse embeddingResponse = this.embeddingModel.embedForResponse(List.of(message));
List<Embedding> embeddings = embeddingResponse.getResults();
embeddings.forEach(k -> {
float[] ff = k.getOutput();
System.out.println(ff.length);
});
return Map.of("embedding", embeddingResponse);
}
}
测试效果如下