153 PropertyGraphIndex 深入解析`__init__`方法

Llama Index中的属性图索引:深入解析__init__方法

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

前置知识

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

  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__init__方法

PropertyGraphIndex__init__方法负责初始化属性图索引对象。它接受多个参数,包括节点、语言模型、属性图存储、向量存储等,并根据这些参数配置索引对象。

参数说明

参数名类型描述默认值
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

代码解析

def __init__(
    self,
    nodes: Optional[Sequence[BaseNode]] = None,
    llm: Optional[LLM] = None,
    kg_extractors: Optional[List[TransformComponent]] = None,
    property_graph_store: Optional[PropertyGraphStore] = None,
    # vector related params
    vector_store: Optional[BasePydanticVectorStore] = None,
    use_async: bool = True,
    embed_model: Optional[EmbedType] = None,
    embed_kg_nodes: bool = True,
    # parent class params
    callback_manager: Optional[CallbackManager] = None,
    transformations: Optional[List[TransformComponent]] = None,
    storage_context: Optional[StorageContext] = None,
    show_progress: bool = False,
    **kwargs: Any,
) -> None:
    """Init params."""
    storage_context = storage_context or StorageContext.from_defaults(
        property_graph_store=property_graph_store
    )

    # lazily initialize the graph store on the storage context
    if property_graph_store is not None:
        storage_context.property_graph_store = property_graph_store
    elif storage_context.property_graph_store is None:
        storage_context.property_graph_store = SimplePropertyGraphStore()

    if vector_store is not None:
        storage_context.vector_stores[DEFAULT_VECTOR_STORE] = vector_store

    if embed_kg_nodes and (
        storage_context.property_graph_store.supports_vector_queries
        or embed_kg_nodes
    ):
        self._embed_model = (
            resolve_embed_model(embed_model)
            if embed_model
            else Settings.embed_model
        )
    else:
        self._embed_model = None  # type: ignore

    self._kg_extractors = kg_extractors or [
        SimpleLLMPathExtractor(llm=llm or Settings.llm),
        ImplicitPathExtractor(),
    ]
    self._use_async = use_async
    self._llm = llm
    self._embed_kg_nodes = embed_kg_nodes
    self._override_vector_store = (
        vector_store is not None
        or not storage_context.property_graph_store.supports_vector_queries
    )

    super().__init__(
        nodes=nodes,
        callback_manager=callback_manager,
        storage_context=storage_context,
        transformations=transformations,
        show_progress=show_progress,
        **kwargs,
    )

设计思路

  1. 初始化存储上下文

    • 如果storage_context未提供,则使用默认的存储上下文。
    • 如果property_graph_store未提供,则创建一个新的SimplePropertyGraphStore
  2. 配置向量存储

    • 如果vector_store已提供,则将其添加到存储上下文的向量存储中。
  3. 配置嵌入模型

    • 如果embed_kg_nodes为True且属性图存储支持向量查询,则使用提供的嵌入模型或默认的嵌入模型。
  4. 配置三元组提取器

    • 使用默认的三元组提取器(SimpleLLMPathExtractorImplicitPathExtractor)或用户提供的提取器。
  5. 配置异步和语言模型

    • 设置是否使用异步进行转换。
    • 设置语言模型。
  6. 调用父类初始化方法

    • 调用父类的初始化方法,传递节点、回调管理器、存储上下文、转换和进度条显示等参数。

代码示例

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,
)

总结

通过Llama Index的PropertyGraphIndex,我们可以轻松地构建和查询属性图。__init__方法的设计思路清晰,参数配置灵活,能够满足不同场景的需求。希望这篇博客能帮助你更好地理解和应用属性图技术。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

需要重新演唱

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

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

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

打赏作者

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

抵扣说明:

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

余额充值