Home / Function/ create_history_aware_retriever() — langchain Function Reference

create_history_aware_retriever() — langchain Function Reference

Architecture documentation for the create_history_aware_retriever() function in history_aware_retriever.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  ca92c95b_f325_922c_63a7_8e9a977ee528["create_history_aware_retriever()"]
  d958d9a5_f42d_8682_de85_5bfc79247db0["history_aware_retriever.py"]
  ca92c95b_f325_922c_63a7_8e9a977ee528 -->|defined in| d958d9a5_f42d_8682_de85_5bfc79247db0
  style ca92c95b_f325_922c_63a7_8e9a977ee528 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain/langchain_classic/chains/history_aware_retriever.py lines 10–68

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

Frequently Asked Questions

What does create_history_aware_retriever() do?
create_history_aware_retriever() is a function in the langchain codebase, defined in libs/langchain/langchain_classic/chains/history_aware_retriever.py.
Where is create_history_aware_retriever() defined?
create_history_aware_retriever() is defined in libs/langchain/langchain_classic/chains/history_aware_retriever.py at line 10.

Analyze Your Own Codebase

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

Try Supermodel Free