Spring AI使用Ollama

一、Maven完整配置

java 复制代码
<?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.14</version>
        <relativePath/>
    </parent>
    <groupId>org.panda</groupId>
    <artifactId>spring_ollama_ai</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring_ollama_ai</name>
    <description>spring_ollama_ai</description>

    <properties>
        <java.version>17</java.version>
        <lombok.version>1.18.42</lombok.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- Spring AI Ollama -->
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.16.1</version>
        </dependency>


    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-configuration-processor</artifactId>
                        </path>
                        <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>

    <repositories>
        <!-- Spring Milestones 仓库 -->
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>

        <!-- Spring Snapshots 仓库 -->
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <releases>
                <enabled>false</enabled>
            </releases>
        </repository>
    </repositories>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.ai</groupId>
                <artifactId>spring-ai-bom</artifactId>
                <version>1.0.0-M6</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>


</project>

二、yaml配置

java 复制代码
spring:
  application:
    name: spring_ollama_ai
  ai:
    ollama:
      base-url: http://localhost:11434
      timeout: 120s
      chat:
        options:
          model: iot-deepseek:latest
          keep_alive: 30m
          temperature: 0.7
          top-p: 0.9
          num-ctx: 4096

三、测试Controller

java 复制代码
package org.panda.controller;

import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.beans.factory.annotation.Autowired;
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;

@RestController
@RequestMapping("/chat")
public class ChatController {

    @Autowired
    private OllamaChatModel chatModel;


    @GetMapping("/stream")
    public Flux<String> chatStream(@RequestParam String message) {
        return chatModel.stream(message);
    }
}
相关推荐
在繁华处2 分钟前
Java从零到熟练(十):JVM基础与性能优化
java·jvm·性能优化
Database_Cool_3 分钟前
数据仓库弹性扩缩容怎么实现?阿里云 AnalyticDB MySQL Serverless 弹性架构详解
数据库·人工智能·阿里云
羑悻的小杀马特3 分钟前
从 Claude Code 到 QClaw:AgentSkills 规范的跨生态实践与工程取舍!
人工智能·自动化·agent·skills·openclaw·qclaw
zhangfeng11333 分钟前
超算中心 高性能计算 htc命令module use的作用
人工智能·机器学习
Rocky Ding*4 分钟前
深入浅出完整解析AIGC时代中GAN(Generative Adversarial Network)系列模型核心基础知识(下篇)
论文阅读·人工智能·深度学习·机器学习·aigc·gan·ai-native
Demon1_Coder5 分钟前
Day1-SpringAI-1.0.0版本
java·开发语言·前端
软件开发技术深度爱好者6 分钟前
当前的AI或者说大语言模型与《中庸》思想关联意义的探讨
人工智能·学习心得
装不满的克莱因瓶7 分钟前
什么是特征分解?从数学定义到现实问题的映射
人工智能·数学·算法·机器学习·ai·特征分解
Dylan的码园9 分钟前
告别AI空交互!ToDesk AI实测:能自主操控电脑的落地式AI生产力助手
人工智能·电脑
呆呆敲代码的小Y9 分钟前
【最新Codex教程】 | 安装、入门和快速使用,适合新手
人工智能·gpt·ai·llm·openai·agent·codex