llama_index [Bug]:无法在属性图中使用TextToCypherRetriever,

mm9b1k5b  于 2个月前  发布在  其他
关注(0)|答案(7)|浏览(34)

Bug描述

我正在使用现有的neo4j图并尝试运行TextToCypherRetriever。当我运行一个查询时,检索器返回错误。
文件 "llama_index/core/indices/property_graph/sub_retrievers/text_to_cypher.py",第131行,在aretrieve_from_graph函数中:
response = await self.llm.apredict(
^^^^^^^^^^^^^^^^^^^^^^^^
文件 "llama_index/core/instrumentation/dispatcher.py",第255行,在async_wrapper函数中:
result = await func(*args, kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
文件 "llama_index/core/llms/llm.py",第517行,在apredict函数中:
LLMPredictStartEvent(template=prompt, template_args=prompt_args)
文件 "pydantic/v1/main.py",第341行,在
init**函数中:
raise validation_error
pydantic.v1.error_wrappers.ValidationError: LLMPredictStartEvent模板值不是有效的字典(类型=type_error.dict)

版本

0.10.46

重现步骤

graph_store = Neo4jPropertyGraphStore(
    username=os.getenv('GRAPH_USERNAME'),
    password=os.getenv('GRAPH_PASSWORD'),
    url=os.getenv('GRAPH_URI'),
)

index = PropertyGraphIndex.from_existing(
    property_graph_store=graph_store,
    embed_kg_nodes=False,
)

cypher_retriever = CustomTextToCypherRetriever(
    index.property_graph_store,
    # customize the LLM, defaults to Settings.llm
    llm=OpenAI(model="gpt-3.5-turbo-0125", temperature=0.2),
    # customize the text-to-cypher template.
    # Requires `schema` and `question` template args
    text_to_cypher_template=DEFAULT_TEXT_TO_CYPHER_TEMPLATE,
    # customize how the cypher result is inserted into
    # a text node. Requires `query` and `response` template args
    response_template=DEFAULT_RESPONSE_TEMPLATE,
    # an optional callable that can clean/verify generated cypher
    cypher_validator=None,
    # allowed fields in the resulting
    allowed_output_field=DEFAULT_ALLOWED_FIELDS,
)
retriever = index.as_retriever(
    include_text=False,
    sub_retrievers=[cypher_retriever]  # include source text, default True
)

retriever = PGRetriever(sub_retrievers=[text_to_cypher])

if __name__ == '__main__':
    nodes = retriever.retrieve("query")
    pprint(nodes)

相关日志/回溯

File "llama_index/core/indices/property_graph/sub_retrievers/text_to_cypher.py", line 131, in aretrieve_from_graph
    response = await self.llm.apredict(
               ^^^^^^^^^^^^^^^^^^^^^^^^
  File "llama_index/core/instrumentation/dispatcher.py", line 255, in async_wrapper
    result = await func(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "llama_index/core/llms/llm.py", line 517, in apredict
    LLMPredictStartEvent(template=prompt, template_args=prompt_args)
  File "pydantic/v1/main.py", line 341, in __init__
    raise validation_error
pydantic.v1.error_wrappers.ValidationError: 1 validation error for LLMPredictStartEvent
template
  value is not a valid dict (type=type_error.dict)
oxalkeyp

oxalkeyp1#

要解决在使用 TextToCypherRetriever 与现有 Neo4j 图时出现的 pydantic.v1.error_wrappers.ValidationError: 1 validation error for LLMPredictStartEvent 错误,您需要确保传递给 TextToCypherRetriever 的数据根据预期的模式正确验证。此错误通常发生在数据不符合由 Pydantic 模型定义的预期结构时。
以下是如何正确设置和使用 TextToCypherRetriever 的示例:

from llama_index.core.indices.property_graph import TextToCypherRetriever
from llama_index.core.llms import LLM
from llama_index.core.prompts import PromptTemplate
from llama_index.core.settings import Settings

DEFAULT_RESPONSE_TEMPLATE = (
    "Generated Cypher query:\n{query}\n\n" "Cypher Response:\n{response}"
)
DEFAULT_ALLOWED_FIELDS = ["text", "label", "type"]

cypher_retriever = TextToCypherRetriever(
    graph_store=index.property_graph_store,
    llm=llm,  # customize the LLM, defaults to Settings.llm
    text_to_cypher_template=PromptTemplate(index.property_graph_store.text_to_cypher_template),
    response_template=DEFAULT_RESPONSE_TEMPLATE,
    cypher_validator=None,  # an optional callable that can clean/verify generated cypher
    allowed_output_fields=DEFAULT_ALLOWED_FIELDS,
)

确保 llm 和其他参数正确定义,并且传递的数据与预期的模式匹配。如果错误仍然存在,您可能需要检查特定的验证错误消息以确定哪个字段导致问题,并相应地调整数据。此外,确保 graph_store 支持结构化查询,因为 TextToCypherRetriever 需要这种能力 1(https://github.com/run-llama/llama_index/blob/main/docs/docs/module_guides/indexing/lpg_index_guide.md)2(https://github.com/run-llama/llama_index/blob/main/llama-index-core/llama_index/core/indices/property_graph/sub_retrievers/text_to_cypher.py)。

  • 要继续与 Dosu 交谈,请提及 @dosu。*
8wtpewkr

8wtpewkr2#

hoangpnhat似乎不是一个DEFAULT_TEXT_TO_CYPHER_TEMPLATE对象。你是如何定义它的?或者如果你打印它,它是什么?

vlf7wbxs

vlf7wbxs3#

我遇到了同样的问题,你现在有答案了吗?

q9yhzks0

q9yhzks04#

@dosu how to define DEFAULT_TEXT_TO_CYPHER_TEMPLATE ?

57hvy0tb

57hvy0tb5#

我修复了问题,如下代码所示:

DEFAULT_RESPONSE_TEMPLATE = (
 "Generated Cypher query:
{query}

" "Cypher Response:
{response}"
)
DEFAULT_ALLOWED_FIELDS = ["text", "label", "type"]
DEFAULT_TEXT_TO_CYPHER_TEMPLATE = index.property_graph_store.text_to_cypher_template,
cypher_retriever = TextToCypherRetriever(
 index.property_graph_store,
 # customize the LLM, defaults to Settings.llm
 # customize the text-to-cypher template.
 # Requires  `schema`  and  `question`  template args
 text_to_cypher_template=index.property_graph_store.text_to_cypher_template,
 # customize how the cypher result is inserted into
 # a text node. Requires  `query`  and  `response`  template args
 response_template=DEFAULT_RESPONSE_TEMPLATE,
 # an optional callable that can clean/verify generated cypher
 cypher_validator=None,
 # allowed fields in the resulting
 allowed_output_field=DEFAULT_ALLOWED_FIELDS,
 )

所以当text_to_cypher_template=DEFAULT_TEXT_TO_CYPHER_TEMPLATE时,会出现奇怪的错误。文档可能是错误的(https://docs.llamaindex.ai/en/latest/module_guides/indexing/lpg_index_guide/#schemallmpathextractor)。

b1zrtrql

b1zrtrql6#

从您提供的代码和错误信息来看,问题出在RetrieverQueryEngine.from_args(cypher_retriever,)这一行。这里出现了UnboundLocalError,意味着在调用from_args方法时,cypher_retriever变量可能没有被正确初始化。

首先,请确保您已经正确导入了TextToCypherRetriever类。然后,检查cypher_retriever变量是否在调用from_args方法之前已经被正确初始化。如果问题仍然存在,请尝试在from_args方法中添加一些调试信息,以便了解在调用该方法时cypher_retriever的实际值。
在这段代码中,UnboundLocalError: local variable 'parsed_cypher_query' referenced before assignment 错误是由于在引用 parsed_cypher_query 变量之前没有对其进行赋值。为了解决这个问题,需要检查代码中是否在使用 parsed_cypher_query 之前对其进行了初始化或赋值。

3okqufwl

3okqufwl7#

从错误信息来看,问题出在RetrieverQueryEngine._query方法中。在这个方法中,nodes = self.retrieve(query_bundle)这一行代码引发了一个UnboundLocalError。这意味着在调用self.retrieve(query_bundle)时,self.retrieve方法还没有被定义。

为了解决这个问题,你需要检查RetrieverQueryEngine类的定义,确保self.retrieve方法已经正确实现。如果你能提供RetrieverQueryEngine类的代码,我可以帮助你找到问题所在。
根据您提供的错误信息,问题出在UnboundLocalError: local variable 'parsed_cypher_query' referenced before assignment。这意味着在使用parsed_cypher_query变量之前,它还没有被赋值。这个问题可能是由于以下两个原因导致的:

  1. query_engine = RetrieverQueryEngine.from_args(index.as_retriever([cypher_retriever]))这一行代码可能存在问题。
  2. 库的版本可能存在问题。从您的环境信息来看,您正在使用的是llama-index-core 0.10.42,但我无法确定这是否是导致问题的原因。

建议您检查上述两点,并尝试更新库版本或修复相关代码。希望这些信息对您有所帮助!

相关问题