mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-19 21:33:51 +00:00
docs: document anthropic features (#30030)
Update integrations page with extended thinking feature. Update API reference with extended thinking and citations.
This commit is contained in:
parent
6c7c8a164f
commit
0dbcc1d099
@ -315,6 +315,59 @@
|
||||
"ai_msg.tool_calls"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "6e36d25c-f358-49e5-aefa-b99fbd3fec6b",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Extended thinking\n",
|
||||
"\n",
|
||||
"Claude 3.7 Sonnet supports an [extended thinking](https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking) feature, which will output the step-by-step reasoning process that led to its final answer.\n",
|
||||
"\n",
|
||||
"To use it, specify the `thinking` parameter when initializing `ChatAnthropic`. It can also be passed in as a kwarg during invocation.\n",
|
||||
"\n",
|
||||
"You will need to specify a token budget to use this feature. See usage example below:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"id": "a34cf93b-8522-43a6-a3f3-8a189ddf54a7",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[\n",
|
||||
" {\n",
|
||||
" \"signature\": \"ErUBCkYIARgCIkCx7bIPj35jGPHpoVOB2y5hvPF8MN4lVK75CYGftmVNlI4axz2+bBbSexofWsN1O/prwNv8yPXnIXQmwT6zrJsKEgwJzvks0yVRZtaGBScaDOm9xcpOxbuhku1zViIw9WDgil/KZL8DsqWrhVpC6TzM0RQNCcsHcmgmyxbgG9g8PR0eJGLxCcGoEw8zMQu1Kh1hQ1/03hZ2JCOgigpByR9aNPTwwpl64fQUe6WwIw==\",\n",
|
||||
" \"thinking\": \"To find the cube root of 50.653, I need to find the value of $x$ such that $x^3 = 50.653$.\\n\\nI can try to estimate this first. \\n$3^3 = 27$\\n$4^3 = 64$\\n\\nSo the cube root of 50.653 will be somewhere between 3 and 4, but closer to 4.\\n\\nLet me try to compute this more precisely. I can use the cube root function:\\n\\ncube root of 50.653 = 50.653^(1/3)\\n\\nLet me calculate this:\\n50.653^(1/3) \\u2248 3.6998\\n\\nLet me verify:\\n3.6998^3 \\u2248 50.6533\\n\\nThat's very close to 50.653, so I'm confident that the cube root of 50.653 is approximately 3.6998.\\n\\nActually, let me compute this more precisely:\\n50.653^(1/3) \\u2248 3.69981\\n\\nLet me verify once more:\\n3.69981^3 \\u2248 50.652998\\n\\nThat's extremely close to 50.653, so I'll say that the cube root of 50.653 is approximately 3.69981.\",\n",
|
||||
" \"type\": \"thinking\"\n",
|
||||
" },\n",
|
||||
" {\n",
|
||||
" \"text\": \"The cube root of 50.653 is approximately 3.6998.\\n\\nTo verify: 3.6998\\u00b3 = 50.6530, which is very close to our original number.\",\n",
|
||||
" \"type\": \"text\"\n",
|
||||
" }\n",
|
||||
"]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import json\n",
|
||||
"\n",
|
||||
"from langchain_anthropic import ChatAnthropic\n",
|
||||
"\n",
|
||||
"llm = ChatAnthropic(\n",
|
||||
" model=\"claude-3-7-sonnet-latest\",\n",
|
||||
" max_tokens=5000,\n",
|
||||
" thinking={\"type\": \"enabled\", \"budget_tokens\": 2000},\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"response = llm.invoke(\"What is the cube root of 50.653?\")\n",
|
||||
"print(json.dumps(response.content, indent=2))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "301d372f-4dec-43e6-b58c-eee25633e1a6",
|
||||
|
@ -509,6 +509,93 @@ class ChatAnthropic(BaseChatModel):
|
||||
|
||||
"The image depicts a sunny day with a partly cloudy sky. The sky is a brilliant blue color with scattered white clouds drifting across. The lighting and cloud patterns suggest pleasant, mild weather conditions. The scene shows a grassy field or meadow with a wooden boardwalk trail leading through it, indicating an outdoor setting on a nice day well-suited for enjoying nature."
|
||||
|
||||
Extended thinking:
|
||||
Claude 3.7 Sonnet supports an
|
||||
`extended thinking <https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking>`_
|
||||
feature, which will output the step-by-step reasoning process that led to its
|
||||
final answer.
|
||||
|
||||
To use it, specify the `thinking` parameter when initializing `ChatAnthropic`.
|
||||
It can also be passed in as a kwarg during invocation.
|
||||
|
||||
You will need to specify a token budget to use this feature. See usage example:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
from langchain_anthropic import ChatAnthropic
|
||||
|
||||
llm = ChatAnthropic(
|
||||
model="claude-3-7-sonnet-latest",
|
||||
max_tokens=5000,
|
||||
thinking={"type": "enabled", "budget_tokens": 2000},
|
||||
)
|
||||
|
||||
response = llm.invoke("What is the cube root of 50.653?")
|
||||
response.content
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
[{'signature': '...', 'thinking': "To find the cube root of 50.653...", 'type': 'thinking'}, {'text': 'The cube root of 50.653 is ...', 'type': 'text'}]
|
||||
|
||||
Citations:
|
||||
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.
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
from langchain_anthropic import ChatAnthropic
|
||||
|
||||
llm = ChatAnthropic(model="claude-3-5-haiku-latest")
|
||||
|
||||
messages = [
|
||||
{
|
||||
"role": "user",
|
||||
"content": [
|
||||
{
|
||||
"type": "document",
|
||||
"source": {
|
||||
"type": "text",
|
||||
"media_type": "text/plain",
|
||||
"data": "The grass is green. The sky is blue.",
|
||||
},
|
||||
"title": "My Document",
|
||||
"context": "This is a trustworthy document.",
|
||||
"citations": {"enabled": True},
|
||||
},
|
||||
{"type": "text", "text": "What color is the grass and sky?"},
|
||||
],
|
||||
}
|
||||
]
|
||||
response = llm.invoke(messages)
|
||||
response.content
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
[{'text': 'Based on the document, ', 'type': 'text'},
|
||||
{'text': 'the grass is green',
|
||||
'type': 'text',
|
||||
'citations': [{'type': 'char_location',
|
||||
'cited_text': 'The grass is green. ',
|
||||
'document_index': 0,
|
||||
'document_title': 'My Document',
|
||||
'start_char_index': 0,
|
||||
'end_char_index': 20}]},
|
||||
{'text': ', and ', 'type': 'text'},
|
||||
{'text': 'the sky is blue',
|
||||
'type': 'text',
|
||||
'citations': [{'type': 'char_location',
|
||||
'cited_text': 'The sky is blue.',
|
||||
'document_index': 0,
|
||||
'document_title': 'My Document',
|
||||
'start_char_index': 20,
|
||||
'end_char_index': 36}]},
|
||||
{'text': '.', 'type': 'text'}]
|
||||
|
||||
Token usage:
|
||||
.. code-block:: python
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user