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);
    }
}
相关推荐
邵宇然7 分钟前
编译期安全编程的边界探索:当 Rust 的类型系统还不足以表达我们的意图
人工智能
loopne8 分钟前
AI网文写作实验笔记(十三):系列总结——12 篇实验、8 条核心结论,把“AI 写小说“每一步拆开验证
人工智能·经验分享·笔记·ai写作·智能写作
2601_9670972212 分钟前
白光干涉仪品牌众多怎么筛选靠谱厂家?选购要点及优可测等品牌参考
人工智能
zandy101115 分钟前
claude code用不了?国内外AI 编程工具的演进与三类路径选择
人工智能
武汉海翎光电19 分钟前
从零开始了解数据采集——工业数据采集新趋势:边缘计算与云计算的强强联合
人工智能·云计算·边缘计算
l1t20 分钟前
DeepSeek总结的DuckDB 如何更快地运行递归 CTE
java·开发语言·数据库·mysql·duckdb
AI产品测评官22 分钟前
AI智能体在招聘场景的工程实践:屏幕语义理解与风控规避
人工智能·求职招聘
TechEdu20260624 分钟前
[人工智能]GPT(OpenAI):模型体系、智能体与企业工程实践
人工智能·ai
大力财经25 分钟前
抖音生活服务推动直播合规经营,消费者人脸保护功能覆盖超10万个直播间
大数据·人工智能·生活
小手cool32 分钟前
使用递归对数组进行反转操作
java·数据结构·算法