[GraphRAG] [Index][graph][extractors] description_summary_extractor
SummarizationResult
@dataclass
class SummarizationResult:
"""Unipartite graph extraction result class definition."""
items: str | tuple[str, str]
description: str
用于表示单个图节点或边的总结结果。
items:描述的是哪个图节点或边,可能是单个节点的 ID 或一个边的节点对。description:该节点或边的总结描述。
SummarizeExtractor
用于提取和总结图的节点或边的描述。它依赖于一个 LLM(如 OpenAI 提供的模型),并通过指定的提示语(prompt)来生成描述的总结。
主要功能包括:
- 从图的节点或边中提取多个描述信息。
- 使用指定的总结策略(如 LLM)将多个描述合并为一个简洁的总结。
- 该过程支持通过自定义的错误处理机制来处理总结过程中可能遇到的任何问题
关键属性
_llm: 用于总结的语言模型实例。_entity_name_key,_input_descriptions_key: 用于访问实体名称和描述的键。_summarization_prompt: 用于总结的提示语。_on_error: 错误处理回调函数。_max_summary_length: 总结文本的最大长度(以 tokens 为单位)。_max_input_tokens: 输入提示的最大 tokens 数量,影响一次可以处理的描述数量。
主要方法
__init__: 用于初始化提取器,支持可选的配置参数。__call__: 总结描述,将多个描述合并为一个描述,或者返回单独的描述。_summarize_descriptions: 收集描述并确保它们适合在 token 限制内,然后将它们传递给 LLM 进行总结。_summarize_descriptions_with_llm: 将描述和项目名称传递给 LLM,以获取总结结果。
步骤
初始化和配置
__init__
llm_invoker: 一个 LLM 调用器对象,用来与大型语言模型进行交互。entity_name_key: 图中实体的标识符的键名,默认为"entity_name"。input_descriptions_key: 输入描述信息的键名,默认为"description_list"。summarization_prompt: 用于总结描述的提示语,默认为SUMMARIZE_PROMPT。on_error: 处理错误的回调函数,默认为一个什么都不做的空函数。max_summary_length: 模型回答的最大 token 数量,默认为 500。max_input_tokens: 输入给模型的最大 token 数量,默认为 4000。
任务模板
You are a helpful assistant responsible for generating a comprehensive summary of the data provided below.
Given one or two entities, and a list of descriptions, all related to the same entity or group of entities.
Please concatenate all of these into a single, comprehensive description. Make sure to include information collected from all the descriptions.
If the provided descriptions are contradictory, please resolve the contradictions and provide a single, coherent summary.
Make sure it is written in third person, and include the entity names so we have the full context.
#######
-Data-
Entities: {entity_name}
Description List: {description_list}
#######
Output:
Descriptions Summarize
- 如果
descriptions列表为空,result取空字符串。 - 如果
descriptions只有一个元素,result直接取该描述。 - 如果
descriptions有多个元素,则调用self._summarize_descriptions方法对这些描述进行总结,
返回 SummarizationResult
async def __call__(
self,
items: str | tuple[str, str],
descriptions: list[str],
) -> SummarizationResult:
"""Call method definition."""
result = ""
if len(descriptions) == 0:
result = ""
elif len(descriptions) == 1:
result = descriptions[0]
else:
result = await self._summarize_descriptions(items, descriptions)
return SummarizationResult(
items=items,
description=result or "",
)
-
_summarize_descriptions_with_llm->str:-
调用 LLM 来总结描述信息。
-
它会发送一个包含
items和descriptions的请求,并设置max_tokens参数来限制模型回答的长度 -
返回字符串
-
-
_summarize_descriptions->nx.Graph:-
对
items进行排序 (当items 为边 (relationship)的时候保证 item 的一致性) -
对
descriptions进行排序,安全性检查等 -
计算当前描述占用的 tokens 数量,并从可用的 tokens 中减去,将描述加入到
descriptions_collected列表中。如果:-
当前描述的 token 数量已经使可用 tokens 超过限制,且已收集了多个描述,或者
-
已处理到最后一个描述,
则调用
_summarize_descriptions_with_llm方法对收集到的描述进行总结,得到最终结果。 -
-
如果并没有处理完所有描述(即还可以继续总结),则重置
descriptions_collected,将当前的总结结果result作为下一轮的描述,并重新计算剩余的可用 tokens。
-