Home / File/ re_phraser.py — langchain Source File

re_phraser.py — langchain Source File

Architecture documentation for re_phraser.py, a python file in the langchain codebase. 9 imports, 0 dependents.

Entity Profile

Dependency Diagram

graph LR
  47609051_c54c_ec02_b1d7_e467e1376bf7["re_phraser.py"]
  2a7f66a7_8738_3d47_375b_70fcaa6ac169["logging"]
  47609051_c54c_ec02_b1d7_e467e1376bf7 --> 2a7f66a7_8738_3d47_375b_70fcaa6ac169
  f3bc7443_c889_119d_0744_aacc3620d8d2["langchain_core.callbacks"]
  47609051_c54c_ec02_b1d7_e467e1376bf7 --> f3bc7443_c889_119d_0744_aacc3620d8d2
  c554676d_b731_47b2_a98f_c1c2d537c0aa["langchain_core.documents"]
  47609051_c54c_ec02_b1d7_e467e1376bf7 --> c554676d_b731_47b2_a98f_c1c2d537c0aa
  ba43b74d_3099_7e1c_aac3_cf594720469e["langchain_core.language_models"]
  47609051_c54c_ec02_b1d7_e467e1376bf7 --> ba43b74d_3099_7e1c_aac3_cf594720469e
  83d7c7fd_1989_762c_9cf3_cecb50ada22b["langchain_core.output_parsers"]
  47609051_c54c_ec02_b1d7_e467e1376bf7 --> 83d7c7fd_1989_762c_9cf3_cecb50ada22b
  e6b4f61e_7b98_6666_3641_26b069517d4a["langchain_core.prompts"]
  47609051_c54c_ec02_b1d7_e467e1376bf7 --> e6b4f61e_7b98_6666_3641_26b069517d4a
  c17bcf07_a2ef_b992_448f_5088d46a1e79["langchain_core.prompts.prompt"]
  47609051_c54c_ec02_b1d7_e467e1376bf7 --> c17bcf07_a2ef_b992_448f_5088d46a1e79
  38bc5323_3713_7377_32f8_091293bea54b["langchain_core.retrievers"]
  47609051_c54c_ec02_b1d7_e467e1376bf7 --> 38bc5323_3713_7377_32f8_091293bea54b
  2ceb1686_0f8c_8ae0_36d1_7c0b702fda1c["langchain_core.runnables"]
  47609051_c54c_ec02_b1d7_e467e1376bf7 --> 2ceb1686_0f8c_8ae0_36d1_7c0b702fda1c
  style 47609051_c54c_ec02_b1d7_e467e1376bf7 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import logging

from langchain_core.callbacks import (
    AsyncCallbackManagerForRetrieverRun,
    CallbackManagerForRetrieverRun,
)
from langchain_core.documents import Document
from langchain_core.language_models import BaseLLM
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import BasePromptTemplate
from langchain_core.prompts.prompt import PromptTemplate
from langchain_core.retrievers import BaseRetriever
from langchain_core.runnables import Runnable

logger = logging.getLogger(__name__)

# Default template
DEFAULT_TEMPLATE = """You are an assistant tasked with taking a natural language \
query from a user and converting it into a query for a vectorstore. \
In this process, you strip out information that is not relevant for \
the retrieval task. Here is the user query: {question}"""

# Default prompt
DEFAULT_QUERY_PROMPT = PromptTemplate.from_template(DEFAULT_TEMPLATE)


class RePhraseQueryRetriever(BaseRetriever):
    """Given a query, use an LLM to re-phrase it.

    Then, retrieve docs for the re-phrased query.
    """

    retriever: BaseRetriever
    llm_chain: Runnable

    @classmethod
    def from_llm(
        cls,
        retriever: BaseRetriever,
        llm: BaseLLM,
        prompt: BasePromptTemplate = DEFAULT_QUERY_PROMPT,
    ) -> "RePhraseQueryRetriever":
        """Initialize from llm using default template.

        The prompt used here expects a single input: `question`

        Args:
            retriever: retriever to query documents from
            llm: llm for query generation using DEFAULT_QUERY_PROMPT
            prompt: prompt template for query generation

        Returns:
            RePhraseQueryRetriever
        """
        llm_chain = prompt | llm | StrOutputParser()
        return cls(
            retriever=retriever,
            llm_chain=llm_chain,
        )

    def _get_relevant_documents(
        self,
        query: str,
        *,
        run_manager: CallbackManagerForRetrieverRun,
    ) -> list[Document]:
        """Get relevant documents given a user question.

        Args:
            query: user question
            run_manager: callback handler to use

        Returns:
            Relevant documents for re-phrased question
        """
        re_phrased_question = self.llm_chain.invoke(
            query,
            {"callbacks": run_manager.get_child()},
        )
        logger.info("Re-phrased question: %s", re_phrased_question)
        return self.retriever.invoke(
            re_phrased_question,
            config={"callbacks": run_manager.get_child()},
        )

    async def _aget_relevant_documents(
        self,
        query: str,
        *,
        run_manager: AsyncCallbackManagerForRetrieverRun,
    ) -> list[Document]:
        raise NotImplementedError

Subdomains

Dependencies

  • langchain_core.callbacks
  • langchain_core.documents
  • langchain_core.language_models
  • langchain_core.output_parsers
  • langchain_core.prompts
  • langchain_core.prompts.prompt
  • langchain_core.retrievers
  • langchain_core.runnables
  • logging

Frequently Asked Questions

What does re_phraser.py do?
re_phraser.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, Serialization subdomain.
What does re_phraser.py depend on?
re_phraser.py imports 9 module(s): langchain_core.callbacks, langchain_core.documents, langchain_core.language_models, langchain_core.output_parsers, langchain_core.prompts, langchain_core.prompts.prompt, langchain_core.retrievers, langchain_core.runnables, and 1 more.
Where is re_phraser.py in the architecture?
re_phraser.py is located at libs/langchain/langchain_classic/retrievers/re_phraser.py (domain: CoreAbstractions, subdomain: Serialization, directory: libs/langchain/langchain_classic/retrievers).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free