123
This commit is contained in:
1 parent
07f5922a7d
commit
90d74de0d8
5 files changed
+130
No files matched your search
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user