LangChain AWS integration providing chat models, embeddings, and retrievers for seamless AWS service connections.
npx @tessl/cli install tessl/npm-langchain--aws@0.1.0@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.
npm install @langchain/awsimport {
ChatBedrockConverse,
BedrockEmbeddings,
AmazonKendraRetriever,
AmazonKnowledgeBaseRetriever
} from "@langchain/aws";For CommonJS:
const {
ChatBedrockConverse,
BedrockEmbeddings,
AmazonKendraRetriever,
AmazonKnowledgeBaseRetriever
} = require("@langchain/aws");import { ChatBedrockConverse } from "@langchain/aws";
import { HumanMessage } from "@langchain/core/messages";
// Initialize chat model with AWS credentials
const model = new ChatBedrockConverse({
region: process.env.BEDROCK_AWS_REGION ?? "us-east-1",
credentials: {
secretAccessKey: process.env.BEDROCK_AWS_SECRET_ACCESS_KEY,
accessKeyId: process.env.BEDROCK_AWS_ACCESS_KEY_ID,
},
model: "anthropic.claude-3-5-sonnet-20240620-v1:0",
temperature: 0.7,
maxTokens: 1000
});
// Generate response
const response = await model.invoke([
new HumanMessage("What is the capital of France?")
]);
console.log(response.content);@langchain/aws is built around several key components:
ChatBedrockConverse class providing both streaming and non-streaming interfaces with full AWS Bedrock Converse API supportBedrockEmbeddings for generating text embeddings using Amazon Titan and other Bedrock embedding modelsAmazonKendraRetriever for intelligent search and AmazonKnowledgeBaseRetriever for RAG workflowsAdvanced conversational AI capabilities using AWS Bedrock's Converse API with support for streaming, function calling, multimodal input, and structured output.
class ChatBedrockConverse extends BaseChatModel {
constructor(fields?: ChatBedrockConverseInput);
invoke(messages: BaseMessage[], options?: ChatBedrockConverseCallOptions): Promise<AIMessage>;
stream(messages: BaseMessage[], options?: ChatBedrockConverseCallOptions): AsyncGenerator<AIMessageChunk>;
bindTools(tools: ChatBedrockConverseToolType[], kwargs?: Partial<ChatBedrockConverseCallOptions>): Runnable;
withStructuredOutput<T>(outputSchema: InteropZodType<T> | Record<string, any>, config?: StructuredOutputMethodOptions): Runnable<BaseLanguageModelInput, T>;
}
interface ChatBedrockConverseInput extends BaseChatModelParams {
client?: BedrockRuntimeClient;
clientOptions?: BedrockRuntimeClientConfig;
streaming?: boolean;
model?: string;
region?: string;
credentials?: CredentialType;
temperature?: number;
maxTokens?: number;
endpointHost?: string;
topP?: number;
additionalModelRequestFields?: __DocumentType;
streamUsage?: boolean;
guardrailConfig?: GuardrailConfiguration;
performanceConfig?: PerformanceConfiguration;
supportsToolChoiceValues?: Array<"auto" | "any" | "tool">;
}Generate high-quality text embeddings using AWS Bedrock embedding models with automatic batching and text preprocessing.
class BedrockEmbeddings extends Embeddings {
constructor(fields?: BedrockEmbeddingsParams);
embedQuery(document: string): Promise<number[]>;
embedDocuments(documents: string[]): Promise<number[][]>;
}
interface BedrockEmbeddingsParams extends EmbeddingsParams {
model?: string;
client?: BedrockRuntimeClient;
clientOptions?: BedrockRuntimeClientConfig;
region?: string;
credentials?: CredentialType;
}Retrieve relevant documents from AWS services for RAG applications and knowledge search workflows.
class AmazonKendraRetriever extends BaseRetriever {
constructor(args: AmazonKendraRetrieverArgs);
getRelevantDocuments(query: string): Promise<Document[]>;
}
class AmazonKnowledgeBaseRetriever extends BaseRetriever {
constructor(args: AmazonKnowledgeBaseRetrieverArgs);
getRelevantDocuments(query: string): Promise<Document[]>;
}
interface AmazonKendraRetrieverArgs {
indexId: string;
topK: number;
region: string;
attributeFilter?: AttributeFilter;
clientOptions?: KendraClientConfig;
}
interface AmazonKnowledgeBaseRetrieverArgs {
knowledgeBaseId: string;
topK: number;
region: string;
clientOptions?: BedrockAgentRuntimeClientConfig;
filter?: RetrievalFilter;
overrideSearchType?: SearchType;
}Flexible authentication mechanisms supporting multiple AWS credential types and custom client configurations.
type CredentialType = AwsCredentialIdentity | Provider<AwsCredentialIdentity>;
type ConverseCommandParams = ConstructorParameters<typeof ConverseCommand>[0];
type BedrockToolChoice = ToolChoice.AnyMember | ToolChoice.AutoMember | ToolChoice.ToolMember;
type ChatBedrockConverseToolType = BindToolsInput | BedrockTool;
type BedrockConverseToolChoice = "auto" | "any" | string | BedrockToolChoice;interface ChatBedrockConverseCallOptions extends BaseChatModelCallOptions {
stop?: string[];
tools?: ChatBedrockConverseToolType[];
tool_choice?: BedrockConverseToolChoice;
additionalModelRequestFields?: __DocumentType;
streamUsage?: boolean;
guardrailConfig?: GuardrailConfiguration;
performanceConfig?: PerformanceConfiguration;
}
type MessageContentReasoningBlock =
| MessageContentReasoningBlockReasoningText
| MessageContentReasoningBlockRedacted
| MessageContentReasoningBlockReasoningTextPartial;
interface MessageContentReasoningBlockReasoningText {
type: "reasoning_content";
reasoningText: {
text: string;
signature: string;
};
}
interface MessageContentReasoningBlockRedacted {
type: "reasoning_content";
redactedContent: string;
}
interface MessageContentReasoningBlockReasoningTextPartial {
type: "reasoning_content";
reasoningText: { text: string } | { signature: string };
}