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;
@Autowired
public 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