CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-langchain--aws

LangChain AWS integration providing chat models, embeddings, and retrievers for seamless AWS service connections.

Overview
Eval results
Files

embeddings.mddocs/

Text Embeddings

Generate high-quality text embeddings using AWS Bedrock embedding models with automatic batching, text preprocessing, and comprehensive error handling for vector search and similarity applications.

Capabilities

BedrockEmbeddings Class

Primary embeddings class for generating text embeddings using AWS Bedrock embedding models, extending LangChain's Embeddings base class.

/**
 * Generate text embeddings using AWS Bedrock embedding models
 */
class BedrockEmbeddings extends Embeddings implements BedrockEmbeddingsParams {
  constructor(fields?: BedrockEmbeddingsParams);
  
  /** Generate embedding for a single document/query */
  embedQuery(document: string): Promise<number[]>;
  
  /** Generate embeddings for multiple documents in parallel */
  embedDocuments(documents: string[]): Promise<number[][]>;
  
  /** Internal method for embedding text with retry logic */
  protected _embedText(text: string): Promise<number[]>;
}

Usage Examples:

import { BedrockEmbeddings } from "@langchain/aws";

// Basic initialization
const embeddings = new BedrockEmbeddings({
  region: "us-east-1",
  model: "amazon.titan-embed-text-v1",
  credentials: {
    accessKeyId: process.env.AWS_ACCESS_KEY_ID,
    secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
  }
});

// Embed a single query
const queryEmbedding = await embeddings.embedQuery(
  "What is the capital of France?"
);
console.log(queryEmbedding.length); // 1536 for Titan v1

// Embed multiple documents
const documents = [
  "Paris is the capital of France.",
  "London is the capital of England.",
  "Berlin is the capital of Germany."
];

const documentEmbeddings = await embeddings.embedDocuments(documents);
console.log(documentEmbeddings.length); // 3
console.log(documentEmbeddings[0].length); // 1536 for each embedding

Constructor Configuration

Configuration options for initializing BedrockEmbeddings instances.

interface BedrockEmbeddingsParams extends EmbeddingsParams {
  /** Embedding model to use (default: "amazon.titan-embed-text-v1") */
  model?: string;
  
  /** Custom BedrockRuntimeClient instance */
  client?: BedrockRuntimeClient;
  
  /** Configuration options for BedrockRuntimeClient */
  clientOptions?: BedrockRuntimeClientConfig;
  
  /** AWS region for API calls */
  region?: string;
  
  /** AWS credentials for authentication */
  credentials?: CredentialType;
}

Supported Models

Popular embedding models available through AWS Bedrock:

Amazon Titan Models:

  • amazon.titan-embed-text-v1 - 1536 dimensions, optimized for English text
  • amazon.titan-embed-text-v2:0 - 1024 dimensions, improved multilingual support

Cohere Models:

  • cohere.embed-english-v3 - English-optimized embeddings
  • cohere.embed-multilingual-v3 - Multilingual embeddings

Usage Examples:

// Using Titan v2
const titanV2 = new BedrockEmbeddings({
  region: "us-east-1",
  model: "amazon.titan-embed-text-v2:0"
});

// Using Cohere multilingual
const cohereMulti = new BedrockEmbeddings({
  region: "us-east-1",
  model: "cohere.embed-multilingual-v3"
});

// Test different models
const text = "Machine learning is transforming industries";
const titanEmbedding = await titanV2.embedQuery(text);
const cohereEmbedding = await cohereMulti.embedQuery(text);

console.log("Titan dimensions:", titanEmbedding.length);
console.log("Cohere dimensions:", cohereEmbedding.length);

Advanced Configuration

Custom Client Configuration

import { BedrockRuntimeClient } from "@aws-sdk/client-bedrock-runtime";

// Custom client with specific configuration
const customClient = new BedrockRuntimeClient({
  region: "us-west-2",
  credentials: {
    accessKeyId: "your-access-key",
    secretAccessKey: "your-secret-key"
  },
  maxAttempts: 3,
  requestHandler: {
    connectionTimeout: 5000,
    socketTimeout: 60000
  }
});

const embeddings = new BedrockEmbeddings({
  client: customClient,
  model: "amazon.titan-embed-text-v1"
});

Client Options

// Configure client options without providing custom client
const embeddings = new BedrockEmbeddings({
  region: "us-east-1",
  model: "amazon.titan-embed-text-v1",
  clientOptions: {
    maxAttempts: 5,
    requestHandler: {
      connectionTimeout: 10000,
      socketTimeout: 120000
    }
  }
});

Batch Processing

Automatic batching capabilities for processing large sets of documents efficiently.

Properties:

class BedrockEmbeddings {
  /** Maximum batch size for processing documents (default: 512) */
  batchSize: number;
}

Usage Examples:

// Configure batch size
const embeddings = new BedrockEmbeddings({
  region: "us-east-1",
  model: "amazon.titan-embed-text-v1"
});

embeddings.batchSize = 100; // Process 100 documents at a time

// Process large document set
const largeDocumentSet = Array.from({ length: 1000 }, (_, i) => 
  `Document ${i + 1}: This is sample content for document number ${i + 1}.`
);

