search function within GraphRAG class does not validate empty answers.
In the search function of GraphRAG class, theres a step to retrieve data: https://github.com/neo4j/neo4j-graphrag-python/blob/4db2c459d6de6bb98276cba260eb93536ea9dc42/src/neo4j_graphrag/generation/graphrag.py#L134
If the result comes empty, then the value for retriever_result.items is an empty list ([]). This is fine.
Problem is the function later does not validate for this scenario, finally passing the input, message_history and system_instruction to the function: https://github.com/neo4j/neo4j-graphrag-python/blob/4db2c459d6de6bb98276cba260eb93536ea9dc42/src/neo4j_graphrag/generation/graphrag.py#L143
Which could lead to potential problems, hallucinations and undesired answers.
A simple solution could be to just validate the items:
if not retriever_result.items:
result: dict[str, Any] = {"answer": ""}
A more robust solution would be to return either a predefined message or user defined message (as an argument to the function) before the invoke of the llm (as to not invoke an llm over an empty answer) in scenarios where the result is an empty list.
Hi @CrisBMoya ,
Thank you for raising the issue. You're right that having a test to prevent a useless LLM call if you don't want any answer if the context is empty would be a good to have feature. I think your second option with a user defined message is the cleanest option. I've just labelled this issue as "Good first issue" for people who might want to contribute to the repo.
In version 1.8.0 released yesterday, you can now add a response fallback that will be returned if the context is empty:
rag = GraphRAG(retriever=retriever, llm=llm)
result = rag.search(
"Tell me more about Avatar movies",
return_context=True,
response_fallback="I can't answer this question without context", # NEW
)