mirror of
https://gitcode.com/JianFeeeee/TrulyMEM-TrueHumanMEM.git
synced 2026-09-21 17:38:18 +00:00
feat: 新增 persona_remove 工具(删除单条人设属性),改进 persona_clear 支持动态查询所有属性类型
This commit is contained in:
@ -97,8 +97,9 @@ subject, relation, object 每个字段必须是一个**短关键字**(1~5个
|
|||||||
### 人设工具
|
### 人设工具
|
||||||
| 工具 | 时机 | 说明 |
|
| 工具 | 时机 | 说明 |
|
||||||
|------|------|------|
|
|------|------|------|
|
||||||
| `persona_update` | AI自身角色改变 | 更新AI的角色、性格、说话风格、能力。**注意**:这是设定你自己的属性,不是记录用户信息 |
|
| `persona_update` | AI自身角色改变 | 更新AI的角色、性格、说话风格、能力。`mode="replace"` 替换全部,`mode="merge"` 增量添加 |
|
||||||
| `persona_clear` | 需要重置人设 | 清除所有AI人设属性 |
|
| `persona_remove` | 只需删除某一条属性 | 删除单条人设属性(如只删除说话风格,保留扮演角色不变) |
|
||||||
|
| `persona_clear` | 需要完全重置 | 清除所有AI人设属性。**此操作不可逆,需要 confirm=true** |
|
||||||
|
|
||||||
### 任务工具(生命周期管理)
|
### 任务工具(生命周期管理)
|
||||||
| 工具 | 时机 | 说明 |
|
| 工具 | 时机 | 说明 |
|
||||||
|
|||||||
@ -75,6 +75,11 @@ def execute_tool(graph: Any, tool_name: str, arguments: dict) -> str:
|
|||||||
result = execute_persona_update(graph, arguments)
|
result = execute_persona_update(graph, arguments)
|
||||||
return json.dumps(result, ensure_ascii=False, default=str)
|
return json.dumps(result, ensure_ascii=False, default=str)
|
||||||
|
|
||||||
|
elif tool_name == "persona_remove":
|
||||||
|
recorder.record("delete", tool_name, arguments.get("attribute", ""))
|
||||||
|
result = execute_persona_remove(graph, arguments)
|
||||||
|
return json.dumps(result, ensure_ascii=False, default=str)
|
||||||
|
|
||||||
elif tool_name == "persona_clear":
|
elif tool_name == "persona_clear":
|
||||||
recorder.record("delete", tool_name, "所有人设")
|
recorder.record("delete", tool_name, "所有人设")
|
||||||
result = execute_persona_clear(graph, arguments)
|
result = execute_persona_clear(graph, arguments)
|
||||||
@ -203,40 +208,74 @@ def execute_persona_update(graph: Any, arguments: dict) -> dict:
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def execute_persona_remove(graph: Any, arguments: dict) -> dict:
|
||||||
|
"""删除单条人设属性"""
|
||||||
|
attribute = arguments.get("attribute")
|
||||||
|
if not attribute:
|
||||||
|
return {"status": "error", "message": "请指定要删除的属性名"}
|
||||||
|
|
||||||
|
# 查询当前AI的所有人设关系,找到匹配属性名的
|
||||||
|
recall_result = graph.recall(query_intent="AI,人设,角色", depth=1)
|
||||||
|
found = False
|
||||||
|
deleted_count = 0
|
||||||
|
|
||||||
|
for rel in recall_result.get("relations", []):
|
||||||
|
if rel.get("source") == "AI" and rel.get("type") == attribute:
|
||||||
|
result = graph.purge(
|
||||||
|
criteria={"subject_contains": "AI", "relation_type": attribute},
|
||||||
|
mode="soft"
|
||||||
|
)
|
||||||
|
deleted_count += result.get("deleted_count", 0)
|
||||||
|
found = True
|
||||||
|
|
||||||
|
if not found:
|
||||||
|
# 也许属性名不完全匹配,尝试直接用这个类型删除
|
||||||
|
result = graph.purge(
|
||||||
|
criteria={"subject_contains": "AI", "relation_type": attribute},
|
||||||
|
mode="soft"
|
||||||
|
)
|
||||||
|
deleted_count = result.get("deleted_count", 0)
|
||||||
|
|
||||||
|
return {
|
||||||
|
"status": "success" if deleted_count > 0 else "not_found",
|
||||||
|
"deleted_attribute": attribute,
|
||||||
|
"deleted_count": deleted_count,
|
||||||
|
"message": f"已删除属性「{attribute}」" if deleted_count > 0 else f"未找到属性「{attribute}」"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def execute_persona_clear(graph: Any, arguments: dict) -> dict:
|
def execute_persona_clear(graph: Any, arguments: dict) -> dict:
|
||||||
"""清除人设"""
|
"""清除所有人设"""
|
||||||
if not arguments.get("confirm", True):
|
if not arguments.get("confirm"):
|
||||||
return {"status": "cancelled", "message": "需要确认才能清除人设"}
|
return {"status": "cancelled", "message": "请设置 confirm=true 确认清除人设"}
|
||||||
|
|
||||||
# 删除所有人设相关关系
|
# 先查询AI的所有人设关系
|
||||||
result1 = graph.purge(
|
recall_result = graph.recall(query_intent="AI,人设,角色", depth=1)
|
||||||
criteria={"subject_contains": "AI", "relation_type": "扮演角色"},
|
|
||||||
mode="soft"
|
# 收集所有AI到其他实体的关系类型
|
||||||
)
|
relation_types = set()
|
||||||
result2 = graph.purge(
|
for rel in recall_result.get("relations", []):
|
||||||
criteria={"subject_contains": "AI", "relation_type": "说话风格"},
|
if rel.get("source") == "AI" and rel.get("type"):
|
||||||
mode="soft"
|
relation_types.add(rel.get("type"))
|
||||||
)
|
|
||||||
result3 = graph.purge(
|
total_deleted = 0
|
||||||
criteria={"subject_contains": "AI", "relation_type": "性格特点"},
|
deleted_types = []
|
||||||
mode="soft"
|
|
||||||
)
|
for rtype in relation_types:
|
||||||
result4 = graph.purge(
|
result = graph.purge(
|
||||||
criteria={"subject_contains": "AI", "relation_type": "语气特征"},
|
criteria={"subject_contains": "AI", "relation_type": rtype},
|
||||||
mode="soft"
|
mode="soft"
|
||||||
)
|
)
|
||||||
|
count = result.get("deleted_count", 0)
|
||||||
total_deleted = (
|
if count > 0:
|
||||||
result1.get("deleted_count", 0) +
|
total_deleted += count
|
||||||
result2.get("deleted_count", 0) +
|
deleted_types.append(rtype)
|
||||||
result3.get("deleted_count", 0) +
|
|
||||||
result4.get("deleted_count", 0)
|
|
||||||
)
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"status": "success",
|
"status": "success",
|
||||||
"deleted_count": total_deleted,
|
"deleted_count": total_deleted,
|
||||||
"message": "人设已清除,恢复默认身份"
|
"deleted_types": deleted_types,
|
||||||
|
"message": f"人设已清除,恢复默认身份(删除了 {len(deleted_types)} 类属性)"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -39,7 +39,7 @@ class ToolLimiter:
|
|||||||
category: 'persona', 'task', 'memory'
|
category: 'persona', 'task', 'memory'
|
||||||
operation: 'query', 'update'
|
operation: 'query', 'update'
|
||||||
"""
|
"""
|
||||||
if tool_name in ('persona_update', 'persona_clear'):
|
if tool_name in ('persona_update', 'persona_remove', 'persona_clear'):
|
||||||
return ('persona', 'update')
|
return ('persona', 'update')
|
||||||
|
|
||||||
if tool_name in ('task_create', 'task_set_state', 'task_delete', 'task_link_info'):
|
if tool_name in ('task_create', 'task_set_state', 'task_delete', 'task_link_info'):
|
||||||
|
|||||||
@ -332,21 +332,37 @@ PERSONA_TOOLS = [
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"type": "function",
|
||||||
|
"function": {
|
||||||
|
"name": "persona_remove",
|
||||||
|
"description": "删除单条人设属性。删除指定的属性(如说话风格、扮演角色等),保留其他人设不变。",
|
||||||
|
"parameters": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"attribute": {
|
||||||
|
"type": "string",
|
||||||
|
"description": "要删除的属性名(如:扮演角色、说话风格、性格特点、口头禅)"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": ["attribute"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"type": "function",
|
"type": "function",
|
||||||
"function": {
|
"function": {
|
||||||
"name": "persona_clear",
|
"name": "persona_clear",
|
||||||
"description": "清除人设。删除AI的角色设定,恢复默认身份。",
|
"description": "清除人设。删除AI所有角色设定,恢复默认身份。注意:此操作不可逆。",
|
||||||
"parameters": {
|
"parameters": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
"confirm": {
|
"confirm": {
|
||||||
"type": "boolean",
|
"type": "boolean",
|
||||||
"description": "确认清除",
|
"description": "确认清除全部人设"
|
||||||
"default": True
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"required": []
|
"required": ["confirm"]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user