50 lines
1.9 KiB
Python
50 lines
1.9 KiB
Python
|
|
from typing import Any
|
|||
|
|
|
|||
|
|
|
|||
|
|
def resolve_entities(context: dict[str, Any]) -> list[dict]:
|
|||
|
|
extracted = context.get('extracted_entities', [])
|
|||
|
|
existing = context.get('existing_entities', [])
|
|||
|
|
episode_content = context.get('episode_content', '')
|
|||
|
|
|
|||
|
|
extracted_text = '\n'.join(
|
|||
|
|
f' [{i}] {e.get("name", "")}({e.get("entity_type", "未知")}):{e.get("description", "")}'
|
|||
|
|
for i, e in enumerate(extracted)
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
existing_text = '\n'.join(
|
|||
|
|
f' [candidate_id={c.get("candidate_id", i)}] {c.get("name", "")}({c.get("entity_type", "未知")}):{c.get("summary", "")[:100]}'
|
|||
|
|
for i, c in enumerate(existing)
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
user_prompt = f"""
|
|||
|
|
<当前会议内容>
|
|||
|
|
{episode_content}
|
|||
|
|
</当前会议内容>
|
|||
|
|
|
|||
|
|
<新抽取的实体>
|
|||
|
|
{extracted_text}
|
|||
|
|
</新抽取的实体>
|
|||
|
|
|
|||
|
|
<图谱中已有的实体>
|
|||
|
|
{existing_text}
|
|||
|
|
</图谱中已有的实体>
|
|||
|
|
|
|||
|
|
任务:判断<新抽取的实体>中的每一个是否与<图谱中已有的实体>中的某个是同一个真实世界对象。
|
|||
|
|
|
|||
|
|
判断标准:
|
|||
|
|
- **是重复**:两个名称指向同一个真实世界的人、组织、地点、项目、指标等。
|
|||
|
|
- **不是重复**:名称相似但指向不同实体(如两个同名但不同的人、同名的不同项目)。
|
|||
|
|
|
|||
|
|
对每个新抽取的实体,返回:
|
|||
|
|
- id: 对应新抽取实体列表中的序号
|
|||
|
|
- name: 实体的最佳名称(优先使用已有实体中的更完整名称)
|
|||
|
|
- duplicate_candidate_id: 匹配到的已有实体的 candidate_id,如果无匹配则填 -1
|
|||
|
|
|
|||
|
|
返回格式 JSON 数组:[{{"id": 0, "name": "张三", "duplicate_candidate_id": -1}}, ...]
|
|||
|
|
必须为新抽取的每个实体返回一条记录。id 从 0 开始连续编号。
|
|||
|
|
"""
|
|||
|
|
return [
|
|||
|
|
{'role': 'system', 'content': '你是实体去重助手。判断两个实体是否指向同一个真实世界对象。'},
|
|||
|
|
{'role': 'user', 'content': user_prompt},
|
|||
|
|
]
|