This commit is contained in:
lu7213 committed 2026-07-07 17:21:13 +08:00
1 parent 07f5922a7d
commit 90d74de0d8
5 files changed
+130

No files matched your search

@@ -0,0 +1,22 @@
package com.ruoyi.system.controller;
import com.ruoyi.system.service.DeepSeekService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/deepseek")
public class DeepSeekController {
@Autowired
private DeepSeekService deepSeekService;
@GetMapping("/stock")
public String stock(String code) {
String prompt =
"分析股票 " + code +
" 的投资价值";
return deepSeekService.chat(prompt);
}
}
@@ -0,0 +1,21 @@
package com.ruoyi.system.domain;
import lombok.Data;
import java.util.List;
@Data
public class DeepSeekRequest {
private String model;
private List<Message> messages;
@Data
public static class Message {
private String role;
private String content;
public Message(String role, String content) {
this.role = role;
this.content = content;
}
}
}
@@ -0,0 +1,21 @@
package com.ruoyi.system.domain;
import lombok.Data;
import java.util.List;
@Data
public class DeepSeekResponse {
private List<Choice> choices;
@Data
public static class Choice {
private Message message;
}
@Data
public static class Message {
private String content;
}
}
@@ -0,0 +1,5 @@
package com.ruoyi.system.service;
public interface DeepSeekService {
String chat(String prompt);
}
@@ -0,0 +1,61 @@
package com.ruoyi.system.service.impl;
import com.ruoyi.system.domain.DeepSeekRequest;
import com.ruoyi.system.domain.DeepSeekResponse;
import com.ruoyi.system.service.DeepSeekService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.Arrays;
@Service
public class DeepSeekServiceImpl implements DeepSeekService {
@Value("${deepseek.api-key}")
private String apiKey;
@Value("${deepseek.base-url}")
private String url;
private final RestTemplate restTemplate = new RestTemplate();
public String chat(String prompt) {
DeepSeekRequest request = new DeepSeekRequest();
// DeepSeek-R1
request.setModel("deepseek-reasoner");
request.setMessages(Arrays.asList(
new DeepSeekRequest.Message(
"system",
"你是一名A股分析师"
),
new DeepSeekRequest.Message(
"user",
prompt
)
));
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setBearerAuth(apiKey);
HttpEntity<DeepSeekRequest> entity =
new HttpEntity<>(request, headers);
ResponseEntity<DeepSeekResponse> response =
restTemplate.exchange(
url,
HttpMethod.POST,
entity,
DeepSeekResponse.class
);
if(response.getBody() == null){
throw new RuntimeException("deep seek api faild");
}
return response.getBody()
.getChoices()
.get(0)
.getMessage()
.getContent();
}
}