提交 21151fb8 作者: 925993793@qq.com

【fix】自定义专题创建时,关键词推荐功能换用LLM:glm-4-flash

上级 a6a40fad
...@@ -253,7 +253,15 @@ public class SubjectManageController { ...@@ -253,7 +253,15 @@ public class SubjectManageController {
jsonObject.put("article", statisticsKeyWordVos); jsonObject.put("article", statisticsKeyWordVos);
} }
if (StringUtils.isNotBlank(words)) { if (StringUtils.isNotBlank(words)) {
List<StatisticsKeyWordVo> statisticsKeyWordVos = extractWords(words); //List<StatisticsKeyWordVo> statisticsKeyWordVos = extractWords(words);
List<StatisticsKeyWordVo> statisticsKeyWordVos = new ArrayList<>();
List<String> list = LLMUtil.wordRecommend(words);
for (String s : list) {
StatisticsKeyWordVo statisticsKeyWordVo = new StatisticsKeyWordVo();
statisticsKeyWordVo.setName(s);
statisticsKeyWordVo.setValue(0);
statisticsKeyWordVos.add(statisticsKeyWordVo);
}
jsonObject.put("word", statisticsKeyWordVos); jsonObject.put("word", statisticsKeyWordVos);
} }
return Result.OK(jsonObject); return Result.OK(jsonObject);
...@@ -1262,9 +1270,9 @@ public class SubjectManageController { ...@@ -1262,9 +1270,9 @@ public class SubjectManageController {
for (SpecialInformation information : records) { for (SpecialInformation information : records) {
text.append(information.getTitle()).append(information.getContent()); text.append(information.getTitle()).append(information.getContent());
} }
List<String> wordsList = pythonUtil.extractKeyword(text.toString(), 10); //List<String> wordsList = pythonUtil.extractKeyword(text.toString(), 10);
wordList = formatWordInfo(text.toString(), wordsList); //wordList = formatWordInfo(text.toString(), wordsList);
/*List<Map.Entry<String, Integer>> extractKeyWordsByText = HanlpUtil.extractKeyWordsByText(text.toString(), 10); List<Map.Entry<String, Integer>> extractKeyWordsByText = HanlpUtil.extractKeyWordsByText(text.toString(), 10);
if (CollectionUtils.isNotEmpty(extractKeyWordsByText)) { if (CollectionUtils.isNotEmpty(extractKeyWordsByText)) {
for (Map.Entry<String, Integer> entry : extractKeyWordsByText) { for (Map.Entry<String, Integer> entry : extractKeyWordsByText) {
StatisticsKeyWordVo statisticsKeyWordVo = new StatisticsKeyWordVo(); StatisticsKeyWordVo statisticsKeyWordVo = new StatisticsKeyWordVo();
...@@ -1272,7 +1280,7 @@ public class SubjectManageController { ...@@ -1272,7 +1280,7 @@ public class SubjectManageController {
statisticsKeyWordVo.setValue(entry.getValue()); statisticsKeyWordVo.setValue(entry.getValue());
wordList.add(statisticsKeyWordVo); wordList.add(statisticsKeyWordVo);
} }
}*/ }
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
......
package com.zzsn.event.util;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* dify大模型接口调用
*
* @author lkg
* @date 2025/3/21
*/
@Slf4j
public class LLMUtil {
final static String ChatGlm4_URL = "https://open.bigmodel.cn/api/paas/v4/chat/completions";
final static String API_KEY = "2eb40c7bb8e4d22b389cd9ada5a8d6cc.bAbE6g6y71TeC0on";
/**
* LLM-推荐关键词
*
* @param keyword 关键词
* @author lkg
* @date 2025/3/21
*/
public static List<String> wordRecommend(String keyword) {
List<String> list = new ArrayList<>();
JSONObject params = new JSONObject();
List<JSONObject> messages = new ArrayList<>();
JSONObject message = new JSONObject();
String query = String.format("请推荐[%s]相关的10个近义词且只要结果", keyword);
message.put("content", query);
message.put("role", "user");
messages.add(message);
params.put("messages", messages);
params.put("model", "glm-4-flash");
JSONObject responseFormat = new JSONObject();
responseFormat.put("type", "json_object");
params.put("response_format", responseFormat);
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
headers.put("Authorization", "Bearer " + API_KEY);
String response = null;
try {
response = HttpUtil.doPostWithHeader(ChatGlm4_URL, params, 30000, headers);
JSONObject jsonObject = JSON.parseObject(response);
JSONArray choices = jsonObject.getJSONArray("choices");
JSONObject choice = choices.getJSONObject(0);
JSONObject content = choice.getJSONObject("message").getJSONObject("content");
JSONArray answer = content.getJSONArray("answer");
list = answer.toJavaList(String.class);
} catch (Exception e) {
log.info("大模型-{},调用异常:{}","glm-4-flash",e.getMessage());
log.info("返回结果:{}",response);
}
return list;
}
public static void main(String[] args) {
List<String> list = wordRecommend("人工智能|汽车");
System.out.println(JSON.toJSONString(list));
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论