spring-boot + ollama + 大模型集成(以deepseek为演示)
注:该文章不包括 ollama 部署
ai-ollama pom文件内容
<?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>
<groupId>com.minimalism</groupId>
<artifactId>ai-ollama</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>17</java.version>
<project-module.version>0.0.1-SNAPSHOT</project-module.version>
<project-module-ai.version>0.0.1-SNAPSHOT-AI</project-module-ai.version>
<project.Encoding>UTF-8</project.Encoding>
<spring-boot.version>3.2.5</spring-boot.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<project.build.sourceEncoding>${project.Encoding}</project.build.sourceEncoding>
<project.reporting.outputEncoding>${project.Encoding}</project.reporting.outputEncoding>
<hutool.version>5.8.25</hutool.version>
<lombok.version>1.18.26</lombok.version>
<ai.ollama.version>1.0.0</ai.ollama.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>io.springboot.ai</groupId>
<artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
<version>${ai.ollama.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>6.1.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!--远程仓库-->
<repositories>
<repository>
<id>aliyun-public</id>
<url>https://maven.aliyun.com/nexus/content/groups/public/</url>
</repository>
<repository>
<id>aliyun</id>
<url>https://maven.aliyun.com/repository/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>snapshots-repo</id>
<url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>${project.Encoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>test</id>
<properties>
<spring.profiles.active>test</spring.profiles.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>dev</id>
<properties>
<spring.profiles.active>dev</spring.profiles.active>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<spring.profiles.active>prod</spring.profiles.active>
</properties>
</profile>
</profiles>
</project>
核心配置
配置application.yml文件
spring:
profiles:
#@spring.profiles.active@==>占位符
active: @spring.profiles.active@
ai:
ollama:
base-url: http://<ollama:ip>:<ollama:port>
chat:
model: deepseek-r1:latest
server:
port: 8099
AiOllamaController.java 文件
package com.minimalism.ai.ollama.controller;
import cn.hutool.extra.spring.SpringUtil;
//import io.swagger.v3.oas.annotations.Operation;
//import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.ai.ollama.OllamaChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
/**
* @Author yan
* @Date 2025/3/1 12:31:33
* @Description
*/
@RequestMapping("/ai/ollama")
@RestController //@Tag(name = "Ai-Ollama")
public class AiOllamaController {
private OllamaChatClient ollamaChatClient = SpringUtil.getBean(OllamaChatClient.class);
//@Operation(summary = "[String] ollama chat v1")
@GetMapping("/chat/v1")
public String ollamaChatV1(@RequestParam(value = "msg") String msg) {
String call = ollamaChatClient.call(msg);
return call;
}
//@Operation(summary = "[Flux<String>] ollama chat v2")
@GetMapping(value = "/chat/v2", produces = "text/html;charset=UTF-8")
public Flux<String> ollamaChatV2(@RequestParam(value = "msg") String msg) {
return ollamaChatClient.stream(msg);
}
}