Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question]: I want to make sure that the chatbot uses the retriever only when the question is related to ingested documents. #14142

Closed
1 task done
SiddharthMurjani opened this issue Jun 14, 2024 · 1 comment
Labels
question Further information is requested

Comments

@SiddharthMurjani
Copy link

SiddharthMurjani commented Jun 14, 2024

Question Validation

  • I have searched both the documentation and discord for an answer.

Question

I have this code here that goes to the vector db for every user_query to provide an answer.

chat_engine = vector_index.as_chat_engine(
        chat_mode=ChatMode.CONTEXT,
        memory=memory,
        chat_store=chat_store,
        verbose=True
    )

    response = chat_engine.chat(user_query)

Here, if the user_query is "HI" or "thankyou!" the chat_engine still goes through the RAG process to provide an answer.
Can we do something to make sure that the chat_engine uses the knowledge base only when the user_query is related to it?

@SiddharthMurjani SiddharthMurjani added the question Further information is requested label Jun 14, 2024
Copy link

dosubot bot commented Jun 14, 2024

To ensure that the chatbot uses the retriever only when the question is related to ingested documents, you can implement a check within the _generate_context and _agenerate_context methods of the ContextChatEngine class. This check can determine if the query is related to the ingested documents before proceeding with the retrieval process.

Here is an example of how you might modify the _generate_context and _agenerate_context methods to include such a check:

class ContextChatEngine(BaseChatEngine):
    # ... other methods ...

    def _generate_context(self, message: str) -> Tuple[str, List[NodeWithScore]]:
        """Generate context information from a message."""
        if not self._is_related_to_ingested_documents(message):
            return "The question is not related to the ingested documents.", []

        nodes = self._retriever.retrieve(message)
        for postprocessor in self._node_postprocessors:
            nodes = postprocessor.postprocess_nodes(
                nodes, query_bundle=QueryBundle(message)
            )

        context_str = "\n\n".join(
            [n.node.get_content(metadata_mode=MetadataMode.LLM).strip() for n in nodes]
        )

        return self._context_template.format(context_str=context_str), nodes

    async def _agenerate_context(self, message: str) -> Tuple[str, List[NodeWithScore]]:
        """Generate context information from a message."""
        if not self._is_related_to_ingested_documents(message):
            return "The question is not related to the ingested documents.", []

        nodes = await self._retriever.aretrieve(message)
        for postprocessor in self._node_postprocessors:
            nodes = postprocessor.postprocess_nodes(
                nodes, query_bundle=QueryBundle(message)
            )
        context_str = "\n\n".join(
            [n.node.get_content(metadata_mode=MetadataMode.LLM).strip() for n in nodes]
        )

        return self._context_template.format(context_str=context_str), nodes

    def _is_related_to_ingested_documents(self, query_str: str) -> bool:
        # Implement your logic to determine if the query is related to ingested documents
        # For example, you could check for specific keywords or use a more complex NLP model
        return True  # Placeholder implementation

In this example, the _is_related_to_ingested_documents method should contain the logic to determine if the query is related to the ingested documents. This could be a simple keyword check or a more sophisticated natural language processing (NLP) model. If the query is not related, the method returns a message indicating that the question is not related to the ingested documents [1][2][3].

To continue talking to Dosu, mention @dosu.

@dosubot dosubot bot added the stale Issue has not had recent activity or appears to be solved. Stale issues will be automatically closed label Sep 13, 2024
@dosubot dosubot bot closed this as not planned Won't fix, can't repro, duplicate, stale Sep 20, 2024
@dosubot dosubot bot removed the stale Issue has not had recent activity or appears to be solved. Stale issues will be automatically closed label Sep 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant