Home / File/ retriever.py — langchain Source File

retriever.py — langchain Source File

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

File python AgentOrchestration ActionLogic 7 imports 2 functions 1 classes

Entity Profile

Dependency Diagram

graph LR
  87a75ac0_c22c_15b1_c987_523cbc0cf44d["retriever.py"]
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  87a75ac0_c22c_15b1_c987_523cbc0cf44d --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  6e58aaea_f08e_c099_3cc7_f9567bfb1ae7["pydantic"]
  87a75ac0_c22c_15b1_c987_523cbc0cf44d --> 6e58aaea_f08e_c099_3cc7_f9567bfb1ae7
  f3bc7443_c889_119d_0744_aacc3620d8d2["langchain_core.callbacks"]
  87a75ac0_c22c_15b1_c987_523cbc0cf44d --> f3bc7443_c889_119d_0744_aacc3620d8d2
  c554676d_b731_47b2_a98f_c1c2d537c0aa["langchain_core.documents"]
  87a75ac0_c22c_15b1_c987_523cbc0cf44d --> c554676d_b731_47b2_a98f_c1c2d537c0aa
  e6b4f61e_7b98_6666_3641_26b069517d4a["langchain_core.prompts"]
  87a75ac0_c22c_15b1_c987_523cbc0cf44d --> e6b4f61e_7b98_6666_3641_26b069517d4a
  b92c1fb2_4ef7_7241_137f_75491d966733["langchain_core.tools.structured"]
  87a75ac0_c22c_15b1_c987_523cbc0cf44d --> b92c1fb2_4ef7_7241_137f_75491d966733
  38bc5323_3713_7377_32f8_091293bea54b["langchain_core.retrievers"]
  87a75ac0_c22c_15b1_c987_523cbc0cf44d --> 38bc5323_3713_7377_32f8_091293bea54b
  style 87a75ac0_c22c_15b1_c987_523cbc0cf44d fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Retriever tool."""

from __future__ import annotations

from typing import TYPE_CHECKING, Literal

from pydantic import BaseModel, Field

# Cannot move Callbacks and Document to TYPE_CHECKING as StructuredTool's
# func/coroutine parameter annotations are evaluated at runtime.
from langchain_core.callbacks import Callbacks  # noqa: TC001
from langchain_core.documents import Document  # noqa: TC001
from langchain_core.prompts import (
    BasePromptTemplate,
    PromptTemplate,
    aformat_document,
    format_document,
)
from langchain_core.tools.structured import StructuredTool

if TYPE_CHECKING:
    from langchain_core.retrievers import BaseRetriever


class RetrieverInput(BaseModel):
    """Input to the retriever."""

    query: str = Field(description="query to look up in retriever")


def create_retriever_tool(
    retriever: BaseRetriever,
    name: str,
    description: str,
    *,
    document_prompt: BasePromptTemplate | None = None,
    document_separator: str = "\n\n",
    response_format: Literal["content", "content_and_artifact"] = "content",
) -> StructuredTool:
    r"""Create a tool to do retrieval of documents.

    Args:
        retriever: The retriever to use for the retrieval
        name: The name for the tool.

            This will be passed to the language model, so should be unique and somewhat
            descriptive.
        description: The description for the tool.

            This will be passed to the language model, so should be descriptive.
        document_prompt: The prompt to use for the document.
        document_separator: The separator to use between documents.
        response_format: The tool response format.

            If `'content'` then the output of the tool is interpreted as the contents of
            a `ToolMessage`. If `'content_and_artifact'` then the output is expected to
            be a two-tuple corresponding to the `(content, artifact)` of a `ToolMessage`
            (artifact being a list of documents in this case).

    Returns:
        Tool class to pass to an agent.
    """
    document_prompt_ = document_prompt or PromptTemplate.from_template("{page_content}")

    def func(
        query: str, callbacks: Callbacks = None
    ) -> str | tuple[str, list[Document]]:
        docs = retriever.invoke(query, config={"callbacks": callbacks})
        content = document_separator.join(
            format_document(doc, document_prompt_) for doc in docs
        )
        if response_format == "content_and_artifact":
            return (content, docs)
        return content

    async def afunc(
        query: str, callbacks: Callbacks = None
    ) -> str | tuple[str, list[Document]]:
        docs = await retriever.ainvoke(query, config={"callbacks": callbacks})
        content = document_separator.join(
            [await aformat_document(doc, document_prompt_) for doc in docs]
        )
        if response_format == "content_and_artifact":
            return (content, docs)
        return content

    return StructuredTool(
        name=name,
        description=description,
        func=func,
        coroutine=afunc,
        args_schema=RetrieverInput,
        response_format=response_format,
    )

Subdomains

Classes

Dependencies

  • langchain_core.callbacks
  • langchain_core.documents
  • langchain_core.prompts
  • langchain_core.retrievers
  • langchain_core.tools.structured
  • pydantic
  • typing

Frequently Asked Questions

What does retriever.py do?
retriever.py is a source file in the langchain codebase, written in python. It belongs to the AgentOrchestration domain, ActionLogic subdomain.
What functions are defined in retriever.py?
retriever.py defines 2 function(s): create_retriever_tool, langchain_core.
What does retriever.py depend on?
retriever.py imports 7 module(s): langchain_core.callbacks, langchain_core.documents, langchain_core.prompts, langchain_core.retrievers, langchain_core.tools.structured, pydantic, typing.
Where is retriever.py in the architecture?
retriever.py is located at libs/core/langchain_core/tools/retriever.py (domain: AgentOrchestration, subdomain: ActionLogic, directory: libs/core/langchain_core/tools).

Analyze Your Own Codebase

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

Try Supermodel Free