Skip to content

Commit

Permalink
feat(anthropic): Adds Anthropic citations support (#7651)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoblee93 authored Feb 5, 2025
1 parent 4587906 commit 116982f
Show file tree
Hide file tree
Showing 8 changed files with 357 additions and 76 deletions.
219 changes: 206 additions & 13 deletions docs/core_docs/docs/integrations/chat/anthropic.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 2,
"id": "bba739ed",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -783,7 +783,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 3,
"id": "6e47de9b",
"metadata": {},
"outputs": [
Expand All @@ -793,16 +793,15 @@
"text": [
"USAGE: {\n",
" input_tokens: 19,\n",
" cache_creation_input_tokens: 2925,\n",
" cache_creation_input_tokens: 2921,\n",
" cache_read_input_tokens: 0,\n",
" output_tokens: 327\n",
" output_tokens: 355\n",
"}\n"
]
}
],
"source": [
"import { ChatAnthropic } from \"@langchain/anthropic\";\n",
"import { HumanMessage, SystemMessage } from \"@langchain/core/messages\";\n",
"\n",
"const modelWithCaching = new ChatAnthropic({\n",
" model: \"claude-3-haiku-20240307\",\n",
Expand All @@ -820,7 +819,8 @@
"${CACHED_TEXT}`;\n",
"\n",
"const messages = [\n",
" new SystemMessage({\n",
" {\n",
" role: \"system\",\n",
" content: [\n",
" {\n",
" type: \"text\",\n",
Expand All @@ -829,10 +829,11 @@
" cache_control: { type: \"ephemeral\" },\n",
" },\n",
" ],\n",
" }),\n",
" new HumanMessage({\n",
" },\n",
" {\n",
" role: \"user\",\n",
" content: \"What types of messages are supported in LangChain?\",\n",
" }),\n",
" },\n",
"];\n",
"\n",
"const res = await modelWithCaching.invoke(messages);\n",
Expand All @@ -852,7 +853,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 4,
"id": "5d264f8b",
"metadata": {},
"outputs": [
Expand All @@ -863,8 +864,8 @@
"USAGE: {\n",
" input_tokens: 19,\n",
" cache_creation_input_tokens: 0,\n",
" cache_read_input_tokens: 2925,\n",
" output_tokens: 250\n",
" cache_read_input_tokens: 2921,\n",
" output_tokens: 357\n",
"}\n"
]
}
Expand Down Expand Up @@ -961,6 +962,198 @@
"await modelWithCustomClient.invoke([{ role: \"user\", content: \"Hello!\" }]);"
]
},
{
"cell_type": "markdown",
"id": "68a85a61",
"metadata": {},
"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",
"\n",
"### Simple example\n",
"\n",
"In this example we pass a [plain text document](https://docs.anthropic.com/en/docs/build-with-claude/citations#plain-text-documents). In the background, Claude [automatically chunks](https://docs.anthropic.com/en/docs/build-with-claude/citations#plain-text-documents) the input text into sentences, which are used when generating citations."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "d3f1c754",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[\n",
" {\n",
" \"type\": \"text\",\n",
" \"text\": \"Based on the document, I can tell you that:\\n\\n- \"\n",
" },\n",
" {\n",
" \"type\": \"text\",\n",
" \"text\": \"The grass is green\",\n",
" \"citations\": [\n",
" {\n",
" \"type\": \"char_location\",\n",
" \"cited_text\": \"The grass is green. \",\n",
" \"document_index\": 0,\n",
" \"document_title\": \"My Document\",\n",
" \"start_char_index\": 0,\n",
" \"end_char_index\": 20\n",
" }\n",
" ]\n",
" },\n",
" {\n",
" \"type\": \"text\",\n",
" \"text\": \"\\n- \"\n",
" },\n",
" {\n",
" \"type\": \"text\",\n",
" \"text\": \"The sky is blue\",\n",
" \"citations\": [\n",
" {\n",
" \"type\": \"char_location\",\n",
" \"cited_text\": \"The sky is blue.\",\n",
" \"document_index\": 0,\n",
" \"document_title\": \"My Document\",\n",
" \"start_char_index\": 20,\n",
" \"end_char_index\": 36\n",
" }\n",
" ]\n",
" }\n",
"]\n"
]
}
],
"source": [
"import { ChatAnthropic } from \"@langchain/anthropic\";\n",
"\n",
"const citationsModel = new ChatAnthropic({\n",
" model: \"claude-3-5-haiku-latest\",\n",
"});\n",
"\n",
"const messagesWithCitations = [\n",
" {\n",
" role: \"user\",\n",
" content: [\n",
" {\n",
" type: \"document\",\n",
" source: {\n",
" type: \"text\",\n",
" media_type: \"text/plain\",\n",
" data: \"The grass is green. The sky is blue.\",\n",
" },\n",
" title: \"My Document\",\n",
" context: \"This is a trustworthy document.\",\n",
" citations: {\n",
" enabled: true,\n",
" },\n",
" },\n",
" {\n",
" type: \"text\",\n",
" text: \"What color is the grass and sky?\",\n",
" },\n",
" ],\n",
" }\n",
"];\n",
"\n",
"const responseWithCitations = await citationsModel.invoke(messagesWithCitations);\n",
"\n",
"console.log(JSON.stringify(responseWithCitations.content, null, 2));"
]
},
{
"cell_type": "markdown",
"id": "14269f15",
"metadata": {},
"source": [
"### Using with text splitters\n",
"\n",
"Anthropic also lets you specify your own splits using [custom document](https://docs.anthropic.com/en/docs/build-with-claude/citations#custom-content-documents) types. LangChain [text splitters](/docs/concepts/text_splitters/) can be used to generate meaningful splits for this purpose. See the below example, where we split the LangChain.js README (a markdown document) and pass it to Claude as context:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5e9f3213",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[\n",
" {\n",
" \"type\": \"text\",\n",
" \"text\": \"Based on the documentation, I can provide you with a link to LangChain's tutorials:\\n\\n\"\n",
" },\n",
" {\n",
" \"type\": \"text\",\n",
" \"text\": \"The tutorials can be found at: https://js.langchain.com/docs/tutorials/\",\n",
" \"citations\": [\n",
" {\n",
" \"type\": \"content_block_location\",\n",
" \"cited_text\": \"[Tutorial](https://js.langchain.com/docs/tutorials/)walkthroughs\",\n",
" \"document_index\": 0,\n",
" \"document_title\": null,\n",
" \"start_block_index\": 191,\n",
" \"end_block_index\": 194\n",
" }\n",
" ]\n",
" }\n",
"]\n"
]
}
],
"source": [
"import { ChatAnthropic } from \"@langchain/anthropic\";\n",
"import { MarkdownTextSplitter } from \"langchain/text_splitter\";\n",
"\n",
"function formatToAnthropicDocuments(documents: string[]) {\n",
" return {\n",
" type: \"document\",\n",
" source: {\n",
" type: \"content\",\n",
" content: documents.map((document) => ({ type: \"text\", text: document })),\n",
" },\n",
" citations: { enabled: true },\n",
" };\n",
"}\n",
"\n",
"// Pull readme\n",
"const readmeResponse = await fetch(\n",
" \"https://raw.githubusercontent.com/langchain-ai/langchainjs/master/README.md\"\n",
");\n",
"\n",
"const readme = await readmeResponse.text();\n",
"\n",
"// Split into chunks\n",
"const splitter = new MarkdownTextSplitter({\n",
" chunkOverlap: 0,\n",
" chunkSize: 50,\n",
"});\n",
"const documents = await splitter.splitText(readme);\n",
"\n",
"// Construct message\n",
"const messageWithSplitDocuments = {\n",
" role: \"user\",\n",
" content: [\n",
" formatToAnthropicDocuments(documents),\n",
" { type: \"text\", text: \"Give me a link to LangChain's tutorials. Cite your sources\" },\n",
" ],\n",
"};\n",
"\n",
"// Query LLM\n",
"const citationsModelWithSplits = new ChatAnthropic({\n",
" model: \"claude-3-5-sonnet-latest\",\n",
"});\n",
"const resWithSplits = await citationsModelWithSplits.invoke([messageWithSplitDocuments]);\n",
"\n",
"console.log(JSON.stringify(resWithSplits.content, null, 2));"
]
},
{
"cell_type": "markdown",
"id": "3a5bb5ca-c3ae-4a58-be67-2cd18574b9a3",
Expand Down Expand Up @@ -992,4 +1185,4 @@
},
"nbformat": 4,
"nbformat_minor": 5
}
}
2 changes: 1 addition & 1 deletion libs/langchain-anthropic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"author": "LangChain",
"license": "MIT",
"dependencies": {
"@anthropic-ai/sdk": "^0.32.1",
"@anthropic-ai/sdk": "^0.36.3",
"fast-xml-parser": "^4.4.1",
"zod": "^3.22.4",
"zod-to-json-schema": "^3.22.4"
Expand Down
Loading

0 comments on commit 116982f

Please sign in to comment.