### Install Dependencies Source: https://github.com/jparkerweb/bedrock-wrapper/blob/main/AGENTS.md Installs project dependencies. Run this command after cloning the repository. ```bash npm install ``` -------------------------------- ### Install bedrock-wrapper Source: https://github.com/jparkerweb/bedrock-wrapper/blob/main/_autodocs/README.md Install the bedrock-wrapper package using npm. ```bash npm install bedrock-wrapper ``` -------------------------------- ### Converse API Request Example (Kimi-K2) Source: https://github.com/jparkerweb/bedrock-wrapper/blob/main/test-final-output.txt This is an example of a request sent to the Converse API for Kimi-K2. ```json { "modelId": "moonshot.kimi-k2-thinking", "messages": [ { "role": "user", "content": [ { "text": "Respond with exactly one word: What is 1+1?" } ] } ], "inferenceConfig": { "maxTokens": 800, "temperature": 0.2 } } ``` -------------------------------- ### Example AWS Credentials Configuration Source: https://github.com/jparkerweb/bedrock-wrapper/blob/main/_autodocs/types.md Provides an example of how to configure AWS credentials using environment variables. This object is used as the first parameter to bedrockWrapper(). ```javascript const awsCreds = { region: "us-west-2", accessKeyId: process.env.AWS_ACCESS_KEY_ID, secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY, }; ``` -------------------------------- ### Complete Chat Application Example Source: https://github.com/jparkerweb/bedrock-wrapper/blob/main/_autodocs/README.md This example demonstrates how to build a complete chat application using the bedrock-wrapper. It includes setting up AWS credentials, defining a chat function that handles user messages, streaming responses, and querying available models. ```javascript import dotenv from 'dotenv'; import { bedrockWrapper, listBedrockWrapperSupportedModels } from 'bedrock-wrapper'; dotenv.config(); const awsCreds = { region: process.env.AWS_REGION, accessKeyId: process.env.AWS_ACCESS_KEY_ID, secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY, }; async function chat(userMessage) { const request = { messages: [ { role: "system", content: "You are a helpful assistant." }, { role: "user", content: userMessage } ], model: "Claude-3-5-Sonnet", max_tokens: 500, stream: true, temperature: 0.7, }; console.log("Assistant: "); let response = ""; try { for await (const chunk of bedrockWrapper(awsCreds, request, { useConverseAPI: true })) { response += chunk; process.stdout.write(chunk); } console.log("\n"); return response; } catch (error) { console.error("Error:", error.message); throw error; } } // Usage const response = await chat("What is artificial intelligence?"); console.log("Complete response:", response); // Query models const models = await listBedrockWrapperSupportedModels(); console.log(`Available: ${models.length} models`); ``` -------------------------------- ### Environment Setup Source: https://github.com/jparkerweb/bedrock-wrapper/blob/main/AGENTS.md Configuration for AWS region, credentials, and LLM parameters. Create a .env file with these variables. ```dotenv AWS_REGION=us-west-2 AWS_ACCESS_KEY_ID=your_key AWS_SECRET_ACCESS_KEY=your_secret LLM_MAX_GEN_TOKENS=1024 LLM_TEMPERATURE=0.2 ``` -------------------------------- ### Clean Reinstall Source: https://github.com/jparkerweb/bedrock-wrapper/blob/main/AGENTS.md Cleans the project by removing node_modules and package-lock.json, followed by a fresh install. ```bash npm run clean ``` -------------------------------- ### Invoke API Response Example (DeepSeek V3.1) Source: https://github.com/jparkerweb/bedrock-wrapper/blob/main/test-run-output.txt This is an example of a non-streaming response from the Invoke API for the DeepSeek V3.1 model, showing the assistant's text output. ```json { "output": { "message": { "role": "assistant", "content": [ { "text": "two" } ] } }, "stopReason": "end_turn", "usage": { "inputTokens": 17, "outputTokens": 2, "totalTokens": 19 }, "metrics": { "latencyMs": 306 }, "$metadata": { "httpStatusCode": 200, "requestId": "b7addef3-7683-46ca-b69d-568d7cdbff8e", "attempts": 1, "totalRetryDelay": 0 } } ``` -------------------------------- ### With Thinking Mode (Claude) Source: https://github.com/jparkerweb/bedrock-wrapper/blob/main/_autodocs/api-reference/bedrockWrapper.md Illustrates how to enable and utilize the 'thinking' mode for Claude models to get reasoning steps. ```APIDOC ## With Thinking Mode (Claude) ### Description This example enables the 'thinking' mode for Claude models, which includes reasoning steps in the output, typically enclosed in `` tags. ### Method `bedrockWrapper(awsCreds, request)` ### Parameters - **awsCreds** (object) - AWS credentials and region. - **request** (object) - Configuration for the model request, including `model` set to a thinking-enabled model, `temperature` (required), and `include_thinking_data: true`. ### Request Example ```javascript const request = { messages: [ { role: "user", content: "Solve: What's 2^100 in scientific notation?" } ], model: "Claude-4-5-Sonnet-Thinking", max_tokens: 4000, stream: true, temperature: 1.0, // Required for thinking include_thinking_data: true }; for await (const chunk of bedrockWrapper(awsCreds, request)) { process.stdout.write(chunk); } // Output: "...reasoning...\n\n" ``` ### Response - **chunk** (string) - Streaming response chunks, potentially including reasoning tags and the final response. ``` -------------------------------- ### Invoke API Request for GPT-OSS-20B Source: https://github.com/jparkerweb/bedrock-wrapper/blob/main/test-run-output.txt Example of a request sent to the Invoke API for the GPT-OSS-20B model, including formatted prompts. ```javascript Final formatted prompt: [ { role: 'user', content: 'Respond with exactly one word: What is 1+1?' }, { role: 'assistant', content: '' } ] Final request: { "messages": [ { "role": "user", "content": "Respond with exactly one word: What is 1+1?" }, { "role": "assistant", "content": "" } ], "max_completion_tokens": 800, "temperature": 0.2 } ``` -------------------------------- ### Converse API Response Example (Claude-3-Haiku) Source: https://github.com/jparkerweb/bedrock-wrapper/blob/main/test-converse-output.txt This is an example of a successful Converse API response for Claude-3-Haiku. ```json { "output": { "message": { "role": "assistant", "content": [ { "text": "Two." } ] } }, "stopReason": "end_turn", "usage": { "inputTokens": 21, "outputTokens": 4, "totalTokens": 25 }, "metrics": { "latencyMs": 654 }, "$metadata": { "httpStatusCode": 200, "requestId": "1ad9346f-baa6-4ade-9f33-3245795f4cb8", "attempts": 1, "totalRetryDelay": 0 } } ``` -------------------------------- ### Converse API Response Example (Claude-3-5-Haiku) Source: https://github.com/jparkerweb/bedrock-wrapper/blob/main/test-converse-output.txt This is an example of a successful Converse API response for Claude-3-5-Haiku. ```json { "output": { "message": { "role": "assistant", "content": [ { "text": "Two" } ] } }, "stopReason": "end_turn", "usage": { "inputTokens": 21, "outputTokens": 4, "totalTokens": 25, "cacheReadInputTokens": 0, "cacheWriteInputTokens": 0 }, "metrics": { "latencyMs": 654 }, "$metadata": { "httpStatusCode": 200, "requestId": "1ad9346f-baa6-4ade-9f33-3245795f4cb8", "attempts": 1, "totalRetryDelay": 0 } } ``` -------------------------------- ### BedrockWrapper Options Example Source: https://github.com/jparkerweb/bedrock-wrapper/blob/main/_autodocs/types.md Illustrates how to instantiate the bedrockWrapper with custom options, such as enabling logging and specifying the use of the Converse API. ```javascript const options = { logging: true, // See API requests/responses useConverseAPI: true // Use unified Converse API }; for await (const chunk of bedrockWrapper(awsCreds, request, options)) { console.log(chunk); } ``` -------------------------------- ### Invoke API Request Example Source: https://github.com/jparkerweb/bedrock-wrapper/blob/main/test-converse-output.txt This is an example of a request sent to the Invoke API for text generation. ```json { "messages": [ { "role": "user", "content": "Respond with exactly one word: What is 1+1?" }, { "role": "assistant", "content": "" } ], "max_tokens": 800, "temperature": 0.2, "anthropic_version": "bedrock-2023-05-31" } ``` -------------------------------- ### Converse API Response Example (Gemma-3-27b) Source: https://github.com/jparkerweb/bedrock-wrapper/blob/main/test-final-output.txt This is an example of a successful non-streaming response from the Converse API for Gemma-3-27b. ```json { "output": { "message": { "role": "assistant", "content": [ { "text": "Two." } ] } }, "stopReason": "end_turn", "usage": { "inputTokens": 22, "outputTokens": 3, "totalTokens": 25 }, "metrics": { "latencyMs": 230 }, "$metadata": { "httpStatusCode": 200, "requestId": "3c9153ea-d63b-4ee3-899f-0514757f6b11", "attempts": 1, "totalRetryDelay": 0 } } ``` -------------------------------- ### Complete Bedrock Wrapper Configuration and Execution Source: https://github.com/jparkerweb/bedrock-wrapper/blob/main/_autodocs/configuration.md This snippet demonstrates setting up AWS credentials, defining a multi-modal request (text and image), configuring wrapper options for streaming, and executing the request using the bedrockWrapper function. It includes error handling and logs the complete response. ```javascript import dotenv from 'dotenv'; import { bedrockWrapper } from 'bedrock-wrapper'; dotenv.config(); // AWS credentials const awsCreds = { region: process.env.AWS_REGION, accessKeyId: process.env.AWS_ACCESS_KEY_ID, secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY, }; // Request configuration const request = { messages: [ { role: "system", content: "You are a helpful assistant." }, { role: "user", content: [ { type: "text", text: "Analyze this image:" }, { type: "image_url", image_url: { url: "https://example.com/photo.jpg" } } ] } ], model: "Claude-3-5-Sonnet", max_tokens: 1024, stream: true, temperature: 0.7, stop_sequences: ["END"], }; // Wrapper options const options = { logging: false, // Disable in production useConverseAPI: true // Use Converse API }; // Execute request try { let response = ""; for await (const chunk of bedrockWrapper(awsCreds, request, options)) { response += chunk; process.stdout.write(chunk); } console.log("\nāœ… Complete response:", response); } catch (error) { console.error("āŒ Error:", error.message); } ``` -------------------------------- ### Converse API Request Example (Ministral-3-14b) Source: https://github.com/jparkerweb/bedrock-wrapper/blob/main/test-converse-output.txt Example of a request payload for the Converse API for the Ministral-3-14b model. ```json { "modelId": "mistral.ministral-3-14b-instruct", "messages": [ { "role": "user", "content": [ { "text": "Respond with exactly one word: What is 1+1?" } ] } ], "inferenceConfig": { "maxTokens": 800, "temperature": 0.2 } } ``` -------------------------------- ### Environment Variables Example Source: https://github.com/jparkerweb/bedrock-wrapper/blob/main/_autodocs/types.md Lists essential environment variables required for AWS authentication and optional variables for LLM generation parameters. These are typically loaded using a `.env` file. ```bash AWS_REGION=us-west-2 AWS_ACCESS_KEY_ID=your_access_key AWS_SECRET_ACCESS_KEY=your_secret_key LLM_MAX_GEN_TOKENS=1024 LLM_TEMPERATURE=0.7 ``` -------------------------------- ### Converse API Response Example Source: https://github.com/jparkerweb/bedrock-wrapper/blob/main/test-converse-output.txt This is an example of a successful Converse API response for a text generation task. ```json { "output": { "message": { "role": "assistant", "content": [ { "text": "Two" } ] } }, "stopReason": "end_turn", "usage": { "inputTokens": 21, "outputTokens": 4, "totalTokens": 25 }, "metrics": { "latencyMs": 597 }, "$metadata": { "httpStatusCode": 200, "requestId": "2ad19370-0f4c-4c28-93ea-b789541e1220", "attempts": 1, "totalRetryDelay": 0 } } ``` -------------------------------- ### Run All Test Suites Source: https://github.com/jparkerweb/bedrock-wrapper/blob/main/README.md Execute all available test suites within the package. ```bash npm run test:all ``` -------------------------------- ### Set up Environment Variables for AWS Credentials Source: https://github.com/jparkerweb/bedrock-wrapper/blob/main/_autodocs/configuration.md Create a .env file in your project root to store AWS credentials and optional model parameters. Load these variables using the dotenv package. ```bash # AWS Credentials (required) AWS_REGION=us-west-2 AWS_ACCESS_KEY_ID=your_access_key_id AWS_SECRET_ACCESS_KEY=your_access_key_id # Optional: Common model parameters (not used by library, but useful for examples) LLM_MAX_GEN_TOKENS=1024 LLM_TEMPERATURE=0.7 ``` -------------------------------- ### Converse API Response Example (Ministral-3-14b) Source: https://github.com/jparkerweb/bedrock-wrapper/blob/main/test-converse-output.txt Example of a successful non-streaming response from the Converse API for the Ministral-3-14b model. ```json { "output": { "message": { "role": "assistant", "content": [ { "text": "Two." } ] } }, "stopReason": "end_turn", "usage": { "inputTokens": 17, "outputTokens": 3, "totalTokens": 20 }, "metrics": { "latencyMs": 224 }, "$metadata": { "httpStatusCode": 200, "requestId": "dc28cf9c-8bfd-4749-8900-f7dc1f05a710", "attempts": 1, "totalRetryDelay": 0 } } ``` -------------------------------- ### CLI Usage for Supported Models Source: https://github.com/jparkerweb/bedrock-wrapper/blob/main/_autodocs/api-reference/listBedrockWrapperSupportedModels.md Shows how to use the function in a command-line interface context, outputting the parsed model objects in a pretty-printed JSON format. ```javascript import { listBedrockWrapperSupportedModels } from "bedrock-wrapper"; const models = await listBedrockWrapperSupportedModels(); console.log(JSON.stringify(models.map(m => JSON.parse(m)), null, 2)); ``` -------------------------------- ### Converse API Response Example (Mistral-3-8b) Source: https://github.com/jparkerweb/bedrock-wrapper/blob/main/test-converse-output.txt Example of a successful non-streaming response from the Converse API for the Mistral-3-8b model. ```json { "output": { "message": { "role": "assistant", "content": [ { "text": "Two." } ] } }, "stopReason": "end_turn", "usage": { "inputTokens": 17, "outputTokens": 3, "totalTokens": 20 }, "metrics": { "latencyMs": 197 }, "$metadata": { "httpStatusCode": 200, "requestId": "7eca7cf4-d5a7-4135-b152-8542f933d29a", "attempts": 1, "totalRetryDelay": 0 } } ``` -------------------------------- ### Converse API Request Example Source: https://github.com/jparkerweb/bedrock-wrapper/blob/main/test-converse-output.txt Example of a request payload for the Converse API, specifying model, messages, and inference configuration. ```json { "modelId": "mistral.ministral-3-8b-instruct", "messages": [ { "role": "user", "content": [ { "text": "Respond with exactly one word: What is 1+1?" } ] } ], "inferenceConfig": { "maxTokens": 800, "temperature": 0.2 } } ``` -------------------------------- ### Example Message with Images Source: https://github.com/jparkerweb/bedrock-wrapper/blob/main/_autodocs/types.md Demonstrates how to include both text and image content within a user message. Supports both base64 encoded images and image URLs. ```javascript { role: "user", content: [ { type: "text", text: "Analyze these images:" }, { type: "image_url", image_url: { url: "data:image/jpeg;base64,/9j/4AAQSkZJRgA..." } }, { type: "image_url", image_url: { url: "https://example.com/photo.jpg" } } ] } ``` -------------------------------- ### Converse API Request Example (Claude-3-Haiku) Source: https://github.com/jparkerweb/bedrock-wrapper/blob/main/test-converse-output.txt This is an example of a request sent to the Converse API for text generation using Claude-3-Haiku. ```json { "modelId": "us.anthropic.claude-3-haiku-20240307-v1:0", "messages": [ { "role": "user", "content": [ { "text": "Respond with exactly one word: What is 1+1?" } ] } ], "inferenceConfig": { "maxTokens": 800, "temperature": 0.2 } } ``` -------------------------------- ### Converse API Request Example (Claude-3-5-Haiku) Source: https://github.com/jparkerweb/bedrock-wrapper/blob/main/test-converse-output.txt This is an example of a request sent to the Converse API for text generation using Claude-3-5-Haiku. ```json { "modelId": "us.anthropic.claude-3-5-haiku-20241022-v1:0", "messages": [ { "role": "user", "content": [ { "text": "Respond with exactly one word: What is 1+1?" } ] } ], "inferenceConfig": { "maxTokens": 800, "temperature": 0.2 } } ``` -------------------------------- ### Basic Usage of listBedrockWrapperSupportedModels Source: https://github.com/jparkerweb/bedrock-wrapper/blob/main/_autodocs/api-reference/listBedrockWrapperSupportedModels.md Demonstrates how to call the function and log the total number of supported models. It also shows how to parse each stringified model entry to access its name and ID. ```javascript import { listBedrockWrapperSupportedModels } from "bedrock-wrapper"; const models = await listBedrockWrapperSupportedModels(); console.log(`${models.length} models supported`); // Parse each stringified entry models.forEach(modelStr => { const model = JSON.parse(modelStr); console.log(`${model.modelName} → ${model.modelId}`); }); ``` -------------------------------- ### Configure AWS Credentials Source: https://github.com/jparkerweb/bedrock-wrapper/blob/main/_autodocs/README.md Set up AWS credentials and region using a .env file and load them with dotenv. ```bash AWS_REGION=us-west-2 AWS_ACCESS_KEY_ID=your_key AWS_SECRET_ACCESS_KEY=your_secret ``` ```javascript import dotenv from 'dotenv'; dotenv.config(); ``` -------------------------------- ### Configure OpenAI Chat Completions Object Source: https://github.com/jparkerweb/bedrock-wrapper/blob/main/README.md Prepare an object for chat completions, mirroring OpenAI's format. Specify model, max tokens, streaming, and temperature. Optional stop sequences can be provided. ```javascript const openaiChatCompletionsCreateObject = { "messages": messages, "model": "Claude-4-5-Sonnet", "max_tokens": LLM_MAX_GEN_TOKENS, "stream": true, "temperature": LLM_TEMPERATURE, "stop_sequences": ["STOP", "END"], // Optional: sequences that will stop generation }; ``` -------------------------------- ### With Vision (Image Analysis) Source: https://github.com/jparkerweb/bedrock-wrapper/blob/main/_autodocs/api-reference/bedrockWrapper.md Demonstrates how to use bedrockWrapper for image analysis by providing an image URL in the request. ```APIDOC ## With Vision (Image Analysis) ### Description This example shows how to perform image analysis by including an image URL within the `messages` content. ### Method `bedrockWrapper(awsCreds, request)` ### Parameters - **awsCreds** (object) - AWS credentials and region. - **request** (object) - Configuration for the model request, including a `content` array with `type: "image_url"`. ### Request Example ```javascript const request = { messages: [ { role: "user", content: [ { type: "text", text: "Describe this image:" }, { type: "image_url", image_url: { url: "https://example.com/image.jpg" } } ] } ], model: "Claude-3-5-Sonnet", // Must have vision: true max_tokens: 500, stream: true, temperature: 0.5, }; for await (const chunk of bedrockWrapper(awsCreds, request)) { process.stdout.write(chunk); } ``` ### Response - **chunk** (string) - Streaming response chunks containing the image description. ``` -------------------------------- ### Invoke API Request Example (DeepSeek V3.1) Source: https://github.com/jparkerweb/bedrock-wrapper/blob/main/test-run-output.txt This is an example of a request sent to the Invoke API for the DeepSeek V3.1 model, similar to the Converse API request structure. ```json { "modelId": "deepseek.v3-v1:0", "messages": [ { "role": "user", "content": [ { "text": "Respond with exactly one word: What is 1+1?" } ] } ], "inferenceConfig": { "maxTokens": 800, "temperature": 0.2 } } ``` -------------------------------- ### Basic Streaming with Claude Source: https://github.com/jparkerweb/bedrock-wrapper/blob/main/_autodocs/api-reference/bedrockWrapper.md Demonstrates a basic streaming request to a Claude model using bedrockWrapper. ```APIDOC ## Basic Streaming with Claude ### Description This example shows how to initiate a streaming request to a Claude model. ### Method `bedrockWrapper(awsCreds, request)` ### Parameters - **awsCreds** (object) - AWS credentials and region. - **request** (object) - Configuration for the model request, including messages, model name, max tokens, and stream: true. ### Request Example ```javascript import { bedrockWrapper } from "bedrock-wrapper"; const awsCreds = { region: "us-west-2", accessKeyId: process.env.AWS_ACCESS_KEY_ID, secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY, }; const request = { messages: [ { role: "system", content: "You are a helpful assistant." }, { role: "user", content: "Explain quantum computing in 2 sentences." } ], model: "Claude-3-5-Sonnet", max_tokens: 200, stream: true, temperature: 0.7, }; let response = ""; for await (const chunk of bedrockWrapper(awsCreds, request)) { response += chunk; process.stdout.write(chunk); } console.log("\nComplete:", response); ``` ### Response - **chunk** (string) - Streaming response chunks from the model. ```