run-llama / llama_index

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

[Bug]: LLMRerank Default Prompt will fail and cause an error if user make an answer not releated to the docs like "great!" #11092

Closed marco-bertelli closed 3 months ago

marco-bertelli commented 6 months ago

Bug Description

If you will use LLMRerank class the DEFAULT_CHOICE_SELECT_PROMPT has a problem, the problem is that if the question is a general conversation thing like "great job!", the LLM model reply with something similar that: ['The question "ottimo" is not clear and does not seem to relate to any of the documents provided. Therefore', ' no documents are relevant.'] this will cause another error in this response parser that is: answer_num = int(line_tokens[0].split(":")[1].strip()) IndexError: list index out of range. this because for example you choose top 3 docs but the LLM model will reply with only 1

Version

0.10.9

Steps to Reproduce

instanciate a reranker: reranker = LLMRerank( choice_batch_size=6, choice_select_prompt=CUSTOM_RERANK_TEMPLATE, top_n=2, service_context=service_context, )

after that use it anywhere and ask a question not related to the context. this will cause the error in the description: the problem is that if the question is a general conversation thing like "great job!", the LLM model reply with something similar that: ['The question "ottimo" is not clear and does not seem to relate to any of the documents provided. Therefore', ' no documents are relevant.'] this will cause another error in this response parser that is: answer_num = int(line_tokens[0].split(":")[1].strip()) IndexError: list index out of range. this because for example you choose top 3 docs but the LLM model will reply with only 1

Relevant Logs/Tracbacks

['The question "ottimo" is not clear and does not seem to relate to any of the documents provided. Therefore', ' no documents are relevant.']
[2024-02-21 12:27:08,420] ERROR in app: Exception on /chat/65d5d9a7c19655bfdf44485a/answer [GET]
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/flask/app.py", line 2070, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/lib/python3/dist-packages/flask/app.py", line 1515, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/marco/.local/lib/python3.10/site-packages/flask_cors/extension.py", line 176, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/usr/lib/python3/dist-packages/flask/app.py", line 1513, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/lib/python3/dist-packages/flask/app.py", line 1499, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "/home/marco/.local/lib/python3.10/site-packages/flask_limiter/extension.py", line 1298, in __inner
    return cast(R, flask.current_app.ensure_sync(obj)(*a, **k))
  File "/home/marco/Desktop/RuneLab/messe-rag-chatbot/apis/chats.py", line 179, in query_index
    response = chat_engine.stream_chat(message=query_text)
  File "/home/marco/.local/lib/python3.10/site-packages/llama_index/callbacks/utils.py", line 41, in wrapper
    return func(self, *args, **kwargs)
  File "/home/marco/.local/lib/python3.10/site-packages/llama_index/chat_engine/context.py", line 191, in stream_chat
    context_str_template, nodes = self._generate_context(message)
  File "/home/marco/.local/lib/python3.10/site-packages/llama_index/chat_engine/context.py", line 105, in _generate_context
    nodes = postprocessor.postprocess_nodes(
  File "/home/marco/.local/lib/python3.10/site-packages/llama_index/postprocessor/types.py", line 55, in postprocess_nodes
    return self._postprocess_nodes(nodes, query_bundle)
  File "/home/marco/.local/lib/python3.10/site-packages/llama_index/postprocessor/llm_rerank.py", line 96, in _postprocess_nodes
    raw_choices, relevances = self._parse_choice_select_answer_fn(
  File "/home/marco/.local/lib/python3.10/site-packages/llama_index/indices/utils.py", line 107, in default_parse_choice_select_answer_fn
    answer_num = int(line_tokens[0].split(":")[1].strip())
IndexError: list index out of range
dosubot[bot] commented 6 months ago

🤖

Hey @marco-bertelli! 👋 I'm Dosu, your friendly neighborhood bot, here to lend a hand with bugs, answer questions, and help you contribute while we wait for a human maintainer to join us. I'm diving into your issue and will get back to you with a full answer soon. Hang tight! 🤖

marco-bertelli commented 6 months ago

i have found a solution for that and create a PR, if you think is a good solution merge it, thanks for the awesome job guys: https://github.com/run-llama/llama_index/pull/11094

dosubot[bot] commented 6 months ago

🤖

Thank you for reporting this issue and for your contribution to the LlamaIndex project. It seems like the error you're encountering is due to the LLM model returning a single document when the LLMRerank class expects more. This discrepancy is causing an IndexError in the default_parse_choice_select_answer_fn function.

Your solution seems to address this issue by handling the case where the LLM model returns a single document. However, as an AI, I'm unable to review or merge pull requests. I recommend reaching out to the repository maintainers directly for them to review your solution.

Regarding your question about the expected behavior of the LLMRerank class when it encounters a question unrelated to the context, the LLMRerank class does not have a specific behavior for such questions. It simply reranks the nodes based on the scores returned by the LLM predictor. This behavior is independent of whether the question is related to the context or not.

As for the changes made to the LLMRerank class between version v0.10.9 and v0.10.10, I'm unable to provide that information as it's not available in the repository. You might want to check the release notes or commit history for those versions to get more details.

I hope this helps! If you have any more questions or need further clarification, feel free 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.
Sudar278 commented 6 months ago

I'm facing a issue while querying my pinecone vectordb after new version before it worked fine

class LLMRerankRetriever(BaseRetriever):
    def __init__(
        self,
        vector_retriever: VectorIndexRetriever,
        service_context: ServiceContext
    ):
        self._vector_retriever = vector_retriever
        self._service_context = service_context
        super().__init__()

    def _retrieve(self, query_bundle: QueryBundle):
        retrieved_nodes = self._vector_retriever.retrieve(query_bundle)

        reranker = LLMRerank(service_context=self._service_context,choice_batch_size=7,top_n=3)
        retrieved_nodes = reranker.postprocess_nodes(retrieved_nodes, query_bundle)

        return retrieved_nodes
vector_retriever = VectorIndexRetriever(index=index, similarity_top_k=21)
custom_retriever = LLMRerankRetriever(vector_retriever, service_context)

response_synthesizer = get_response_synthesizer(verbose=True)

custom_query_engine = RetrieverQueryEngine(
    retriever=custom_retriever,
    response_synthesizer=response_synthesizer
)

memory = ChatMemoryBuffer.from_defaults(token_limit=250)
new_engine=CondensePlusContextChatEngine.from_defaults(
    retriever=custom_query_engine,memory=memory,
    context_prompt=prompt)

I'm using a chatengine in combination with a RetrieverQueryEngine

Traceback (most recent call last): File "d:\Sudar\Generative AI_Casy\chatbot_cli_vers\Chatbot_CLI_4.py", line 108, in chat_loop() File "d:\Sudar\Generative AI_Casy\chatbot_cli_vers\Chatbot_CLI_4.py", line 101, in chat_loop result = new_engine.chat(query) ^^^^^^^^^^^^^^^^^^^^^^ File "D:\Sudar\Generative AI_Casy\myenv\Lib\site-packages\llama_index\core\callbacks\utils.py", line 41, in wrapper return func(self, *args, kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\Sudar\Generative AI_Casy\myenv\Lib\site-packages\llama_index\core\chat_engine\condense_plus_context.py", line 286, in chat chat_messages, context_source, context_nodes = self._run_c3( ^^^^^^^^^^^^^ File "D:\Sudar\Generative AI_Casy\myenv\Lib\site-packages\llama_index\core\chat_engine\condense_plus_context.py", line 202, in _run_c3
context_str, context_nodes = self._retrieve_context(condensed_question) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\Sudar\Generative AI_Casy\myenv\Lib\site-packages\llama_index\core\chat_engine\condense_plus_context.py", line 168, in _retrieve_context nodes = self._retriever.retrieve(message) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\Sudar\Generative AI_Casy\myenv\Lib\site-packages\llama_index\core\query_engine\retriever_query_engine.py", line 142, in retrieve
nodes = self._retriever.retrieve(query_bundle) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\Sudar\Generative AI_Casy\myenv\Lib\site-packages\llama_index\core\base\base_retriever.py", line 229, in retrieve nodes = self._retrieve(query_bundle) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "d:\Sudar\Generative AI_Casy\chatbot_cli_vers\Chatbot_CLI_4.py", line 54, in _retrieve retrieved_nodes = self._vector_retriever.retrieve(query_bundle) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\Sudar\Generative AI_Casy\myenv\Lib\site-packages\llama_index\core\base\base_retriever.py", line 229, in retrieve nodes = self._retrieve(query_bundle) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\Sudar\Generative AI_Casy\myenv\Lib\site-packages\llama_index\core\indices\vector_store\retrievers\retriever.py", line 94, in _retrieve return self._get_nodes_with_embeddings(query_bundle) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\Sudar\Generative AI_Casy\myenv\Lib\site-packages\llama_index\core\indices\vector_store\retrievers\retriever.py", line 170, in _get_nodes_with_embeddings query_result = self._vector_store.query(query,
self._kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\Sudar\Generative AI_Casy\myenv\Lib\site-packages\llama_index\vector_stores\pinecone\base.py", line 426, in query response = self._pinecone_index.query( ^^^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'Pinecone' object has no attribute 'query'

My pip versions

llama-index==0.10.11 llama-index-agent-openai==0.1.4 llama-index-cli==0.1.3 llama-index-core==0.10.11.post1 llama-index-embeddings-openai==0.1.5 llama-index-indices-managed-llama-cloud==0.1.2 llama-index-legacy==0.9.48 llama-index-llms-openai==0.1.5 llama-index-multi-modal-llms-openai==0.1.3 llama-index-program-openai==0.1.3 llama-index-question-gen-openai==0.1.2 llama-index-readers-file==0.1.4 llama-index-readers-llama-parse==0.1.2 llama-index-vector-stores-chroma==0.1.2 llama-index-vector-stores-pinecone==0.1.3 llama-parse==0.3.4 llamaindex-py-client==0.1.13 pinecone-client==3.0.3

Looking forward your support guyss!!!

Thanks in advance