Spring AI 入门教程,使用Ollama本地模型集成,实现对话记忆功能。

2025-12-12 0 625

Spring AI 入门教程:Ollama 本地模型集成

1. 环境准备

1.1 安装 Ollama

Ollama 是一个用于在本地运行大语言模型的工具。首先需要安装 Ollama:

  • MacOS: 可以通过 Homebrew 安装
    brew install ollama
    
  • Windows/Linux: 访问 Ollama 官网 下载对应的安装包

1.2 拉取 Ollama 模型

安装完成后,拉取一个模型(例如 Llama 3):

ollama pull llama3

您可以根据需要选择其他模型,如 Mistral、DeepSeek 等。

1.3 验证 Ollama 服务

启动 Ollama 服务(通常安装后会自动启动),可以通过以下方式验证服务是否正常运行:

ollama list

正常情况下,会显示已下载的模型列表。Ollama 服务默认运行在 http://localhost:11434。

2. 创建 Spring Boot 项目

2.1 使用 Spring Initializr 创建项目

创建一个 Spring Boot 3.2+ 项目,选择以下依赖:

  • Spring Web
  • Spring Boot DevTools

2.2 添加 Spring AI 1.0.0-M6 依赖

pom.xml 文件中添加以下依赖:

<properties>
    <spring-ai.version>1.0.0-M6</spring-ai.version>
</properties>

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

<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>

3. 配置 Ollama

3.1 配置文件设置

application.ymlapplication.properties 中配置 Ollama:

application.yml 示例:

server:
  port: 8080

spring:
  ai:
    ollama:
      base-url: http://localhost:11434  # Ollama 服务的URL
      chat:
        options:
          model: deepseek-r1:1.5b  # 使用的模型名称
          temperature: 0.7  # 可选:温度参数
          max-tokens: 1024  # 可选:最大生成长度

application.properties 示例:

server.port=8080

# Ollama 配置
spring.ai.ollama.base-url=http://localhost:11434
spring.ai.ollama.chat.options.model=deepseek-r1:1.5b
spring.ai.ollama.chat.options.temperature=0.7
spring.ai.ollama.chat.options.max-tokens=1024

4. 使用 ChatModel 与 Ollama 交互

ChatModel 是 Spring AI 中与聊天模型交互的基础接口。下面演示如何使用它与 Ollama 本地模型交互。

4.1 创建 ChatModel 服务

import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.prompt.messages.UserMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OllamaChatService {

    private final ChatModel chatModel;

    @Autowired
    public OllamaChatService(ChatModel chatModel) {
        this.chatModel = chatModel;
    }

    public String chat(String message) {
        // 创建用户消息
        UserMessage userMessage = new UserMessage(message);
        
        // 创建提示
        Prompt prompt = new Prompt(userMessage);
        
        // 调用 Ollama 模型并获取响应
        return chatModel.call(prompt).getResult().getOutput().getContent();
    }
}

4.2 创建 ChatModel 控制器

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;

@RestController
public class OllamaChatController {

    private final OllamaChatService chatService;

    @Autowired
    public OllamaChatController(OllamaChatService chatService) {
        this.chatService = chatService;
    }

