run-llama / llama_index

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

[Bug]: KnowledgeGraphIndex.as_retriever ignores 'retriever_mode' parameter #10708

Closed lukeherron closed 9 months ago

lukeherron commented 9 months ago

Bug Description

Prior to the v0.10.0 refactor, 'retriever_mode' parameter was being passed via kwargs to KGTableRetriever: https://github.com/run-llama/llama_index/blob/3823389e3f91cab47b72e2cc2814826db9f98e32/llama-index-legacy/llama_index/legacy/indices/knowledge_graph/base.py#L105-L114

'retriever_mode' is now a non-kwargs param and is not subsequently being passed to KGTableRetriever: https://github.com/run-llama/llama_index/blob/3823389e3f91cab47b72e2cc2814826db9f98e32/llama-index-core/llama_index/core/indices/knowledge_graph/base.py#L122-L142

The end result being retriever mode defaults to keyword and embeddings are ignored for any kg index queries.

Version

v0.10.4

Steps to Reproduce

  1. Crete KnowledgeGraphIndex
  2. query_engine = kg_index.as_query_engine(retriever_mode=KGRetrieverMode.EMBEDDING)
    query_engine.query("Some query that really needs a good embedding")
  3. Keyword query is performed, embeddings are ignored

Relevant Logs/Tracbacks

No response

dosubot[bot] commented 9 months ago

🤖

Thank you for bringing this to our attention. It seems like there's indeed a discrepancy between the expected behavior and the actual behavior of the 'retriever_mode' parameter in the KnowledgeGraphIndex.as_retriever method in the v0.10.4 version of LlamaIndex.

As per the documentation and the codebase, the 'retriever_mode' parameter is supposed to determine the mode of operation for the retriever. It can take one of three values: "keyword", "embedding", or "hybrid". Depending on the 'retriever_mode' specified, the Knowledge Graph Index should return a KGTableRetriever that operates in the specified mode.

However, as you've pointed out, regardless of the 'retriever_mode' specified, the Knowledge Graph Index always returns a KGTableRetriever that operates in the 'keyword' mode. This is indeed a bug and not the intended behavior.

Unfortunately, as a technical support rep, I don't have the ability to create pull requests or issues. However, I would recommend you to create a new issue in the LlamaIndex repository detailing this bug. Please include the steps to reproduce the bug, the expected behavior, and the actual behavior. This will help the maintainers of the LlamaIndex repository to understand and fix the bug.

In the meantime, as a workaround, you might want to manually set the 'retriever_mode' of the KGTableRetriever after it has been returned by the KnowledgeGraphIndex.as_retriever method. Here's how you can do it:

kg_index = KnowledgeGraphIndex(...)
query_engine = kg_index.as_query_engine()
query_engine.retriever.retriever_mode = KGRetrieverMode.EMBEDDING
query_engine.query("Some query that really needs a good embedding")

Please note that this is just a temporary workaround and not a permanent solution. The maintainers of the LlamaIndex repository will need to fix the bug for a permanent solution.

I hope this helps! If you have any other questions or need further clarification, please don't hesitate to ask.

Sources

#### About Dosu This response is meant to be useful and save you time. It is **not meant to be a precise solution**, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. If you want to continue the conversation, start your reply with @dosu-bot.
nerdai commented 9 months ago

Thanks for raising @lukeherron. I believe the fix should just be to add the missing retriever_mode param in the construction of the KGTableRetriever. Just submitted a PR for this.