Commit ed4aafc7 authored by zhouwei's avatar zhouwei

新闻标签

parent f462e6f9
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -12,8 +12,11 @@ import com.nanyan.securitylink.entity.Message; ...@@ -12,8 +12,11 @@ import com.nanyan.securitylink.entity.Message;
import com.nanyan.securitylink.execption.BaseException; import com.nanyan.securitylink.execption.BaseException;
import com.nanyan.securitylink.service.AIService; import com.nanyan.securitylink.service.AIService;
import com.nanyan.securitylink.vo.AIResponse; import com.nanyan.securitylink.vo.AIResponse;
import com.nanyan.securitylink.vo.CodeVO;
import com.nanyan.securitylink.vo.ResultVO;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity; import org.springframework.http.HttpEntity;
...@@ -23,7 +26,9 @@ import org.springframework.stereotype.Service; ...@@ -23,7 +26,9 @@ import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
@Slf4j @Slf4j
...@@ -33,17 +38,60 @@ public class DeepSeekAIServiceImpl implements AIService { ...@@ -33,17 +38,60 @@ public class DeepSeekAIServiceImpl implements AIService {
AppConfig appConfig; AppConfig appConfig;
@Autowired @Autowired
RestTemplate restTemplate; RestTemplate restTemplate;
private static final Map<String, String> LABEL_MAP = new HashMap<>();
static {
LABEL_MAP.put("枪击", "TagShooting");
LABEL_MAP.put("抢劫", "TagRobbery");
LABEL_MAP.put("爆炸", "TagExplosion");
LABEL_MAP.put("恐怖袭击", "TagTerroristAttack");
LABEL_MAP.put("纵火", "TagArson");
LABEL_MAP.put("大规模伤亡事件", "TagMassCasualtyIncident");
LABEL_MAP.put("人质事件", "TagHostageSituation");
LABEL_MAP.put("毒气泄漏", "TagToxicGasLeak");
LABEL_MAP.put("生物危害", "TagBiologicalHazard");
LABEL_MAP.put("交通意外", "TagTrafficAccident");
LABEL_MAP.put("建筑物倒塌", "TagBuildingCollapse");
LABEL_MAP.put("食品安全", "TagFoodSafety");
LABEL_MAP.put("社会冲突", "TagSocialConflict");
LABEL_MAP.put("群体性事件", "TagMassGatheringIncident");
LABEL_MAP.put("环境污染", "TagEnvironmentalPollution");
LABEL_MAP.put("重大事故", "TagMajorAccident");
LABEL_MAP.put("抗议示威", "TagProtestDemonstration");
LABEL_MAP.put("集体维权", "TagCollectiveAdvocacy");
LABEL_MAP.put("活动失控骚乱", "TagUncontrolledEventRiot");
LABEL_MAP.put("地域冲突", "TagTerritorialConflict");
LABEL_MAP.put("性别冲突", "TagGenderConflict");
LABEL_MAP.put("宗教冲突", "TagReligiousConflict");
LABEL_MAP.put("民族冲突", "TagEthnicConflict");
LABEL_MAP.put("阶级冲突", "TagClassConflict");
LABEL_MAP.put("食物中毒", "TagFoodPoisoning");
LABEL_MAP.put("公共卫生事件", "PublicHealthEmergency");
LABEL_MAP.put("环境污染", "TagEnvironmentalPollution");
LABEL_MAP.put("活动失控骚乱", "TagUncontrolledEventRiot");
LABEL_MAP.put("抗议示威", "TagProtestDemonstration");
LABEL_MAP.put("工业事故", "IndustriaAccident");
LABEL_MAP.put("核事故", "NuclearAccident");
LABEL_MAP.put("性别冲突", "TagGenderConflict");
LABEL_MAP.put("矿难", "MiningAccident");
LABEL_MAP.put("游行抗议", "Protest");
LABEL_MAP.put("水坝溃坝", "DamBurst");
LABEL_MAP.put("油轮泄漏", "OilSpill");
LABEL_MAP.put("火灾", "Fire");
}
private final static ObjectMapper objectMapper = new ObjectMapper(); private final static ObjectMapper objectMapper = new ObjectMapper();
// private final static String DEEP_SEEK_URL = "https://api.deepseek.com/chat/completions"; // private final static String DEEP_SEEK_URL = "https://api.deepseek.com/chat/completions";
private final static String DEEP_SEEK_URL = "https://ark.cn-beijing.volces.com/api/v3/chat/completions"; private final static String DEEP_SEEK_URL = "https://ark.cn-beijing.volces.com/api/v3/chat/completions";
@Override @Override
public AIResponse translate(TranslateDTO translateDTO) { public AIResponse translate(TranslateDTO translateDTO) {
ChatInputData chatInputData = new ChatInputData(); ChatInputData chatInputData = new ChatInputData();
buildTranslate(chatInputData,translateDTO); buildTranslate(chatInputData, translateDTO);
ChatCompletionResponse chatCompletionResponse = aiRequest(chatInputData, appConfig.getApiTranslateKey()); ChatCompletionResponse chatCompletionResponse = aiRequest(chatInputData, appConfig.getApiTranslateKey());
if(CollectionUtils.isNotEmpty(chatCompletionResponse.getChoices())){ if (CollectionUtils.isNotEmpty(chatCompletionResponse.getChoices())) {
AIResponse aiResponse = new AIResponse(); AIResponse aiResponse = new AIResponse();
aiResponse.setAnswer(chatCompletionResponse.getChoices().get(0).getMessage().getContent()); aiResponse.setAnswer(chatCompletionResponse.getChoices().get(0).getMessage().getContent());
return aiResponse; return aiResponse;
...@@ -54,12 +102,27 @@ public class DeepSeekAIServiceImpl implements AIService { ...@@ -54,12 +102,27 @@ public class DeepSeekAIServiceImpl implements AIService {
public AIResponse newsTags(TranslateDTO translateDTO) { public AIResponse newsTags(TranslateDTO translateDTO) {
ChatInputData chatInputData = new ChatInputData(); ChatInputData chatInputData = new ChatInputData();
buildNewsTag(chatInputData,translateDTO); buildNewsTag(chatInputData, translateDTO);
ChatCompletionResponse chatCompletionResponse = aiRequest(chatInputData, appConfig.getApiTranslateKey()); ChatCompletionResponse chatCompletionResponse = aiRequest(chatInputData, appConfig.getApiTranslateKey());
if(CollectionUtils.isNotEmpty(chatCompletionResponse.getChoices())){ if (CollectionUtils.isNotEmpty(chatCompletionResponse.getChoices())) {
AIResponse aiResponse = new AIResponse(); AIResponse aiResponse = new AIResponse();
aiResponse.setAnswer(chatCompletionResponse.getChoices().get(0).getMessage().getContent()); String content = chatCompletionResponse.getChoices().get(0).getMessage().getContent();
if(StringUtils.isNotEmpty(content)){
String[] split = content.split(",", -1);
List<CodeVO> result = new ArrayList<>();
ResultVO outputs = new ResultVO();
outputs.setResult(result);
aiResponse.setOutputs(outputs);
for (String s : split) {
if(LABEL_MAP.containsKey(s)){
CodeVO codeVO = new CodeVO();
codeVO.setCode(LABEL_MAP.get(s));
codeVO.setName(s);
result.add(codeVO);
}
}
}
return aiResponse; return aiResponse;
} }
return null; return null;
...@@ -77,7 +140,7 @@ public class DeepSeekAIServiceImpl implements AIService { ...@@ -77,7 +140,7 @@ public class DeepSeekAIServiceImpl implements AIService {
} }
@NotNull @NotNull
private ChatCompletionResponse aiRequest(ChatInputData chatInputData , String apiKey) { private ChatCompletionResponse aiRequest(ChatInputData chatInputData, String apiKey) {
String writeValueAsString = null; String writeValueAsString = null;
try { try {
writeValueAsString = objectMapper.writeValueAsString(chatInputData); writeValueAsString = objectMapper.writeValueAsString(chatInputData);
...@@ -86,9 +149,9 @@ public class DeepSeekAIServiceImpl implements AIService { ...@@ -86,9 +149,9 @@ public class DeepSeekAIServiceImpl implements AIService {
} }
log.info("translate json:{}", writeValueAsString); log.info("translate json:{}", writeValueAsString);
HttpEntity header = getHeader(apiKey); HttpEntity header = getHeader(apiKey);
HttpEntity<String> entity = new HttpEntity<>(writeValueAsString,header.getHeaders()); HttpEntity<String> entity = new HttpEntity<>(writeValueAsString, header.getHeaders());
ResponseEntity<ChatCompletionResponse> response = restTemplate.postForEntity(DEEP_SEEK_URL, entity, ChatCompletionResponse.class); ResponseEntity<ChatCompletionResponse> response = restTemplate.postForEntity(DEEP_SEEK_URL, entity, ChatCompletionResponse.class);
if(response.getStatusCode().is2xxSuccessful()){ if (response.getStatusCode().is2xxSuccessful()) {
log.info("translate response:{}", JSONObject.toJSONString(response.getBody())); log.info("translate response:{}", JSONObject.toJSONString(response.getBody()));
return response.getBody(); return response.getBody();
} }
...@@ -108,18 +171,19 @@ public class DeepSeekAIServiceImpl implements AIService { ...@@ -108,18 +171,19 @@ public class DeepSeekAIServiceImpl implements AIService {
private static Message getDeepSeekMessage(TranslateDTO translateDTO) { private static Message getDeepSeekMessage(TranslateDTO translateDTO) {
Message systemRole = new Message(); Message systemRole = new Message();
systemRole.setRole("user"); systemRole.setRole("user");
systemRole.setContent("需要翻译的JSON:"+ translateDTO.getInputs().getRecord_json() + "\n" + systemRole.setContent("需要翻译的JSON:" + translateDTO.getInputs().getRecord_json() + "\n" +
"\n" + "\n" +
"你是一个 JSON 翻译员,能够将给定的 JSON 中的 value 值部分翻译成 `"+ translateDTO.getInputs().getLanguage() +"`。\n" + "你是一个 JSON 翻译员,能够将给定的 JSON 中的 value 值部分翻译成 `" + translateDTO.getInputs().getLanguage() + "`。\n" +
"\n" + "\n" +
"## 限制\n" + "## 限制\n" +
"- 严格按照要求进行翻译,不改变 JSON 的结构和无意义的 key 的 value。\n" + "- 严格按照要求进行翻译,不改变 JSON 的结构和无意义的 key 的 value。\n" +
"- 仅翻译有意义的 value 部分,确保翻译准确、通顺。"+ "- 仅翻译有意义的 value 部分,确保翻译准确、通顺。" +
"- 无需任何其他说明。"+ "- 无需任何其他说明。" +
"- 数字、特殊符号(@#%&等)连接的单词无需翻译,其他还需翻译。" "- 数字、特殊符号(@#%&等)连接的单词无需翻译,其他还需翻译。"
); );
return systemRole; return systemRole;
} }
private static Message getUserTagDeepSeekMessage(TranslateDTO translateDTO) { private static Message getUserTagDeepSeekMessage(TranslateDTO translateDTO) {
Message userRole = new Message(); Message userRole = new Message();
userRole.setRole("user"); userRole.setRole("user");
...@@ -177,10 +241,10 @@ public class DeepSeekAIServiceImpl implements AIService { ...@@ -177,10 +241,10 @@ public class DeepSeekAIServiceImpl implements AIService {
return systemRole; return systemRole;
} }
private HttpEntity getHeader(String token){ private HttpEntity getHeader(String token) {
HttpHeaders httpHeaders = new HttpHeaders(); HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set("Content-Type","application/json"); httpHeaders.set("Content-Type", "application/json");
httpHeaders.set("Authorization",String.format("Bearer %s",token)); httpHeaders.set("Authorization", String.format("Bearer %s", token));
return new HttpEntity<>(httpHeaders); return new HttpEntity<>(httpHeaders);
} }
} }
...@@ -10,5 +10,6 @@ public class AIResponse { ...@@ -10,5 +10,6 @@ public class AIResponse {
private String message_id; private String message_id;
private String mode; private String mode;
private String answer; private String answer;
private ResultVO outputs;
private long created_at; private long created_at;
} }
package com.nanyan.securitylink.vo;
import lombok.Data;
@Data
public class CodeVO {
String code;
String name;
}
package com.nanyan.securitylink.vo;
import lombok.Data;
import java.util.List;
@Data
public class ResultVO {
List<CodeVO> result;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment