Commit 8202f94f authored by zhouwei's avatar zhouwei

新增提取新闻中的国家和城市信息

parent d1bd4b38
...@@ -37,4 +37,9 @@ public class AIController { ...@@ -37,4 +37,9 @@ public class AIController {
public Response<AIResponse<JSONObject>> dealUrgentNotice(@RequestBody AIRequestDTO AIRequestDTO) { public Response<AIResponse<JSONObject>> dealUrgentNotice(@RequestBody AIRequestDTO AIRequestDTO) {
return Response.SUCCESS(aiService.dealUrgentNotice(AIRequestDTO)); return Response.SUCCESS(aiService.dealUrgentNotice(AIRequestDTO));
} }
@PostMapping("/news/country/city")
public Response<AIResponse<JSONObject>> collectNewsCountryAndCity(@RequestBody AIRequestDTO AIRequestDTO) {
return Response.SUCCESS(aiService.collectNewsCountryAndCity(AIRequestDTO));
}
} }
...@@ -14,4 +14,6 @@ public interface AIService { ...@@ -14,4 +14,6 @@ public interface AIService {
AIResponse<JSONObject> newsTagAndTranslate(AIRequestDTO aiRequestDTO); AIResponse<JSONObject> newsTagAndTranslate(AIRequestDTO aiRequestDTO);
AIResponse<JSONObject> dealUrgentNotice(AIRequestDTO aiRequestDTO); AIResponse<JSONObject> dealUrgentNotice(AIRequestDTO aiRequestDTO);
AIResponse<JSONObject> collectNewsCountryAndCity(AIRequestDTO aiRequestDTO);
} }
...@@ -462,6 +462,61 @@ public class AIServiceImpl implements AIService { ...@@ -462,6 +462,61 @@ public class AIServiceImpl implements AIService {
return null; return null;
} }
@Override
public AIResponse<JSONObject> collectNewsCountryAndCity(AIRequestDTO aiRequestDTO) {
ChatInputData chatInputData = new ChatInputData();
String systemPrompt = "```xml\n" +
"<instruction>\n" +
"你是一个专业的地理信息分析师,请严格遵循以下步骤处理新闻文本:\n" +
"\n" +
"1. 仔细阅读新闻正文内容,忽略所有非事件描述的信息(如媒体机构、记者信息等)\n" +
"2. 定位事件直接关联的具体地理位置:\n" +
" - 优先提取明确提及的规范国家名称和城市名称\n" +
" - 城市必须能明确归属于国家行政体系\n" +
"3. 处理模糊表述:\n" +
" - 对\"某国\"\"该地区\"等表述,需结合上下文地理特征、政治背景推断\n" +
" - 非中文表述需翻译为中文规范名称\n" +
"4. 输出规范:\n" +
" - 仅返回标准JSON格式:{\"country\": \"国家\", \"city\": \"城市\"}\n" +
" - 无地理信息时返回:{\"country\": \"\", \"city\": \"\"}\n" +
" - 禁止包含任何解释性文字或XML标签\n" +
"5. 验证逻辑:\n" +
" - 确保城市隶属于国家(如东京属于日本)\n" +
" - 拒绝推测性结论,必须有文本依据\n" +
"\n" +
"注意事项:\n" +
"- 国家城市名称必须使用新闻原文中的语言版本\n" +
"- 不存在的行政关系需视为无效信息(如\"巴黎属于德国\")\n" +
"- 多地点新闻只提取核心事件发生地\n" +
"\n" +
"<additional_rules>\n" +
"1. 国家城市名称标准化:\n" +
" - 优先使用主权国家名称(如\"中国\"而非\"中国大陆\")\n";
buildChatInputData(chatInputData, aiRequestDTO.getInputs().getMsg_info(), systemPrompt);
String apiKey = getModelApiKey();
ChatCompletionResponse chatCompletionResponse = aiRequest(chatInputData, apiKey);
if (CollectionUtils.isNotEmpty(chatCompletionResponse.getChoices())) {
AIResponse<JSONObject> aiResponse = new AIResponse<>();
String content = chatCompletionResponse.getChoices().get(0).getMessage().getContent();
aiResponse.setFinish_reason(chatCompletionResponse.getChoices().get(0).getFinish_reason());
if(StringUtils.isNotEmpty(content)){
if(content.contains("</think>")){
content = content.split("</think>", -1)[1];
}
content = content.replaceAll("```json","").replaceAll("```", "").replaceAll("\n","");
JSONObject jsonObject = JSONObject.parseObject(content);
List<JSONObject> jsonObjects = new ArrayList<>();
jsonObjects.add(jsonObject);
ResultVO<JSONObject> outputs = new ResultVO<>();
outputs.setResult(jsonObjects);
aiResponse.setOutputs(outputs);
}
return aiResponse;
}
return null;
}
private String getModelApiKey() { private String getModelApiKey() {
UserHeader userHeader = UserThreadLocal.get(); UserHeader userHeader = UserThreadLocal.get();
String apiKey = appConfig.getApiTranslateKey(); String apiKey = appConfig.getApiTranslateKey();
......
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