Giter Club home page Giter Club logo

Comments (1)

dosubot avatar dosubot commented on June 26, 2024 2

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.

from llama_index.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.