Commit 92231307 authored by zhouwei's avatar zhouwei

Merge branch 'dev' into 'master'

增强新闻提取国家和城市信息逻辑

See merge request !21
parents 94923ced b4190407
package com.nanyan.securitylink.entity;
import lombok.Data;
import java.util.List;
@Data
public class NewsCountryCity {
List<String> country;
List<String> city;
}
...@@ -25,10 +25,7 @@ import org.springframework.http.ResponseEntity; ...@@ -25,10 +25,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import java.util.ArrayList; import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j @Slf4j
...@@ -596,8 +593,8 @@ public class AIServiceImpl implements AIService { ...@@ -596,8 +593,8 @@ public class AIServiceImpl implements AIService {
" - 对\"某国\"\"该地区\"等表述,需结合上下文地理特征、政治背景推断\n" + " - 对\"某国\"\"该地区\"等表述,需结合上下文地理特征、政治背景推断\n" +
" - 非中文表述需翻译为中文规范名称\n" + " - 非中文表述需翻译为中文规范名称\n" +
"4. 输出规范:\n" + "4. 输出规范:\n" +
" - 仅返回标准JSON格式:{\"country\": \"country\", \"city\": \"city\"}\n" + " - 仅返回标准JSON格式:{\"country\": [\"country\"], \"city\": [\"city\"]}\n" +
" - 无地理信息时返回:{\"country\": \"\", \"city\": \"\"}\n" + " - 无地理信息时返回:{\"country\": [], \"city\": []}\n" +
" - 禁止包含任何解释性文字或XML标签\n" + " - 禁止包含任何解释性文字或XML标签\n" +
" - 翻译成英文\n" + " - 翻译成英文\n" +
"5. 验证逻辑:\n" + "5. 验证逻辑:\n" +
...@@ -608,10 +605,11 @@ public class AIServiceImpl implements AIService { ...@@ -608,10 +605,11 @@ public class AIServiceImpl implements AIService {
"- 国家城市名称必须使用新闻原文中的语言版本\n" + "- 国家城市名称必须使用新闻原文中的语言版本\n" +
"- 不存在的行政关系需视为无效信息(如\"巴黎属于德国\")\n" + "- 不存在的行政关系需视为无效信息(如\"巴黎属于德国\")\n" +
"- 多地点新闻只提取核心事件发生地\n" + "- 多地点新闻只提取核心事件发生地\n" +
"- 返回的国家和城市名称必须是通俗意义的常见的英文名称,不能输出国家和城市中文名称\n" +
"\n" + "\n" +
"<additional_rules>\n" + "<additional_rules>\n" +
"1. 国家城市名称标准化:\n" + "1. 国家城市名称标准化:\n" +
" - 优先使用主权国家名称(如\"中国\"而非\"中国大陆\")\n"; " - 优先使用主权国家名称(如\"中国\"而非\"中国大陆\")";
buildChatInputData(chatInputData, aiRequestDTO.getInputs().getMsg_info(), systemPrompt); buildChatInputData(chatInputData, aiRequestDTO.getInputs().getMsg_info(), systemPrompt);
String apiKey = getModelApiKey(); String apiKey = getModelApiKey();
ChatCompletionResponse chatCompletionResponse = aiRequest(chatInputData, apiKey); ChatCompletionResponse chatCompletionResponse = aiRequest(chatInputData, apiKey);
...@@ -626,6 +624,7 @@ public class AIServiceImpl implements AIService { ...@@ -626,6 +624,7 @@ public class AIServiceImpl implements AIService {
} }
content = content.replaceAll("```json","").replaceAll("```", "").replaceAll("\n",""); content = content.replaceAll("```json","").replaceAll("```", "").replaceAll("\n","");
JSONObject jsonObject = JSONObject.parseObject(content); JSONObject jsonObject = JSONObject.parseObject(content);
jsonObject = convertData(jsonObject);
List<JSONObject> jsonObjects = new ArrayList<>(); List<JSONObject> jsonObjects = new ArrayList<>();
jsonObjects.add(jsonObject); jsonObjects.add(jsonObject);
ResultVO<JSONObject> outputs = new ResultVO<>(); ResultVO<JSONObject> outputs = new ResultVO<>();
...@@ -636,6 +635,56 @@ public class AIServiceImpl implements AIService { ...@@ -636,6 +635,56 @@ public class AIServiceImpl implements AIService {
} }
return null; return null;
} }
private static Map<String,String> country = new HashMap<>();
private static Map<String,String> city = new HashMap<>();
static {
country.put("us", "United States");
country.put("usa", "United States");
country.put("the united states", "United States");
city.put("washington", "Washington");
city.put(" d.c.", "Washington");
city.put("washington dc", "Washington");
city.put("washington d.c.", "Washington");
city.put("san diego", "Santiago");
city.put("伊斯兰堡", "Islamabad");
city.put("Manila", "metro manila");
city.put("Kiev", "kyiv");
city.put("孟买", "Mumbai");
city.put("bombay", "Mumbai");
city.put("delhi-ncr", "New Delhi");
city.put("伦敦", "London");
city.put("new york city", "New York");
city.put("旧金山", "San Francisco");
city.put("santa cruz de la sierra", "Santa Cruz");
city.put("bengaluru", "Bangalore");
city.put("达卡", "Dhaka");
city.put("尼斯", "saint paul");
}
private JSONObject convertData(JSONObject jsonObject){
try {
NewsCountryCity newsCountryCity = JSONObject.parseObject(JSONObject.toJSONString(jsonObject), NewsCountryCity.class);
if(CollectionUtils.isNotEmpty(newsCountryCity.getCity())){
Set<String> set = new HashSet<>();
for (String value : newsCountryCity.getCity()) {
set.add(city.getOrDefault(value, value));
}
newsCountryCity.getCity().clear();
newsCountryCity.getCity().addAll(set);
}
if(CollectionUtils.isNotEmpty(newsCountryCity.getCountry())){
Set<String> set = new HashSet<>();
for (String value : newsCountryCity.getCountry()) {
set.add(country.getOrDefault(value, value));
}
}
return JSONObject.parseObject(JSONObject.toJSONString(newsCountryCity));
}catch (Exception e){
log.info("", e);
return jsonObject;
}
}
private String getModelApiKey() { private String getModelApiKey() {
UserHeader userHeader = UserThreadLocal.get(); UserHeader userHeader = UserThreadLocal.get();
......
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