Llama Index中的属性图索引:从入门到精通
在现代数据科学和人工智能领域,属性图(Property Graph)已成为处理复杂信息的重要工具。属性图通过结构化的方式表示实体及其关系,使得信息的检索和理解变得更加高效。本文将深入探讨Llama Index中的PropertyGraphIndex
类,帮助程序员全面理解其工作原理及实际应用。
前置知识
在开始之前,确保你具备以下基础知识:
- Python基础:熟悉Python编程。
- OpenAI API密钥:你需要一个OpenAI API密钥来使用
OpenAI
模型。 - Llama Index:使用
pip install llama-index
安装Llama Index库。
环境设置
首先,让我们通过安装所需的包并配置OpenAI API密钥来设置环境。
# 安装Llama Index
%pip install llama-index
# 设置OpenAI API密钥
import os
os.environ["OPENAI_API_KEY"] = "sk-..."
# 配置日志
import logging
import sys
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
PropertyGraphIndex概述
PropertyGraphIndex
是Llama Index中的一个核心数据结构,用于构建和查询属性图。它通过提取三元组(triplets)并利用这些三元组在查询时构建属性图。
参数说明
参数名 | 类型 | 描述 | 默认值 |
---|---|---|---|
nodes | Optional[Sequence[BaseNode]] | 要插入索引的节点列表。 | None |
llm | Optional[LLM] | 用于提取三元组的语言模型。默认使用Settings.llm。 | None |
kg_extractors | Optional[List[TransformComponent]] | 用于提取三元组的转换组件列表。默认使用[SimpleLLMPathExtractor(llm=llm), ImplicitEdgeExtractor()]。 | None |
property_graph_store | Optional[PropertyGraphStore] | 要使用的属性图存储。如果未提供,将创建一个新的SimplePropertyGraphStore。 | None |
vector_store | Optional[BasePydanticVectorStore] | 如果图存储不支持向量查询,则使用的向量存储索引。 | None |
use_async | bool | 是否使用异步进行转换。默认值为True。 | True |
embed_model | Optional[EmbedType] | 用于嵌入节点的嵌入模型。如果未提供且embed_kg_nodes=True,则使用Settings.embed_model。 | None |
embed_kg_nodes | bool | 是否嵌入KG节点。默认值为True。 | True |
callback_manager | Optional[CallbackManager] | 要使用的回调管理器。 | None |
transformations | Optional[List[TransformComponent]] | 在插入节点之前应用于节点的转换列表。这些转换在kg_extractors之前应用。 | None |
storage_context | Optional[StorageContext] | 要使用的存储上下文。 | None |
show_progress | bool | 是否显示转换的进度条。默认值为False。 | False |
主要方法
property_graph_store属性
property_graph_store: PropertyGraphStore
描述:获取标记属性图存储。
ref_doc_info属性
ref_doc_info: Dict[str, RefDocInfo]
描述:检索已摄取文档及其节点和元数据的字典映射。
from_existing类方法
@classmethod
def from_existing(
cls,
property_graph_store: PropertyGraphStore,
vector_store: Optional[BasePydanticVectorStore] = None,
llm: Optional[LLM] = None,
kg_extractors: Optional[List[TransformComponent]] = None,
use_async: bool = True,
embed_model: Optional[EmbedType] = None,
embed_kg_nodes: bool = True,
callback_manager: Optional[CallbackManager] = None,
transformations: Optional[List[TransformComponent]] = None,
storage_context: Optional[StorageContext] = None,
show_progress: bool = False,
**kwargs: Any
) -> PropertyGraphIndex:
描述:从现有的属性图存储(和可选的向量存储)创建索引。
as_retriever方法
def as_retriever(
self,
sub_retrievers: Optional[List[BasePGRetriever]] = None,
include_text: bool = True,
**kwargs: Any
) -> BaseRetriever:
描述:返回索引的检索器。
参数:
参数名 | 类型 | 描述 | 默认值 |
---|---|---|---|
sub_retrievers | Optional[List[BasePGRetriever]] | 要使用的子检索器列表。如果未提供,将使用默认列表:[LLMSynonymRetriever, VectorContextRetriever](如果图存储支持向量查询)。 | None |
include_text | bool | 是否在检索器结果中包含源文本。 | True |
**kwargs | Any | 传递给检索器的其他kwargs。 | {} |
代码示例
创建PropertyGraphIndex
from llama_index.core import PropertyGraphIndex, StorageContext
from llama_index.core.graph_stores import SimplePropertyGraphStore
from llama_index.llms.openai import OpenAI
# 定义LLM
llm = OpenAI(temperature=0, model="gpt-3.5-turbo")
# 创建属性图存储
property_graph_store = SimplePropertyGraphStore()
storage_context = StorageContext.from_defaults(graph_store=property_graph_store)
# 创建PropertyGraphIndex
pg_index = PropertyGraphIndex(
storage_context=storage_context,
llm=llm,
embed_kg_nodes=True,
show_progress=True,
)
从现有属性图存储创建索引
from llama_index.core import PropertyGraphIndex
from llama_index.core.graph_stores import SimplePropertyGraphStore
from llama_index.llms.openai import OpenAI
# 定义LLM
llm = OpenAI(temperature=0, model="gpt-3.5-turbo")
# 创建属性图存储
property_graph_store = SimplePropertyGraphStore()
# 从现有属性图存储创建索引
pg_index = PropertyGraphIndex.from_existing(
property_graph_store=property_graph_store,
llm=llm,
embed_kg_nodes=True,
show_progress=True,
)
获取检索器
retriever = pg_index.as_retriever(include_text=True)
查询示例
response = retriever.query("Tell me about Peter Quill")
print(response)
总结
通过Llama Index的PropertyGraphIndex
,我们可以轻松地构建和查询属性图。无论是从现有属性图存储创建索引,还是获取检索器进行查询,Llama Index都提供了强大的工具来处理属性图任务。希望这篇博客能帮助你更好地理解和应用属性图技术。