解决场景1. 基础文本生成 (/ai/generate)场景最简单的 AI 对话输入消息返回生成内容用途通用问答、闲聊、内容创作等2. 带参数的模型调用 (/ai/generateWithOptions)场景自定义模型参数如模型版本、温度系数用途需要精细控制生成效果的场景如创意写作需要较高温度精确回答需要较低温度3. 工具调用Tool Calling (/ai/generateWithTool和/ai/generateWithToolBySelf)场景AI 调用外部函数获取实时数据示例查询巴黎、东京、纽约的天气用途扩展 AI 能力让模型能获取实时信息、操作外部系统数据库、API 等两种实现方式方式一使用ChatClient简化 API方式二手动处理工具调用循环while循环处理多轮调用4. 代码生成带前缀提示 (/ai/generatePythonCode)场景生成 Python 代码且自动补全代码块特点通过stopSequences控制输出结束位置使用prefix预填充代码块标记用途代码助手、自动化编程5. 推理过程展示DeepSeek Reasoner (/ai/deepSeekReasoningExample)场景获取模型的思维链Chain of Thought推理内容示例比较 9.11 和 9.8 的大小用途需要展示推理过程的教育场景、逻辑分析、复杂问题解答6. 结构化输出 (/ai/structuredOutput)场景将非结构化文本转换为结构化 JSON对象输出字段标题、摘要、关键词、情感分数用途信息抽取、内容摘要、情感分析、数据清洗7. 流式输出 (/ai/generateStream)场景使用Flux实时返回生成内容用途需要实时响应的场景如 AI 对话打字机效果提升用户体验架构图代码实现POMdependency groupIdorg.springframework.ai/groupId artifactIdspring-ai-starter-model-deepseek/artifactId /dependencyapplication.propertiesspring.ai.deepseek.api-key${DEEPSEEK_API_KEY:} spring.ai.deepseek.base-url${DEEPSEEK_API_BASE_URL:https://api.deepseek.com}ChatControllerimport com.haiwei.javaai.entities.ArticleSummary; import com.haiwei.javaai.entities.WeatherRequest; import com.haiwei.javaai.service.impl.WeatherService; import com.haiwei.javaai.service.impl.WeatherService1; import com.haiwei.javaai.utils.JsonUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.ai.chat.client.ChatClient; import org.springframework.ai.chat.messages.Message; import org.springframework.ai.chat.messages.UserMessage; import org.springframework.ai.chat.model.ChatResponse; import org.springframework.ai.chat.prompt.ChatOptions; import org.springframework.ai.chat.prompt.Prompt; import org.springframework.ai.deepseek.DeepSeekAssistantMessage; import org.springframework.ai.deepseek.DeepSeekChatModel; import org.springframework.ai.deepseek.DeepSeekChatOptions; import org.springframework.ai.deepseek.api.DeepSeekApi; import org.springframework.ai.model.tool.ToolCallingManager; import org.springframework.ai.model.tool.ToolExecutionResult; import org.springframework.ai.support.ToolCallbacks; import org.springframework.ai.tool.ToolCallback; import org.springframework.ai.tool.function.FunctionToolCallback; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import reactor.core.publisher.Flux; import java.util.List; import java.util.Map; import java.util.function.Consumer; Slf4j RestController public class ChatController { private final DeepSeekChatModel chatModel; Autowired public ChatController(DeepSeekChatModel chatModel) { this.chatModel chatModel; } GetMapping(/ai/generate) public Map generate(RequestParam(value message, defaultValue Tell me a joke) String message) { return Map.of(generation, chatModel.call(message)); } GetMapping(/ai/generateWithOptions) public Map generateWithOptions(RequestParam(value message, defaultValue Tell me a joke) String message) { DeepSeekChatOptions options DeepSeekChatOptions.builder() .model(DeepSeekApi.ChatModel.DEEPSEEK_V4_PRO.getValue()) .temperature(0.8) .build(); Prompt prompt new Prompt(message, options); ChatResponse response chatModel.call(prompt); return Map.of(generation, response.toString()); } GetMapping(/ai/generateWithTool) public Map generateWithTool(RequestParam(value message, defaultValue Tell me a joke) String message) { ToolCallback weatherCallback FunctionToolCallback.builder(getCurrentWeather, new WeatherService()) .description(Get the weather in location) .inputType(WeatherRequest.class) .build(); // Synchronous String response ChatClient.create(chatModel) .prompt() .user(Whats the weather in Paris, Tokyo, and New York?) .tools(weatherCallback) .call() .content(); return Map.of(generation, response); } GetMapping(/ai/generateWithToolBySelf) public Map generateWithToolBySelf() { ToolCallingManager toolCallingManager ToolCallingManager.builder().build(); DeepSeekChatOptions options DeepSeekChatOptions.builder() .toolCallbacks(ToolCallbacks.from(new WeatherService1())) .build(); Prompt prompt new Prompt(Whats the weather in Paris, Tokyo, and New York?, options); ChatResponse response chatModel.call(prompt); log.info(response:{}, JsonUtil.toJson(response)); while (response.hasToolCalls()) { ToolExecutionResult result toolCallingManager.executeToolCalls(prompt, response); log.info(result:{}, JsonUtil.toJson(result)); prompt new Prompt(result.conversationHistory(), options); response chatModel.call(prompt); log.info(response:{}, JsonUtil.toJson(response)); } return Map.of(generation, response); } GetMapping(/ai/generatePythonCode) public String generatePythonCode(RequestParam(value message, defaultValue Please write quick sort code) String message) { UserMessage userMessage new UserMessage(message); Message assistantMessage DeepSeekAssistantMessage .builder() .content(python\\n) .prefix(true) .build(); Prompt prompt new Prompt(List.of(userMessage, assistantMessage) , DeepSeekChatOptions.builder().stopSequences(List.of()).build() ); ChatResponse response chatModel.call(prompt); return response.getResult().getOutput().getText(); } GetMapping(/ai/deepSeekReasoningExample) public MapString, String deepSeekReasoningExample() { DeepSeekChatOptions promptOptions DeepSeekChatOptions.builder() .build(); Prompt prompt new Prompt(9.11 and 9.8, which is greater?, promptOptions); ChatResponse response chatModel.call(prompt); // Get the CoT content generated by the model DeepSeekAssistantMessage deepSeekAssistantMessage (DeepSeekAssistantMessage) response.getResult().getOutput(); String reasoningContent deepSeekAssistantMessage.getReasoningContent(); String text deepSeekAssistantMessage.getText(); return Map.of(reasoningContent, text); } GetMapping(/ai/structuredOutput) public ArticleSummary structuredOutput( RequestParam(value message, defaultValue Spring AI 是一个用于构建 AI 应用的 Java 框架支持 RAG、Tool Calling 和结构化输出。) String message) { log.info([StructuredOutput] request message{}, message); ConsumerChatClient.PromptUserSpec text u - u.text( 请对以下文本做结构化摘要严格按 JSON 格式返回 - title: 标题10字以内 - summary: 摘要50字以内 - keywords: 3-5个关键词 - sentimentScore: 情感分数 1-10 文本{text} ).param(text, message); ArticleSummary result ChatClient.create(chatModel) .prompt() .user(text) .call() .entity(ArticleSummary.class); log.info([StructuredOutput] result{}, JsonUtil.toJson(result)); return result; } GetMapping(/ai/generateStream) public FluxChatResponse generateStream(RequestParam(value message, defaultValue Tell me a joke) String message) { var prompt new Prompt(new UserMessage(message)); return chatModel.stream(prompt); } }ArticleSummaryimport java.util.List; public record ArticleSummary( String title, String summary, ListString keywords, int sentimentScore ) { }WeatherRequestimport lombok.Data; Data public class WeatherRequest { private String local; }WeatherServiceimport com.haiwei.javaai.entities.WeatherRequest; import com.haiwei.javaai.entities.WeatherResponse; import lombok.extern.slf4j.Slf4j; import java.util.function.Function; Slf4j public class WeatherService implements FunctionWeatherRequest, WeatherResponse { public WeatherResponse apply(WeatherRequest request) { log.info(WeatherService apply ..... request.getLocal()); return new WeatherResponse(30.0); } }WeatherService1import com.haiwei.javaai.entities.WeatherRequest; import com.haiwei.javaai.entities.WeatherResponse; import lombok.extern.slf4j.Slf4j; import org.springframework.ai.tool.annotation.Tool; import org.springframework.ai.tool.annotation.ToolParam; import java.util.function.Function; Slf4j public class WeatherService1 { Tool(description Get the weather in location) WeatherResponse getWeather(ToolParam(required false) WeatherRequest request) { log.info(getWeather apply ..... {} , request.getLocal()); return new WeatherResponse(30.0); } }JsonUtilimport com.google.gson.*; import com.google.gson.reflect.TypeToken; import java.lang.reflect.Type; import java.math.BigInteger; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class JsonUtil { public static Gson gson null; static { builder(); } private JsonUtil() { // no-op, just to avoid new instance. } private static void builder() { LongSerializer serializer new LongSerializer(); gson new GsonBuilder().setDateFormat(yyyy-MM-ddTHH:mm:ssZ) // Serialize BigInteger/Long/long as String for frontend .registerTypeAdapter(BigInteger.class, serializer) .registerTypeAdapter(long.class, serializer) .registerTypeAdapter(Long.class, serializer) .create(); } public static synchronized Gson newInstance() { if (gson null) { builder(); } return gson; } public static String toJson(Object obj) { return gson.toJson(obj); } public static T T toBean(String json, ClassT clz) { return gson.fromJson(json, clz); } public static T T toBean(Object sourceObject, ClassT targetClass) { return gson.fromJson(toJson(sourceObject), targetClass); } public static T MapString, T toMap(String json, ClassT clz) { SuppressWarnings(serial) MapString, JsonObject map gson.fromJson(json, new TypeTokenMapString, JsonObject() {}.getType()); MapString, T result new HashMap(8); for(Map.EntryString, JsonObject entry : map.entrySet()) { result.put(entry.getKey(), gson.fromJson(entry.getValue(), clz)); } return result; } SuppressWarnings(serial) public static MapString, Object toMap(String json) { return gson.fromJson(json, new TypeTokenMapString, Object() {}.getType()); } public static T ListT toList(String json, ClassT targetClass) { JsonArray array new JsonParser().parse(json).getAsJsonArray(); ListT list new ArrayList(); for (final JsonElement elem : array) { list.add(gson.fromJson(elem, targetClass)); } return list; } public static T ListT toList(Object sourceObject, ClassT clz) { return toList(toJson(sourceObject), clz); } private static class LongSerializer implements JsonSerializerLong { Override public JsonElement serialize(Long src, Type typeOfSrc, JsonSerializationContext context) { if (src ! null) { String strSrc src.toString(); if (strSrc.length() 15) { return new JsonPrimitive(strSrc); } } return new JsonPrimitive(src); } } }总结对照表场景端点核心技术点基础对话/ai/generate简单call参数调优/ai/generateWithOptionsChatOptions、Prompt工具调用/ai/generateWithToolFunctionToolCallback手动工具循环/ai/generateWithToolBySelfToolCallingManager代码生成/ai/generatePythonCode前缀提示、停止序列推理过程/ai/deepSeekReasoningExamplereasoningContent结构化输出/ai/structuredOutput.entity()反序列化流式响应/ai/generateStreamFluxChatResponse