run-llama / llama_index

LlamaIndex is a data framework for your LLM applications
https://docs.llamaindex.ai
MIT License
33.4k stars 4.68k forks source link

[Bug]: Cannot use TextToCypherRetriever in Property Graph #14536

Open hoangpnhat opened 5 days ago

hoangpnhat commented 5 days ago

Bug Description

I'm using an existing neo4j graph and trying to run TextToCypherRetriever When I run a query, the retriever returns error 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)

Version

0.10.46

Steps to Reproduce

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)

Relevant Logs/Tracbacks

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)
dosubot[bot] commented 5 days ago

To resolve the pydantic.v1.error_wrappers.ValidationError: 1 validation error for LLMPredictStartEvent error when using TextToCypherRetriever with an existing Neo4j graph, you need to ensure that the data being passed to the TextToCypherRetriever is correctly validated according to the expected schema. This error typically occurs when the data does not conform to the expected structure defined by Pydantic models.

Here is an example of how to correctly set up and use the 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,
)

Ensure that the llm and other parameters are correctly defined and that the data being passed matches the expected schema. If the error persists, you may need to inspect the specific validation error message to identify which field is causing the issue and adjust your data accordingly. Additionally, make sure that the graph_store supports structured queries, as the TextToCypherRetriever requires this capability [1][2].

To continue talking to Dosu, mention @dosu.

logan-markewich commented 5 days ago

@hoangpnhat seems like DEFAULT_TEXT_TO_CYPHER_TEMPLATE is not a PromptTemplate object. How did you define it? Or if you print it, what is it?