Skip to content

Commit ff0ac6e

Browse files
authored
docs[minor]: bedrock tool calling/WSO docs (#5735)
* community[minor]: Add withStructuredOutput to bedrock * implement .bind, fix call options, add docs * chore: lint files * merge main
1 parent b737245 commit ff0ac6e

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed

docs/core_docs/docs/integrations/chat/bedrock.mdx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,31 @@ Anthropic Claude-3 models hosted on Bedrock have multimodal capabilities and can
5656
import BedrockMultimodalExample from "@examples/models/chat/integration_bedrock_multimodal.ts";
5757

5858
<CodeBlock language="typescript">{BedrockMultimodalExample}</CodeBlock>
59+
60+
### Tool calling
61+
62+
:::info
63+
Not all Bedrock models support tool calling. Please refer to the [model documentation](https://docs.aws.amazon.com/bedrock/latest/APIReference/welcome.html) for more information.
64+
:::
65+
66+
The examples below demonstrate how to use tool calling, along with the `withStructuredOutput` method to easily compose structured output LLM calls.
67+
68+
import ToolCalling from "@examples/models/chat/integration_bedrock_tools.ts";
69+
70+
<CodeBlock language="typescript">{ToolCalling}</CodeBlock>
71+
72+
:::tip
73+
See the LangSmith trace [here](https://smith.langchain.com/public/003a684d-90eb-406e-a146-8ee5e617921b/r)
74+
:::
75+
76+
#### `.withStructuredOutput({ ... })`
77+
78+
Using the `.withStructuredOutput` method, you can easily make the LLM return structured output, given only a Zod or JSON schema:
79+
80+
import WSOExample from "@examples/models/chat/integration_bedrock_wso.ts";
81+
82+
<CodeBlock language="typescript">{WSOExample}</CodeBlock>
83+
84+
:::tip
85+
See the LangSmith trace [here](https://smith.langchain.com/public/1f7b1ad8-e4ac-4965-8ce1-fae06005f3d7/r)
86+
:::
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { BedrockChat } from "@langchain/community/chat_models/bedrock";
2+
// Or, from web environments:
3+
// import { BedrockChat } from "@langchain/community/chat_models/bedrock/web";
4+
import { z } from "zod";
5+
import { zodToJsonSchema } from "zod-to-json-schema";
6+
7+
const model = new BedrockChat({
8+
region: process.env.BEDROCK_AWS_REGION,
9+
model: "anthropic.claude-3-sonnet-20240229-v1:0",
10+
maxRetries: 0,
11+
credentials: {
12+
secretAccessKey: process.env.BEDROCK_AWS_SECRET_ACCESS_KEY!,
13+
accessKeyId: process.env.BEDROCK_AWS_ACCESS_KEY_ID!,
14+
},
15+
});
16+
17+
const weatherSchema = z
18+
.object({
19+
city: z.string().describe("The city to get the weather for"),
20+
state: z.string().describe("The state to get the weather for").optional(),
21+
})
22+
.describe("Get the weather for a city");
23+
24+
const modelWithTools = model.bindTools([
25+
{
26+
name: "weather_tool",
27+
description: weatherSchema.description,
28+
input_schema: zodToJsonSchema(weatherSchema),
29+
},
30+
]);
31+
// Optionally, you can bind tools via the `.bind` method:
32+
// const modelWithTools = model.bind({
33+
// tools: [
34+
// {
35+
// name: "weather_tool",
36+
// description: weatherSchema.description,
37+
// input_schema: zodToJsonSchema(weatherSchema),
38+
// },
39+
// ],
40+
// });
41+
42+
const res = await modelWithTools.invoke("What's the weather in New York?");
43+
console.log(res);
44+
45+
/*
46+
AIMessage {
47+
additional_kwargs: { id: 'msg_bdrk_01JF7hb4PNQPywP4gnBbgpHi' },
48+
response_metadata: {
49+
stop_reason: 'tool_use',
50+
usage: { input_tokens: 300, output_tokens: 85 }
51+
},
52+
tool_calls: [
53+
{
54+
name: 'weather_tool',
55+
args: {
56+
city: 'New York',
57+
state: 'NY'
58+
},
59+
id: 'toolu_bdrk_01AtEZRTCKioFXqhoNcpgaV7'
60+
}
61+
],
62+
}
63+
*/
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { BedrockChat } from "@langchain/community/chat_models/bedrock";
2+
// Or, from web environments:
3+
// import { BedrockChat } from "@langchain/community/chat_models/bedrock/web";
4+
import { z } from "zod";
5+
6+
const model = new BedrockChat({
7+
region: process.env.BEDROCK_AWS_REGION,
8+
model: "anthropic.claude-3-sonnet-20240229-v1:0",
9+
maxRetries: 0,
10+
credentials: {
11+
secretAccessKey: process.env.BEDROCK_AWS_SECRET_ACCESS_KEY!,
12+
accessKeyId: process.env.BEDROCK_AWS_ACCESS_KEY_ID!,
13+
},
14+
});
15+
16+
const weatherSchema = z
17+
.object({
18+
city: z.string().describe("The city to get the weather for"),
19+
state: z.string().describe("The state to get the weather for").optional(),
20+
})
21+
.describe("Get the weather for a city");
22+
23+
const modelWithStructuredOutput = model.withStructuredOutput(weatherSchema, {
24+
name: "weather_tool", // Optional, defaults to 'extract'
25+
});
26+
27+
const res = await modelWithStructuredOutput.invoke(
28+
"What's the weather in New York?"
29+
);
30+
console.log(res);
31+
32+
/*
33+
{ city: 'New York', state: 'NY' }
34+
*/

0 commit comments

Comments
 (0)