Skip to content

Commit 1eb683a

Browse files
committed
feat:fix knowledge-gragh model context problem
1 parent 8ef7db7 commit 1eb683a

File tree

10 files changed

+41
-19
lines changed

10 files changed

+41
-19
lines changed

docs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ mysql,redis,emqx和influxdb环境。
8888
欢迎加入群聊一起交流讨论有关Aiot相关的话题,免费获取智控台的前端源码,链接过期了可以issue或email提醒一下作者。
8989

9090
<div style="width: 250px;margin: 0 auto;">
91-
<img src="./images/7324111a30d61b0b9a0cc63f2d1c8781.jpg" width="250px"/>
91+
<img src="./images/0accee4cf2e6b999e567432173cbf62c.jpg" width="250px"/>
9292
</div>
9393

9494

167 KB
Loading
-165 KB
Binary file not shown.

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ mysql,redis,emqx和influxdb环境,安装详情请看官方文档。
116116
欢迎加入群聊一起交流讨论有关Aiot相关的话题,有机会获取项目的免费部署咨询,链接过期了可以issue或email提醒一下作者。
117117

118118
<div style="width: 250px;margin: 0 auto;">
119-
<img src="./docs/images/7324111a30d61b0b9a0cc63f2d1c8781.jpg" width="250px"/>
119+
<img src="./docs/images/0accee4cf2e6b999e567432173cbf62c.jpg" width="250px"/>
120120
</div>
121121

122122
## 致谢

