152 PropertyGraphIndex使用

Llama Index中的属性图索引:从入门到精通

在现代数据科学和人工智能领域,属性图(Property Graph)已成为处理复杂信息的重要工具。属性图通过结构化的方式表示实体及其关系,使得信息的检索和理解变得更加高效。本文将深入探讨Llama Index中的PropertyGraphIndex类,帮助程序员全面理解其工作原理及实际应用。

前置知识

在开始之前,确保你具备以下基础知识:

  1. Python基础:熟悉Python编程。
  2. OpenAI API密钥:你需要一个OpenAI API密钥来使用OpenAI模型。
  3. 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)并利用这些三元组在查询时构建属性图。

参数说明

参数名类型描述默认值
nodesOptional[Sequence[BaseNode]]要插入索引的节点列表。None
llmOptional[LLM]用于提取三元组的语言模型。默认使用Settings.llm。None
kg_extractorsOptional[List[TransformComponent]]用于提取三元组的转换组件列表。默认使用[SimpleLLMPathExtractor(llm=llm), ImplicitEdgeExtractor()]。None
property_graph_storeOptional[PropertyGraphStore]要使用的属性图存储。如果未提供,将创建一个新的SimplePropertyGraphStore。None
vector_storeOptional[BasePydanticVectorStore]如果图存储不支持向量查询,则使用的向量存储索引。None
use_asyncbool是否使用异步进行转换。默认值为True。True
embed_modelOptional[EmbedType]用于嵌入节点的嵌入模型。如果未提供且embed_kg_nodes=True,则使用Settings.embed_model。None
embed_kg_nodesbool是否嵌入KG节点。默认值为True。True
callback_managerOptional[CallbackManager]要使用的回调管理器。None
transformationsOptional[List[TransformComponent]]在插入节点之前应用于节点的转换列表。这些转换在kg_extractors之前应用。None
storage_contextOptional[StorageContext]要使用的存储上下文。None
show_progressbool是否显示转换的进度条。默认值为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_retrieversOptional[List[BasePGRetriever]]要使用的子检索器列表。如果未提供,将使用默认列表:[LLMSynonymRetriever, VectorContextRetriever](如果图存储支持向量查询)。None
include_textbool是否在检索器结果中包含源文本。True
**kwargsAny传递给检索器的其他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都提供了强大的工具来处理属性图任务。希望这篇博客能帮助你更好地理解和应用属性图技术。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

需要重新演唱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值