spring ai alibaba之项目搭建

一.本地ollama 方式接入

1.环境 JDK 17 spring boot 3.4.0

2.引入依赖

xml 复制代码
<dependency>
    <groupId>com.alibaba.cloud.ai</groupId>
    <artifactId>spring-ai-alibaba-starter</artifactId>
</dependency>

3.配置application.yml

yaml 复制代码
spring:
  ai:
    ollama:
      base-url: http://localhost:11434   //参考上一章服务器部署 localhost改成对应IP即可
      chat: 
        model: qwen3.5

4.建造控制器提供调用大模型接口

4.1 简单问答模式

kotlin 复制代码
@RestController
public class ChatController {
    private final ChatClient chatClient;
    public ChatController(ChatClient.Builder builder) {
        this.chatClient = builder
                //.defaultSystem("你是一个各种数据库sql优化大师")  //提示词
                .build();
    }


    @GetMapping("/chat")
    public String chat(@RequestParam(value = "input") String input) {

        return this.chatClient.prompt()
                .user(input)
                .call()
                .content();
    }
}

api调用结果

4.2 增加限制提示词模式

在构造器中 builder后面增加.defaultSystem("你是一个各种数据库sql优化大师") //增加提示词操作

复制代码
api再次调用结果   根据提示词直接给出对应的专业优化建议和调整

4.3 增加RAG检索模式

当你问道一些大模型没有的知识体系数据的时候,比如你今天都做了什么 大模型是不知道的 这个时候就需要知识库 让大模型读取知识库知识 从而知道你做了什么 然后大模型整理数据返回出来

加入知识库后 上代码

增加配置文件

kotlin 复制代码
package com.example.springai.config;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.ollama.OllamaEmbeddingModel;
import org.springframework.ai.ollama.api.OllamaApi;
import org.springframework.ai.ollama.api.OllamaOptions;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


import java.io.File;
import java.io.IOException;

@Configuration
public class AiConfig {

    @Value("${spring.ai.ollama.base-url}")
    private String ollamaBaseUrl;

    @Value("${spring.ai.ollama.chat.model}")
    private String ollamaModel;
scss 复制代码
// 默认提示词
    @Bean
    public ChatClient chatClient(OllamaChatModel chatModel) {
        return ChatClient.builder(chatModel)
                .defaultSystem("你是一个智能助手,回答简洁准确,不编造内容")
                .build();
    }// 本地持久化向量库(重启不丢数据)
    @Bean
    public VectorStore vectorStore(EmbeddingModel embeddingModel) throws IOException {
        SimpleVectorStore vectorStore = SimpleVectorStore.builder(embeddingModel)
                .build();

        // 数据持久化到项目目录的 vector-store.json 文件
        File persistFile = new File("vector-store.json");
        if (persistFile.exists()) {
            // 启动时加载已有数据
            vectorStore.load(persistFile);
        } else {
            // 首次启动创建空文件
            persistFile.createNewFile();
        }

        // 注册关闭钩子,项目停止时自动保存数据
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            vectorStore.save(persistFile);
        }));

        return vectorStore;
    }
}

增加 文档读取(支持文件上传)

xml 复制代码
  <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-tika-document-reader</artifactId>
            <version>0.8.1</version>
  </dependency>

输入命令:ollama pull mxbai-embed-large //拉取向量存储模型

上传数据到向量数据库(使用的是本地向量存储,正式环境可以改成其他向量库)

开始调用基于知识库回答接口

ps:因为是4月1问的 知识库里只有昨天的数据 接口问法改了下问昨天吃了什么

项目搭建就先到这了,后面再进阶演示下function calling 和mcp服务

未完待续。。。。

复制代码
 
相关推荐
声讯电子几秒前
AU-60 全功能 AI 语音处理模组,一站式解决 USB / 模拟 / I2S 全音频开发痛点
人工智能·语音识别·ai降噪·回声消除·回音消除·波束定向
Mr_凌宇4 分钟前
# AI 应用工程入门学习笔记 一
人工智能·面试·程序员
qiaozhangmenai5 分钟前
企业开源节流AI替代软件:2026年技术演进与选型指南
人工智能
大鱼>6 分钟前
宠物活动轨迹追踪系统:GPS/BDS+UWB+BLE多定位融合方案
人工智能·深度学习·算法·iot·宠物
蓦然回首却已人去楼空16 分钟前
Build a Large Language Model (From Scratch) 第三章 编码注意力机制
人工智能·深度学习·语言模型
lisw0518 分钟前
基于零信任的云-边-端访问控制系统,使用可信标签
人工智能·机器学习·软件工程
QYR_1119 分钟前
离心滚筒光饰机:精密制造后处理的“效率引擎”与价值高地
人工智能
白帽小阳20 分钟前
我的AI辅助开发工具链2026版:从编码到部署的全栈智能实践
网络·人工智能·学习·安全·自动化
HackTwoHub20 分钟前
AntiDebug Mcp、AI逆向绕过前端,Hook 加密接口,抓取 Vue 路由漏洞,接入 MCP 让 AI 自动化挖掘前端漏洞
前端·vue.js·人工智能·安全·web安全·网络安全·系统安全
俊哥V22 分钟前
每日 AI 研究简报 · 2026-07-14
人工智能·ai