666ghj commited on
Commit
e0d68f7
·
1 Parent(s): e2d1868

Enhance text output formatting and remove truncation in zep_tools.py

Browse files

- Updated the `to_text` method in the `PanoramaResult` class to provide complete outputs for current facts, historical facts, and involved entities, improving data visibility.
- Modified the `to_text` method in the `AgentInterview` class to display the full agent bio without truncation.
- Adjusted the `ZepToolsService` class to retrieve all related entity details and facts without limiting the output, ensuring comprehensive data representation.

Files changed (1) hide show
  1. backend/app/services/zep_tools.py +22 -31
backend/app/services/zep_tools.py CHANGED
@@ -246,41 +246,35 @@ class PanoramaResult:
246
  }
247
 
248
  def to_text(self) -> str:
249
- """转换为文本格式"""
250
  text_parts = [
251
- f"## 未来全景预测",
252
- f"预测主题: {self.query}",
253
- f"\n### 预测世界概览",
254
- f"- 总实体数: {self.total_nodes}",
255
- f"- 总关系数: {self.total_edges}",
256
- f"- 当前有效预测: {self.active_count}条",
257
- f"- 历史演变记录: {self.historical_count}条"
258
  ]
259
 
260
- # 当前有效的事实
261
  if self.active_facts:
262
  text_parts.append(f"\n### 【当前有效事实】(模拟结果原文)")
263
- for i, fact in enumerate(self.active_facts[:30], 1):
264
  text_parts.append(f"{i}. \"{fact}\"")
265
- if len(self.active_facts) > 30:
266
- text_parts.append(f"... 还有 {len(self.active_facts) - 30} 条")
267
 
268
- # 历史/过期事实
269
  if self.historical_facts:
270
  text_parts.append(f"\n### 【历史/过期事实】(演变过程记录)")
271
- for i, fact in enumerate(self.historical_facts[:20], 1):
272
  text_parts.append(f"{i}. \"{fact}\"")
273
- if len(self.historical_facts) > 20:
274
- text_parts.append(f"... 还有 {len(self.historical_facts) - 20} 条")
275
 
276
- # 关键实体
277
  if self.all_nodes:
278
  text_parts.append(f"\n### 【涉及实体】")
279
- for node in self.all_nodes[:20]:
280
  entity_type = next((l for l in node.labels if l not in ["Entity", "Node"]), "实体")
281
  text_parts.append(f"- **{node.name}** ({entity_type})")
282
- if len(self.all_nodes) > 20:
283
- text_parts.append(f"... 还有 {len(self.all_nodes) - 20} 个实体")
284
 
285
  return "\n".join(text_parts)
286
 
@@ -307,11 +301,8 @@ class AgentInterview:
307
 
308
  def to_text(self) -> str:
309
  text = f"**{self.agent_name}** ({self.agent_role})\n"
310
- # 显示完整的agent_bio,只在超过1000字符时截断
311
- if len(self.agent_bio) > 1000:
312
- text += f"_简介: {self.agent_bio[:1000]}..._\n\n"
313
- else:
314
- text += f"_简介: {self.agent_bio}_\n\n"
315
  text += f"**Q:** {self.question}\n\n"
316
  text += f"**A:** {self.response}\n"
317
  if self.key_quotes:
@@ -1024,11 +1015,11 @@ class ZepToolsService:
1024
  if target_uuid:
1025
  entity_uuids.add(target_uuid)
1026
 
1027
- # 只获取相关实体的详情(限制数量,避免获取过多)
1028
  entity_insights = []
1029
  node_map = {} # 用于后续关系链构建
1030
 
1031
- for uuid in list(entity_uuids)[:50]: # 最多30个实体
1032
  if not uuid:
1033
  continue
1034
  try:
@@ -1038,7 +1029,7 @@ class ZepToolsService:
1038
  node_map[uuid] = node
1039
  entity_type = next((l for l in node.labels if l not in ["Entity", "Node"]), "实体")
1040
 
1041
- # 获取该实体相关的事实
1042
  related_facts = [
1043
  f for f in all_facts
1044
  if node.name.lower() in f.lower()
@@ -1049,7 +1040,7 @@ class ZepToolsService:
1049
  "name": node.name,
1050
  "type": entity_type,
1051
  "summary": node.summary,
1052
- "related_facts": related_facts[:5]
1053
  })
