优秀的编程知识分享平台

网站首页 > 技术文章 正文

SpringBoot集成DeepSeek

nanyue 2025-08-02 20:43:07 技术文章 1 ℃

SpringBoot集成DeepSeek的步骤如下:

  1. 关于deepseek部分的开发
import com.kclm.springboot3deepseek.deepseek.model.ChatRequest;
import com.kclm.springboot3deepseek.deepseek.model.ChatResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
/**
 * 类名称:DeepSeekApiClient
 * 类描述:负责与DeepSeek API的HTTP通信
 */
@Component
public class DeepSeekApiClient {

    private final RestTemplate restTemplate;
    private final String apiKey;
    private final String apiUrl;

    public DeepSeekApiClient(
            RestTemplate restTemplate,
            @Value("${deepseek.api.key}") String apiKey,
            @Value("${deepseek.api.url}") String apiUrl) {
        this.restTemplate = restTemplate;
        this.apiKey = apiKey;
        this.apiUrl = apiUrl;
    }

    /**
     * 发送聊天请求到DeepSeek API
     *
     * @param chatRequest 聊天请求对象
     * @return API的响应
     */
    public ChatResponse sendChatRequest(ChatRequest chatRequest) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Authorization", "Bearer " + apiKey);

        HttpEntity<ChatRequest> requestEntity = new HttpEntity<>(chatRequest, headers);
        return restTemplate.postForObject(apiUrl, requestEntity, ChatResponse.class);
    }
}
 
package com.kclm.springboot3deepseek.deepseek.model;

import java.util.List;

public class ChatRequest {
    private String model;
    private List<Message> messages;
    private double temperature;
    private int max_tokens;
    private boolean stream;
}
import java.util.List;

public class ChatResponse {
    private String id;
    private String object;
    private long created;
    private String model;
    private List<Choice> choices;
}
/**
 * 类名称:Choice
 * 类描述:表示DeepSeek API响应中的选择项
 */
public class Choice {
    private int index;
    private Message message;
    private String finishReason;

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public Message getMessage() {
        return message;
    }

    public void setMessage(Message message) {
        this.message = message;
    }

    public String getFinishReason() {
        return finishReason;
    }

    public void setFinishReason(String finishReason) {
        this.finishReason = finishReason;
    }
}
package com.kclm.springboot3deepseek.deepseek.model;

/**
 * 类名称:Message
 * 类描述:表示聊天消息的模型类
 */
public class Message {
    private String role;
    private String content;
}
import com.kclm.springboot3deepseek.deepseek.client.DeepSeekApiClient;
import com.kclm.springboot3deepseek.deepseek.model.ChatRequest;
import com.kclm.springboot3deepseek.deepseek.model.ChatResponse;
import com.kclm.springboot3deepseek.deepseek.model.Message;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.List;
/**
 * 类名称:DeepSeekChatService
 * 类描述:主服务类,协调API客户端和对话历史管理
 */
@Service
public class DeepSeekChatService {

    private final DeepSeekApiClient apiClient;
    private final ConversationService conversationService;
    private final String model;
    private final double temperature;
    private final int maxTokens;

    public DeepSeekChatService(
            DeepSeekApiClient apiClient,
            ConversationService conversationService,
            @Value("${deepseek.model}") String model,
            @Value("${deepseek.temperature:0.7}") double temperature,
            @Value("${deepseek.max-tokens:2048}") int maxTokens) {
        this.apiClient = apiClient;
        this.conversationService = conversationService;
        this.model = model;
        this.temperature = temperature;
        this.maxTokens = maxTokens;
    }

    /**
     * 与DeepSeek进行聊天
     *
     * @param userMessage 用户消息
     * @return AI的响应
     */
    public String chatWithDeepSeek(String userMessage) {
        // 添加用户消息到对话历史
        Message userMsg = new Message("user", userMessage);
        conversationService.addMessage(userMsg);

        // 准备请求
        ChatRequest chatRequest = new ChatRequest();
        chatRequest.setModel(model);
        chatRequest.setMessages(conversationService.getConversationHistory());
        chatRequest.setTemperature(temperature);
        chatRequest.setMax_tokens(maxTokens);
        chatRequest.setStream(false);

        // 发送请求并获取响应
        ChatResponse response = apiClient.sendChatRequest(chatRequest);

        // 处理响应
        if (response != null && response.getChoices() != null && !response.getChoices().isEmpty()) {
            Message assistantMessage = response.getChoices().get(0).getMessage();
            // 添加AI响应到对话历史
            conversationService.addMessage(assistantMessage);
            return assistantMessage.getContent();
        }

        return "抱歉,我无法生成回复。";
    }

