ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

Spring AI总结

2026/8/12 17:29:52 拓冰建站 浏览量
Spring AI总结 一. Spring AI的设计Spring AI 抽象了一层之前Java访问不同的大模型需要单独写适配的API接口现在有了spring AI可以使用一套统一的API接口换模型不再需要修改java代码只需要修改配置即可。chatClient.prompt().user(你好).call().content();例如:1. spring:ai:deepseek:api-key: xxxchat:options:model: deepseek-chat2. spring:ai:openai:api-key: xxxchat:options:model: gpt-4o-mini一句话总结spring AI的优势换大模型通常只需要换配置不需要改业务代码。二. 在企业项目中怎么选三. Spring AI的Demo搭建流程1. 引入依赖Spring Boot 3.x Spring AI 1.xdependencies!-- Spring Web --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency!-- Spring AI OpenAI --dependencygroupIdorg.springframework.ai/groupIdartifactIdspring-ai-starter-model-openai/artifactId/dependency/dependencies2. 配置秘钥API Key application.ymlspring:ai:openai:api-key: sk-xxxxxxchat:options:model: gpt-4o-mini3. 创建 ChatClientpackage com.itcast.springaiparent;import org.springframework.ai.chat.client.ChatClient;import org.springframework.stereotype.Service;Servicepublic class AiService {private final ChatClient chatClient;public AiService(ChatClient.Builder builder){this.chatClient builder.build();}public String chat(String message){return chatClient.prompt().user(message).call().content();}}4. 写 Controller 测试package com.itcast.springaiparent;import org.springframework.web.bind.annotation.*;RestControllerRequestMapping(/ai)public class AiController {private final AiService aiService;public AiController(AiService aiService){this.aiService aiService;}GetMapping(/chat)public String chat(RequestParam String msg){return aiService.chat(msg);}}四. Function Calling工具调用1. 项目结构2. 定义天气Tool关键就是Tool注解。Component public class WeatherTool { /** * Tool让模型能真正调用这个工具 * param city * return */ Tool(description 根据城市名称查询天气) public String getWeather( ToolParam(description 城市名称例如北京)String city) { return city 今天晴气温 25℃; } }description告诉 AI我这里有一个工具可以根据城市查询天气。3. 创建 ChatClientpackage com.example.service;import com.example.tool.WeatherTool;import org.springframework.ai.chat.client.ChatClient;import org.springframework.stereotype.Service;Servicepublic class AiService {private final ChatClient chatClient;public AiService(ChatClient.Builder builder,WeatherTool weatherTool){this.chatClient builder.defaultTools(weatherTool).build();}public String chat(String message){return chatClient.prompt().user(message).call().content();}}关键代码.defaultTools(weatherTool)告诉 AI: 这个 Java 对象里面的方法你可以调用。/五. Controller测试RestControllerRequestMapping(/ai)public class AiController {private final AiService aiService;public AiController(AiService aiService){this.aiServiceaiService;}GetMapping(/chat)public String chat(String msg){return aiService.chat(msg);}}六. 如果有多个Tool大模型来根据 需求决定调用哪个Componentpublic class OrderTool {Tool(description查询订单状态)public String queryOrder(String orderId){return 订单已发货;}}然后.defaultTools(weatherTool,orderTool)Function Calling本质以前用户|你的代码判断|if(天气)调天气接口if(订单)查订单现在用户|LLM|判断意图|选择Tool|执行Java方法|返回答案五. Function Calling和MCP区别Function CallingMCP工具在哪里你的Java代码独立服务标准模型厂商各自实现统一协议适合单体应用企业级AgentSpring AI支持支持六. RAG的实现原理实现功能用户问“公司的请假制度是什么”AI 不知道答案→ 去知识库搜索→ 找到《员工手册.pdf》→ 把相关内容交给 GPT→ AI 根据文档回答1. RAG的整体工作流程第一次准备PDF / Word / txt||vDocument Reader||vText Splitter||vEmbedding模型||vVector DB(向量数据库)用户提问用户:公司年假多少天?|vEmbedding问题向量|vVector DB搜索相似内容|v找到相关文档片段|vPrompt增强|vLLM回答2. 技术选型组件技术Spring AI1.xLLMOpenAI / OllamaEmbeddingOpenAI / BGE向量库Chroma文档txt3. 添加依赖dependencies!-- Spring Web --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency!-- Spring AI OpenAI --dependencygroupIdorg.springframework.ai/groupIdartifactIdspring-ai-starter-model-openai/artifactId/dependency!-- 向量数据库 Chroma --dependencygroupIdorg.springframework.ai/groupIdartifactIdspring-ai-starter-vector-store-chroma/artifactId/dependency/dependencies4. 配置spring:ai:openai:api-key: sk-xxxxchat:options:model: gpt-4o-minivectorstore:chroma:client:host: localhostport: 80004. 加载文档进入向量数据库Componentpublic class DataLoader {private final VectorStore vectorStore;public DataLoader(VectorStore vectorStore){this.vectorStorevectorStore;}PostConstructpublic void load(){Resource resource new ClassPathResource(docs/company.txt);TextReader reader new TextReader(resource);ListDocument documents reader.get();vectorStore.add(documents);}}5. 创建 RAG 查询Servicepublic class RagService {private final ChatClient chatClient;public RagService(ChatClient.Builder builder,VectorStore vectorStore){this.chatClient builder.defaultAdvisors(new QuestionAnswerAdvisor(vectorStore)).build();}public String ask(String question){return chatClient.prompt().user(question).call().content();}}重点代码.defaultAdvisors(new QuestionAnswerAdvisor(vectorStore))问题|向量搜索|找文档|拼接Prompt|调用LLM