DeepSeek模型调用-专业版 Spring AI 注意 Spring AI要求使用JDK 17或更高版本,Spring AI需要在Spring Boot 3.2.x或更高版本的环境中运行。 核心代码如下: OpenAIConfig配置类: java package com.demo.aitest; import jakarta.annotation.PostConstruct; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.ai.openai.OpenAiChatClient; import org.springframework.ai.openai.OpenAiChatOptions; import org.springframework.ai.openai.api.OpenAiApi; @Configuration public class OpenAIConfig { @Value("${spring.ai.openai.apikey}") private String apiKey; @Value("${spring.ai.openai.baseurl}") private String baseUrl; @Value("${spring.ai.openai.chat.options.model}") private String model; @Bean public OpenAiChatClient openAiChatClient() { OpenAiApi openAiApi new OpenAiApi(baseUrl, apiKey); OpenAiChatOptions options new OpenAiChatOptions(); options.setModel(model); options.setFrequencyPenalty(0.0f); options.setMaxTokens(2000); return new OpenAiChatClient(openAiApi, options); } } ChatController 类: java package com.lili.aitest; import org.springframework.ai.openai.OpenAiChatClient; import org.springframework.ai.chat.messages.UserMessage; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.ai.chat.ChatResponse; import org.springframework.ai.chat.prompt.Prompt; import org.springframework.web.bind.annotation.; import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; @RestController public class ChatController { private static final Logger logger LoggerFactory.getLogger(ChatController.class); @Autowired private OpenAiChatClient chatClient; @GetMapping("/chat") public String chat(@RequestParam String message) { try { ChatResponse response chatClient.call(new Prompt(message)); String content response.getResult().getOutput().getContent(); logger.info("Response received successfully"); return content; } catch (Exception e) { logger.error("Error processing chat request: {}", e.getMessage()); throw e; } } @GetMapping(value "/chat/stream", produces "text/eventstream;charsetUTF8") public SseEmitter streamChat(@RequestParam String message) { SseEmitter emitter new SseEmitter(1L); // 无限超时 try { // Create prompt with UserMessage Prompt prompt new Prompt(new UserMessage(message)); // Stream the response chatClient.stream(prompt).subscribe( chunk > { try { if (chunk ! null && chunk.getResult() ! null) { String content chunk.getResult().getOutput().getContent(); if (content ! null && !content.isEmpty()) { emitter.send(content); } } } catch (IOException e) { logger.error("Error sending chunk: {}", e.getMessage()); emitter.completeWithError(e); } }, error > { logger.error("Error in stream: {}", error.getMessage()); emitter.completeWithError(error); }, () > { logger.info("Stream completed successfully"); emitter.complete(); } ); } catch (Exception e) { logger.error("Error processing streaming chat request: {}", e.getMessage()); emitter.completeWithError(e); } return emitter; } } application.yml配置如下: html spring: ai: openai: apikey: YourAPPKEY