src/main/java/top/rslly/iot/utility/ai/tools/KnowledgeGraphicTool.java

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222
import com.alibaba.fastjson.JSONObject;
2323
import lombok.Data;
2424
import lombok.extern.slf4j.Slf4j;
25+
import org.jspecify.annotations.NonNull;
2526
import org.springframework.beans.factory.annotation.Autowired;
2627
import org.springframework.beans.factory.annotation.Value;
28+
import org.springframework.scheduling.annotation.Async;
2729
import org.springframework.stereotype.Component;
2830
import top.rslly.iot.models.KnowledgeGraphicNodeEntity;
2931
import top.rslly.iot.param.request.KnowledgeGraphicNode;
@@ -42,7 +44,7 @@
4244
@Component
4345
@Data
4446
@Slf4j
45-
public class KnowledgeGraphicTool implements BaseTool<String> {
47+
public class KnowledgeGraphicTool {
4648
@Autowired
4749
private ProductService productService;
4850
@Autowired
@@ -53,40 +55,62 @@ public class KnowledgeGraphicTool implements BaseTool<String> {
5355
private KnowledgeGraphicPrompt knowledgeGraphicPrompt;
5456
@Value("${ai.knowledge_graphic_llm}")
5557
private String llmName;
58+
@Value("${ai.knowledge_graphic.max_graphic_length:30000}")
59+
private int maxGraphicLength;
5660
private String name = "knowledgeGraphicTool";
5761
private String description = """
5862
This tool is working for knowledge graphic generation to help LLM
5963
handle relations between entities.
6064
""";
6165

62-
@Override
6366
public String run(String question) {
6467
return null;
6568
}
6669

67-
@Override
68-
public String run(String question, Map<String, Object> globalMessage) {
70+
@Async("taskExecutor")
71+
public void run(String question, Map<String, Object> globalMessage) {
6972
int productId = (int) globalMessage.get("productId");
7073
if (productService.findAllById(productId).isEmpty())
71-
return "";
74+
return;
7275
LLM llm = llmDiyUtility.getDiyLlm(productId, llmName, "knowledgeGraphic");
7376
List<ModelMessage> memory =
7477
Optional.ofNullable((List<ModelMessage>) globalMessage.get("memory"))
7578
.orElse(Collections.emptyList());
7679
List<ModelMessage> messages = new ArrayList<>();
80+
String knowledgeSystemPrompt =
81+
knowledgeGraphicPrompt.getKnowledgeGraphicPrompt(productId) + memory;
7782
ModelMessage systemMessage = new ModelMessage(ModelMessageRole.SYSTEM.value(),
78-
knowledgeGraphicPrompt.getKnowledgeGraphicPrompt(productId) + memory);
79-
ModelMessage userMessage = new ModelMessage(ModelMessageRole.USER.value(),
80-
"Start generate knowledge graphic.");
83+
knowledgeSystemPrompt);
84+
ModelMessage userMessage = getUserMessage(knowledgeSystemPrompt);
8185
messages.add(systemMessage);
8286
messages.add(userMessage);
87+
if (knowledgeSystemPrompt.length() > maxGraphicLength) {
88+
log.error("Knowledge Graphic extract error:\n{}", "Knowledge Graphic is too long.");
89+
return;
90+
}
8391
var obj = llm.jsonChat(question, messages, true).getJSONObject("action");
8492
try {
8593
process_llm_result(obj, productId);
8694
} catch (Exception e) {
8795
log.error("Knowledge Graphic extract error:\n{}", e.getMessage());
8896
}
89-
return null;
97+
}
98+
99+
private @NonNull ModelMessage getUserMessage(String knowledgeSystemPrompt) {
100+
ModelMessage userMessage;
101+
if (knowledgeSystemPrompt.length() > maxGraphicLength / 2) {
102+
log.info("Knowledge start summary");
103+
userMessage = new ModelMessage(ModelMessageRole.USER.value(),
104+
"""
105+
The graph has reached its maximum capacity.
106+
Summarize the graph immediately, ensuring that the most recent content is not lost,
107+
and begin generating the graph.
108+
""");
109+
} else {
110+
userMessage = new ModelMessage(ModelMessageRole.USER.value(),
111+
"Start generate knowledge graphic.");
112+
}
113+
return userMessage;
90114
}
91115

92116
private void process_llm_result(JSONObject jsonObject, int productId) {

src/main/java/top/rslly/iot/utility/smartVoice/XiaoZhiUtil.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,6 @@ public void dealWithAudio(List<byte[]> audioList, String chatId, int productId,
277277
}
278278
while ((res != null && !res.isDone()) || Router.queueMap.containsKey(chatId)
279279
&& Router.queueMap.get(chatId).size() > 0) {
280-
if (!Router.queueMap.containsKey(chatId)) {
281-
log.warn("queueMap已被释放,停止处理: chatId={}", chatId);
282-
break;
283-
}
284280
if (emotionRes != null && emotionRes.isDone() && !emotionFlag) {
285281
try {
286282
Map<String, String> emotionResult = emotionRes.get();

src/main/resources/application.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ js:
123123
local:
124124
use_js_sandbox: true
125125
monitor_thread_pool_size: 4
126-
max_cpu_time: 300000
126+
max_cpu_time: 10000
127127
max_black_list_duration_sec: 60
128128
max_requests_timeout: 10
129129
js_thread_pool_size: 16
@@ -198,6 +198,8 @@ ai:
198198
longMemoryTool-llm: silicon-Qwen/Qwen2.5-14B-Instruct
199199
wx_smart_robot_llm: glm
200200
knowledge_graphic_llm: silicon-Qwen/Qwen3-Next-80B-A3B-Instruct
201+
knowledge_graphic:
202+
max_graphic_length: 30000
201203
mcp:
202204
agent-llm: silicon-Qwen/Qwen3-235B-A22B-Instruct-2507
203205
agent-speedUp: true

web/.env.development

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
VITE_MODE='development'
22
VITE_BASE_PATH='/'
3-
VITE_BASE_URL ='http://localhost:8080'
3+
VITE_BASE_URL ='https://api-ic.atdak.com/'
44
VITE_LOCAL_TEST_URL="http://localhost:3002/"

web/.env.production

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
VITE_MODE='production'
22
VITE_BASE_PATH='/'
3-
VITE_BASE_URL ='http://localhost:8080'
3+
VITE_BASE_URL ='https://api-ic.atdak.com/'
44
VITE_LOCAL_TEST_URL="http://localhost:3002/"

web/src/views/knowledgeGraphic/index.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ function fetchProducts() {
132132
if (errorCode === 200 && data && Array.isArray(data)) {
133133
products.value = data.map((item, index) => {
134134
return {
135-
key: item.keyvalue ? item.keyvalue : index,
135+
key: item.id,
136136
value: item.id,
137137
label: item.productName,
138138
}

0 commit comments

Comments
 (0)