Home / File/ history_aware_retriever.py — langchain Source File

history_aware_retriever.py — langchain Source File

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

Entity Profile

Dependency Diagram

graph LR
  8fa402b1_e07d_08fe_77ad_a440f3b3a72f["history_aware_retriever.py"]
  ba43b74d_3099_7e1c_aac3_cf594720469e["langchain_core.language_models"]
  8fa402b1_e07d_08fe_77ad_a440f3b3a72f --> ba43b74d_3099_7e1c_aac3_cf594720469e
  83d7c7fd_1989_762c_9cf3_cecb50ada22b["langchain_core.output_parsers"]
  8fa402b1_e07d_08fe_77ad_a440f3b3a72f --> 83d7c7fd_1989_762c_9cf3_cecb50ada22b
  e6b4f61e_7b98_6666_3641_26b069517d4a["langchain_core.prompts"]
  8fa402b1_e07d_08fe_77ad_a440f3b3a72f --> e6b4f61e_7b98_6666_3641_26b069517d4a
  38bc5323_3713_7377_32f8_091293bea54b["langchain_core.retrievers"]
  8fa402b1_e07d_08fe_77ad_a440f3b3a72f --> 38bc5323_3713_7377_32f8_091293bea54b
  2ceb1686_0f8c_8ae0_36d1_7c0b702fda1c["langchain_core.runnables"]
  8fa402b1_e07d_08fe_77ad_a440f3b3a72f --> 2ceb1686_0f8c_8ae0_36d1_7c0b702fda1c
  style 8fa402b1_e07d_08fe_77ad_a440f3b3a72f fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

from __future__ import annotations

from langchain_core.language_models import LanguageModelLike
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import BasePromptTemplate
from langchain_core.retrievers import RetrieverLike, RetrieverOutputLike
from langchain_core.runnables import RunnableBranch


def create_history_aware_retriever(
    llm: LanguageModelLike,
    retriever: RetrieverLike,
    prompt: BasePromptTemplate,
) -> RetrieverOutputLike:
    """Create a chain that takes conversation history and returns documents.

    If there is no `chat_history`, then the `input` is just passed directly to the
    retriever. If there is `chat_history`, then the prompt and LLM will be used
    to generate a search query. That search query is then passed to the retriever.

    Args:
        llm: Language model to use for generating a search term given chat history
        retriever: `RetrieverLike` object that takes a string as input and outputs
            a list of `Document` objects.
        prompt: The prompt used to generate the search query for the retriever.

    Returns:
        An LCEL Runnable. The runnable input must take in `input`, and if there
        is chat history should take it in the form of `chat_history`.
        The `Runnable` output is a list of `Document` objects

    Example:
        ```python
        # pip install -U langchain langchain-community

        from langchain_openai import ChatOpenAI
        from langchain_classic.chains import create_history_aware_retriever
        from langchain_classic import hub

        rephrase_prompt = hub.pull("langchain-ai/chat-langchain-rephrase")
        model = ChatOpenAI()
        retriever = ...
        chat_retriever_chain = create_history_aware_retriever(
            model, retriever, rephrase_prompt
        )

        chain.invoke({"input": "...", "chat_history": })

        ```
    """
    if "input" not in prompt.input_variables:
        msg = (
            "Expected `input` to be a prompt variable, "
            f"but got {prompt.input_variables}"
        )
        raise ValueError(msg)

    retrieve_documents: RetrieverOutputLike = RunnableBranch(
        (
            # Both empty string and empty list evaluate to False
            lambda x: not x.get("chat_history", False),
            # If no chat history, then we just pass input to retriever
            (lambda x: x["input"]) | retriever,
        ),
        # If chat history, then we pass inputs to LLM chain, then to retriever
        prompt | llm | StrOutputParser() | retriever,
    ).with_config(run_name="chat_retriever_chain")
    return retrieve_documents

Subdomains

Dependencies

  • langchain_core.language_models
  • langchain_core.output_parsers
  • langchain_core.prompts
  • langchain_core.retrievers
  • langchain_core.runnables

Frequently Asked Questions

What does history_aware_retriever.py do?
history_aware_retriever.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, RunnableInterface subdomain.
What functions are defined in history_aware_retriever.py?
history_aware_retriever.py defines 1 function(s): create_history_aware_retriever.
What does history_aware_retriever.py depend on?
history_aware_retriever.py imports 5 module(s): langchain_core.language_models, langchain_core.output_parsers, langchain_core.prompts, langchain_core.retrievers, langchain_core.runnables.
Where is history_aware_retriever.py in the architecture?
history_aware_retriever.py is located at libs/langchain/langchain_classic/chains/history_aware_retriever.py (domain: CoreAbstractions, subdomain: RunnableInterface, directory: libs/langchain/langchain_classic/chains).

Analyze Your Own Codebase

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

Try Supermodel Free