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

未完待续。。。。

复制代码
 
相关推荐
江畔柳前堤17 小时前
roLabelImg 详细安装教程
开发语言·人工智能·后端·云原生
阿里云大数据AI技术17 小时前
分链路差异化设计的DSP准实时数仓|钛动科技基于阿里云实时计算 Flink 版 + DLF Paimon + EMR Serverless StarRocks 的实践
人工智能·flink
陕西企来客17 小时前
2026年7月AI智能搜索曝光趋势研判
大数据·人工智能·机器学习·ai智能搜索曝光
阿里云大数据AI技术18 小时前
从算力到智能体,面向 Agentic AI 的基础设施演进
人工智能·agent
hangyuekejiGEO19 小时前
GEO技术服务选型指南
大数据·人工智能·python
阿里云大数据AI技术19 小时前
EMR Serverless Spark AI Function 的双维降本实践
人工智能·sql·spark
维基框架19 小时前
GitHub源码处理提速 一趟扫描反而更慢
人工智能·github
冬奇Lab19 小时前
代码库知识库系列(05):向量检索 vs 知识图谱——加了调用图并没有变更好
人工智能
AKAMAI19 小时前
你的源服务器可能是你做出的最昂贵决定
运维·人工智能·云计算
冬奇Lab19 小时前
【无标题】
人工智能·开源