diff --git a/docs/docs/integrations/chat/anthropic.ipynb b/docs/docs/integrations/chat/anthropic.ipynb index e17b7d2cdde..4ac890e18b9 100644 --- a/docs/docs/integrations/chat/anthropic.ipynb +++ b/docs/docs/integrations/chat/anthropic.ipynb @@ -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", diff --git a/docs/docs/integrations/chat/writer.ipynb b/docs/docs/integrations/chat/writer.ipynb index a455dfb5e6b..3e8378dfbe3 100644 --- a/docs/docs/integrations/chat/writer.ipynb +++ b/docs/docs/integrations/chat/writer.ipynb @@ -15,9 +15,9 @@ "## Overview\n", "\n", "### Integration details\n", - "| Class | Package | Local | Serializable | JS support | Package downloads | Package latest |\n", - "|:--------------------------------------------------------------------------------------------------------------------------------------------|:-----------------| :---: | :---: |:----------:|:------------------------------------------------------------------------------------------------:|:---------------------------------------------------------------------------------------------:|\n", - "| ChatWriter | [langchain-writer](https://pypi.org/project/langchain-writer/) | ❌ | ❌ | ❌ | ![PyPI - Downloads](https://img.shields.io/pypi/dm/langchain-writer?style=flat-square&label=%20) | ![PyPI - Version](https://img.shields.io/pypi/v/langchain-writer?style=flat-square&label=%20) |\n", + "| Class | Package | Local | Serializable | JS support | Package downloads | Package latest |\n", + "|:-------------------------------------------------------------------------------------------------------------------------|:-----------------| :---: | :---: |:----------:|:------------------------------------------------------------------------------------------------:|:---------------------------------------------------------------------------------------------:|\n", + "| [ChatWriter](https://github.com/writer/langchain-writer/blob/main/langchain_writer/chat_models.py#L308) | [langchain-writer](https://pypi.org/project/langchain-writer/) | ❌ | ❌ | ❌ | ![PyPI - Downloads](https://img.shields.io/pypi/dm/langchain-writer?style=flat-square&label=%20) | ![PyPI - Version](https://img.shields.io/pypi/v/langchain-writer?style=flat-square&label=%20) |\n", "### Model features\n", "| [Tool calling](/docs/how_to/tool_calling) | Structured output | JSON mode | Image input | Audio input | Video input | [Token-level streaming](/docs/how_to/chat_streaming/) | Native async | [Token usage](/docs/how_to/chat_token_usage_tracking/) | Logprobs |\n", "| :---: |:-----------------:| :---: | :---: | :---: | :---: | :---: | :---: |:--------------------------------:|:--------:|\n", @@ -36,17 +36,21 @@ }, { "cell_type": "code", - "execution_count": null, "id": "433e8d2b-9519-4b49-b2c4-7ab65b046c94", - "metadata": {}, - "outputs": [], + "metadata": { + "jupyter": { + "is_executing": true + } + }, "source": [ "import getpass\n", "import os\n", "\n", "if not os.getenv(\"WRITER_API_KEY\"):\n", " os.environ[\"WRITER_API_KEY\"] = getpass.getpass(\"Enter your Writer API key: \")" - ] + ], + "outputs": [], + "execution_count": null }, { "cell_type": "markdown", @@ -58,14 +62,14 @@ }, { "cell_type": "code", - "execution_count": null, "id": "a15d341e-3e26-4ca3-830b-5aab30ed66de", "metadata": {}, - "outputs": [], "source": [ "# os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n", "# os.environ[\"LANGCHAIN_API_KEY\"] = getpass.getpass(\"Enter your LangSmith API key: \")" - ] + ], + "outputs": [], + "execution_count": null }, { "cell_type": "markdown", @@ -79,13 +83,13 @@ }, { "cell_type": "code", - "execution_count": null, "id": "652d6238-1f87-422a-b135-f5abbb8652fc", "metadata": {}, - "outputs": [], "source": [ "%pip install -qU langchain-writer" - ] + ], + "outputs": [], + "execution_count": null }, { "cell_type": "markdown", @@ -99,10 +103,8 @@ }, { "cell_type": "code", - "execution_count": null, "id": "cb09c344-1836-4e0c-acf8-11d13ac1dbae", "metadata": {}, - "outputs": [], "source": [ "from langchain_writer import ChatWriter\n", "\n", @@ -113,7 +115,9 @@ " timeout=None,\n", " max_retries=2,\n", ")" - ] + ], + "outputs": [], + "execution_count": null }, { "cell_type": "markdown", @@ -127,12 +131,10 @@ }, { "cell_type": "code", - "execution_count": null, "id": "62e0dbc3", "metadata": { "tags": [] }, - "outputs": [], "source": [ "messages = [\n", " (\n", @@ -143,7 +145,9 @@ "]\n", "ai_msg = llm.invoke(messages)\n", "ai_msg" - ] + ], + "outputs": [], + "execution_count": null }, { "cell_type": "markdown", @@ -155,13 +159,13 @@ }, { "cell_type": "code", - "execution_count": null, "id": "d86145b3-bfef-46e8-b227-4dda5c9c2705", "metadata": {}, - "outputs": [], "source": [ "print(ai_msg.content)" - ] + ], + "outputs": [], + "execution_count": null }, { "cell_type": "markdown", @@ -175,10 +179,8 @@ }, { "cell_type": "code", - "execution_count": null, "id": "4a0f2112b3a4c79e", "metadata": {}, - "outputs": [], "source": [ "messages = [\n", " (\n", @@ -189,7 +191,9 @@ "]\n", "ai_stream = llm.stream(messages)\n", "ai_stream" - ] + ], + "outputs": [], + "execution_count": null }, { "cell_type": "markdown", @@ -201,14 +205,14 @@ }, { "cell_type": "code", - "execution_count": null, "id": "8c4b7b9b9308c757", "metadata": {}, - "outputs": [], "source": [ "for chunk in ai_stream:\n", " print(chunk.content, end=\"\")" - ] + ], + "outputs": [], + "execution_count": null }, { "cell_type": "markdown", @@ -236,10 +240,8 @@ }, { "cell_type": "code", - "execution_count": null, "id": "47e2f0faceca533", "metadata": {}, - "outputs": [], "source": [ "from pydantic import BaseModel, Field\n", "\n", @@ -251,7 +253,9 @@ "\n", "\n", "llm.bind_tools([GetWeather])" - ] + ], + "outputs": [], + "execution_count": null }, { "cell_type": "markdown", @@ -263,16 +267,16 @@ }, { "cell_type": "code", - "execution_count": null, "id": "765527dd533ec967", "metadata": {}, - "outputs": [], "source": [ "ai_msg = llm.invoke(\n", " \"what is the weather like in New York City\",\n", ")\n", "ai_msg" - ] + ], + "outputs": [], + "execution_count": null }, { "cell_type": "markdown", @@ -284,13 +288,13 @@ }, { "cell_type": "code", - "execution_count": null, "id": "f361c4769e772fe", "metadata": {}, - "outputs": [], "source": [ "print(ai_msg.tool_calls)" - ] + ], + "outputs": [], + "execution_count": null }, { "cell_type": "markdown", @@ -316,10 +320,8 @@ }, { "cell_type": "code", - "execution_count": null, "id": "c8a217f6190747fe", "metadata": {}, - "outputs": [], "source": [ "ai_batch = llm.batch(\n", " [\n", @@ -330,7 +332,9 @@ " config={\"max_concurrency\": 3},\n", ")\n", "ai_batch" - ] + ], + "outputs": [], + "execution_count": null }, { "cell_type": "markdown", @@ -342,15 +346,15 @@ }, { "cell_type": "code", - "execution_count": null, "id": "b6a228d448f3df23", "metadata": {}, - "outputs": [], "source": [ "for batch in ai_batch:\n", " print(batch.content)\n", " print(\"-\" * 100)" - ] + ], + "outputs": [], + "execution_count": null }, { "cell_type": "markdown", @@ -374,10 +378,8 @@ }, { "cell_type": "code", - "execution_count": null, "id": "e197d1d7-a070-4c96-9f8a-a0e86d046e0b", "metadata": {}, - "outputs": [], "source": [ "from langchain_core.prompts import ChatPromptTemplate\n", "\n", @@ -399,7 +401,9 @@ " \"input\": \"I love programming.\",\n", " }\n", ")" - ] + ], + "outputs": [], + "execution_count": null }, { "cell_type": "markdown", diff --git a/docs/docs/integrations/document_loaders/parsers/writer_pdf_parser.ipynb b/docs/docs/integrations/document_loaders/parsers/writer_pdf_parser.ipynb index a6d5d9e82a4..4dfe1c3bfc6 100644 --- a/docs/docs/integrations/document_loaders/parsers/writer_pdf_parser.ipynb +++ b/docs/docs/integrations/document_loaders/parsers/writer_pdf_parser.ipynb @@ -14,9 +14,9 @@ "## Overview\n", "\n", "### Integration details\n", - "| Class | Package | Local | Serializable | JS support | Package downloads | Package latest |\n", - "|:------------------------------------------------------------------------------------------------------------------------------------------|:-----------------| :---: | :---: |:----------:|:------------------------------------------------------------------------------------------------:|:---------------------------------------------------------------------------------------------:|\n", - "| PDFParser | [langchain-writer](https://pypi.org/project/langchain-writer/) | ❌ | ❌ | ❌ | ![PyPI - Downloads](https://img.shields.io/pypi/dm/langchain-writer?style=flat-square&label=%20) | ![PyPI - Version](https://img.shields.io/pypi/v/langchain-writer?style=flat-square&label=%20) |" + "| Class | Package | Local | Serializable | JS support | Package downloads | Package latest |\n", + "|:-----------------------------------------------------------------------------------------------------------------------------------|:-----------------| :---: | :---: |:----------:|:------------------------------------------------------------------------------------------------:|:---------------------------------------------------------------------------------------------:|\n", + "| [PDFParser](https://github.com/writer/langchain-writer/blob/main/langchain_writer/pdf_parser.py#L55) | [langchain-writer](https://pypi.org/project/langchain-writer/) | ❌ | ❌ | ❌ | ![PyPI - Downloads](https://img.shields.io/pypi/dm/langchain-writer?style=flat-square&label=%20) | ![PyPI - Version](https://img.shields.io/pypi/v/langchain-writer?style=flat-square&label=%20) |" ] }, { @@ -31,17 +31,13 @@ }, { "cell_type": "code", - "execution_count": null, "id": "a8d653f15b7ee32d", - "metadata": { - "jupyter": { - "is_executing": true - } - }, - "outputs": [], + "metadata": {}, "source": [ "%pip install --quiet -U langchain-writer" - ] + ], + "outputs": [], + "execution_count": null }, { "cell_type": "markdown", @@ -55,17 +51,17 @@ }, { "cell_type": "code", - "execution_count": null, "id": "2983e19c9d555e58", "metadata": {}, - "outputs": [], "source": [ "import getpass\n", "import os\n", "\n", "if not os.getenv(\"WRITER_API_KEY\"):\n", " os.environ[\"WRITER_API_KEY\"] = getpass.getpass(\"Enter your Writer API key: \")" - ] + ], + "outputs": [], + "execution_count": null }, { "cell_type": "markdown", @@ -77,14 +73,14 @@ }, { "cell_type": "code", - "execution_count": null, "id": "98d8422ecee77403", "metadata": {}, - "outputs": [], "source": [ "# os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n", "# os.environ[\"LANGCHAIN_API_KEY\"] = getpass.getpass()" - ] + ], + "outputs": [], + "execution_count": null }, { "cell_type": "markdown", @@ -98,15 +94,15 @@ }, { "cell_type": "code", - "execution_count": null, "id": "787b3ba8af32533f", "metadata": {}, - "outputs": [], "source": [ "from langchain_writer.pdf_parser import PDFParser\n", "\n", "parser = PDFParser(format=\"markdown\")" - ] + ], + "outputs": [], + "execution_count": null }, { "cell_type": "markdown", @@ -124,18 +120,18 @@ }, { "cell_type": "code", - "execution_count": null, "id": "d1a24b81a8a96f09", "metadata": {}, - "outputs": [], "source": [ "from langchain_core.documents.base import Blob\n", "\n", - "file = Blob.from_path(\"../../data/page_to_parse.pdf\")\n", + "file = Blob.from_path(\"../example_data/layout-parser-paper.pdf\")\n", "\n", "parsed_pages = parser.parse(blob=file)\n", "parsed_pages" - ] + ], + "outputs": [], + "execution_count": null }, { "cell_type": "markdown", @@ -149,14 +145,14 @@ }, { "cell_type": "code", - "execution_count": null, "id": "e2f7fd52b7188c6c", "metadata": {}, - "outputs": [], "source": [ "parsed_pages_async = await parser.aparse(blob=file)\n", "parsed_pages_async" - ] + ], + "outputs": [], + "execution_count": null }, { "cell_type": "markdown", diff --git a/docs/docs/integrations/providers/writer.mdx b/docs/docs/integrations/providers/writer.mdx index ea1cd21c886..8da193c2e07 100644 --- a/docs/docs/integrations/providers/writer.mdx +++ b/docs/docs/integrations/providers/writer.mdx @@ -51,3 +51,5 @@ Support of basic function calls defined via dicts, Pydantic, python functions et ```python from langchain_writer.tools import GraphTool ``` + +Writer-specific remotely invoking tool \ No newline at end of file diff --git a/docs/docs/integrations/splitters/writer_text_splitter.ipynb b/docs/docs/integrations/splitters/writer_text_splitter.ipynb index f72a5c46dfa..050e6a94549 100644 --- a/docs/docs/integrations/splitters/writer_text_splitter.ipynb +++ b/docs/docs/integrations/splitters/writer_text_splitter.ipynb @@ -14,9 +14,9 @@ "## Overview\n", "\n", "### Integration details\n", - "| Class | Package | Local | Serializable | JS support | Package downloads | Package latest |\n", - "|:-----------------------------------------------------------------------------------------------------------------------------------------------------------|:-----------------| :---: | :---: |:----------:|:------------------------------------------------------------------------------------------------:|:---------------------------------------------------------------------------------------------:|\n", - "| WriterTextSplitter | [langchain-writer](https://pypi.org/project/langchain-writer/) | ❌ | ❌ | ❌ | ![PyPI - Downloads](https://img.shields.io/pypi/dm/langchain-writer?style=flat-square&label=%20) | ![PyPI - Version](https://img.shields.io/pypi/v/langchain-writer?style=flat-square&label=%20) |" + "| Class | Package | Local | Serializable | JS support | Package downloads | Package latest |\n", + "|:-----------------------------------------------------------------------------------------------------------------------------------------|:-----------------| :---: | :---: |:----------:|:------------------------------------------------------------------------------------------------:|:---------------------------------------------------------------------------------------------:|\n", + "| [WriterTextSplitter](https://github.com/writer/langchain-writer/blob/main/langchain_writer/text_splitter.py#L11) | [langchain-writer](https://pypi.org/project/langchain-writer/) | ❌ | ❌ | ❌ | ![PyPI - Downloads](https://img.shields.io/pypi/dm/langchain-writer?style=flat-square&label=%20) | ![PyPI - Version](https://img.shields.io/pypi/v/langchain-writer?style=flat-square&label=%20) |" ] }, { @@ -31,11 +31,11 @@ }, { "cell_type": "code", - "execution_count": null, "id": "a8d653f15b7ee32d", "metadata": {}, + "source": "%pip install --quiet -U langchain-writer", "outputs": [], - "source": "%pip install --quiet -U langchain-writer" + "execution_count": null }, { "cell_type": "markdown", @@ -49,17 +49,17 @@ }, { "cell_type": "code", - "execution_count": null, "id": "2983e19c9d555e58", "metadata": {}, - "outputs": [], "source": [ "import getpass\n", "import os\n", "\n", "if not os.getenv(\"WRITER_API_KEY\"):\n", " os.environ[\"WRITER_API_KEY\"] = getpass.getpass(\"Enter your Writer API key: \")" - ] + ], + "outputs": [], + "execution_count": null }, { "cell_type": "markdown", @@ -71,14 +71,14 @@ }, { "cell_type": "code", - "execution_count": null, "id": "98d8422ecee77403", "metadata": {}, - "outputs": [], "source": [ "# os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n", "# os.environ[\"LANGCHAIN_API_KEY\"] = getpass.getpass()" - ] + ], + "outputs": [], + "execution_count": null }, { "cell_type": "markdown", @@ -96,15 +96,15 @@ }, { "cell_type": "code", - "execution_count": null, "id": "787b3ba8af32533f", "metadata": {}, - "outputs": [], "source": [ "from langchain_writer.text_splitter import WriterTextSplitter\n", "\n", "splitter = WriterTextSplitter(strategy=\"fast_split\")" - ] + ], + "outputs": [], + "execution_count": null }, { "cell_type": "markdown", @@ -120,10 +120,8 @@ }, { "cell_type": "code", - "execution_count": null, "id": "d1a24b81a8a96f09", "metadata": {}, - "outputs": [], "source": [ "text = \"\"\"Reeeeeeeeeeeeeeeeeeeeeaally long text you want to divide into smaller chunks. For example you can add a poem multiple times:\n", "Two roads diverged in a yellow wood,\n", @@ -201,7 +199,9 @@ "\n", "chunks = splitter.split_text(text)\n", "chunks" - ] + ], + "outputs": [], + "execution_count": null }, { "cell_type": "markdown", @@ -213,13 +213,13 @@ }, { "cell_type": "code", - "execution_count": null, "id": "a470daa875d99006", "metadata": {}, - "outputs": [], "source": [ "print(len(chunks))" - ] + ], + "outputs": [], + "execution_count": null }, { "cell_type": "markdown", @@ -232,14 +232,14 @@ }, { "cell_type": "code", - "execution_count": null, "id": "e2f7fd52b7188c6c", "metadata": {}, - "outputs": [], "source": [ "async_chunks = await splitter.asplit_text(text)\n", "async_chunks" - ] + ], + "outputs": [], + "execution_count": null }, { "cell_type": "markdown", @@ -251,13 +251,13 @@ }, { "cell_type": "code", - "execution_count": null, "id": "a1439db14e687fa4", "metadata": {}, - "outputs": [], "source": [ "print(len(async_chunks))" - ] + ], + "outputs": [], + "execution_count": null }, { "cell_type": "markdown", diff --git a/docs/docs/integrations/tools/writer.ipynb b/docs/docs/integrations/tools/writer.ipynb index 5c96a5cc5ec..1a1e91fa769 100644 --- a/docs/docs/integrations/tools/writer.ipynb +++ b/docs/docs/integrations/tools/writer.ipynb @@ -14,9 +14,9 @@ "\n", "### Integration details\n", "\n", - "| Class | Package | Local | Serializable | JS support | Package downloads | Package latest |\n", - "|:-------------------------------------------------------------------------------------------------------------------------------------------|:-----------------| :---: | :---: |:----------:|:------------------------------------------------------------------------------------------------:|:---------------------------------------------------------------------------------------------:|\n", - "| GraphTool | [langchain-writer](https://pypi.org/project/langchain-writer/) | ❌ | ❌ | ❌ | ![PyPI - Downloads](https://img.shields.io/pypi/dm/langchain-writer?style=flat-square&label=%20) | ![PyPI - Version](https://img.shields.io/pypi/v/langchain-writer?style=flat-square&label=%20) |\n", + "| Class | Package | Local | Serializable | JS support | Package downloads | Package latest |\n", + "|:-----------------------------------------------------------------------------------------------------------|:-----------------| :---: | :---: |:----------:|:------------------------------------------------------------------------------------------------:|:---------------------------------------------------------------------------------------------:|\n", + "| [GraphTool](https://github.com/writer/langchain-writer/blob/main/langchain_writer/tools.py#L9) | [langchain-writer](https://pypi.org/project/langchain-writer/) | ❌ | ❌ | ❌ | ![PyPI - Downloads](https://img.shields.io/pypi/dm/langchain-writer?style=flat-square&label=%20) | ![PyPI - Version](https://img.shields.io/pypi/v/langchain-writer?style=flat-square&label=%20) |\n", "\n", "### Features\n", "\n", @@ -43,17 +43,17 @@ }, { "cell_type": "code", - "execution_count": null, "id": "80d4e1a791aaa8", "metadata": {}, - "outputs": [], "source": [ "import getpass\n", "import os\n", "\n", "if not os.getenv(\"WRITER_API_KEY\"):\n", " os.environ[\"WRITER_API_KEY\"] = getpass.getpass(\"Enter your Writer API key: \")" - ] + ], + "outputs": [], + "execution_count": null }, { "cell_type": "markdown", @@ -77,10 +77,8 @@ }, { "cell_type": "code", - "execution_count": null, "id": "6faaae25509f0f28", "metadata": {}, - "outputs": [], "source": [ "from langchain_writer.chat_models import ChatWriter\n", "from langchain_writer.tools import GraphTool\n", @@ -89,7 +87,9 @@ "\n", "graph_id = getpass.getpass(\"Enter Writer Knowledge Graph ID: \")\n", "graph_tool = GraphTool(graph_ids=[graph_id])" - ] + ], + "outputs": [], + "execution_count": null }, { "cell_type": "markdown", @@ -99,10 +99,8 @@ }, { "cell_type": "code", - "execution_count": null, "id": "e98d7deedb0e5c6f", "metadata": {}, - "outputs": [], "source": [ "from typing import Optional\n", "\n", @@ -154,7 +152,9 @@ " },\n", " },\n", "}" - ] + ], + "outputs": [], + "execution_count": null }, { "cell_type": "markdown", @@ -167,15 +167,15 @@ }, { "cell_type": "code", - "execution_count": null, "id": "a4833f2597a87777", "metadata": {}, - "outputs": [], "source": [ "chat.bind_tools(\n", " [graph_tool, get_supercopa_trophies_count, GetWeather, get_product_info]\n", ")" - ] + ], + "outputs": [], + "execution_count": null }, { "cell_type": "markdown", @@ -187,13 +187,13 @@ }, { "cell_type": "code", - "execution_count": null, "id": "ccb61b945a56672b", "metadata": {}, - "outputs": [], "source": [ "chat.tools" - ] + ], + "outputs": [], + "execution_count": null }, { "cell_type": "markdown", @@ -205,13 +205,13 @@ }, { "cell_type": "code", - "execution_count": null, "id": "381f0d4b9a8357a4", "metadata": {}, - "outputs": [], "source": [ "chat.tool_choice" - ] + ], + "outputs": [], + "execution_count": null }, { "cell_type": "markdown", @@ -225,10 +225,8 @@ }, { "cell_type": "code", - "execution_count": null, "id": "74df06b58b5dc2e9", "metadata": {}, - "outputs": [], "source": [ "from langchain_core.messages import HumanMessage\n", "\n", @@ -240,7 +238,9 @@ "\n", "response = chat.invoke(messages)\n", "messages.append(response)" - ] + ], + "outputs": [], + "execution_count": null }, { "cell_type": "markdown", @@ -252,13 +252,13 @@ }, { "cell_type": "code", - "execution_count": null, "id": "e271e0fc677446b2", "metadata": {}, - "outputs": [], "source": [ "print(response.tool_calls)" - ] + ], + "outputs": [], + "execution_count": null }, { "cell_type": "markdown", @@ -270,10 +270,8 @@ }, { "cell_type": "code", - "execution_count": null, "id": "156b58108aa9b367", "metadata": {}, - "outputs": [], "source": [ "for tool_call in response.tool_calls:\n", " selected_tool = {\n", @@ -284,7 +282,9 @@ "\n", "response = chat.invoke(messages)\n", "print(response.content)" - ] + ], + "outputs": [], + "execution_count": null }, { "cell_type": "markdown", @@ -296,13 +296,13 @@ }, { "cell_type": "code", - "execution_count": null, "id": "4b3c6f05096fc9e3", "metadata": {}, - "outputs": [], "source": [ "print(response.additional_kwargs[\"graph_data\"])" - ] + ], + "outputs": [], + "execution_count": null }, { "cell_type": "markdown", @@ -314,13 +314,13 @@ }, { "cell_type": "code", - "execution_count": null, "id": "eb6e0da74b10b8fc", "metadata": {}, - "outputs": [], "source": [ "print(response.content)" - ] + ], + "outputs": [], + "execution_count": null }, { "cell_type": "markdown", @@ -329,7 +329,7 @@ "source": [ "## Chaining\n", "\n", - "#TODO: fill chaining section" + "Due to specificity of Writer Graph tool (you don't need to call it manually, Writer server will call it by himself and return RAG based generation) it's impossible to invoke it separately, so GraphTool can't be used as part of chain" ] }, { diff --git a/libs/community/langchain_community/callbacks/bedrock_anthropic_callback.py b/libs/community/langchain_community/callbacks/bedrock_anthropic_callback.py index 4d14fbe99e8..d7608fe1efa 100644 --- a/libs/community/langchain_community/callbacks/bedrock_anthropic_callback.py +++ b/libs/community/langchain_community/callbacks/bedrock_anthropic_callback.py @@ -11,6 +11,7 @@ MODEL_COST_PER_1K_INPUT_TOKENS = { "anthropic.claude-3-sonnet-20240229-v1:0": 0.003, "anthropic.claude-3-5-sonnet-20240620-v1:0": 0.003, "anthropic.claude-3-5-sonnet-20241022-v2:0": 0.003, + "anthropic.claude-3-7-sonnet-20250219-v1:0": 0.003, "anthropic.claude-3-haiku-20240307-v1:0": 0.00025, "anthropic.claude-3-opus-20240229-v1:0": 0.015, "anthropic.claude-3-5-haiku-20241022-v1:0": 0.0008, @@ -23,6 +24,7 @@ MODEL_COST_PER_1K_OUTPUT_TOKENS = { "anthropic.claude-3-sonnet-20240229-v1:0": 0.015, "anthropic.claude-3-5-sonnet-20240620-v1:0": 0.015, "anthropic.claude-3-5-sonnet-20241022-v2:0": 0.015, + "anthropic.claude-3-7-sonnet-20250219-v1:0": 0.015, "anthropic.claude-3-haiku-20240307-v1:0": 0.00125, "anthropic.claude-3-opus-20240229-v1:0": 0.075, "anthropic.claude-3-5-haiku-20241022-v1:0": 0.004, diff --git a/libs/community/langchain_community/retrievers/tavily_search_api.py b/libs/community/langchain_community/retrievers/tavily_search_api.py index 61539d4ba3f..aa0a08e3bf5 100644 --- a/libs/community/langchain_community/retrievers/tavily_search_api.py +++ b/libs/community/langchain_community/retrievers/tavily_search_api.py @@ -123,7 +123,7 @@ class TavilySearchAPIRetriever(BaseRetriever): Document( page_content=result.get("content", "") if not self.include_raw_content - else result.get("raw_content", ""), + else (result.get("raw_content") or ""), metadata={ "title": result.get("title", ""), "source": result.get("url", ""), diff --git a/libs/community/langchain_community/tools/tavily_search/tool.py b/libs/community/langchain_community/tools/tavily_search/tool.py index 1527458f30a..765f99daecf 100644 --- a/libs/community/langchain_community/tools/tavily_search/tool.py +++ b/libs/community/langchain_community/tools/tavily_search/tool.py @@ -51,9 +51,12 @@ class TavilySearchResults(BaseTool): # type: ignore[override, override] tool.invoke({'query': 'who won the last french open'}) - .. code-block:: python + .. code-block:: json - '{\n "url": "https://www.nytimes.com...", "content": "Novak Djokovic won the last French Open by beating Casper Ruud ...' + { + "url": "https://www.nytimes.com...", + "content": "Novak Djokovic won the last French Open by beating Casper Ruud ..." + } Invoke with tool call: @@ -64,7 +67,7 @@ class TavilySearchResults(BaseTool): # type: ignore[override, override] .. code-block:: python ToolMessage( - content='{\n "url": "https://www.nytimes.com...", "content": "Novak Djokovic won the last French Open by beating Casper Ruud ...', + content='{ "url": "https://www.nytimes.com...", "content": "Novak Djokovic won the last French Open by beating Casper Ruud ..." }', artifact={ 'query': 'who won the last french open', 'follow_up_questions': None, diff --git a/libs/partners/anthropic/langchain_anthropic/chat_models.py b/libs/partners/anthropic/langchain_anthropic/chat_models.py index 5a21188ade5..c5ca716f662 100644 --- a/libs/partners/anthropic/langchain_anthropic/chat_models.py +++ b/libs/partners/anthropic/langchain_anthropic/chat_models.py @@ -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 `_ + 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 `_ + feature that lets Claude attach context to its answers based on source + documents supplied by the user. When + `document content blocks `_ + 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 diff --git a/libs/partners/openai/tests/unit_tests/chat_models/test_azure.py b/libs/partners/openai/tests/unit_tests/chat_models/test_azure.py index bee3f742a5e..f1d97ab8ebb 100644 --- a/libs/partners/openai/tests/unit_tests/chat_models/test_azure.py +++ b/libs/partners/openai/tests/unit_tests/chat_models/test_azure.py @@ -4,6 +4,7 @@ import os from unittest import mock import pytest +from langchain_core.messages import HumanMessage from typing_extensions import TypedDict from langchain_openai import AzureChatOpenAI @@ -81,3 +82,20 @@ def test_structured_output_old_model() -> None: # assert tool calling was used instead of json_schema assert "tools" in llm.steps[0].kwargs # type: ignore assert "response_format" not in llm.steps[0].kwargs # type: ignore + + +def test_max_completion_tokens_in_payload() -> None: + llm = AzureChatOpenAI( + azure_deployment="o1-mini", + api_version="2024-12-01-preview", + azure_endpoint="my-base-url", + model_kwargs={"max_completion_tokens": 300}, + ) + messages = [HumanMessage("Hello")] + payload = llm._get_request_payload(messages) + assert payload == { + "messages": [{"content": "Hello", "role": "user"}], + "model": None, + "stream": False, + "max_completion_tokens": 300, + }