Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: langchain-ai/langchainjs
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 0.1.3
Choose a base ref
...
head repository: langchain-ai/langchainjs
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 0.1.4
Choose a head ref

Commits on Jan 15, 2024

  1. Release 0.1.3

    jacoblee93 committed Jan 15, 2024
    Copy the full SHA
    ad65b94 View commit details
  2. Merge pull request #4031 from langchain-ai/release

    langchain[patch]: Release 0.1.3
    jacoblee93 authored Jan 15, 2024
    2
    Copy the full SHA
    0519809 View commit details
  3. core[minor]: Add ability for runnable passthrough to call a func (#3998)

    * core[minor]: Add ability for runnable passthrough to call a func
    
    * chore: lint files
    
    * add tests & update typing
    
    * Fix implementation of passthrough func
    
    ---------
    
    Co-authored-by: Nuno Campos <nuno@langchain.dev>
    bracesproul and nfcampos authored Jan 15, 2024
    2
    Copy the full SHA
    c189308 View commit details

Commits on Jan 16, 2024

  1. core[patch]: Allow runnable func to have async (#4033)

    * core[patch]: Allow runnable func to have async
    
    * chore: lint files
    
    * cr
    
    * undo cr
    
    * pass config through
    
    * tests
    
    * chore: lint files
    bracesproul authored Jan 16, 2024
    2
    Copy the full SHA
    6e970fb View commit details
  2. Fix return type of .pipe() (#4032)

    Co-authored-by: Brace Sproul <braceasproul@gmail.com>
    nfcampos and bracesproul authored Jan 16, 2024
    2
    Copy the full SHA
    9a06f3f View commit details
  3. Release 0.1.14

    jacoblee93 committed Jan 16, 2024
    Copy the full SHA
    c0bd150 View commit details
  4. Merge pull request #4040 from langchain-ai/release

    core[patch]: Release 0.1.14
    jacoblee93 authored Jan 16, 2024
    2
    Copy the full SHA
    9adb273 View commit details
  5. Populate streamed_output for all runs handled by transformStreamWithC…

    …onfig (#4042)
    
    * Populate streamed_output for all runs handled by transformStreamWithConfig
    
    * Move test
    
    * Fix lint
    jacoblee93 authored Jan 16, 2024
    2

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    c23297d View commit details
  6. all[patch]: Fix yarn lint:fix everywhere, add to turbo (#4045)

    * all[patch]: Fix lint:fix everywhere, add to turbo
    
    * cr
    
    * cr
    bracesproul authored Jan 16, 2024
    2
    Copy the full SHA
    29dba0e View commit details

Commits on Jan 17, 2024

  1. core[minor]: Fix configs not getting passes into all batch invocations (

    #4047)
    
    * core[minor]: Fix configs not getting passes into all batch invocations
    
    * lint
    
    * cr
    
    * test
    
    * drop bad test
    
    * add test
    
    * format
    bracesproul authored Jan 17, 2024
    2
    Copy the full SHA
    7921408 View commit details
  2. core[patch]: Suppress run map warnings (#4051)

    * Suppress warnings
    
    * Add comment
    jacoblee93 authored Jan 17, 2024
    2
    Copy the full SHA
    a324237 View commit details
  3. core[patch]: Refactor patchConfig method and add ensureConfig (#4048)

    * Refactor patchConfig, add ensureConfig
    
    * Fix lint
    
    * Merge configurable fields in patch
    jacoblee93 authored Jan 17, 2024
    2
    Copy the full SHA
    abdc880 View commit details
  4. core[patch]: Use new batch maxConcurrency parameter in runnable config (

    #4052)
    
    * Use new batch maxConcurrency parameter in runnable config
    
    * Make language model call options extend runnable config
    
    * Update batch examples
    
    * Update example
    jacoblee93 authored Jan 17, 2024
    2
    Copy the full SHA
    cb36523 View commit details
  5. Release 0.1.15

    jacoblee93 committed Jan 17, 2024
    Copy the full SHA
    d55cb60 View commit details
  6. Merge pull request #4053 from langchain-ai/release

    core[patch]: Release 0.1.15
    jacoblee93 authored Jan 17, 2024
    2
    Copy the full SHA
    d77f9c8 View commit details
  7. community[patch]: Add streaming for HuggingFace Inference (also add t…

    …he support of stop-sequences) (#3932)
    
    * Add streaming for HuggingFace Inference (also add the support of stop-sequences)
    
    * Respect bound stop sequences too
    
    ---------
    
    Co-authored-by: Brace Sproul <braceasproul@gmail.com>
    Co-authored-by: jacoblee93 <jacoblee93@gmail.com>
    3 people authored Jan 17, 2024
    2
    Copy the full SHA
    0bdb2aa View commit details
  8. Add the Milvus partition feature (#3950)

    * Add Milvus partition feature.
    
    * Add Milvus partition feature.
    
    * Formatting style.
    
    * Don't use partitions if naem not provided
    
    ---------
    
    Co-authored-by: Adrien <adrien@debian.veryup>
    Co-authored-by: jacoblee93 <jacoblee93@gmail.com>
    3 people authored Jan 17, 2024
    2
    Copy the full SHA
    15cf1e0 View commit details
  9. bump zod & zod-to-json-schema (#4041)

    Co-authored-by: Stefan Terdell <stefan.terdell+git@proton.me>
    StefanTerdell and Stefan Terdell authored Jan 17, 2024
    1
    Copy the full SHA
    ff8dbc6 View commit details
  10. fix #4027 loadFromPython failed (#4036)

    * fix #4027
    
    * update fixture
    
    * update int test
    ewfian authored Jan 17, 2024
    Copy the full SHA
    2f9b308 View commit details
  11. community[patch]: Add neo4j timeout option (#4057)

    * Add neo4j timeout option
    
    * Narrow type
    
    * fix
    
    * switch to timeout in ms
    
    ---------
    
    Co-authored-by: Oskar Hane <oh@oskarhane.com>
    tomasonjo and oskarhane authored Jan 17, 2024
    1
    Copy the full SHA
    bd2b229 View commit details
  12. 2
    Copy the full SHA
    0987db6 View commit details

Commits on Jan 18, 2024

  1. Update runnable config ensure and merge to match py (#4067)

    * Update runnable config ensure and merge to match py
    
    - merge now supports merging many configs
    - fix issues in edge cases of merge
    - call ensure everywhere configs are used
    - ensure assigns primitive configurable keys to tracer metadata
    
    * Try to fix test
    
    * Update test
    
    * Relax test
    
    ---------
    
    Co-authored-by: jacoblee93 <jacoblee93@gmail.com>
    nfcampos and jacoblee93 authored Jan 18, 2024
    2
    Copy the full SHA
    788e45f View commit details
  2. langchain[minor]: remote runnable stream log, additional serializatio…

    …n updates (#3555)
    
    * langchain[major]: Add streamLog for RemoteRunnable
    
    * cr
    
    * cr
    
    * cr
    
    * cr
    
    * cr
    
    * cr
    
    * cr
    
    * cr
    
    * cr
    
    * cr
    
    * cr
    
    * chore: lint files
    
    * cr
    
    * docs[major]: Adds docs for stream log method
    
    * cr
    
    * chore: lint files
    
    * Remove redundant docs
    
    * fix errors
    
    * cr
    
    * cr
    
    * docs
    
    * lint
    
    * Revamp revive function for RemoteRunnable
    
    * Fix lint
    
    * Update deserializer
    
    * Add test
    
    ---------
    
    Co-authored-by: jacoblee93 <jacoblee93@gmail.com>
    bracesproul and jacoblee93 authored Jan 18, 2024
    2
    Copy the full SHA
    0eb0865 View commit details
  3. community[minor]: feat: turbopuffer vector store (#3780)

    * feat: turbopuffer vector store
    
    * Refactor to community, remove enums for tree shaking
    
    * Test copy
    
    * Fix lint
    
    * cr & add batching
    
    * lint files
    
    * fixed query and added docs
    
    * lint code
    
    * 3000 batch size
    
    * Update with new uuid support
    
    * Adds metadata support
    
    * Update docs and usage examples
    
    * Add delete example
    
    * Nit
    
    ---------
    
    Co-authored-by: jacoblee93 <jacoblee93@gmail.com>
    Co-authored-by: bracesproul <braceasproul@gmail.com>
    3 people authored Jan 18, 2024
    2
    Copy the full SHA
    82a2f49 View commit details
  4. 2
    Copy the full SHA
    7039315 View commit details
  5. Release 0.1.16

    jacoblee93 committed Jan 18, 2024
    Copy the full SHA
    6147b6d View commit details
  6. Merge pull request #4070 from langchain-ai/release

    core[patch]: Release 0.1.16
    jacoblee93 authored Jan 18, 2024
    2
    Copy the full SHA
    0e3c944 View commit details
  7. community[patch]: Change recommended entrypoint, bump versions (#4069)

    * Change recommended entrypoint
    
    * Update docs
    
    * Fix lint
    
    * Bump versions
    
    * Update lock
    jacoblee93 authored Jan 18, 2024
    2
    Copy the full SHA
    c311576 View commit details
  8. Release 0.0.18

    jacoblee93 committed Jan 18, 2024
    Copy the full SHA
    a51a4a7 View commit details
  9. Merge pull request #4071 from langchain-ai/release

    community[patch]: Release 0.0.18
    jacoblee93 authored Jan 18, 2024
    2
    Copy the full SHA
    9072eb6 View commit details
Showing with 3,822 additions and 550 deletions.
  1. +2 βˆ’0 docs/api_refs/typedoc.json
  2. +9 βˆ’2 docs/core_docs/docs/expression_language/interface.mdx
  3. +24 βˆ’0 docs/core_docs/docs/integrations/vectorstores/turbopuffer.mdx
  4. +1 βˆ’1 docs/core_docs/docs/modules/agents/how_to/agent_structured.mdx
  5. +1 βˆ’1 docs/core_docs/docs/modules/agents/how_to/custom_agent.mdx
  6. +166 βˆ’0 docs/core_docs/docs/use_cases/tool_use/agents.mdx
  7. +15 βˆ’0 docs/core_docs/docs/use_cases/tool_use/index.mdx
  8. +191 βˆ’0 docs/core_docs/docs/use_cases/tool_use/multiple_tools.mdx
  9. +172 βˆ’0 docs/core_docs/docs/use_cases/tool_use/parallel.mdx
  10. +326 βˆ’0 docs/core_docs/docs/use_cases/tool_use/quickstart.mdx
  11. +147 βˆ’0 docs/core_docs/docs/use_cases/tool_use/tool_error_handling.mdx
  12. +2 βˆ’1 docs/core_docs/package.json
  13. +4 βˆ’3 examples/package.json
  14. +1 βˆ’1 examples/src/agents/agent_structured.ts
  15. +1 βˆ’1 examples/src/agents/custom_agent.ts
  16. +1 βˆ’4 examples/src/agents/custom_tool.ts
  17. +1 βˆ’1 examples/src/agents/handle_parsing_error.ts
  18. +1 βˆ’1 examples/src/agents/openai_tools_runnable.ts
  19. +1 βˆ’1 examples/src/agents/structured_chat_runnable.ts
  20. +2 βˆ’2 examples/src/guides/expression_language/interface_batch_with_options.ts
  21. +113 βˆ’1 examples/src/guides/expression_language/interface_stream_log.ts
  22. +98 βˆ’0 examples/src/indexes/vector_stores/turbopuffer.ts
  23. +3 βˆ’7 examples/src/retrievers/multi_vector_hypothetical.ts
  24. +3 βˆ’7 examples/src/retrievers/multi_vector_summary.ts
  25. +78 βˆ’0 examples/src/use_cases/tool_use/agents.ts
  26. +96 βˆ’0 examples/src/use_cases/tool_use/multiple_tools.ts
  27. +101 βˆ’0 examples/src/use_cases/tool_use/parallel.ts
  28. +77 βˆ’0 examples/src/use_cases/tool_use/quickstart_agents.ts
  29. +65 βˆ’0 examples/src/use_cases/tool_use/quickstart_chains.ts
  30. +75 βˆ’0 examples/src/use_cases/tool_use/tool_error_handling_fallbacks.ts
  31. +51 βˆ’0 examples/src/use_cases/tool_use/tool_error_handling_intro.ts
  32. +4 βˆ’4 langchain-core/package.json
  33. +3 βˆ’3 langchain-core/src/callbacks/manager.ts
  34. +2 βˆ’6 langchain-core/src/language_models/base.ts
  35. +2 βˆ’2 langchain-core/src/retrievers.ts
  36. +107 βˆ’105 langchain-core/src/runnables/base.ts
  37. +10 βˆ’4 langchain-core/src/runnables/branch.ts
  38. +84 βˆ’11 langchain-core/src/runnables/config.ts
  39. +5 βˆ’1 langchain-core/src/runnables/index.ts
  40. +46 βˆ’3 langchain-core/src/runnables/passthrough.ts
  41. +3 βˆ’3 langchain-core/src/runnables/router.ts
  42. +33 βˆ’0 langchain-core/src/runnables/tests/runnable.test.ts
  43. +63 βˆ’0 langchain-core/src/runnables/tests/runnable_passthrough.test.ts
  44. +213 βˆ’0 langchain-core/src/runnables/tests/runnable_stream_log.test.ts
  45. +124 βˆ’0 langchain-core/src/tools.ts
  46. +7 βˆ’3 langchain-core/src/tracers/base.ts
  47. +39 βˆ’3 langchain-core/src/tracers/log_stream.ts
  48. +119 βˆ’3 langchain-core/src/utils/testing/index.ts
  49. +5 βˆ’5 langchain/package.json
  50. +1 βˆ’3 langchain/src/agents/structured_chat/index.ts
  51. +1 βˆ’1 langchain/src/agents/toolkits/conversational_retrieval/tool.ts
  52. +1 βˆ’1 langchain/src/agents/toolkits/openapi/openapi.ts
  53. +1 βˆ’2 langchain/src/chains/openai_functions/base.ts
  54. +1 βˆ’2 langchain/src/chains/openai_functions/extraction.ts
  55. +5 βˆ’3 langchain/src/chains/openai_functions/openapi.ts
  56. +3 βˆ’2 langchain/src/chains/openai_functions/structured_output.ts
  57. +1 βˆ’2 langchain/src/chains/openai_functions/tagging.ts
  58. +7 βˆ’5 langchain/src/chains/openai_functions/tests/openapi.test.ts
  59. +4 βˆ’2 langchain/src/document_transformers/openai_functions.ts
  60. +1 βˆ’2 langchain/src/experimental/autogpt/prompt_generator.ts
  61. +1 βˆ’2 langchain/src/experimental/plan_and_execute/agent_executor.ts
  62. +1 βˆ’2 langchain/src/experimental/plan_and_execute/prompt.ts
  63. +3 βˆ’0 langchain/src/output_parsers/index.ts
  64. +1 βˆ’1 langchain/src/output_parsers/openai_functions.ts
  65. +76 βˆ’2 langchain/src/output_parsers/openai_tools.ts
  66. +9 βˆ’7 langchain/src/output_parsers/structured.ts
  67. +200 βˆ’72 langchain/src/runnables/remote.ts
  68. +22 βˆ’0 langchain/src/runnables/tests/runnable_remote.int.test.ts
  69. +69 βˆ’22 langchain/src/runnables/tests/runnable_remote.test.ts
  70. +3 βˆ’4 langchain/src/tools/chain.ts
  71. +1 βˆ’2 langchain/src/tools/render.ts
  72. +1 βˆ’1 langchain/src/tools/retriever.ts
  73. +4 βˆ’2 libs/create-langchain-integration/template/package.json
  74. +1 βˆ’1 libs/langchain-anthropic/package.json
  75. +4 βˆ’2 libs/langchain-cloudflare/package.json
  76. +4 βˆ’2 libs/langchain-cohere/package.json
  77. +3 βˆ’0 libs/langchain-community/.gitignore
  78. +15 βˆ’5 libs/langchain-community/package.json
  79. +1 βˆ’0 libs/langchain-community/scripts/create-entrypoints.js
  80. +6 βˆ’0 libs/langchain-community/src/graphs/neo4j_graph.ts
  81. +17 βˆ’0 libs/langchain-community/src/graphs/tests/neo4j_graph.int.test.ts
  82. +66 βˆ’24 libs/langchain-community/src/llms/hf.ts
  83. +32 βˆ’1 libs/langchain-community/src/llms/tests/huggingface_hub.int.test.ts
  84. +1 βˆ’0 libs/langchain-community/src/load/import_map.ts
  85. +1 βˆ’0 libs/langchain-community/src/load/import_type.d.ts
  86. +7 βˆ’132 libs/langchain-community/src/tools/dynamic.ts
  87. +6 βˆ’0 libs/langchain-community/src/vectorstores/faiss.ts
  88. +40 βˆ’2 libs/langchain-community/src/vectorstores/milvus.ts
  89. +3 βˆ’4 libs/langchain-community/src/vectorstores/tests/faiss.int.test.data/faiss.int.test.py
  90. +2 βˆ’1 libs/langchain-community/src/vectorstores/tests/faiss.int.test.data/requirements.txt
  91. +1 βˆ’1 libs/langchain-community/src/vectorstores/tests/faiss.int.test.ts
  92. +131 βˆ’0 libs/langchain-community/src/vectorstores/tests/turbopuffer.int.test.ts
  93. +301 βˆ’0 libs/langchain-community/src/vectorstores/turbopuffer.ts
  94. +4 βˆ’2 libs/langchain-google-genai/package.json
  95. +4 βˆ’2 libs/langchain-mistralai/package.json
  96. +3 βˆ’3 libs/langchain-openai/package.json
  97. +4 βˆ’2 libs/langchain-yandex/package.json
  98. +1 βˆ’1 package.json
  99. +17 βˆ’0 turbo.json
  100. +33 βˆ’33 yarn.lock
2 changes: 2 additions & 0 deletions docs/api_refs/typedoc.json
Original file line number Diff line number Diff line change
@@ -225,6 +225,7 @@
"../../langchain-core/src/utils/async_caller.ts",
"../../langchain-core/src/utils/chunk_array.ts",
"../../langchain-core/src/utils/env.ts",
"../../langchain-core/src/utils/function_calling.ts",
"../../langchain-core/src/utils/hash.ts",
"../../langchain-core/src/utils/json_patch.ts",
"../../langchain-core/src/utils/json_schema.ts",
@@ -235,6 +236,7 @@
"../../langchain-core/src/utils/types.ts",
"../../langchain-core/src/vectorstores.ts",
"../../libs/langchain-anthropic/src/index.ts",
"../../libs/langchain-cloudflare/src/index.ts",
"../../libs/langchain-cohere/src/index.ts",
"../../libs/langchain-community/src/load/index.ts",
"../../libs/langchain-community/src/load/serializable.ts",
11 changes: 9 additions & 2 deletions docs/core_docs/docs/expression_language/interface.mdx
Original file line number Diff line number Diff line change
@@ -68,8 +68,8 @@ import BatchExample from "@examples/guides/expression_language/interface_batch.t

<CodeBlock language="typescript">{BatchExample}</CodeBlock>

You can also pass a `batchOptions` argument to the call. There are options to set maximum concurrency
and whether or not to return exceptions instead of throwing them (useful for gracefully handling failures!):
You can also pass additional arguments to the call. The standard LCEL config object contains an option to set maximum concurrency,
and an additional `batch()` specific config object that includes an option for whether or not to return exceptions instead of throwing them (useful for gracefully handling failures!):

import BatchExampleWithOptions from "@examples/guides/expression_language/interface_batch_with_options.ts";

@@ -84,8 +84,15 @@ You can stream all steps (default) or include/exclude steps by name, tags or met

This method yields [JSONPatch](https://jsonpatch.com/) ops that when applied in the same order as received build up the RunState.

To reconstruct the JSONPatches into a single JSON object you can use the [`applyPatch`](https://api.js.langchain.com/functions/langchain_core_utils_json_patch.applyPatch.html) method.
The example below demonstrates how to pass the patch to the `applyPatch` method.

Here's an example with streaming intermediate documents from a retrieval chain:

import StreamLogExample from "@examples/guides/expression_language/interface_stream_log.ts";

```bash npm2yarn
npm install @langchain/community @langchain/openai
```

<CodeBlock language="typescript">{StreamLogExample}</CodeBlock>
24 changes: 24 additions & 0 deletions docs/core_docs/docs/integrations/vectorstores/turbopuffer.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Turbopuffer

## Setup

First you must sign up for a Turbopuffer account [here](https://turbopuffer.com/join).
Then, once you have an account you can create an API key.

Set your API key as an environment variable:

```bash
export TURBOPUFFER_API_KEY=<YOUR_API_KEY>
```

## Usage

import CodeBlock from "@theme/CodeBlock";
import SimilaritySearchExample from "@examples/indexes/vector_stores/turbopuffer.ts";

Here are some examples of how to use the class. You can filter your queries by previous specified metadata, but
keep in mind that currently only string values are supported.

See [here for more information](https://turbopuffer.com/docs/reference/query#filter-parameters) on acceptable filter formats.

<CodeBlock language="typescript">{SimilaritySearchExample}</CodeBlock>
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@ import { RunnableSequence } from "langchain/runnables";
import { ChatPromptTemplate, MessagesPlaceholder } from "langchain/prompts";
import { ChatOpenAI } from "@langchain/openai";
import { AgentExecutor } from "langchain/agents";
import { DynamicTool } from "langchain/tools";
import { DynamicTool } from "@langchain/core/tools";
import type { FunctionsAgentAction } from "langchain/agents/openai/output_parser";
import { convertToOpenAIFunction } from "@langchain/core/utils/function_calling";

2 changes: 1 addition & 1 deletion docs/core_docs/docs/modules/agents/how_to/custom_agent.mdx
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@ Next, let's define some tools to use.
Let's write a really simple JavaScript function to calculate the length of a word that is passed in.

```ts
import { DynamicTool } from "langchain/tools";
import { DynamicTool } from "@langchain/core/tools";

const customTool = new DynamicTool({
name: "get_word_length",
166 changes: 166 additions & 0 deletions docs/core_docs/docs/use_cases/tool_use/agents.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
---
sidebar_position: 2
---

# Agents

Chains are great when we know the specific sequence of tool usage needed for any user input. But for certain use cases, how many times we use tools depends on the input.
In these cases, we want to let the model itself decide how many times to use tools and in what order. That's where [Agents](/docs/modules/agents/) come in!

LangChain comes with a number of built-in agents that are optimized for different use cases. Read about [all the available agent types](/docs/modules/agents/agent_types/) here.

For this example, let’s try out the OpenAI tools agent, which makes use of the new OpenAI tool-calling API (this is only available in the latest OpenAI models,
and differs from function-calling in that the model can return multiple function invocations at once).

Keep in mind that some agents only support single-argument tools - for these agents, you will need to use a `DynamicTool` instead
and parse the input string yourself.

## Setup

Because we're using OpenAI for this guide, we'll need to install its partner package:

import IntegrationInstallTooltip from "@mdx_components/integration_install_tooltip.mdx";

<IntegrationInstallTooltip></IntegrationInstallTooltip>

```bash npm2yarn
npm install @langchain/openai
```

You'll need to sign up for an OpenAI key and set it as an environment variable named `OPENAI_API_KEY`.

We'll also use the popular validation library [Zod](https://zod.dev) to define our tool schemas. It's already
a dependency of `langchain`, but you can install it explicitly like this too:

```bash npm2yarn
npm install zod
```

## Create tools

First, we need to create some tool to call. For this example, we will create custom tools from functions.
For more information on creating custom tools, please [see this guide](/docs/modules/agents/tools/).

```ts
import { z } from "zod";
import { DynamicStructuredTool } from "@langchain/core/tools";

const addTool = new DynamicStructuredTool({
name: "add",
description: "Add two integers together.",
schema: z.object({
firstInt: z.number(),
secondInt: z.number(),
}),
func: async ({ firstInt, secondInt }) => {
return (firstInt + secondInt).toString();
},
});

const multiplyTool = new DynamicStructuredTool({
name: "multiply",
description: "Multiply two integers together.",
schema: z.object({
firstInt: z.number(),
secondInt: z.number(),
}),
func: async ({ firstInt, secondInt }) => {
return (firstInt * secondInt).toString();
},
});

const exponentiateTool = new DynamicStructuredTool({
name: "exponentiate",
description: "Exponentiate the base to the exponent power.",
schema: z.object({
base: z.number(),
exponent: z.number(),
}),
func: async ({ base, exponent }) => {
return (base ** exponent).toString();
},
});

const tools = [addTool, multiplyTool, exponentiateTool];
```

## Create prompt

```ts
import { pull } from "langchain/hub";
import type { ChatPromptTemplate } from "@langchain/core/prompts";

// Get the prompt to use - you can modify this!
// You can also see the full prompt at:
// https://smith.langchain.com/hub/hwchase17/openai-tools-agent
const prompt = await pull<ChatPromptTemplate>("hwchase17/openai-tools-agent");
```

## Create agent

```ts
import { ChatOpenAI } from "@langchain/openai";
import { AgentExecutor, createOpenAIToolsAgent } from "langchain/agents";

const model = new ChatOpenAI({
modelName: "gpt-3.5-turbo-1106",
temperature: 0,
});

const agent = await createOpenAIToolsAgent({
llm: model,
tools,
prompt,
});

const agentExecutor = new AgentExecutor({
agent,
tools,
verbose: true,
});
```

## Invoke agent

:::info
You can see a LangSmith trace of this example [here](https://smith.langchain.com/public/fc11cacc-f467-4c39-a46e-8bfefa37b1f9/r)
:::

```ts
await agentExecutor.invoke({
input:
"Take 3 to the fifth power and multiply that by the sum of twelve and three, then square the whole result",
});
```

```
> Entering new AgentExecutor chain...
Invoking: `exponentiate` with `{'base': 3, 'exponent': 5}`
243
Invoking: `add` with `{'first_int': 12, 'second_int': 3}`
15
Invoking: `multiply` with `{'first_int': 243, 'second_int': 15}`
3645
Invoking: `exponentiate` with `{'base': 3645, 'exponent': 2}`
13286025
The result of taking 3 to the fifth power and multiplying that by the sum of twelve and three, then squaring the whole result is 13,286,025.
> Finished chain.
```

```
{
input: 'Take 3 to the fifth power and multiply that by the sum of twelve and three, then square the whole result',
output: 'The result of taking 3 to the fifth power and multiplying that by the sum of twelve and three, then squaring the whole result is 13,286,025.'
}
```
15 changes: 15 additions & 0 deletions docs/core_docs/docs/use_cases/tool_use/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
sidebar_position: 2
---

# Tool use

An exciting use case for LLMs is building natural language interfaces for other "tools", whether those are APIs, functions, databases, etc. LangChain is great for building such interfaces because it has:

- Good model output parsing, which makes it easy to extract JSON, XML, OpenAI function-calls, etc. from model outputs.
- A large collection of built-in [Tools](/docs/integrations/tools).
- Provides a lot of flexibility in how you call these tools.

There are two main ways to use tools: [chains](/docs/modules/chains) and [agents](/docs/modules/agents/). Chains lets you create a pre-defined sequence of tool usage(s). Agents let the model use tools in a loop, so that it can decide how many times to use tools.

To get started with both approaches, head to the [Quickstart](/docs/use_cases/tool_use/quickstart) page.
Loading