零基础搞定Spring AI 调用本地大模型

1. 前置条件

在开始之前,请确保你满足以下条件:

  1. JDK 环境

    • 必须使用 JDK 17 或以上(Spring Boot 3.x 要求)
    • 推荐使用 JDK 21(长期支持 LTS)
  2. Ollama 安装

    • macOS 用户(本教程示例默认)

      sql 复制代码
      brew install ollama
      brew services start ollama
    • Windows 用户

      访问 Ollama 官网 下载并安装,启动服务即可。


2. 安装 Ollama & 下载本地千问模型(也可选择其他开源模型)

bash 复制代码
# 安装 Ollama(macOS 示例)
brew install ollama

# 启动 Ollama 服务
brew services start ollama

# 下载通义千问本地模型
ollama pull qwen3:1.7b

# 查看本地模型列表
ollama list

# 查看模型详情
ollama show qwen3:1.7b

3. Ollama 常用命令

bash 复制代码
brew services list            # 查看服务状态
brew services start ollama    # 启动服务
brew services restart ollama  # 重启服务
brew services stop ollama     # 停止服务
ollama ps                     # 查看运行中的模型实例
ollama rm qwen3:1.7b          # 删除模型
ollama run qwen3:1.7b         # 运行模型
/bye 或 Ctrl+C                # 停止运行模型

4. 添加应用配置

application.properties(本地 Ollama 示例)

ini 复制代码
# 本地 Ollama 配置
spring.ai.ollama.base-url=http://localhost:11434
spring.ai.ollama.chat.options.model=qwen3:1.7b
spring.ai.ollama.chat.options.temperature=0.7

Maven 依赖

  • 调用本地大模型(Ollama) → Spring AI 官方依赖

    xml 复制代码
    ```
    <dependency>
        <groupId>org.springframework.ai</groupId>
        <artifactId>spring-ai-starter-model-ollama</artifactId>
        <version>1.0.1</version>
    </dependency>
    ```

5. 编写控制器代码

typescript 复制代码
@RestController
@RequestMapping("/api/chat")
public class ChatController {
    private final ChatClient chatClient;

    public ChatController(ChatClient.Builder chatClientBuilder) {
        this.chatClient = chatClientBuilder.build();
    }

    // GET 请求测试
    @GetMapping
    public String chatGet(@RequestParam(required = false, defaultValue = "你好") String message) {
        return processChat(message);
    }

    // POST 请求
    @PostMapping
    public String chatPost(@RequestBody Map<String, String> request) {
        String message = request.getOrDefault("message", "你好");
        return processChat(message);
    }

    private String processChat(String userMessage) {
        String prompt = "你是一个AI助手,请根据用户的问题返回结果";
        return chatClient.prompt(prompt)
                .user(userMessage)
                .call()
                .content();
    }
}

6. 测试接口

浏览器 GET 请求

bash 复制代码
http://localhost:8080/api/chat?message=你是谁?

Postman POST 请求

bash 复制代码
POST http://localhost:8080/api/chat
Body:
{
    "message": "介绍一下你自己"
}
相关推荐
倔强的石头_11 小时前
不想每次都从头解释:我用 Doubao-Seed-Evolving 做了一个「稿件接力站」
ai编程
大模型码小白13 小时前
【Python零基础教程】继承、多态与魔法函数:面向对象编程三大核心特性详解
java·大数据·开发语言·人工智能·python·ai编程
-XWB-14 小时前
【LLM】Agent Planning 完全指南:8 种纯 LLM 范式 + 8 种混合规划模式详解(二)
人工智能·经验分享·aigc·学习方法·ai编程
沉默王二15 小时前
腾讯一面,我霸气反问:“你说你们在做Agent项目,说说 SubAgent、Plan 模式、Skill 调用这些你们都是怎么做的?”面试官一直在擦汗。。
面试·agent·ai编程
kyriewen15 小时前
我让AI改一个bug——它偷偷动了5个我没让它碰的地方
前端·javascript·ai编程
神奇霸王龙17 小时前
Claude Code屠榜:MiMo与Grok紧追Codex
服务器·网络·人工智能·gpt·ai·ai编程
z1234567898618 小时前
2026最新两款AI编程工具深度对比实测
java·数据库·ai编程
GeekArch19 小时前
第24讲:Vibe模式代码风格控制——适配Keil/STM32工程规范
人工智能·stm32·单片机·嵌入式硬件·mcu·决策树·ai编程
OpenTiny社区19 小时前
深度解析 LSP 如何为 AI 装上“眼睛”
前端·ai编程