    /**
     * 清除对话历史
     */
    public void clearConversationHistory() {
        conversationService.clearConversationHistory();
    }
}
import com.kclm.springboot3deepseek.deepseek.model.Message;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

/**
 * 类名称:ConversationService
 * 类描述:负责管理对话历史
 */
@Service
public class ConversationService {

    private final List<Message> conversationHistory = new ArrayList<>();

    /**
     * 添加消息到对话历史
     *
     * @param message 要添加的消息
     */
    public void addMessage(Message message) {
        conversationHistory.add(message);
    }

    /**
     * 获取当前对话历史
     *
     * @return 对话历史列表
     */
    public List<Message> getConversationHistory() {
        return new ArrayList<>(conversationHistory);
    }

    /**
     * 清除对话历史
     */
    public void clearConversationHistory() {
        conversationHistory.clear();
    }
}
import com.kclm.springboot3deepseek.deepseek.service.DeepSeekChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 类名称:ChatController
 * 类描述:处理聊天相关的HTTP请求
 */
@RestController
public class ChatController {

    private final DeepSeekChatService deepSeekChatService;

    @Autowired
    public ChatController(DeepSeekChatService deepSeekChatService) {
        this.deepSeekChatService = deepSeekChatService;
    }

    @RequestMapping("/api/chat")
    public String chat(@RequestBody String userMessage) {
        System.out.println("收到用户消息: " + userMessage);
        return deepSeekChatService.chatWithDeepSeek(userMessage);
    }

    @PostMapping("/clear")
    public String clearConversation() {
        deepSeekChatService.clearConversationHistory();
        return "对话历史已清除";
    }
}
deepseek.api.key=你自己的key
deepseek.api.url=https://api.deepseek.com/v1/chat/completions
deepseek.model=deepseek-chat
deepseek.temperature=0.7
deepseek.max-tokens=2048
  1. 前端界面使用VUE3
<template>
  <div class="chat-container">
    <div class="chat-history">
      <div v-for="(message, index) in messages" :key="index" :class="['message', message.role]">
        <div class="message-content">{{ message.content }}</div>
      </div>
      <div v-if="loading" class="message assistant">
        <div class="message-content">思考中...</div>
      </div>
    </div>
    <div class="chat-input">
      <textarea v-model="userInput" @keyup.enter="sendMessage" placeholder="输入消息..."></textarea>
      <button @click="sendMessage">发送</button>
      <button @click="clearConversation">清空对话</button>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import axios from 'axios';

const messages = ref([]);
const userInput = ref('');
const loading = ref(false);

const sendMessage = async () => {
  if (!userInput.value.trim()) return;
  
  // 添加用户消息
  messages.value.push({ role: 'user', content: userInput.value });
  const currentInput = userInput.value;
  userInput.value = '';
  loading.value = true;
  
  try {
    // 调用后端API
    const response = await axios.post('http://localhost:8080/api/chat', {
      message: currentInput
    }, {
      headers: {
        'Content-Type': 'application/json'
      }
    });
    
    // 添加AI回复
    messages.value.push({ role: 'assistant', content: response.data });
  } catch (error) {
    console.error('调用API失败:', error);
    messages.value.push({ role: 'assistant', content: '抱歉,出错了: ' + error.message });
  } finally {
    loading.value = false;
  }
};

const clearConversation = async () => {
  try {
    await axios.post('http://localhost:8080/api/chat/clear');
    messages.value = [];
  } catch (error) {
    console.error('清除对话失败:', error);
  }
};
</script>

<style scoped>
.chat-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

.chat-history {
  flex: 1;
  overflow-y: auto;
  margin-bottom: 20px;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}

.message {
  margin-bottom: 15px;
  padding: 10px;
  border-radius: 5px;
}

.message.user {
  background-color: #e3f2fd;
  margin-left: 20%;
  text-align: right;
}

.message.assistant {
  background-color: #f5f5f5;
  margin-right: 20%;
}

.message-content {
  white-space: pre-wrap;
}

.chat-input {
  display: flex;
  gap: 10px;
}

.chat-input textarea {
  flex: 1;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
  resize: none;
  height: 60px;
}

.chat-input button {
  padding: 10px 15px;
  background-color: #1976d2;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
}

.chat-input button:hover {
  background-color: #1565c0;
}
</style>
  1. 效果


Tags:

最近发表
标签列表