Skip to content

Commit

Permalink
merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
bracesproul committed Jul 17, 2024
2 parents 7c1f0f7 + 3027cb6 commit f346a14
Show file tree
Hide file tree
Showing 42 changed files with 2,409 additions and 203 deletions.
12 changes: 12 additions & 0 deletions docs/core_docs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,26 @@ docs/how_to/trim_messages.md
docs/how_to/trim_messages.mdx
docs/how_to/tools_prompting.md
docs/how_to/tools_prompting.mdx
docs/how_to/tools_error.md
docs/how_to/tools_error.mdx
docs/how_to/tools_builtin.md
docs/how_to/tools_builtin.mdx
docs/how_to/tool_streaming.md
docs/how_to/tool_streaming.mdx
docs/how_to/tool_stream_events.md
docs/how_to/tool_stream_events.mdx
docs/how_to/tool_runtime.md
docs/how_to/tool_runtime.mdx
docs/how_to/tool_results_pass_to_model.md
docs/how_to/tool_results_pass_to_model.mdx
docs/how_to/tool_configure.md
docs/how_to/tool_configure.mdx
docs/how_to/tool_calls_multimodal.md
docs/how_to/tool_calls_multimodal.mdx
docs/how_to/tool_calling.md
docs/how_to/tool_calling.mdx
docs/how_to/tool_artifacts.md
docs/how_to/tool_artifacts.mdx
docs/how_to/structured_output.md
docs/how_to/structured_output.mdx
docs/how_to/streaming.md
Expand Down
7 changes: 7 additions & 0 deletions docs/core_docs/docs/how_to/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ These are the core building blocks you can use when building applications.
- [How to: get log probabilities](/docs/how_to/logprobs)
- [How to: stream a response back](/docs/how_to/chat_streaming)
- [How to: track token usage](/docs/how_to/chat_token_usage_tracking)
- [How to: stream tool calls](/docs/how_to/tool_streaming)
- [How to: few shot prompt tool behavior](/docs/how_to/tool_calling#few-shotting-with-tools)

### Messages

Expand Down Expand Up @@ -162,7 +164,12 @@ LangChain [Tools](/docs/concepts/#tools) contain a description of the tool (to p
- [How to: use built-in tools and built-in toolkits](/docs/how_to/tools_builtin)
- [How to: convert Runnables to tools](/docs/how_to/convert_runnable_to_tool)
- [How to: use a chat model to call tools](/docs/how_to/tool_calling/)
- [How to: pass tool results back to model](/docs/how_to/tool_results_pass_to_model/)
- [How to: add ad-hoc tool calling capability to LLMs and Chat Models](/docs/how_to/tools_prompting)
- [How to: pass run time values to tools](/docs/how_to/tool_runtime)
- [How to: handle errors when calling tools](/docs/how_to/tools_error)
- [How to: access the `RunnableConfig` object within a custom tool](/docs/how_to/tool_configure)
- [How to: stream events from child runs within a custom tool](/docs/how_to/tool_stream_events)
- [How to: return extra artifacts from a custom tool](/docs/how_to/tool_artifacts)

### Agents
Expand Down
2 changes: 1 addition & 1 deletion docs/core_docs/docs/how_to/tool_artifacts.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"This guide assumes familiarity with the following concepts:\n",
"\n",
"- [Tools](/docs/concepts/#tools)\n",
"- [Tool calling](/docs/concepts/#tool-calling)\n",
"- [Tool calling](/docs/concepts/#functiontool-calling)\n",
"\n",
":::\n",
"```\n",
Expand Down
114 changes: 114 additions & 0 deletions docs/core_docs/docs/how_to/tool_configure.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# How to access the RunnableConfig object within a custom tool\n",
"\n",
"```{=mdx}\n",
":::info Prerequisites\n",
"\n",
"This guide assumes familiarity with the following concepts:\n",
"\n",
"- [LangChain Tools](/docs/concepts/#tools)\n",
"- [Custom tools](/docs/how_to/custom_tools)\n",
"- [LangChain Expression Language (LCEL)](/docs/concepts/#langchain-expression-language)\n",
"\n",
":::\n",
"```\n",
"\n",
"Tools are runnables, and you can treat them the same way as any other runnable at the interface level - you can call `invoke()`, `batch()`, and `stream()` on them as normal. However, when writing custom tools, you may want to invoke other runnables like chat models or retrievers. In order to properly trace and configure those sub-invocations, you'll need to manually access and pass in the tool's current [`RunnableConfig`](https://api.js.langchain.com/interfaces/langchain_core_runnables.RunnableConfig.html) object.\n",
"\n",
"This guide covers how to do this for custom tools created in different ways.\n",
"\n",
"## From the `tool` method\n",
"\n",
"Accessing the `RunnableConfig` object for a custom tool created with the [`tool`](https://api.js.langchain.com/functions/langchain_core_tools.tool-1.html) helper method is simple - it's always the second parameter passed into your custom function. Here's an example:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import { z } from \"zod\";\n",
"import { tool } from \"@langchain/core/tools\";\n",
"import type { RunnableConfig } from \"@langchain/core/runnables\";\n",
"\n",
"const reverseTool = tool(\n",
" async (input: { text: string }, config?: RunnableConfig) => {\n",
" const originalString = input.text + (config?.configurable?.additional_field ?? \"\");\n",
" return originalString.split(\"\").reverse().join(\"\");\n",
" }, {\n",
" name: \"reverse\",\n",
" description: \"A test tool that combines input text with a configurable parameter.\",\n",
" schema: z.object({\n",
" text: z.string()\n",
" }),\n",
" }\n",
");"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Then, if we invoke the tool with a `config` containing a `configurable` field, we can see that `additional_field` is passed through correctly:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"321cba\n"
]
}
],
"source": [
"await reverseTool.invoke(\n",
" {text: \"abc\"}, {configurable: {additional_field: \"123\"}}\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Next steps\n",
"\n",
"You've now seen how to configure and stream events from within a tool. Next, check out the following guides for more on using tools:\n",
"\n",
"- Pass [tool results back to a model](/docs/how_to/tool_results_pass_to_model)\n",
"- Building [tool-using chains and agents](/docs/how_to#tools)\n",
"- Getting [structured outputs](/docs/how_to/structured_output/) from models"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "TypeScript",
"language": "typescript",
"name": "tslab"
},
"language_info": {
"codemirror_mode": {
"mode": "typescript",
"name": "javascript",
"typescript": true
},
"file_extension": ".ts",
"mimetype": "text/typescript",
"name": "typescript",
"version": "3.7.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading

0 comments on commit f346a14

Please sign in to comment.