|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "raw", |
| 5 | + "id": "67db2992", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "---\n", |
| 9 | + "sidebar_label: Bedrock\n", |
| 10 | + "---" |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "markdown", |
| 15 | + "id": "9597802c", |
| 16 | + "metadata": {}, |
| 17 | + "source": [ |
| 18 | + "# Bedrock\n", |
| 19 | + "\n", |
| 20 | + "```{=mdx}\n", |
| 21 | + "\n", |
| 22 | + ":::caution\n", |
| 23 | + "You are currently on a page documenting the use of Amazon Bedrock models as [text completion models](/docs/concepts/#llms). Many popular models available on Bedrock are [chat completion models](/docs/concepts/#chat-models).\n", |
| 24 | + "\n", |
| 25 | + "You may be looking for [this page instead](/docs/integrations/chat/bedrock/).\n", |
| 26 | + ":::\n", |
| 27 | + "\n", |
| 28 | + "```\n", |
| 29 | + "\n", |
| 30 | + "> [Amazon Bedrock](https://aws.amazon.com/bedrock/) is a fully managed service that makes Foundation Models (FMs)\n", |
| 31 | + "> from leading AI startups and Amazon available via an API. You can choose from a wide range of FMs to find the model that is best suited for your use case.\n", |
| 32 | + "\n", |
| 33 | + "This will help you get started with Bedrock completion models (LLMs) using LangChain. For detailed documentation on `Bedrock` features and configuration options, please refer to the [API reference](https://api.js.langchain.com/classes/langchain_community_llms_bedrock.Bedrock.html).\n", |
| 34 | + "\n", |
| 35 | + "## Overview\n", |
| 36 | + "### Integration details\n", |
| 37 | + "\n", |
| 38 | + "- TODO: Fill in table features.\n", |
| 39 | + "- TODO: Remove JS support link if not relevant, otherwise ensure link is correct.\n", |
| 40 | + "- TODO: Make sure API reference links are correct.\n", |
| 41 | + "\n", |
| 42 | + "| Class | Package | Local | Serializable | [PY support](https://python.langchain.com/docs/integrations/llms/bedrock) | Package downloads | Package latest |\n", |
| 43 | + "| :--- | :--- | :---: | :---: | :---: | :---: | :---: |\n", |
| 44 | + "| [Bedrock](https://api.js.langchain.com/classes/langchain_community_llms_bedrock.Bedrock.html) | [@langchain/community](https://api.js.langchain.com/modules/langchain_community_llms_bedrock.html) | ❌ | ✅ | ✅ |  |  |\n", |
| 45 | + "\n", |
| 46 | + "## Setup\n", |
| 47 | + "\n", |
| 48 | + "To access Bedrock models you'll need to create an AWS account, get an API key, and install the `@langchain/community` integration, along with a few peer dependencies.\n", |
| 49 | + "\n", |
| 50 | + "### Credentials\n", |
| 51 | + "\n", |
| 52 | + "Head to [aws.amazon.com](https://aws.amazon.com) to sign up to AWS Bedrock and generate an API key. Once you've done this set the environment variables:\n", |
| 53 | + "\n", |
| 54 | + "```bash\n", |
| 55 | + "export BEDROCK_AWS_REGION=\"your-region-url\"\n", |
| 56 | + "export BEDROCK_AWS_ACCESS_KEY_ID=\"your-access-key-id\"\n", |
| 57 | + "export BEDROCK_AWS_SECRET_ACCESS_KEY=\"your-secret-access-key\"\n", |
| 58 | + "```\n", |
| 59 | + "\n", |
| 60 | + "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", |
| 61 | + "\n", |
| 62 | + "```bash\n", |
| 63 | + "# export LANGCHAIN_TRACING_V2=\"true\"\n", |
| 64 | + "# export LANGCHAIN_API_KEY=\"your-api-key\"\n", |
| 65 | + "```\n", |
| 66 | + "\n", |
| 67 | + "### Installation\n", |
| 68 | + "\n", |
| 69 | + "The LangChain Bedrock integration lives in the `@langchain/community` package:\n", |
| 70 | + "\n", |
| 71 | + "```{=mdx}\n", |
| 72 | + "import IntegrationInstallTooltip from \"@mdx_components/integration_install_tooltip.mdx\";\n", |
| 73 | + "import Npm2Yarn from \"@theme/Npm2Yarn\";\n", |
| 74 | + "\n", |
| 75 | + "<IntegrationInstallTooltip></IntegrationInstallTooltip>\n", |
| 76 | + "\n", |
| 77 | + "<Npm2Yarn>\n", |
| 78 | + " @langchain/community\n", |
| 79 | + "</Npm2Yarn>\n", |
| 80 | + "\n", |
| 81 | + "And install the peer dependencies:\n", |
| 82 | + "\n", |
| 83 | + "<Npm2Yarn>\n", |
| 84 | + " @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", |
| 85 | + "</Npm2Yarn>\n", |
| 86 | + "\n", |
| 87 | + "You can also use Bedrock in web environments such as Edge functions or Cloudflare Workers by omitting the `@aws-sdk/credential-provider-node` dependency\n", |
| 88 | + "and using the `web` entrypoint:\n", |
| 89 | + "\n", |
| 90 | + "<Npm2Yarn>\n", |
| 91 | + " @aws-crypto/sha256-js @smithy/protocol-http @smithy/signature-v4 @smithy/eventstream-codec @smithy/util-utf8 @aws-sdk/types\n", |
| 92 | + "</Npm2Yarn>\n", |
| 93 | + "\n", |
| 94 | + "```" |
| 95 | + ] |
| 96 | + }, |
| 97 | + { |
| 98 | + "cell_type": "markdown", |
| 99 | + "id": "0a760037", |
| 100 | + "metadata": {}, |
| 101 | + "source": [ |
| 102 | + "## Instantiation\n", |
| 103 | + "\n", |
| 104 | + "Now we can instantiate our model object and generate chat completions:" |
| 105 | + ] |
| 106 | + }, |
| 107 | + { |
| 108 | + "cell_type": "code", |
| 109 | + "execution_count": null, |
| 110 | + "id": "093ae37f", |
| 111 | + "metadata": {}, |
| 112 | + "outputs": [], |
| 113 | + "source": [ |
| 114 | + "// @lc-docs-hide-cell\n", |
| 115 | + "// Deno requires these imports, and way of loading env vars.\n", |
| 116 | + "// we don't want to expose in the docs.\n", |
| 117 | + "// Below this cell we have a typescript markdown codeblock with\n", |
| 118 | + "// the node code.\n", |
| 119 | + "import \"@aws-sdk/credential-provider-node\";\n", |
| 120 | + "import \"@smithy/protocol-http\";\n", |
| 121 | + "import \"@aws-crypto/sha256-js\";\n", |
| 122 | + "import \"@smithy/protocol-http\";\n", |
| 123 | + "import \"@smithy/signature-v4\";\n", |
| 124 | + "import \"@smithy/eventstream-codec\";\n", |
| 125 | + "import \"@smithy/util-utf8\";\n", |
| 126 | + "import \"@aws-sdk/types\";\n", |
| 127 | + "import { Bedrock } from \"@langchain/community/llms/bedrock\"\n", |
| 128 | + "import { getEnvironmentVariable } from \"@langchain/core/utils/env\";\n", |
| 129 | + "\n", |
| 130 | + "const llm = new Bedrock({\n", |
| 131 | + " model: \"anthropic.claude-v2\",\n", |
| 132 | + " region: \"us-east-1\",\n", |
| 133 | + " // endpointUrl: \"custom.amazonaws.com\",\n", |
| 134 | + " credentials: {\n", |
| 135 | + " accessKeyId: getEnvironmentVariable(\"BEDROCK_AWS_ACCESS_KEY_ID\"),\n", |
| 136 | + " secretAccessKey: getEnvironmentVariable(\"BEDROCK_AWS_SECRET_ACCESS_KEY\"),\n", |
| 137 | + " },\n", |
| 138 | + " temperature: 0,\n", |
| 139 | + " maxTokens: undefined,\n", |
| 140 | + " maxRetries: 2,\n", |
| 141 | + " // other params...\n", |
| 142 | + "})" |
| 143 | + ] |
| 144 | + }, |
| 145 | + { |
| 146 | + "cell_type": "markdown", |
| 147 | + "id": "a0562a13", |
| 148 | + "metadata": {}, |
| 149 | + "source": [ |
| 150 | + "```typescript\n", |
| 151 | + "import { Bedrock } from \"@langchain/community/llms/bedrock\"\n", |
| 152 | + "\n", |
| 153 | + "const llm = new Bedrock({\n", |
| 154 | + " model: \"anthropic.claude-v2\",\n", |
| 155 | + " region: process.env.BEDROCK_AWS_REGION ?? \"us-east-1\",\n", |
| 156 | + " // endpointUrl: \"custom.amazonaws.com\",\n", |
| 157 | + " credentials: {\n", |
| 158 | + " accessKeyId: process.env.BEDROCK_AWS_ACCESS_KEY_ID,\n", |
| 159 | + " secretAccessKey: process.env.BEDROCK_AWS_SECRET_ACCESS_KEY,\n", |
| 160 | + " },\n", |
| 161 | + " temperature: 0,\n", |
| 162 | + " maxTokens: undefined,\n", |
| 163 | + " maxRetries: 2,\n", |
| 164 | + " // other params...\n", |
| 165 | + "})\n", |
| 166 | + "```" |
| 167 | + ] |
| 168 | + }, |
| 169 | + { |
| 170 | + "cell_type": "markdown", |
| 171 | + "id": "0ee90032", |
| 172 | + "metadata": {}, |
| 173 | + "source": [ |
| 174 | + "## Invocation\n", |
| 175 | + "\n", |
| 176 | + "Note that some models require specific prompting techniques. For example, Anthropic's Claude-v2 model will throw an error if\n", |
| 177 | + "the prompt does not start with `Human: `." |
| 178 | + ] |
| 179 | + }, |
| 180 | + { |
| 181 | + "cell_type": "code", |
| 182 | + "execution_count": 3, |
| 183 | + "id": "035dea0f", |
| 184 | + "metadata": { |
| 185 | + "tags": [] |
| 186 | + }, |
| 187 | + "outputs": [ |
| 188 | + { |
| 189 | + "data": { |
| 190 | + "text/plain": [ |
| 191 | + "\u001b[32m\" Here are a few key points about Bedrock AI:\\n\"\u001b[39m +\n", |
| 192 | + " \u001b[32m\"\\n\"\u001b[39m +\n", |
| 193 | + " \u001b[32m\"- Bedrock was founded in 2021 and is based in San Fran\"\u001b[39m... 116 more characters" |
| 194 | + ] |
| 195 | + }, |
| 196 | + "execution_count": 3, |
| 197 | + "metadata": {}, |
| 198 | + "output_type": "execute_result" |
| 199 | + } |
| 200 | + ], |
| 201 | + "source": [ |
| 202 | + "const inputText = \"Human: Bedrock is an AI company that\\nAssistant: \"\n", |
| 203 | + "\n", |
| 204 | + "const completion = await llm.invoke(inputText)\n", |
| 205 | + "completion" |
| 206 | + ] |
| 207 | + }, |
| 208 | + { |
| 209 | + "cell_type": "markdown", |
| 210 | + "id": "add38532", |
| 211 | + "metadata": {}, |
| 212 | + "source": [ |
| 213 | + "## Chaining\n", |
| 214 | + "\n", |
| 215 | + "We can [chain](/docs/how_to/sequence/) our completion model with a prompt template like so:" |
| 216 | + ] |
| 217 | + }, |
| 218 | + { |
| 219 | + "cell_type": "code", |
| 220 | + "execution_count": 21, |
| 221 | + "id": "078e9db2", |
| 222 | + "metadata": {}, |
| 223 | + "outputs": [ |
| 224 | + { |
| 225 | + "data": { |
| 226 | + "text/plain": [ |
| 227 | + "\u001b[32m' Here is how to say \"I love programming\" in German:\\n'\u001b[39m +\n", |
| 228 | + " \u001b[32m\"\\n\"\u001b[39m +\n", |
| 229 | + " \u001b[32m\"Ich liebe das Programmieren.\"\u001b[39m" |
| 230 | + ] |
| 231 | + }, |
| 232 | + "execution_count": 21, |
| 233 | + "metadata": {}, |
| 234 | + "output_type": "execute_result" |
| 235 | + } |
| 236 | + ], |
| 237 | + "source": [ |
| 238 | + "import { PromptTemplate } from \"@langchain/core/prompts\"\n", |
| 239 | + "\n", |
| 240 | + "const prompt = PromptTemplate.fromTemplate(\"Human: How to say {input} in {output_language}:\\nAssistant:\")\n", |
| 241 | + "\n", |
| 242 | + "const chain = prompt.pipe(llm);\n", |
| 243 | + "await chain.invoke(\n", |
| 244 | + " {\n", |
| 245 | + " output_language: \"German\",\n", |
| 246 | + " input: \"I love programming.\",\n", |
| 247 | + " }\n", |
| 248 | + ")" |
| 249 | + ] |
| 250 | + }, |
| 251 | + { |
| 252 | + "cell_type": "markdown", |
| 253 | + "id": "e9bdfcef", |
| 254 | + "metadata": {}, |
| 255 | + "source": [ |
| 256 | + "## API reference\n", |
| 257 | + "\n", |
| 258 | + "For detailed documentation of all Bedrock features and configurations head to the API reference: https://api.js.langchain.com/classes/langchain_community_llms_bedrock.Bedrock.html" |
| 259 | + ] |
| 260 | + } |
| 261 | + ], |
| 262 | + "metadata": { |
| 263 | + "kernelspec": { |
| 264 | + "display_name": "Deno", |
| 265 | + "language": "typescript", |
| 266 | + "name": "deno" |
| 267 | + }, |
| 268 | + "language_info": { |
| 269 | + "file_extension": ".ts", |
| 270 | + "mimetype": "text/x.typescript", |
| 271 | + "name": "typescript", |
| 272 | + "nb_converter": "script", |
| 273 | + "pygments_lexer": "typescript", |
| 274 | + "version": "5.3.3" |
| 275 | + }, |
| 276 | + "vscode": { |
| 277 | + "interpreter": { |
| 278 | + "hash": "e971737741ff4ec9aff7dc6155a1060a59a8a6d52c757dbbe66bf8ee389494b1" |
| 279 | + } |
| 280 | + } |
| 281 | + }, |
| 282 | + "nbformat": 4, |
| 283 | + "nbformat_minor": 5 |
| 284 | +} |
0 commit comments