mirror of
https://github.com/hwchase17/langchain.git
synced 2025-08-15 07:36:08 +00:00
Merge branch 'master' into pprados/06-pdfplumber
This commit is contained in:
commit
0cf0f70c45
@ -315,6 +315,59 @@
|
|||||||
"ai_msg.tool_calls"
|
"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",
|
"cell_type": "markdown",
|
||||||
"id": "301d372f-4dec-43e6-b58c-eee25633e1a6",
|
"id": "301d372f-4dec-43e6-b58c-eee25633e1a6",
|
||||||
|
@ -16,8 +16,8 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"### Integration details\n",
|
"### Integration details\n",
|
||||||
"| Class | Package | Local | Serializable | JS support | Package downloads | Package latest |\n",
|
"| Class | Package | Local | Serializable | JS support | Package downloads | Package latest |\n",
|
||||||
"|:--------------------------------------------------------------------------------------------------------------------------------------------|:-----------------| :---: | :---: |:----------:|:------------------------------------------------------------------------------------------------:|:---------------------------------------------------------------------------------------------:|\n",
|
"|:-------------------------------------------------------------------------------------------------------------------------|:-----------------| :---: | :---: |:----------:|:------------------------------------------------------------------------------------------------:|:---------------------------------------------------------------------------------------------:|\n",
|
||||||
"| ChatWriter | [langchain-writer](https://pypi.org/project/langchain-writer/) | ❌ | ❌ | ❌ |  |  |\n",
|
"| [ChatWriter](https://github.com/writer/langchain-writer/blob/main/langchain_writer/chat_models.py#L308) | [langchain-writer](https://pypi.org/project/langchain-writer/) | ❌ | ❌ | ❌ |  |  |\n",
|
||||||
"### Model features\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",
|
"| [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",
|
"| :---: |:-----------------:| :---: | :---: | :---: | :---: | :---: | :---: |:--------------------------------:|:--------:|\n",
|
||||||
@ -36,17 +36,21 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
|
||||||
"id": "433e8d2b-9519-4b49-b2c4-7ab65b046c94",
|
"id": "433e8d2b-9519-4b49-b2c4-7ab65b046c94",
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
"outputs": [],
|
"jupyter": {
|
||||||
|
"is_executing": true
|
||||||
|
}
|
||||||
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"import getpass\n",
|
"import getpass\n",
|
||||||
"import os\n",
|
"import os\n",
|
||||||
"\n",
|
"\n",
|
||||||
"if not os.getenv(\"WRITER_API_KEY\"):\n",
|
"if not os.getenv(\"WRITER_API_KEY\"):\n",
|
||||||
" os.environ[\"WRITER_API_KEY\"] = getpass.getpass(\"Enter your Writer API key: \")"
|
" os.environ[\"WRITER_API_KEY\"] = getpass.getpass(\"Enter your Writer API key: \")"
|
||||||
]
|
],
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": null
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
@ -58,14 +62,14 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
|
||||||
"id": "a15d341e-3e26-4ca3-830b-5aab30ed66de",
|
"id": "a15d341e-3e26-4ca3-830b-5aab30ed66de",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
"source": [
|
||||||
"# os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n",
|
"# os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n",
|
||||||
"# os.environ[\"LANGCHAIN_API_KEY\"] = getpass.getpass(\"Enter your LangSmith API key: \")"
|
"# os.environ[\"LANGCHAIN_API_KEY\"] = getpass.getpass(\"Enter your LangSmith API key: \")"
|
||||||
]
|
],
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": null
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
@ -79,13 +83,13 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
|
||||||
"id": "652d6238-1f87-422a-b135-f5abbb8652fc",
|
"id": "652d6238-1f87-422a-b135-f5abbb8652fc",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
"source": [
|
||||||
"%pip install -qU langchain-writer"
|
"%pip install -qU langchain-writer"
|
||||||
]
|
],
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": null
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
@ -99,10 +103,8 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
|
||||||
"id": "cb09c344-1836-4e0c-acf8-11d13ac1dbae",
|
"id": "cb09c344-1836-4e0c-acf8-11d13ac1dbae",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain_writer import ChatWriter\n",
|
"from langchain_writer import ChatWriter\n",
|
||||||
"\n",
|
"\n",
|
||||||
@ -113,7 +115,9 @@
|
|||||||
" timeout=None,\n",
|
" timeout=None,\n",
|
||||||
" max_retries=2,\n",
|
" max_retries=2,\n",
|
||||||
")"
|
")"
|
||||||
]
|
],
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": null
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
@ -127,12 +131,10 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
|
||||||
"id": "62e0dbc3",
|
"id": "62e0dbc3",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"tags": []
|
"tags": []
|
||||||
},
|
},
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
"source": [
|
||||||
"messages = [\n",
|
"messages = [\n",
|
||||||
" (\n",
|
" (\n",
|
||||||
@ -143,7 +145,9 @@
|
|||||||
"]\n",
|
"]\n",
|
||||||
"ai_msg = llm.invoke(messages)\n",
|
"ai_msg = llm.invoke(messages)\n",
|
||||||
"ai_msg"
|
"ai_msg"
|
||||||
]
|
],
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": null
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
@ -155,13 +159,13 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
|
||||||
"id": "d86145b3-bfef-46e8-b227-4dda5c9c2705",
|
"id": "d86145b3-bfef-46e8-b227-4dda5c9c2705",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
"source": [
|
||||||
"print(ai_msg.content)"
|
"print(ai_msg.content)"
|
||||||
]
|
],
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": null
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
@ -175,10 +179,8 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
|
||||||
"id": "4a0f2112b3a4c79e",
|
"id": "4a0f2112b3a4c79e",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
"source": [
|
||||||
"messages = [\n",
|
"messages = [\n",
|
||||||
" (\n",
|
" (\n",
|
||||||
@ -189,7 +191,9 @@
|
|||||||
"]\n",
|
"]\n",
|
||||||
"ai_stream = llm.stream(messages)\n",
|
"ai_stream = llm.stream(messages)\n",
|
||||||
"ai_stream"
|
"ai_stream"
|
||||||
]
|
],
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": null
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
@ -201,14 +205,14 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
|
||||||
"id": "8c4b7b9b9308c757",
|
"id": "8c4b7b9b9308c757",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
"source": [
|
||||||
"for chunk in ai_stream:\n",
|
"for chunk in ai_stream:\n",
|
||||||
" print(chunk.content, end=\"\")"
|
" print(chunk.content, end=\"\")"
|
||||||
]
|
],
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": null
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
@ -236,10 +240,8 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
|
||||||
"id": "47e2f0faceca533",
|
"id": "47e2f0faceca533",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
"source": [
|
||||||
"from pydantic import BaseModel, Field\n",
|
"from pydantic import BaseModel, Field\n",
|
||||||
"\n",
|
"\n",
|
||||||
@ -251,7 +253,9 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"llm.bind_tools([GetWeather])"
|
"llm.bind_tools([GetWeather])"
|
||||||
]
|
],
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": null
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
@ -263,16 +267,16 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
|
||||||
"id": "765527dd533ec967",
|
"id": "765527dd533ec967",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
"source": [
|
||||||
"ai_msg = llm.invoke(\n",
|
"ai_msg = llm.invoke(\n",
|
||||||
" \"what is the weather like in New York City\",\n",
|
" \"what is the weather like in New York City\",\n",
|
||||||
")\n",
|
")\n",
|
||||||
"ai_msg"
|
"ai_msg"
|
||||||
]
|
],
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": null
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
@ -284,13 +288,13 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
|
||||||
"id": "f361c4769e772fe",
|
"id": "f361c4769e772fe",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
"source": [
|
||||||
"print(ai_msg.tool_calls)"
|
"print(ai_msg.tool_calls)"
|
||||||
]
|
],
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": null
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
@ -316,10 +320,8 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
|
||||||
"id": "c8a217f6190747fe",
|
"id": "c8a217f6190747fe",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
"source": [
|
||||||
"ai_batch = llm.batch(\n",
|
"ai_batch = llm.batch(\n",
|
||||||
" [\n",
|
" [\n",
|
||||||
@ -330,7 +332,9 @@
|
|||||||
" config={\"max_concurrency\": 3},\n",
|
" config={\"max_concurrency\": 3},\n",
|
||||||
")\n",
|
")\n",
|
||||||
"ai_batch"
|
"ai_batch"
|
||||||
]
|
],
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": null
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
@ -342,15 +346,15 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
|
||||||
"id": "b6a228d448f3df23",
|
"id": "b6a228d448f3df23",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
"source": [
|
||||||
"for batch in ai_batch:\n",
|
"for batch in ai_batch:\n",
|
||||||
" print(batch.content)\n",
|
" print(batch.content)\n",
|
||||||
" print(\"-\" * 100)"
|
" print(\"-\" * 100)"
|
||||||
]
|
],
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": null
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
@ -374,10 +378,8 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
|
||||||
"id": "e197d1d7-a070-4c96-9f8a-a0e86d046e0b",
|
"id": "e197d1d7-a070-4c96-9f8a-a0e86d046e0b",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain_core.prompts import ChatPromptTemplate\n",
|
"from langchain_core.prompts import ChatPromptTemplate\n",
|
||||||
"\n",
|
"\n",
|
||||||
@ -399,7 +401,9 @@
|
|||||||
" \"input\": \"I love programming.\",\n",
|
" \"input\": \"I love programming.\",\n",
|
||||||
" }\n",
|
" }\n",
|
||||||
")"
|
")"
|
||||||
]
|
],
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": null
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
|
@ -15,8 +15,8 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"### Integration details\n",
|
"### Integration details\n",
|
||||||
"| Class | Package | Local | Serializable | JS support | Package downloads | Package latest |\n",
|
"| Class | Package | Local | Serializable | JS support | Package downloads | Package latest |\n",
|
||||||
"|:------------------------------------------------------------------------------------------------------------------------------------------|:-----------------| :---: | :---: |:----------:|:------------------------------------------------------------------------------------------------:|:---------------------------------------------------------------------------------------------:|\n",
|
"|:-----------------------------------------------------------------------------------------------------------------------------------|:-----------------| :---: | :---: |:----------:|:------------------------------------------------------------------------------------------------:|:---------------------------------------------------------------------------------------------:|\n",
|
||||||
"| PDFParser | [langchain-writer](https://pypi.org/project/langchain-writer/) | ❌ | ❌ | ❌ |  |  |"
|
"| [PDFParser](https://github.com/writer/langchain-writer/blob/main/langchain_writer/pdf_parser.py#L55) | [langchain-writer](https://pypi.org/project/langchain-writer/) | ❌ | ❌ | ❌ |  |  |"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -31,17 +31,13 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
|
||||||
"id": "a8d653f15b7ee32d",
|
"id": "a8d653f15b7ee32d",
|
||||||
"metadata": {
|
"metadata": {},
|
||||||
"jupyter": {
|
|
||||||
"is_executing": true
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
"source": [
|
||||||
"%pip install --quiet -U langchain-writer"
|
"%pip install --quiet -U langchain-writer"
|
||||||
]
|
],
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": null
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
@ -55,17 +51,17 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
|
||||||
"id": "2983e19c9d555e58",
|
"id": "2983e19c9d555e58",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
"source": [
|
||||||
"import getpass\n",
|
"import getpass\n",
|
||||||
"import os\n",
|
"import os\n",
|
||||||
"\n",
|
"\n",
|
||||||
"if not os.getenv(\"WRITER_API_KEY\"):\n",
|
"if not os.getenv(\"WRITER_API_KEY\"):\n",
|
||||||
" os.environ[\"WRITER_API_KEY\"] = getpass.getpass(\"Enter your Writer API key: \")"
|
" os.environ[\"WRITER_API_KEY\"] = getpass.getpass(\"Enter your Writer API key: \")"
|
||||||
]
|
],
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": null
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
@ -77,14 +73,14 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
|
||||||
"id": "98d8422ecee77403",
|
"id": "98d8422ecee77403",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
"source": [
|
||||||
"# os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n",
|
"# os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n",
|
||||||
"# os.environ[\"LANGCHAIN_API_KEY\"] = getpass.getpass()"
|
"# os.environ[\"LANGCHAIN_API_KEY\"] = getpass.getpass()"
|
||||||
]
|
],
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": null
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
@ -98,15 +94,15 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
|
||||||
"id": "787b3ba8af32533f",
|
"id": "787b3ba8af32533f",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain_writer.pdf_parser import PDFParser\n",
|
"from langchain_writer.pdf_parser import PDFParser\n",
|
||||||
"\n",
|
"\n",
|
||||||
"parser = PDFParser(format=\"markdown\")"
|
"parser = PDFParser(format=\"markdown\")"
|
||||||
]
|
],
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": null
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
@ -124,18 +120,18 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
|
||||||
"id": "d1a24b81a8a96f09",
|
"id": "d1a24b81a8a96f09",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain_core.documents.base import Blob\n",
|
"from langchain_core.documents.base import Blob\n",
|
||||||
"\n",
|
"\n",
|
||||||
"file = Blob.from_path(\"../../data/page_to_parse.pdf\")\n",
|
"file = Blob.from_path(\"../example_data/layout-parser-paper.pdf\")\n",
|
||||||
"\n",
|
"\n",
|
||||||
"parsed_pages = parser.parse(blob=file)\n",
|
"parsed_pages = parser.parse(blob=file)\n",
|
||||||
"parsed_pages"
|
"parsed_pages"
|
||||||
]
|
],
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": null
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
@ -149,14 +145,14 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
|
||||||
"id": "e2f7fd52b7188c6c",
|
"id": "e2f7fd52b7188c6c",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
"source": [
|
||||||
"parsed_pages_async = await parser.aparse(blob=file)\n",
|
"parsed_pages_async = await parser.aparse(blob=file)\n",
|
||||||
"parsed_pages_async"
|
"parsed_pages_async"
|
||||||
]
|
],
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": null
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
|
@ -51,3 +51,5 @@ Support of basic function calls defined via dicts, Pydantic, python functions et
|
|||||||
```python
|
```python
|
||||||
from langchain_writer.tools import GraphTool
|
from langchain_writer.tools import GraphTool
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Writer-specific remotely invoking tool
|
@ -15,8 +15,8 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"### Integration details\n",
|
"### Integration details\n",
|
||||||
"| Class | Package | Local | Serializable | JS support | Package downloads | Package latest |\n",
|
"| Class | Package | Local | Serializable | JS support | Package downloads | Package latest |\n",
|
||||||
"|:-----------------------------------------------------------------------------------------------------------------------------------------------------------|:-----------------| :---: | :---: |:----------:|:------------------------------------------------------------------------------------------------:|:---------------------------------------------------------------------------------------------:|\n",
|
"|:-----------------------------------------------------------------------------------------------------------------------------------------|:-----------------| :---: | :---: |:----------:|:------------------------------------------------------------------------------------------------:|:---------------------------------------------------------------------------------------------:|\n",
|
||||||
"| WriterTextSplitter | [langchain-writer](https://pypi.org/project/langchain-writer/) | ❌ | ❌ | ❌ |  |  |"
|
"| [WriterTextSplitter](https://github.com/writer/langchain-writer/blob/main/langchain_writer/text_splitter.py#L11) | [langchain-writer](https://pypi.org/project/langchain-writer/) | ❌ | ❌ | ❌ |  |  |"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -31,11 +31,11 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
|
||||||
"id": "a8d653f15b7ee32d",
|
"id": "a8d653f15b7ee32d",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
|
"source": "%pip install --quiet -U langchain-writer",
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": "%pip install --quiet -U langchain-writer"
|
"execution_count": null
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
@ -49,17 +49,17 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
|
||||||
"id": "2983e19c9d555e58",
|
"id": "2983e19c9d555e58",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
"source": [
|
||||||
"import getpass\n",
|
"import getpass\n",
|
||||||
"import os\n",
|
"import os\n",
|
||||||
"\n",
|
"\n",
|
||||||
"if not os.getenv(\"WRITER_API_KEY\"):\n",
|
"if not os.getenv(\"WRITER_API_KEY\"):\n",
|
||||||
" os.environ[\"WRITER_API_KEY\"] = getpass.getpass(\"Enter your Writer API key: \")"
|
" os.environ[\"WRITER_API_KEY\"] = getpass.getpass(\"Enter your Writer API key: \")"
|
||||||
]
|
],
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": null
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
@ -71,14 +71,14 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
|
||||||
"id": "98d8422ecee77403",
|
"id": "98d8422ecee77403",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
"source": [
|
||||||
"# os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n",
|
"# os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n",
|
||||||
"# os.environ[\"LANGCHAIN_API_KEY\"] = getpass.getpass()"
|
"# os.environ[\"LANGCHAIN_API_KEY\"] = getpass.getpass()"
|
||||||
]
|
],
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": null
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
@ -96,15 +96,15 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
|
||||||
"id": "787b3ba8af32533f",
|
"id": "787b3ba8af32533f",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain_writer.text_splitter import WriterTextSplitter\n",
|
"from langchain_writer.text_splitter import WriterTextSplitter\n",
|
||||||
"\n",
|
"\n",
|
||||||
"splitter = WriterTextSplitter(strategy=\"fast_split\")"
|
"splitter = WriterTextSplitter(strategy=\"fast_split\")"
|
||||||
]
|
],
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": null
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
@ -120,10 +120,8 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
|
||||||
"id": "d1a24b81a8a96f09",
|
"id": "d1a24b81a8a96f09",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
"source": [
|
||||||
"text = \"\"\"Reeeeeeeeeeeeeeeeeeeeeaally long text you want to divide into smaller chunks. For example you can add a poem multiple times:\n",
|
"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",
|
"Two roads diverged in a yellow wood,\n",
|
||||||
@ -201,7 +199,9 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"chunks = splitter.split_text(text)\n",
|
"chunks = splitter.split_text(text)\n",
|
||||||
"chunks"
|
"chunks"
|
||||||
]
|
],
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": null
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
@ -213,13 +213,13 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
|
||||||
"id": "a470daa875d99006",
|
"id": "a470daa875d99006",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
"source": [
|
||||||
"print(len(chunks))"
|
"print(len(chunks))"
|
||||||
]
|
],
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": null
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
@ -232,14 +232,14 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
|
||||||
"id": "e2f7fd52b7188c6c",
|
"id": "e2f7fd52b7188c6c",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
"source": [
|
||||||
"async_chunks = await splitter.asplit_text(text)\n",
|
"async_chunks = await splitter.asplit_text(text)\n",
|
||||||
"async_chunks"
|
"async_chunks"
|
||||||
]
|
],
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": null
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
@ -251,13 +251,13 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
|
||||||
"id": "a1439db14e687fa4",
|
"id": "a1439db14e687fa4",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
"source": [
|
||||||
"print(len(async_chunks))"
|
"print(len(async_chunks))"
|
||||||
]
|
],
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": null
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
|
@ -15,8 +15,8 @@
|
|||||||
"### Integration details\n",
|
"### Integration details\n",
|
||||||
"\n",
|
"\n",
|
||||||
"| Class | Package | Local | Serializable | JS support | Package downloads | Package latest |\n",
|
"| Class | Package | Local | Serializable | JS support | Package downloads | Package latest |\n",
|
||||||
"|:-------------------------------------------------------------------------------------------------------------------------------------------|:-----------------| :---: | :---: |:----------:|:------------------------------------------------------------------------------------------------:|:---------------------------------------------------------------------------------------------:|\n",
|
"|:-----------------------------------------------------------------------------------------------------------|:-----------------| :---: | :---: |:----------:|:------------------------------------------------------------------------------------------------:|:---------------------------------------------------------------------------------------------:|\n",
|
||||||
"| GraphTool | [langchain-writer](https://pypi.org/project/langchain-writer/) | ❌ | ❌ | ❌ |  |  |\n",
|
"| [GraphTool](https://github.com/writer/langchain-writer/blob/main/langchain_writer/tools.py#L9) | [langchain-writer](https://pypi.org/project/langchain-writer/) | ❌ | ❌ | ❌ |  |  |\n",
|
||||||
"\n",
|
"\n",
|
||||||
"### Features\n",
|
"### Features\n",
|
||||||
"\n",
|
"\n",
|
||||||
@ -43,17 +43,17 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
|
||||||
"id": "80d4e1a791aaa8",
|
"id": "80d4e1a791aaa8",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
"source": [
|
||||||
"import getpass\n",
|
"import getpass\n",
|
||||||
"import os\n",
|
"import os\n",
|
||||||
"\n",
|
"\n",
|
||||||
"if not os.getenv(\"WRITER_API_KEY\"):\n",
|
"if not os.getenv(\"WRITER_API_KEY\"):\n",
|
||||||
" os.environ[\"WRITER_API_KEY\"] = getpass.getpass(\"Enter your Writer API key: \")"
|
" os.environ[\"WRITER_API_KEY\"] = getpass.getpass(\"Enter your Writer API key: \")"
|
||||||
]
|
],
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": null
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
@ -77,10 +77,8 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
|
||||||
"id": "6faaae25509f0f28",
|
"id": "6faaae25509f0f28",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain_writer.chat_models import ChatWriter\n",
|
"from langchain_writer.chat_models import ChatWriter\n",
|
||||||
"from langchain_writer.tools import GraphTool\n",
|
"from langchain_writer.tools import GraphTool\n",
|
||||||
@ -89,7 +87,9 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"graph_id = getpass.getpass(\"Enter Writer Knowledge Graph ID: \")\n",
|
"graph_id = getpass.getpass(\"Enter Writer Knowledge Graph ID: \")\n",
|
||||||
"graph_tool = GraphTool(graph_ids=[graph_id])"
|
"graph_tool = GraphTool(graph_ids=[graph_id])"
|
||||||
]
|
],
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": null
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
@ -99,10 +99,8 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
|
||||||
"id": "e98d7deedb0e5c6f",
|
"id": "e98d7deedb0e5c6f",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
"source": [
|
||||||
"from typing import Optional\n",
|
"from typing import Optional\n",
|
||||||
"\n",
|
"\n",
|
||||||
@ -154,7 +152,9 @@
|
|||||||
" },\n",
|
" },\n",
|
||||||
" },\n",
|
" },\n",
|
||||||
"}"
|
"}"
|
||||||
]
|
],
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": null
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
@ -167,15 +167,15 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
|
||||||
"id": "a4833f2597a87777",
|
"id": "a4833f2597a87777",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
"source": [
|
||||||
"chat.bind_tools(\n",
|
"chat.bind_tools(\n",
|
||||||
" [graph_tool, get_supercopa_trophies_count, GetWeather, get_product_info]\n",
|
" [graph_tool, get_supercopa_trophies_count, GetWeather, get_product_info]\n",
|
||||||
")"
|
")"
|
||||||
]
|
],
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": null
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
@ -187,13 +187,13 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
|
||||||
"id": "ccb61b945a56672b",
|
"id": "ccb61b945a56672b",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
"source": [
|
||||||
"chat.tools"
|
"chat.tools"
|
||||||
]
|
],
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": null
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
@ -205,13 +205,13 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
|
||||||
"id": "381f0d4b9a8357a4",
|
"id": "381f0d4b9a8357a4",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
"source": [
|
||||||
"chat.tool_choice"
|
"chat.tool_choice"
|
||||||
]
|
],
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": null
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
@ -225,10 +225,8 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
|
||||||
"id": "74df06b58b5dc2e9",
|
"id": "74df06b58b5dc2e9",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain_core.messages import HumanMessage\n",
|
"from langchain_core.messages import HumanMessage\n",
|
||||||
"\n",
|
"\n",
|
||||||
@ -240,7 +238,9 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"response = chat.invoke(messages)\n",
|
"response = chat.invoke(messages)\n",
|
||||||
"messages.append(response)"
|
"messages.append(response)"
|
||||||
]
|
],
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": null
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
@ -252,13 +252,13 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
|
||||||
"id": "e271e0fc677446b2",
|
"id": "e271e0fc677446b2",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
"source": [
|
||||||
"print(response.tool_calls)"
|
"print(response.tool_calls)"
|
||||||
]
|
],
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": null
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
@ -270,10 +270,8 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
|
||||||
"id": "156b58108aa9b367",
|
"id": "156b58108aa9b367",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
"source": [
|
||||||
"for tool_call in response.tool_calls:\n",
|
"for tool_call in response.tool_calls:\n",
|
||||||
" selected_tool = {\n",
|
" selected_tool = {\n",
|
||||||
@ -284,7 +282,9 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"response = chat.invoke(messages)\n",
|
"response = chat.invoke(messages)\n",
|
||||||
"print(response.content)"
|
"print(response.content)"
|
||||||
]
|
],
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": null
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
@ -296,13 +296,13 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
|
||||||
"id": "4b3c6f05096fc9e3",
|
"id": "4b3c6f05096fc9e3",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
"source": [
|
||||||
"print(response.additional_kwargs[\"graph_data\"])"
|
"print(response.additional_kwargs[\"graph_data\"])"
|
||||||
]
|
],
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": null
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
@ -314,13 +314,13 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
|
||||||
"id": "eb6e0da74b10b8fc",
|
"id": "eb6e0da74b10b8fc",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
"source": [
|
||||||
"print(response.content)"
|
"print(response.content)"
|
||||||
]
|
],
|
||||||
|
"outputs": [],
|
||||||
|
"execution_count": null
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
@ -329,7 +329,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"## Chaining\n",
|
"## Chaining\n",
|
||||||
"\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"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -11,6 +11,7 @@ MODEL_COST_PER_1K_INPUT_TOKENS = {
|
|||||||
"anthropic.claude-3-sonnet-20240229-v1:0": 0.003,
|
"anthropic.claude-3-sonnet-20240229-v1:0": 0.003,
|
||||||
"anthropic.claude-3-5-sonnet-20240620-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-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-haiku-20240307-v1:0": 0.00025,
|
||||||
"anthropic.claude-3-opus-20240229-v1:0": 0.015,
|
"anthropic.claude-3-opus-20240229-v1:0": 0.015,
|
||||||
"anthropic.claude-3-5-haiku-20241022-v1:0": 0.0008,
|
"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-sonnet-20240229-v1:0": 0.015,
|
||||||
"anthropic.claude-3-5-sonnet-20240620-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-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-haiku-20240307-v1:0": 0.00125,
|
||||||
"anthropic.claude-3-opus-20240229-v1:0": 0.075,
|
"anthropic.claude-3-opus-20240229-v1:0": 0.075,
|
||||||
"anthropic.claude-3-5-haiku-20241022-v1:0": 0.004,
|
"anthropic.claude-3-5-haiku-20241022-v1:0": 0.004,
|
||||||
|
@ -123,7 +123,7 @@ class TavilySearchAPIRetriever(BaseRetriever):
|
|||||||
Document(
|
Document(
|
||||||
page_content=result.get("content", "")
|
page_content=result.get("content", "")
|
||||||
if not self.include_raw_content
|
if not self.include_raw_content
|
||||||
else result.get("raw_content", ""),
|
else (result.get("raw_content") or ""),
|
||||||
metadata={
|
metadata={
|
||||||
"title": result.get("title", ""),
|
"title": result.get("title", ""),
|
||||||
"source": result.get("url", ""),
|
"source": result.get("url", ""),
|
||||||
|
@ -51,9 +51,12 @@ class TavilySearchResults(BaseTool): # type: ignore[override, override]
|
|||||||
|
|
||||||
tool.invoke({'query': 'who won the last french open'})
|
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:
|
Invoke with tool call:
|
||||||
|
|
||||||
@ -64,7 +67,7 @@ class TavilySearchResults(BaseTool): # type: ignore[override, override]
|
|||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
ToolMessage(
|
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={
|
artifact={
|
||||||
'query': 'who won the last french open',
|
'query': 'who won the last french open',
|
||||||
'follow_up_questions': None,
|
'follow_up_questions': None,
|
||||||
|
@ -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."
|
"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:
|
Token usage:
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ import os
|
|||||||
from unittest import mock
|
from unittest import mock
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
from langchain_core.messages import HumanMessage
|
||||||
from typing_extensions import TypedDict
|
from typing_extensions import TypedDict
|
||||||
|
|
||||||
from langchain_openai import AzureChatOpenAI
|
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 tool calling was used instead of json_schema
|
||||||
assert "tools" in llm.steps[0].kwargs # type: ignore
|
assert "tools" in llm.steps[0].kwargs # type: ignore
|
||||||
assert "response_format" not 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,
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user