// Embeddings will be processed in batches automatically
const embeddings_result = await embeddings.embedDocuments(largeDocumentSet);
console.log(`Processed ${embeddings_result.length} documents`);

Text Preprocessing

Automatic text cleaning and preprocessing for optimal embedding performance.

Features:

  • Newline Replacement: Converts newlines to spaces to improve performance
  • Whitespace Normalization: Handles various whitespace characters
  • Error Handling: Comprehensive error handling with descriptive messages

Usage Examples:

// Text with various formatting will be automatically cleaned
const messyText = `
This is a document
with multiple lines
and    extra    spaces.

It has paragraph breaks too.
`;

const embedding = await embeddings.embedQuery(messyText);
// Text is automatically cleaned: "This is a document with multiple lines and extra spaces. It has paragraph breaks too."

Error Handling

Comprehensive error handling with descriptive error messages and retry logic.

Usage Examples:

try {
  const embedding = await embeddings.embedQuery("Sample text");
} catch (error) {
  if (error.message.includes("credentials")) {
    console.error("Authentication failed - check AWS credentials");
  } else if (error.message.includes("region")) {
    console.error("Invalid region - check AWS region configuration");
  } else if (error.message.includes("model")) {
    console.error("Model not available - check model ID and region support");
  } else {
    console.error("Embedding failed:", error.message);
  }
}

// Handle document batch errors
try {
  const embeddings_result = await embeddings.embedDocuments([
    "Valid document",
    "", // Empty document might cause issues
    "Another valid document"
  ]);
} catch (error) {
  console.error("Batch embedding failed:", error.message);
  // Retry with individual documents to identify problematic ones
}

Vector Search Integration

Common patterns for using embeddings with vector databases and similarity search.

Usage Examples:

// Create embeddings for vector search
const documents = [
  "Artificial intelligence is transforming healthcare",
  "Machine learning models require large datasets",
  "Deep learning uses neural networks with multiple layers",
  "Natural language processing enables computers to understand text"
];

const docEmbeddings = await embeddings.embedDocuments(documents);

// Store in vector database (pseudo-code)
const vectorStore = new VectorDatabase();
documents.forEach((doc, index) => {
  vectorStore.add({
    id: index,
    text: doc,
    embedding: docEmbeddings[index]
  });
});

// Query for similar documents
const query = "How does AI help in medical applications?";
const queryEmbedding = await embeddings.embedQuery(query);

// Find similar documents (pseudo-code)
const similarDocs = vectorStore.similaritySearch(queryEmbedding, { limit: 3 });

Performance Optimization

Best practices for optimal performance with BedrockEmbeddings.

Tips:

  1. Batch Processing: Use embedDocuments() for multiple texts instead of individual embedQuery() calls
  2. Text Length: Keep texts under model limits (typically 8192 tokens for Titan models)
  3. Regional Optimization: Use regions closest to your application for lower latency
  4. Connection Reuse: Reuse BedrockEmbeddings instances to benefit from connection pooling
// Efficient batch processing
const embeddings = new BedrockEmbeddings({
  region: "us-east-1", // Use closest region
  model: "amazon.titan-embed-text-v1"
});

// Process in optimal batch sizes
const batchSize = 50; // Adjust based on your needs
const allDocuments = [...]; // Your document array

const allEmbeddings = [];
for (let i = 0; i < allDocuments.length; i += batchSize) {
  const batch = allDocuments.slice(i, i + batchSize);
  const batchEmbeddings = await embeddings.embedDocuments(batch);
  allEmbeddings.push(...batchEmbeddings);
}

Cost Optimization

Understanding and optimizing costs for Bedrock embedding usage.

Cost Factors:

  • Input Tokens: Charged per 1,000 input tokens
  • Model Type: Different models have different pricing
  • Region: Pricing may vary by AWS region

Optimization Strategies:

// Monitor token usage
const embeddings = new BedrockEmbeddings({
  region: "us-east-1",
  model: "amazon.titan-embed-text-v1"
});

// Estimate tokens (rough approximation: 1 token ≈ 4 characters)
function estimateTokens(text: string): number {
  return Math.ceil(text.length / 4);
}

const documents = ["Document 1", "Document 2", "Document 3"];
const totalTokens = documents.reduce((sum, doc) => sum + estimateTokens(doc), 0);
console.log(`Estimated tokens: ${totalTokens}`);

// Consider text truncation for very long documents
const maxTokens = 8000; // Leave buffer for model limits
const truncatedDocs = documents.map(doc => {
  const estimatedTokens = estimateTokens(doc);
  if (estimatedTokens > maxTokens) {
    // Rough truncation - in production, use proper tokenization
    const maxChars = maxTokens * 4;
    return doc.substring(0, maxChars);
  }
  return doc;
});

const embeddings_result = await embeddings.embedDocuments(truncatedDocs);

Install with Tessl CLI

npx tessl i tessl/npm-langchain--aws

docs

authentication.md

chat-models.md

embeddings.md

index.md

retrievers.md

tile.json