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);
    }
}
相关推荐
一点一木4 分钟前
第 23 届 ChinaJoy 刚结束,我把 965 个展台做成了 3D 云展馆
前端·人工智能
2601_955759624 分钟前
ClaudeAPI成本中心与业务标签设计指南
android·java·数据库
茨球是只猫10 分钟前
A 股 AI 量化全链路系统技术拆解:分层架构、双引擎验证与低换手实盘闭环
人工智能·机器学习·架构·量化交易
饼干哥哥13 分钟前
Codex 必改的8 个基础配置
前端·人工智能·后端
ifenxi爱分析16 分钟前
GEO市场规模有多大?2026—2030年中国GEO市场规模预测
大数据·人工智能
码云之上20 分钟前
Context Engineering:让 Agent 在当前步骤看到正确的事实
前端·人工智能·前端工程化
百度Geek说26 分钟前
面向 Coding Agent 的多仓库 Git Worktree
人工智能
张小泡泡27 分钟前
AUTO_EVAL:面向大语言模型的多层次自动化评测框架
论文阅读·人工智能·语言模型·自然语言处理·自动化·微调
AI工具测评与分析34 分钟前
Seedance2.5 赋能飙算画影AI无限画布,无边画布进阶专业创作工作台
人工智能·信息可视化·ai作画·视频生成·ai视频·爆款视频
前端开发江鸟1 小时前
我没有自研 AI 中转站:8 小时跑通 New API、DeepSeek 与 Codex Coding Plan
人工智能