feat:测试代码

law-v2025-01
wangxy 2 months ago
parent d5433f66b7
commit ed92b92573

@ -6,6 +6,7 @@ import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.framework.web.service.DeepSeekApiService;
import com.ruoyi.system.domain.SysColumn;
import com.ruoyi.system.domain.SysColumnVO;
import com.ruoyi.system.domain.SysSpecial;
@ -85,4 +86,15 @@ public class LawController extends BaseController {
}
@Autowired
private DeepSeekApiService deepSeekApiService;
@GetMapping("/call-deepseek")
public R callDeepSeek(@RequestParam(required = false) String content) {
return R.ok(deepSeekApiService.callDeepSeekApi(content));
}
}

@ -0,0 +1,22 @@
package com.ruoyi.framework.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* ClassName: AppConfig
* Package: com.ruoyi.framework.config
* Description:
*
* @Author wangxy
* @Create 2025/2/14 10:06
* @Version 1.0
*/
@Configuration
public class AppConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}

@ -0,0 +1,66 @@
package com.ruoyi.framework.web.service;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.Arrays;
/**
* ClassName: DeepSeekApiService
* Package: com.ruoyi.framework.web.service
* Description:
*
* @Author wangxy
* @Create 2025/2/14 9:52
* @Version 1.0
*/
@Service
public class DeepSeekApiService {
private static final String API_URL = "https://api.deepseek.com/chat/completions";
private static final String API_KEY = "sk-f1a69dbdd92a428db6737a7fe1c64740"; // 替换为你的 API Key
private final RestTemplate restTemplate;
public DeepSeekApiService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public String callDeepSeekApi(String content) {
// 设置请求头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization", "Bearer " + API_KEY);
// 构建请求体对象
DeepSeekRequest.Message systemMessage = new DeepSeekRequest.Message();
systemMessage.setRole("system");
systemMessage.setContent("You are a helpful assistant.");
DeepSeekRequest.Message userMessage = new DeepSeekRequest.Message();
userMessage.setRole("user");
userMessage.setContent(content);
DeepSeekRequest requestBody = new DeepSeekRequest();
requestBody.setModel("deepseek-chat");
requestBody.setMessages(Arrays.asList(systemMessage, userMessage));
requestBody.setStream(false);
// 创建 HttpEntity
HttpEntity<DeepSeekRequest> requestEntity = new HttpEntity<>(requestBody, headers);
// 发送 POST 请求
ResponseEntity<String> response = restTemplate.exchange(
API_URL,
HttpMethod.POST,
requestEntity,
String.class
);
// 处理响应
if (response.getStatusCode() == HttpStatus.OK) {
return response.getBody();
} else {
throw new RuntimeException("Request failed with status code: " + response.getStatusCode());
}
}
}

@ -0,0 +1,68 @@
package com.ruoyi.framework.web.service;
import java.util.List;
/**
* ClassName: DeepSeekRequest
* Package: com.ruoyi.system.domain
* Description:
*
* @Author wangxy
* @Create 2025/2/14 10:28
* @Version 1.0
*/
public class DeepSeekRequest {
private String model;
private List<Message> messages;
private boolean stream;
// Getters and Setters
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public List<Message> getMessages() {
return messages;
}
public void setMessages(List<Message> messages) {
this.messages = messages;
}
public boolean isStream() {
return stream;
}
public void setStream(boolean stream) {
this.stream = stream;
}
// 定义 Message 内部类
public static class Message {
private String role;
private String content;
// Getters and Setters
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
}
Loading…
Cancel
Save