    @GetMapping(\"/chat-model\")
    public String chatWithModel(@RequestParam String message) {
        return chatService.chat(message);
    }
}

5. 使用 ChatClient 与 Ollama 交互

ChatClient 是对 ChatModel 的更高层次封装,提供了更流畅的 API。

5.1 创建 ChatClient 服务

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OllamaClientService {

    private final ChatClient chatClient;

    @Autowired
    public OllamaClientService(ChatClient chatClient) {
        this.chatClient = chatClient;
    }

    public String simpleChat(String message) {
        // 使用流畅的 API 调用 Ollama
        return chatClient.prompt().user(message).call().content();
    }

    public String chatWithSystemPrompt(String message) {
        // 添加系统提示
        return chatClient.prompt()
            .system(\"你是一个专业助手,用简洁的语言回答问题。\")
            .user(message)
            .call()
            .content();
    }
}

5.2 创建 ChatClient 控制器

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;

@RestController
public class OllamaClientController {

    private final OllamaClientService clientService;

    @Autowired
    public OllamaClientController(OllamaClientService clientService) {
        this.clientService = clientService;
    }

    @GetMapping(\"/chat-client\")
    public String chatWithClient(@RequestParam String message) {
        return clientService.simpleChat(message);
    }

    @GetMapping(\"/chat-with-system\")
    public String chatWithSystem(@RequestParam String message) {
        return clientService.chatWithSystemPrompt(message);
    }
}

6. 高级功能

6.1 流式响应

Spring AI 支持流式返回 Ollama 生成的内容,适用于需要实时显示的场景。

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;

@Service
public class OllamaStreamingService {

    private final ChatClient chatClient;

    @Autowired
    public OllamaStreamingService(ChatClient chatClient) {
        this.chatClient = chatClient;
    }

    public Flux streamChat(String message) {
        return chatClient.prompt()
            .user(message)
            .stream()
            .content();
    }
}

对应的控制器:

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 reactor.core.publisher.Flux;

@RestController
public class OllamaStreamingController {

    private final OllamaStreamingService streamingService;

    @Autowired
    public OllamaStreamingController(OllamaStreamingService streamingService) {
        this.streamingService = streamingService;
    }

    @GetMapping(value = \"/stream\", produces = \"text/event-stream\")
    public Flux stream(@RequestParam String message) {
        return streamingService.streamChat(message);
    }
}

6.2 对话记忆

使用 ChatMemory 实现对话上下文管理:

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.messages.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class OllamaConversationService {

    private final ChatClient chatClient;


    @Autowired
    ChatMemory memory;

    @Autowired
    public OllamaConversationService(ChatClient chatClient) {
        this.chatClient = chatClient;
    }

    public String startConversation(String message) {
        String conversationId = UUID.randomUUID().toString();

        MessageChatMemoryAdvisor messageChatMemoryAdvisor = new MessageChatMemoryAdvisor(memory, conversationId, 20, 1);

        String response = chatClient.prompt()
                .user(message)
                .advisors(messageChatMemoryAdvisor)
                .call()
                .content();

        System.out.println(\"ConversationId : \" + conversationId);
        return \"ConversationId : \" + conversationId + \"nResponse: \" + response;
    }

    public String continueConversation(String conversationId, String message) {

        MessageChatMemoryAdvisor messageChatMemoryAdvisor = new MessageChatMemoryAdvisor(memory, conversationId, 20, 1);
        return chatClient.prompt()
                .user(message)
                .advisors(messageChatMemoryAdvisor)
                .call()
                .content();
    }

    public List getMessageList(String conversationId) {
        List messageList = memory.get(conversationId, 20);
        return messageList;
    }
}

对应的控制器:

import com.example.service.OllamaConversationService;
import org.springframework.ai.chat.messages.Message;
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;

@RestController
public class OllamaConversationController {

    private final OllamaConversationService conversationService;

    @Autowired
    public OllamaConversationController(OllamaConversationService conversationService) {
        this.conversationService = conversationService;
    }

    /**
     * 发起会话
     *
     * @param message
     * @return
     */
    @GetMapping(\"/start-conversation\")
    public String startConversation(@RequestParam(defaultValue = \"我叫张三\") String message) {
        return conversationService.startConversation(message);
    }


    /**
     * 持续聊天
     *
     * @param sessionId
     * @param message
     * @return
     */
    @GetMapping(\"/continue-conversation\")
    public String continueConversation(@RequestParam(defaultValue = \"sessionId\") String sessionId, @RequestParam(defaultValue = \"我叫什么?\") String message) {
        return conversationService.continueConversation(sessionId, message);
    }

    /**
     * 获取某个会话的聊天记录
     * @param sessionId
     * @return
     */
    @GetMapping(\"/getMessageList\")
    public List getMessageList(@RequestParam(defaultValue = \"sessionId\") String sessionId) {
        return conversationService.getMessageList(sessionId);
    }
}

7. 自定义 ChatClient 配置

可以通过配置类自定义 ChatClient 的行为:

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.PromptChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.api.Advisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ChatClientConfig {


    @Bean
    public ChatMemory chatMemory(){
        return new InMemoryChatMemory();
    }

    @Bean
    public ChatClient ollamaChatClient(ChatClient.Builder chatClientBuilder) {

        ChatClient chatClient = chatClientBuilder
                // 可以添加默认的系统提示
                .defaultSystem(\"你是一个友好的AI助手,用简洁明了的语言回答问题。\")
                // 可以添加默认的增强器Ø
                .defaultAdvisors(
                        // 注意:如果添加默认对话记忆,所有对话将共享同一个上下文
                        // 对于生产环境,应该根据会话ID管理不同的记忆实例
//                        promptChatMemoryAdvisor()
                )
                .build();
        return chatClient;
    }
}

8. 错误处理

添加全局异常处理以提供更好的用户体验:

import org.springframework.ai.chat.model.ChatException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class GlobalExceptionHandler {

    /** @ExceptionHandler(ChatException.class)
    public ResponseEntity handleChatException(ChatException ex) {
        // 记录错误日志
        System.err.println(\"Ollama 模型调用失败: \" + ex.getMessage());
        // 返回友好的错误提示
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
            .body(\"抱歉,模型调用失败,请检查 Ollama 服务是否正常运行。\");
    }
    */
    @ExceptionHandler(Exception.class)
    public ResponseEntity handleGeneralException(Exception ex) {
        // 记录错误日志
        System.err.println(\"系统错误: \" + ex.getMessage());
        // 返回通用错误提示
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
            .body(\"系统发生错误,请稍后再试。\");
    }
}

9. 完整示例项目结构

src/main/java/com/example/
├── config/
│   └── OllamaConfig.java
├── controller/
│   ├── OllamaChatController.java
│   ├── OllamaClientController.java
│   ├── OllamaStreamingController.java
│   └── OllamaConversationController.java
├── service/
│   ├── OllamaChatService.java
│   ├── OllamaClientService.java
│   ├── OllamaStreamingService.java
│   └── OllamaConversationService.java
├── exception/
│   └── GlobalExceptionHandler.java
└── OllamaSpringAiApplication.java
src/main/resources/
└── application.yml

10. 测试应用

启动 Spring Boot 应用后,可以通过以下方式测试:

  1. 使用浏览器或 curl 测试基本聊天功能

    http://localhost:8080/chat-client?message=你好,请介绍一下自己
    
  2. 测试流式响应

    http://localhost:8080/stream?message=详细解释一下Spring Boot框架
    
  3. 测试对话记忆

    • 首先开始一个对话:
      http://localhost:8080/start-conversation?message=我的名字是张三
      
    • 然后继续对话(使用返回的 sessionId):
      http://localhost:8080/continue-conversation?sessionId=你的sessionId&message=我叫什么名字
      

11. 总结

本教程介绍了如何在 Spring AI 1.0.0-M6 版本中集成 Ollama 本地大模型,主要内容包括:

  1. 环境准备:安装 Ollama 并拉取模型
  2. 项目配置:添加 Spring AI 1.0.0-M6 依赖并配置 Ollama
  3. 使用 ChatModel 与 Ollama 交互的基础方法
  4. 使用 ChatClient 的高级功能,如流畅的 API 和系统提示
  5. 实现流式响应以提供更好的用户体验
  6. 使用对话记忆功能维护多轮对话上下文
  7. 自定义 ChatClient 配置和错误处理

通过本教程,您应该能够成功地在 Spring Boot 应用中集成 Ollama 本地模型,并利用 Spring AI 提供的各种功能构建智能应用。本地模型的优势在于数据隐私性更高、无需网络连接、可定制性更强,特别适合对数据安全有较高要求的场景。

12. 代码链接

gitee.com/tree_boss/s…

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

申明:本文由第三方发布,内容仅代表作者观点,与本网站无关。对本文以及其中全部或者部分内容的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。本网发布或转载文章出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,也不代表本网对其真实性负责。

左子网 编程相关 Spring AI 入门教程,使用Ollama本地模型集成,实现对话记忆功能。 https://www.zuozi.net/35926.html

常见问题
  • 1、自动:拍下后,点击(下载)链接即可下载;2、手动:拍下后,联系卖家发放即可或者联系官方找开发者发货。
查看详情
  • 1、源码默认交易周期:手动发货商品为1-3天,并且用户付款金额将会进入平台担保直到交易完成或者3-7天即可发放,如遇纠纷无限期延长收款金额直至纠纷解决或者退款!;
查看详情
  • 1、描述:源码描述(含标题)与实际源码不一致的(例:货不对板); 2、演示:有演示站时,与实际源码小于95%一致的(但描述中有”不保证完全一样、有变化的可能性”类似显著声明的除外); 3、发货:不发货可无理由退款; 4、安装:免费提供安装服务的源码但卖家不履行的; 5、收费:价格虚标,额外收取其他费用的(但描述中有显著声明或双方交易前有商定的除外); 6、其他:如质量方面的硬性常规问题BUG等。 注:经核实符合上述任一,均支持退款,但卖家予以积极解决问题则除外。
查看详情
  • 1、左子会对双方交易的过程及交易商品的快照进行永久存档,以确保交易的真实、有效、安全! 2、左子无法对如“永久包更新”、“永久技术支持”等类似交易之后的商家承诺做担保,请买家自行鉴别; 3、在源码同时有网站演示与图片演示,且站演与图演不一致时,默认按图演作为纠纷评判依据(特别声明或有商定除外); 4、在没有”无任何正当退款依据”的前提下,商品写有”一旦售出,概不支持退款”等类似的声明,视为无效声明; 5、在未拍下前,双方在QQ上所商定的交易内容,亦可成为纠纷评判依据(商定与描述冲突时,商定为准); 6、因聊天记录可作为纠纷评判依据,故双方联系时,只与对方在左子上所留的QQ、手机号沟通,以防对方不承认自我承诺。 7、虽然交易产生纠纷的几率很小,但一定要保留如聊天记录、手机短信等这样的重要信息,以防产生纠纷时便于左子介入快速处理。
查看详情

相关文章

猜你喜欢
发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务