1054
  except Exception as e:
1055
  logger.debug(f"获取节点 {uuid} 失败: {e}")
@@ -1058,9 +1049,9 @@ class ZepToolsService:
1058
  result.entity_insights = entity_insights
1059
  result.total_entities = len(entity_insights)
1060
 
1061
- # Step 4: 构建关系链
1062
  relationship_chains = []
1063
- for edge_data in all_edges[:20]:
1064
  if isinstance(edge_data, dict):
1065
  source_uuid = edge_data.get('source_node_uuid', '')
1066
  target_uuid = edge_data.get('target_node_uuid', '')
 
246
  }
247
 
248
  def to_text(self) -> str:
249
+ """转换为文本格式(完整版本,不截断)"""
250
  text_parts = [
251
+ f"## 广度搜索结果(未来全景视图)",
252
+ f"查询: {self.query}",
253
+ f"\n### 统计信息",
254
+ f"- 总节点数: {self.total_nodes}",
255
+ f"- 总边数: {self.total_edges}",
256
+ f"- 当前有效事实: {self.active_count}条",
257
+ f"- 历史/过期事实: {self.historical_count}条"
258
  ]
259
 
260
+ # 当前有效的事实(完整输出,不截断)
261
  if self.active_facts:
262
  text_parts.append(f"\n### 【当前有效事实】(模拟结果原文)")
263
+ for i, fact in enumerate(self.active_facts, 1):
264
  text_parts.append(f"{i}. \"{fact}\"")
 
 
265
 
266
+ # 历史/过期事实(完整输出,不截断)
267
  if self.historical_facts:
268
  text_parts.append(f"\n### 【历史/过期事实】(演变过程记录)")
269
+ for i, fact in enumerate(self.historical_facts, 1):
270
  text_parts.append(f"{i}. \"{fact}\"")
 
 
271
 
272
+ # 关键实体(完整输出,不截断)
273
  if self.all_nodes:
274
  text_parts.append(f"\n### 【涉及实体】")
275
+ for node in self.all_nodes:
276
  entity_type = next((l for l in node.labels if l not in ["Entity", "Node"]), "实体")
277
  text_parts.append(f"- **{node.name}** ({entity_type})")
 
 
278
 
279
  return "\n".join(text_parts)
280
 
 
301
 
302
  def to_text(self) -> str:
303
  text = f"**{self.agent_name}** ({self.agent_role})\n"
304
+ # 显示完整的agent_bio,不截断
305
+ text += f"_简介: {self.agent_bio}_\n\n"
 
 
 
306
  text += f"**Q:** {self.question}\n\n"
307
  text += f"**A:** {self.response}\n"
308
  if self.key_quotes:
 
1015
  if target_uuid:
1016
  entity_uuids.add(target_uuid)
1017
 
1018
+ # 获取所有相关实体的详情(不限制数量,完整输出)
1019
  entity_insights = []
1020
  node_map = {} # 用于后续关系链构建
1021
 
1022
+ for uuid in list(entity_uuids): # 处理所有实体,不截断
1023
  if not uuid:
1024
  continue
1025
  try:
 
1029
  node_map[uuid] = node
1030
  entity_type = next((l for l in node.labels if l not in ["Entity", "Node"]), "实体")
1031
 
1032
+ # 获取该实体相关的所有事实(不截断)
1033
  related_facts = [
1034
  f for f in all_facts
1035
  if node.name.lower() in f.lower()
 
1040
  "name": node.name,
1041
  "type": entity_type,
1042
  "summary": node.summary,
1043
+ "related_facts": related_facts # 完整输出,不截断
1044
  })
1045
  except Exception as e:
1046
  logger.debug(f"获取节点 {uuid} 失败: {e}")
 
1049
  result.entity_insights = entity_insights
1050
  result.total_entities = len(entity_insights)
1051
 
1052
+ # Step 4: 构建所有关系链(不限制数量)
1053
  relationship_chains = []
1054
+ for edge_data in all_edges: # 处理所有边,不截断
1055
  if isinstance(edge_data, dict):
1056
  source_uuid = edge_data.get('source_node_uuid', '')
1057
  target_uuid = edge_data.get('target_node_uuid', '')