diff --git a/docs/docs/integrations/chat/anthropic.ipynb b/docs/docs/integrations/chat/anthropic.ipynb index 8a3a20d6514..ceb06d2c05d 100644 --- a/docs/docs/integrations/chat/anthropic.ipynb +++ b/docs/docs/integrations/chat/anthropic.ipynb @@ -893,7 +893,7 @@ "source": [ "## Citations\n", "\n", - "Anthropic supports a [citations](https://docs.anthropic.com/en/docs/build-with-claude/citations) feature that lets Claude attach context to its answers based on source documents supplied by the user. When [document content blocks](https://docs.anthropic.com/en/docs/build-with-claude/citations#document-types) with `\"citations\": {\"enabled\": True}` are included in a query, Claude may generate citations in its response.\n", + "Anthropic supports a [citations](https://docs.anthropic.com/en/docs/build-with-claude/citations) feature that lets Claude attach context to its answers based on source documents supplied by the user. When [document](https://docs.anthropic.com/en/docs/build-with-claude/citations#document-types) or `search result` content blocks with `\"citations\": {\"enabled\": True}` are included in a query, Claude may generate citations in its response.\n", "\n", "### Simple example\n", "\n", @@ -963,6 +963,143 @@ "response.content" ] }, + { + "cell_type": "markdown", + "id": "4ca82106-69b3-4266-bf23-b2ffba873ee2", + "metadata": {}, + "source": [ + "### In tool results (agentic RAG)\n", + "\n", + ":::info Requires ``langchain-anthropic>=0.3.17``\n", + "\n", + ":::\n", + "\n", + "Claude supports a [search_result](https://docs.anthropic.com/en/docs/build-with-claude/search-results) content block representing citable results from queries against a knowledge base or other custom source. These content blocks can be passed to claude both top-line (as in the above example) and within a tool result. This allows Claude to cite elements of its response using the result of a tool call.\n", + "\n", + "To pass search results in response to tool calls, define a tool that returns a list of `search_result` content blocks in Anthropic's native format. For example:\n", + "```python\n", + "def retrieval_tool(query: str) -> list[dict]:\n", + " \"\"\"Access my knowledge base.\"\"\"\n", + "\n", + " # Run a search (e.g., with a LangChain vector store)\n", + " results = vector_store.similarity_search(query=query, k=2)\n", + "\n", + " # Package results into search_result blocks\n", + " return [\n", + " {\n", + " \"type\": \"search_result\",\n", + " \"title\": \"Leave policy\",\n", + " \"source\": \"HR Leave Policy 2025\",\n", + " \"citations\": { \"enabled\": True },\n", + " \"content\": [{\"type\": \"text\", \"text\": doc.page_content}],\n", + " }\n", + " for doc in results\n", + " ]\n", + "```\n", + "\n", + "We also need to specify the `search-results-2025-06-09` beta when instantiating ChatAnthropic. You can see an end-to-end example below.\n", + "\n", + "
\n", + "End to end example with LangGraph\n", + "\n", + "Here we demonstrate an end-to-end example in which we populate a LangChain [vector store](/docs/concepts/vectorstores/) with sample documents and equip Claude with a tool that queries those documents.\n", + "The tool here takes a search query and a `category` string literal, but any valid tool signature can be used.\n", + "\n", + "```python\n", + "from typing import Literal\n", + "\n", + "from langchain.chat_models import init_chat_model\n", + "from langchain.embeddings import init_embeddings\n", + "from langchain_core.documents import Document\n", + "from langchain_core.vectorstores import InMemoryVectorStore\n", + "from langgraph.checkpoint.memory import InMemorySaver\n", + "from langgraph.prebuilt import create_react_agent\n", + "\n", + "\n", + "# Set up vector store\n", + "embeddings = init_embeddings(\"openai:text-embedding-3-small\")\n", + "vector_store = InMemoryVectorStore(embeddings)\n", + "\n", + "document_1 = Document(\n", + " id=\"1\",\n", + " page_content=(\n", + " \"To request vacation days, submit a leave request form through the \"\n", + " \"HR portal. Approval will be sent by email.\"\n", + " ),\n", + " metadata={\"category\": \"HR Policy\"},\n", + ")\n", + "document_2 = Document(\n", + " id=\"2\",\n", + " page_content=\"Managers will review vacation requests within 3 business days.\",\n", + " metadata={\"category\": \"HR Policy\"},\n", + ")\n", + "document_3 = Document(\n", + " id=\"3\",\n", + " page_content=(\n", + " \"Employees with over 6 months tenure are eligible for 20 paid vacation days \"\n", + " \"per year.\"\n", + " ),\n", + " metadata={\"category\": \"Benefits Policy\"},\n", + ")\n", + "\n", + "documents = [document_1, document_2, document_3]\n", + "vector_store.add_documents(documents=documents)\n", + "\n", + "\n", + "# Define tool\n", + "async def retrieval_tool(\n", + " query: str, category: Literal[\"HR Policy\", \"Benefits Policy\"]\n", + ") -> list[dict]:\n", + " \"\"\"Access my knowledge base.\"\"\"\n", + "\n", + " def _filter_function(doc: Document) -> bool:\n", + " return doc.metadata.get(\"category\") == category\n", + "\n", + " results = vector_store.similarity_search(\n", + " query=query, k=2, filter=_filter_function\n", + " )\n", + "\n", + " return [\n", + " {\n", + " \"type\": \"search_result\",\n", + " \"title\": \"Leave policy\",\n", + " \"source\": \"HR Leave Policy 2025\",\n", + " \"citations\": { \"enabled\": True },\n", + " \"content\": [{\"type\": \"text\", \"text\": doc.page_content}],\n", + " }\n", + " for doc in results\n", + " ]\n", + "\n", + "\n", + "\n", + "# Create agent\n", + "llm = init_chat_model(\n", + " \"anthropic:claude-3-5-haiku-latest\",\n", + " betas=[\"search-results-2025-06-09\"],\n", + ")\n", + "\n", + "checkpointer = InMemorySaver()\n", + "agent = create_react_agent(llm, [retrieval_tool], checkpointer=checkpointer)\n", + "\n", + "\n", + "# Invoke on a query\n", + "config = {\"configurable\": {\"thread_id\": \"session_1\"}}\n", + "\n", + "input_message = {\n", + " \"role\": \"user\",\n", + " \"content\": \"How do I request vacation days?\",\n", + "}\n", + "async for step in agent.astream(\n", + " {\"messages\": [input_message]},\n", + " config,\n", + " stream_mode=\"values\",\n", + "):\n", + " step[\"messages\"][-1].pretty_print()\n", + "```\n", + "\n", + "
" + ] + }, { "cell_type": "markdown", "id": "69956596-0e6c-492b-934d-c08ed3c9de9a",