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

未完待续。。。。

复制代码
 
相关推荐
吴佳浩10 分钟前
Hermes Agent 连环 400 真凶找到了:一个 call_id 让人炸毛
人工智能·llm·agent
程序员cxuan44 分钟前
幽默,一个 Github 名字叫“马尾辫”,但是他给你省了 80% 的 token
人工智能·后端·程序员
宋哥转AI1 小时前
Agent记忆模块系列:03存储与检索链路实测验证
人工智能·agent
老金带你玩AI1 小时前
老金开源GoalPro,别让AI把目标越写越烂
人工智能
Bigfish_coding1 小时前
前端转agent-【python】-08 用 LangGraph 把 Agent 做成状态机:像写 Vue 3 状态管理一样编排 AI 流程
人工智能
刺猬的温驯2 小时前
语音克隆模型的难点之一:音素对齐及交叉注意力早期失效问题 (兼论旋转位置编码)——F5-TTS、SupertonicTTS、VoxFlash-TTS 对比
人工智能·语音合成·tts
道友可好3 小时前
AI 是最好的混乱放大器:代码熵管理实战
前端·人工智能·后端
不加辣椒4 小时前
第7章 边界与约束技术:确保输出的准确性与安全性
人工智能
AI悦创Python辅导4 小时前
Claude Code 越用越乱?Sub-Agents 才是上下文污染的解法
人工智能
Bigfish_coding4 小时前
前端转agent-【python】-07 长期记忆进阶:用 ChromaDB + 语义搜索给 Agent 装上真正的长期记忆
人工智能