LangChain AWS integration providing chat models, embeddings, and retrievers for seamless AWS service connections.
npx @tessl/cli install tessl/npm-langchain--aws@0.1.00
# @langchain/aws
1
2
@langchain/aws provides comprehensive LangChain integrations for AWS services, enabling developers to seamlessly connect their applications with AWS Bedrock chat models, embeddings, and Kendra/Knowledge Base retrievers. Built with TypeScript for enhanced developer experience, it offers production-ready authentication mechanisms and maintains compatibility with the broader LangChain ecosystem.
3
4
## Package Information
5
6
- **Package Name**: @langchain/aws
7
- **Package Type**: npm
8
- **Language**: TypeScript
9
- **Installation**: `npm install @langchain/aws`
10
- **Node.js Requirements**: >= 18
11
12
## Core Imports
13
14
```typescript
15
import {
16
ChatBedrockConverse,
17
BedrockEmbeddings,
18
AmazonKendraRetriever,
19
AmazonKnowledgeBaseRetriever
20
} from "@langchain/aws";
21
```
22
23
For CommonJS:
24
25
```javascript
26
const {
27
ChatBedrockConverse,
28
BedrockEmbeddings,
29
AmazonKendraRetriever,
30
AmazonKnowledgeBaseRetriever
31
} = require("@langchain/aws");
32
```
33
34
## Basic Usage
35
36
```typescript
37
import { ChatBedrockConverse } from "@langchain/aws";
38
import { HumanMessage } from "@langchain/core/messages";
39
40
// Initialize chat model with AWS credentials
41
const model = new ChatBedrockConverse({
42
region: process.env.BEDROCK_AWS_REGION ?? "us-east-1",
43
credentials: {
44
secretAccessKey: process.env.BEDROCK_AWS_SECRET_ACCESS_KEY,
45
accessKeyId: process.env.BEDROCK_AWS_ACCESS_KEY_ID,
46
},
47
model: "anthropic.claude-3-5-sonnet-20240620-v1:0",
48
temperature: 0.7,
49
maxTokens: 1000
50
});
51
52
// Generate response
53
const response = await model.invoke([
54
new HumanMessage("What is the capital of France?")
55
]);
56
57
console.log(response.content);
58
```
59
60
## Architecture
61
62
@langchain/aws is built around several key components:
63
64
- **Chat Models**: `ChatBedrockConverse` class providing both streaming and non-streaming interfaces with full AWS Bedrock Converse API support
65
- **Embeddings**: `BedrockEmbeddings` for generating text embeddings using Amazon Titan and other Bedrock embedding models
66
- **Retrievers**: Document retrieval through `AmazonKendraRetriever` for intelligent search and `AmazonKnowledgeBaseRetriever` for RAG workflows
67
- **Authentication**: Flexible credential management supporting AWS access keys, bearer tokens, and credential provider chains
68
- **Type Safety**: Complete TypeScript integration with interfaces for all configuration options and responses
69
70
## Capabilities
71
72
### Chat Models
73
74
Advanced conversational AI capabilities using AWS Bedrock's Converse API with support for streaming, function calling, multimodal input, and structured output.
75
76
```typescript { .api }
77
class ChatBedrockConverse extends BaseChatModel {
78
constructor(fields?: ChatBedrockConverseInput);
79
invoke(messages: BaseMessage[], options?: ChatBedrockConverseCallOptions): Promise<AIMessage>;
80
stream(messages: BaseMessage[], options?: ChatBedrockConverseCallOptions): AsyncGenerator<AIMessageChunk>;
81
bindTools(tools: ChatBedrockConverseToolType[], kwargs?: Partial<ChatBedrockConverseCallOptions>): Runnable;
82
withStructuredOutput<T>(outputSchema: InteropZodType<T> | Record<string, any>, config?: StructuredOutputMethodOptions): Runnable<BaseLanguageModelInput, T>;
83
}
84
85
interface ChatBedrockConverseInput extends BaseChatModelParams {
86
client?: BedrockRuntimeClient;
87
clientOptions?: BedrockRuntimeClientConfig;
88
streaming?: boolean;
89
model?: string;
90
region?: string;
91
credentials?: CredentialType;
92
temperature?: number;
93
maxTokens?: number;
94
endpointHost?: string;
95
topP?: number;
96
additionalModelRequestFields?: __DocumentType;
97
streamUsage?: boolean;
98
guardrailConfig?: GuardrailConfiguration;
99
performanceConfig?: PerformanceConfiguration;
100
supportsToolChoiceValues?: Array<"auto" | "any" | "tool">;
101
}
102
```
103
104
[Chat Models](./chat-models.md)
105
106
### Text Embeddings
107
108
Generate high-quality text embeddings using AWS Bedrock embedding models with automatic batching and text preprocessing.
109
110
```typescript { .api }
111
class BedrockEmbeddings extends Embeddings {
112
constructor(fields?: BedrockEmbeddingsParams);
113
embedQuery(document: string): Promise<number[]>;
114
embedDocuments(documents: string[]): Promise<number[][]>;
115
}
116
117
interface BedrockEmbeddingsParams extends EmbeddingsParams {
118
model?: string;
119
client?: BedrockRuntimeClient;
120
clientOptions?: BedrockRuntimeClientConfig;
121
region?: string;
122
credentials?: CredentialType;
123
}
124
```
125
126
[Embeddings](./embeddings.md)
127
128
### Document Retrieval
129
130
Retrieve relevant documents from AWS services for RAG applications and knowledge search workflows.
131
132
```typescript { .api }
133
class AmazonKendraRetriever extends BaseRetriever {
134
constructor(args: AmazonKendraRetrieverArgs);
135
getRelevantDocuments(query: string): Promise<Document[]>;
136
}
137
138
class AmazonKnowledgeBaseRetriever extends BaseRetriever {
139
constructor(args: AmazonKnowledgeBaseRetrieverArgs);
140
getRelevantDocuments(query: string): Promise<Document[]>;
141
}
142
143
interface AmazonKendraRetrieverArgs {
144
indexId: string;
145
topK: number;
146
region: string;
147
attributeFilter?: AttributeFilter;
148
clientOptions?: KendraClientConfig;
149
}
150
151
interface AmazonKnowledgeBaseRetrieverArgs {
152
knowledgeBaseId: string;
153
topK: number;
154
region: string;
155
clientOptions?: BedrockAgentRuntimeClientConfig;
156
filter?: RetrievalFilter;
157
overrideSearchType?: SearchType;
158
}
159
```
160
161
[Retrievers](./retrievers.md)
162
163
### Authentication & Configuration
164
165
Flexible authentication mechanisms supporting multiple AWS credential types and custom client configurations.
166
167
```typescript { .api }
168
type CredentialType = AwsCredentialIdentity | Provider<AwsCredentialIdentity>;
169
170
type ConverseCommandParams = ConstructorParameters<typeof ConverseCommand>[0];
171
172
type BedrockToolChoice = ToolChoice.AnyMember | ToolChoice.AutoMember | ToolChoice.ToolMember;
173
174
type ChatBedrockConverseToolType = BindToolsInput | BedrockTool;
175
176
type BedrockConverseToolChoice = "auto" | "any" | string | BedrockToolChoice;
177
```
178
179
[Authentication](./authentication.md)
180
181
## Types
182
183
```typescript { .api }
184
interface ChatBedrockConverseCallOptions extends BaseChatModelCallOptions {
185
stop?: string[];
186
tools?: ChatBedrockConverseToolType[];
187
tool_choice?: BedrockConverseToolChoice;
188
additionalModelRequestFields?: __DocumentType;
189
streamUsage?: boolean;
190
guardrailConfig?: GuardrailConfiguration;
191
performanceConfig?: PerformanceConfiguration;
192
}
193
194
type MessageContentReasoningBlock =
195
| MessageContentReasoningBlockReasoningText
196
| MessageContentReasoningBlockRedacted
197
| MessageContentReasoningBlockReasoningTextPartial;
198
199
interface MessageContentReasoningBlockReasoningText {
200
type: "reasoning_content";
201
reasoningText: {
202
text: string;
203
signature: string;
204
};
205
}
206
207
interface MessageContentReasoningBlockRedacted {
208
type: "reasoning_content";
209
redactedContent: string;
210
}
211
212
interface MessageContentReasoningBlockReasoningTextPartial {
213
type: "reasoning_content";
214
reasoningText: { text: string } | { signature: string };
215
}
216
```