|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "raw", |
| 5 | + "id": "afaf8039", |
| 6 | + "metadata": { |
| 7 | + "vscode": { |
| 8 | + "languageId": "raw" |
| 9 | + } |
| 10 | + }, |
| 11 | + "source": [ |
| 12 | + "---\n", |
| 13 | + "sidebar_label: Amazon Bedrock\n", |
| 14 | + "---" |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "markdown", |
| 19 | + "id": "e49f1e0d", |
| 20 | + "metadata": {}, |
| 21 | + "source": [ |
| 22 | + "# BedrockChat\n", |
| 23 | + "\n", |
| 24 | + "[Amazon Bedrock](https://aws.amazon.com/bedrock/) is a fully managed service that offers a choice of high-performing foundation models (FMs) from leading AI companies like AI21 Labs, Anthropic, Cohere, Meta, Stability AI, and Amazon via a single API, along with a broad set of capabilities you need to build generative AI applications with security, privacy, and responsible AI. \n", |
| 25 | + "\n", |
| 26 | + "This will help you getting started with Amazon Bedrock [chat models](/docs/concepts/#chat-models). For detailed documentation of all `BedrockChat` features and configurations head to the [API reference](https://api.js.langchain.com/classes/langchain_community_chat_models_bedrock.BedrockChat.html).\n", |
| 27 | + "\n", |
| 28 | + ":::tip\n", |
| 29 | + "The newer [`ChatBedrockConverse` chat model is now available via the dedicated `@langchain/aws`](/docs/integrations/chat/bedrock_converse) integration package. Use [tool calling](/docs/concepts#functiontool-calling) with more models with this package.\n", |
| 30 | + ":::\n", |
| 31 | + "\n", |
| 32 | + "## Overview\n", |
| 33 | + "### Integration details\n", |
| 34 | + "\n", |
| 35 | + "| Class | Package | Local | Serializable | [PY support](https://python.langchain.com/v0.2/docs/integrations/chat/bedrock/) | Package downloads | Package latest |\n", |
| 36 | + "| :--- | :--- | :---: | :---: | :---: | :---: | :---: |\n", |
| 37 | + "| [`BedrockChat`](https://api.js.langchain.com/classes/langchain_community_chat_models_bedrock.BedrockChat.html) | [`@langchain/community`](https://npmjs.com/@langchain/community) | ❌ | ✅ | ✅ |  |  |\n", |
| 38 | + "\n", |
| 39 | + "### Model features\n", |
| 40 | + "\n", |
| 41 | + "See the links in the table headers below for guides on how to use specific features.\n", |
| 42 | + "\n", |
| 43 | + "| [Tool calling](/docs/how_to/tool_calling) | [Structured output](/docs/how_to/structured_output/) | JSON mode | [Image input](/docs/how_to/multimodal_inputs/) | Audio input | Video input | [Token-level streaming](/docs/how_to/chat_streaming/) | [Token usage](/docs/how_to/chat_token_usage_tracking/) | [Logprobs](/docs/how_to/logprobs/) |\n", |
| 44 | + "| :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: | :---: |\n", |
| 45 | + "| ✅ | ✅ | ❌ | ✅ | ❌ | ❌ | ✅ | ✅ | ❌ | \n", |
| 46 | + "\n", |
| 47 | + "## Setup\n", |
| 48 | + "\n", |
| 49 | + "To access Bedrock models you'll need to create an AWS account, set up the Bedrock API service, get an access key ID and secret key, and install the `@langchain/community` integration package.\n", |
| 50 | + "\n", |
| 51 | + "### Credentials\n", |
| 52 | + "\n", |
| 53 | + "Head to the [AWS docs](https://docs.aws.amazon.com/bedrock/latest/userguide/getting-started.html) to sign up for AWS and setup your credentials. You'll also need to turn on model access for your account, which you can do by [following these instructions](https://docs.aws.amazon.com/bedrock/latest/userguide/model-access.html).\n", |
| 54 | + "\n", |
| 55 | + "If you want to get automated tracing of your model calls you can also set your [LangSmith](https://docs.smith.langchain.com/) API key by uncommenting below:\n", |
| 56 | + "\n", |
| 57 | + "```bash\n", |
| 58 | + "# export LANGCHAIN_TRACING_V2=\"true\"\n", |
| 59 | + "# export LANGCHAIN_API_KEY=\"your-api-key\"\n", |
| 60 | + "```\n", |
| 61 | + "\n", |
| 62 | + "### Installation\n", |
| 63 | + "\n", |
| 64 | + "The LangChain `BedrockChat` integration lives in the `@langchain/community` package. You'll also need to install several official AWS packages as peer dependencies:\n", |
| 65 | + "\n", |
| 66 | + "```{=mdx}\n", |
| 67 | + "import IntegrationInstallTooltip from \"@mdx_components/integration_install_tooltip.mdx\";\n", |
| 68 | + "import Npm2Yarn from \"@theme/Npm2Yarn\";\n", |
| 69 | + "\n", |
| 70 | + "<IntegrationInstallTooltip></IntegrationInstallTooltip>\n", |
| 71 | + "\n", |
| 72 | + "<Npm2Yarn>\n", |
| 73 | + " @langchain/community @aws-crypto/sha256-js @aws-sdk/credential-provider-node @smithy/protocol-http @smithy/signature-v4 @smithy/eventstream-codec @smithy/util-utf8 @aws-sdk/types\n", |
| 74 | + "</Npm2Yarn>\n", |
| 75 | + "```\n", |
| 76 | + "\n", |
| 77 | + "You can also use BedrockChat in web environments such as Edge functions or Cloudflare Workers by omitting the @aws-sdk/credential-provider-node dependency and using the web entrypoint:\n", |
| 78 | + "\n", |
| 79 | + "```{=mdx}\n", |
| 80 | + "<IntegrationInstallTooltip></IntegrationInstallTooltip>\n", |
| 81 | + "\n", |
| 82 | + "<Npm2Yarn>\n", |
| 83 | + " @langchain/community @aws-crypto/sha256-js @smithy/protocol-http @smithy/signature-v4 @smithy/eventstream-codec @smithy/util-utf8 @aws-sdk/types\n", |
| 84 | + "</Npm2Yarn>\n", |
| 85 | + "\n", |
| 86 | + "```" |
| 87 | + ] |
| 88 | + }, |
| 89 | + { |
| 90 | + "cell_type": "markdown", |
| 91 | + "id": "a38cde65-254d-4219-a441-068766c0d4b5", |
| 92 | + "metadata": {}, |
| 93 | + "source": [ |
| 94 | + "## Instantiation\n", |
| 95 | + "\n", |
| 96 | + "Currently, only Anthropic, Cohere, and Mistral models are supported with the chat model integration. For foundation models from AI21 or Amazon, see the [text generation Bedrock variant](/docs/integrations/llms/bedrock/).\n", |
| 97 | + "\n", |
| 98 | + "There are a few different ways to authenticate with AWS - the below examples rely on an access key, secret access key and region set in your environment variables:" |
| 99 | + ] |
| 100 | + }, |
| 101 | + { |
| 102 | + "cell_type": "code", |
| 103 | + "execution_count": 1, |
| 104 | + "id": "cb09c344-1836-4e0c-acf8-11d13ac1dbae", |
| 105 | + "metadata": {}, |
| 106 | + "outputs": [], |
| 107 | + "source": [ |
| 108 | + "import { BedrockChat } from \"@langchain/community/chat_models/bedrock\";\n", |
| 109 | + "\n", |
| 110 | + "const llm = new BedrockChat({\n", |
| 111 | + " model: \"anthropic.claude-3-5-sonnet-20240620-v1:0\",\n", |
| 112 | + " region: process.env.BEDROCK_AWS_REGION,\n", |
| 113 | + " credentials: {\n", |
| 114 | + " accessKeyId: process.env.BEDROCK_AWS_ACCESS_KEY_ID!,\n", |
| 115 | + " secretAccessKey: process.env.BEDROCK_AWS_SECRET_ACCESS_KEY!,\n", |
| 116 | + " },\n", |
| 117 | + " // endpointUrl: \"custom.amazonaws.com\",\n", |
| 118 | + " // modelKwargs: {\n", |
| 119 | + " // anthropic_version: \"bedrock-2023-05-31\",\n", |
| 120 | + " // },\n", |
| 121 | + "});" |
| 122 | + ] |
| 123 | + }, |
| 124 | + { |
| 125 | + "cell_type": "markdown", |
| 126 | + "id": "2b4f3e15", |
| 127 | + "metadata": {}, |
| 128 | + "source": [ |
| 129 | + "## Invocation" |
| 130 | + ] |
| 131 | + }, |
| 132 | + { |
| 133 | + "cell_type": "code", |
| 134 | + "execution_count": 2, |
| 135 | + "id": "62e0dbc3", |
| 136 | + "metadata": { |
| 137 | + "tags": [] |
| 138 | + }, |
| 139 | + "outputs": [ |
| 140 | + { |
| 141 | + "name": "stdout", |
| 142 | + "output_type": "stream", |
| 143 | + "text": [ |
| 144 | + "AIMessage {\n", |
| 145 | + " \"content\": \"J'adore la programmation.\",\n", |
| 146 | + " \"additional_kwargs\": {\n", |
| 147 | + " \"id\": \"msg_bdrk_01RwhfuWkLLcp7ks1X3u8bwd\"\n", |
| 148 | + " },\n", |
| 149 | + " \"response_metadata\": {\n", |
| 150 | + " \"type\": \"message\",\n", |
| 151 | + " \"role\": \"assistant\",\n", |
| 152 | + " \"model\": \"claude-3-5-sonnet-20240620\",\n", |
| 153 | + " \"stop_reason\": \"end_turn\",\n", |
| 154 | + " \"stop_sequence\": null,\n", |
| 155 | + " \"usage\": {\n", |
| 156 | + " \"input_tokens\": 29,\n", |
| 157 | + " \"output_tokens\": 11\n", |
| 158 | + " }\n", |
| 159 | + " },\n", |
| 160 | + " \"tool_calls\": [],\n", |
| 161 | + " \"invalid_tool_calls\": []\n", |
| 162 | + "}\n" |
| 163 | + ] |
| 164 | + } |
| 165 | + ], |
| 166 | + "source": [ |
| 167 | + "const aiMsg = await llm.invoke([\n", |
| 168 | + " [\n", |
| 169 | + " \"system\",\n", |
| 170 | + " \"You are a helpful assistant that translates English to French. Translate the user sentence.\",\n", |
| 171 | + " ],\n", |
| 172 | + " [\"human\", \"I love programming.\"],\n", |
| 173 | + "])\n", |
| 174 | + "aiMsg" |
| 175 | + ] |
| 176 | + }, |
| 177 | + { |
| 178 | + "cell_type": "code", |
| 179 | + "execution_count": 3, |
| 180 | + "id": "d86145b3-bfef-46e8-b227-4dda5c9c2705", |
| 181 | + "metadata": {}, |
| 182 | + "outputs": [ |
| 183 | + { |
| 184 | + "name": "stdout", |
| 185 | + "output_type": "stream", |
| 186 | + "text": [ |
| 187 | + "J'adore la programmation.\n" |
| 188 | + ] |
| 189 | + } |
| 190 | + ], |
| 191 | + "source": [ |
| 192 | + "console.log(aiMsg.content)" |
| 193 | + ] |
| 194 | + }, |
| 195 | + { |
| 196 | + "cell_type": "markdown", |
| 197 | + "id": "18e2bfc0-7e78-4528-a73f-499ac150dca8", |
| 198 | + "metadata": {}, |
| 199 | + "source": [ |
| 200 | + "## Chaining\n", |
| 201 | + "\n", |
| 202 | + "We can [chain](/docs/how_to/sequence/) our model with a prompt template like so:" |
| 203 | + ] |
| 204 | + }, |
| 205 | + { |
| 206 | + "cell_type": "code", |
| 207 | + "execution_count": 4, |
| 208 | + "id": "e197d1d7-a070-4c96-9f8a-a0e86d046e0b", |
| 209 | + "metadata": {}, |
| 210 | + "outputs": [ |
| 211 | + { |
| 212 | + "name": "stdout", |
| 213 | + "output_type": "stream", |
| 214 | + "text": [ |
| 215 | + "AIMessage {\n", |
| 216 | + " \"content\": \"Here's the German translation:\\n\\nIch liebe Programmieren.\",\n", |
| 217 | + " \"additional_kwargs\": {\n", |
| 218 | + " \"id\": \"msg_bdrk_01RtUH3qrYJPUdutYoxphFkv\"\n", |
| 219 | + " },\n", |
| 220 | + " \"response_metadata\": {\n", |
| 221 | + " \"type\": \"message\",\n", |
| 222 | + " \"role\": \"assistant\",\n", |
| 223 | + " \"model\": \"claude-3-5-sonnet-20240620\",\n", |
| 224 | + " \"stop_reason\": \"end_turn\",\n", |
| 225 | + " \"stop_sequence\": null,\n", |
| 226 | + " \"usage\": {\n", |
| 227 | + " \"input_tokens\": 23,\n", |
| 228 | + " \"output_tokens\": 18\n", |
| 229 | + " }\n", |
| 230 | + " },\n", |
| 231 | + " \"tool_calls\": [],\n", |
| 232 | + " \"invalid_tool_calls\": []\n", |
| 233 | + "}\n" |
| 234 | + ] |
| 235 | + } |
| 236 | + ], |
| 237 | + "source": [ |
| 238 | + "import { ChatPromptTemplate } from \"@langchain/core/prompts\"\n", |
| 239 | + "\n", |
| 240 | + "const prompt = ChatPromptTemplate.fromMessages(\n", |
| 241 | + " [\n", |
| 242 | + " [\n", |
| 243 | + " \"system\",\n", |
| 244 | + " \"You are a helpful assistant that translates {input_language} to {output_language}.\",\n", |
| 245 | + " ],\n", |
| 246 | + " [\"human\", \"{input}\"],\n", |
| 247 | + " ]\n", |
| 248 | + ")\n", |
| 249 | + "\n", |
| 250 | + "const chain = prompt.pipe(llm);\n", |
| 251 | + "await chain.invoke(\n", |
| 252 | + " {\n", |
| 253 | + " input_language: \"English\",\n", |
| 254 | + " output_language: \"German\",\n", |
| 255 | + " input: \"I love programming.\",\n", |
| 256 | + " }\n", |
| 257 | + ")" |
| 258 | + ] |
| 259 | + }, |
| 260 | + { |
| 261 | + "cell_type": "markdown", |
| 262 | + "id": "d1ee55bc-ffc8-4cfa-801c-993953a08cfd", |
| 263 | + "metadata": {}, |
| 264 | + "source": [ |
| 265 | + "## Tool calling\n", |
| 266 | + "\n", |
| 267 | + "Tool calling with Bedrock models works in a similar way to [other models](/docs/how_to/tool_calling), but note that not all Bedrock models support tool calling. Please refer to the [AWS model documentation](https://docs.aws.amazon.com/bedrock/latest/APIReference/welcome.html) for more information." |
| 268 | + ] |
| 269 | + }, |
| 270 | + { |
| 271 | + "cell_type": "markdown", |
| 272 | + "id": "3a5bb5ca-c3ae-4a58-be67-2cd18574b9a3", |
| 273 | + "metadata": {}, |
| 274 | + "source": [ |
| 275 | + "## API reference\n", |
| 276 | + "\n", |
| 277 | + "For detailed documentation of all __module_name__ features and configurations head to the API reference: __api_ref_module__" |
| 278 | + ] |
| 279 | + } |
| 280 | + ], |
| 281 | + "metadata": { |
| 282 | + "kernelspec": { |
| 283 | + "display_name": "TypeScript", |
| 284 | + "language": "typescript", |
| 285 | + "name": "tslab" |
| 286 | + }, |
| 287 | + "language_info": { |
| 288 | + "codemirror_mode": { |
| 289 | + "mode": "typescript", |
| 290 | + "name": "javascript", |
| 291 | + "typescript": true |
| 292 | + }, |
| 293 | + "file_extension": ".ts", |
| 294 | + "mimetype": "text/typescript", |
| 295 | + "name": "typescript", |
| 296 | + "version": "3.7.2" |
| 297 | + } |
| 298 | + }, |
| 299 | + "nbformat": 4, |
| 300 | + "nbformat_minor": 5 |
| 301 | +} |
0 commit comments