You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
The text was updated successfully, but these errors were encountered:
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:
classContextChatEngine(BaseChatEngine):
# ... other methods ...def_generate_context(self, message: str) ->Tuple[str, List[NodeWithScore]]:
"""Generate context information from a message."""ifnotself._is_related_to_ingested_documents(message):
return"The question is not related to the ingested documents.", []
nodes=self._retriever.retrieve(message)
forpostprocessorinself._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() forninnodes]
)
returnself._context_template.format(context_str=context_str), nodesasyncdef_agenerate_context(self, message: str) ->Tuple[str, List[NodeWithScore]]:
"""Generate context information from a message."""ifnotself._is_related_to_ingested_documents(message):
return"The question is not related to the ingested documents.", []
nodes=awaitself._retriever.aretrieve(message)
forpostprocessorinself._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() forninnodes]
)
returnself._context_template.format(context_str=context_str), nodesdef_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 modelreturnTrue# 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].
Question Validation
Question
I have this code here that goes to the vector db for every
user_query
to provide an answer.Here, if the
user_query
is "HI" or "thankyou!" the chat_engine still goes through theRAG
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?The text was updated successfully, but these errors were encountered: