欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 手游 > springboot-ai接入DeepSeek

springboot-ai接入DeepSeek

2025/4/9 18:04:23 来源:https://blog.csdn.net/liu1251303815/article/details/147027923  浏览:    关键词:springboot-ai接入DeepSeek

1、引入pom依赖

<dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-starter-model-openai</artifactId>
</dependency><dependencyManagement><dependencies><dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-bom</artifactId><version>1.0.0-SNAPSHOT</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>

2 、 配置LLM

2.1 到deepseek 申请api-key

https://platform.deepseek.com/api_keys 首次注册会送10元 限时额度

2.2 添加springboot-ai配置

spring.ai.openai.base-url=https://api.deepseek.com
spring.ai.openai.api-key=xxxx

3、编写MemoryService 记录历史对话

import org.springframework.ai.chat.messages.AssistantMessage;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.stereotype.Service;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;@Service
public class MemoryService {HashMap<String, List<Message>> memory = new HashMap<>();public List<Message> getMemory(String userId) {List<Message> messages = memory.get(userId);if (messages == null) {return new ArrayList<>();}return messages;}public void addMemory(String userId, Message message) {List<Message> messages = memory.get(userId);if (messages == null) {messages = new ArrayList<>();}messages.add(message);memory.put(userId, messages);}public String buildHisChat(String userId){StringBuilder builder = new StringBuilder();for (Message message : memory.get(userId)) {if (message instanceof UserMessage) {builder.append("[user]").append(message.getText()).append("\n");} else if (message instanceof AssistantMessage) {builder.append("[assistant]").append(message.getText()).append("\n");builder.append("===============================================\n");}}return builder.toString();}public void cleanMemory(String userId) {memory.remove(userId);}
}

4、编写ChatService 调用LLM

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.Prompt;
import org.springframework.ai.model.Media;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.multipart.MultipartFile;import java.util.ArrayList;
import java.util.List;@Service
public class ChatService {private static final String MODEL = "deepseek-chat";private final OpenAiChatModel chatModel;public ChatService(OpenAiChatModel chatModel) {this.chatModel = chatModel;}public Message buildMessageWithImage(String question, List<MultipartFile> files) {if (!CollectionUtils.isEmpty(files)) {ArrayList<Media> mediaList = new ArrayList<>();for (MultipartFile file : files) {mediaList.add(new Media(MimeTypeUtils.IMAGE_PNG, file.getResource()));}return new UserMessage(question, mediaList);} else {return new UserMessage(question);}}public String chat(String userId, List<Message> messages) {Prompt prompt = new Prompt(messages,OpenAiChatOptions.builder().model(MODEL).build());ChatResponse response = chatModel.call(prompt);return response.getResults().get(0).getOutput().getText();}
}

5、UI层代码

@RestController
@RequestMapping("/ai/chat")
@Slf4j
public class ChatController {private final MemoryService memory;private final ChatService chatService;@Autowiredpublic ChatController(ChatService chatService, MemoryService service) {this.chatService = chatService;this.memory = service;}@GetMapping()public String chat(String userId, String question) {memory.addMemory(userId, new UserMessage(question));List<Message> messages = memory.getMemory(userId);String text = chatService.chat(userId, messages);memory.addMemory(userId, new AssistantMessage(text));return memory.buildHisChat(userId);}@GetMapping("image")public String chatWithImage(String userId, String question, String imageType, List<MultipartFile> files) {memory.addMemory(userId, chatService.buildMessageWithImage(question,files));List<Message> messages = memory.getMemory(userId);String text = chatService.chat(userId, messages);memory.addMemory(userId, new AssistantMessage(text));return memory.buildHisChat(userId);}@DeleteMapping("memory")public String cleanMemory(String userId) {memory.cleanMemory(userId);return "ok";}

6、参考链接

https://spring.io/projects/spring-ai

7、原码:

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词