create_retriever_tool() — langchain Function Reference
Architecture documentation for the create_retriever_tool() function in retriever.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD ca5cf150_7bcf_99b1_7bbd_5b6612450ce0["create_retriever_tool()"] bb53592c_609b_56c7_ded2_aeaa0c66b60b["retriever.py"] ca5cf150_7bcf_99b1_7bbd_5b6612450ce0 -->|defined in| bb53592c_609b_56c7_ded2_aeaa0c66b60b style ca5cf150_7bcf_99b1_7bbd_5b6612450ce0 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/core/langchain_core/tools/retriever.py lines 31–94
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,
)
Domain
Subdomains
Defined In
Source
Frequently Asked Questions
What does create_retriever_tool() do?
create_retriever_tool() is a function in the langchain codebase, defined in libs/core/langchain_core/tools/retriever.py.
Where is create_retriever_tool() defined?
create_retriever_tool() is defined in libs/core/langchain_core/tools/retriever.py at line 31.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free