AWS code-library documentation change
Summary
Modified code examples by removing dirname import and adjusting __filename declaration order
Security assessment
Code structure changes related to file path resolution show no security implications. These are standard code maintenance adjustments without security context.
Diff
diff --git a/code-library/latest/ug/javascript_3_bedrock-runtime_code_examples.md b/code-library/latest/ug/javascript_3_bedrock-runtime_code_examples.md index a38735b9a..209c787a5 100644 --- a//code-library/latest/ug/javascript_3_bedrock-runtime_code_examples.md +++ b//code-library/latest/ug/javascript_3_bedrock-runtime_code_examples.md @@ -5 +5 @@ -ScenariosAmazon NovaAmazon Nova CanvasAmazon Titan TextAnthropic ClaudeCohere CommandMeta LlamaMistral AI +ScenariosAmazon NovaAmazon Nova CanvasAnthropic ClaudeCohere CommandMeta LlamaMistral AI @@ -117,2 +116,0 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - * Amazon Titan Text - @@ -259,2 +256,0 @@ The primary execution of the scenario flow. This scenario orchestrates the conve - import { dirname } from "node:path"; - const __filename = fileURLToPath(import.meta.url); @@ -263,0 +260,2 @@ The primary execution of the scenario flow. This scenario orchestrates the conve + const __filename = fileURLToPath(import.meta.url); + @@ -435,0 +434 @@ The primary execution of the scenario flow. This scenario orchestrates the conve + default: "", @@ -955,2 +953,0 @@ The primary execution of the scenario flow. This scenario orchestrates the conve - import { dirname } from "node:path"; - const __filename = fileURLToPath(import.meta.url); @@ -959,0 +957,2 @@ The primary execution of the scenario flow. This scenario orchestrates the conve + const __filename = fileURLToPath(import.meta.url); + @@ -1131,0 +1131 @@ The primary execution of the scenario flow. This scenario orchestrates the conve + default: "", @@ -1362,217 +1361,0 @@ Create an image with Amazon Nova Canvas. -## Amazon Titan Text - -The following code example shows how to send a text message to Amazon Titan Text, using Bedrock's Converse API. - -**SDK for JavaScript (v3)** - - -###### Note - -There's more on GitHub. Find the complete example and learn how to set up and run in the [AWS Code Examples Repository](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javascriptv3/example_code/bedrock-runtime#code-examples). - -Send a text message to Amazon Titan Text, using Bedrock's Converse API. - - - // Use the Conversation API to send a text message to Amazon Titan Text. - - import { - BedrockRuntimeClient, - ConverseCommand, - } from "@aws-sdk/client-bedrock-runtime"; - - // Create a Bedrock Runtime client in the AWS Region you want to use. - const client = new BedrockRuntimeClient({ region: "us-east-1" }); - - // Set the model ID, e.g., Titan Text Premier. - const modelId = "amazon.titan-text-premier-v1:0"; - - // Start a conversation with the user message. - const userMessage = - "Describe the purpose of a 'hello world' program in one line."; - const conversation = [ - { - role: "user", - content: [{ text: userMessage }], - }, - ]; - - // Create a command with the model ID, the message, and a basic configuration. - const command = new ConverseCommand({ - modelId, - messages: conversation, - inferenceConfig: { maxTokens: 512, temperature: 0.5, topP: 0.9 }, - }); - - try { - // Send the command to the model and wait for the response - const response = await client.send(command); - - // Extract and print the response text. - const responseText = response.output.message.content[0].text; - console.log(responseText); - } catch (err) { - console.log(`ERROR: Can't invoke '${modelId}'. Reason: ${err}`); - process.exit(1); - } - - - - - * For API details, see [Converse](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/bedrock-runtime/command/ConverseCommand) in _AWS SDK for JavaScript API Reference_. - - - - -The following code example shows how to send a text message to Amazon Titan Text, using Bedrock's Converse API and process the response stream in real-time. - -**SDK for JavaScript (v3)** - - -###### Note - -There's more on GitHub. Find the complete example and learn how to set up and run in the [AWS Code Examples Repository](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javascriptv3/example_code/bedrock-runtime#code-examples). - -Send a text message to Amazon Titan Text, using Bedrock's Converse API and process the response stream in real-time. - - - // Use the Conversation API to send a text message to Amazon Titan Text. - - import { - BedrockRuntimeClient, - ConverseStreamCommand, - } from "@aws-sdk/client-bedrock-runtime"; - - // Create a Bedrock Runtime client in the AWS Region you want to use. - const client = new BedrockRuntimeClient({ region: "us-east-1" }); - - // Set the model ID, e.g., Titan Text Premier. - const modelId = "amazon.titan-text-premier-v1:0"; - - // Start a conversation with the user message. - const userMessage = - "Describe the purpose of a 'hello world' program in one line."; - const conversation = [ - { - role: "user", - content: [{ text: userMessage }], - }, - ]; - - // Create a command with the model ID, the message, and a basic configuration. - const command = new ConverseStreamCommand({ - modelId, - messages: conversation, - inferenceConfig: { maxTokens: 512, temperature: 0.5, topP: 0.9 }, - }); - - try { - // Send the command to the model and wait for the response - const response = await client.send(command); - - // Extract and print the streamed response text in real-time. - for await (const item of response.stream) { - if (item.contentBlockDelta) { - process.stdout.write(item.contentBlockDelta.delta?.text); - } - } - } catch (err) { - console.log(`ERROR: Can't invoke '${modelId}'. Reason: ${err}`); - process.exit(1); - } - - - - - * For API details, see [ConverseStream](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/bedrock-runtime/command/ConverseStreamCommand) in _AWS SDK for JavaScript API Reference_. - - - - -The following code example shows how to send a text message to Amazon Titan Text, using the Invoke Model API. - -**SDK for JavaScript (v3)** - - -###### Note - -There's more on GitHub. Find the complete example and learn how to set up and run in the [AWS Code Examples Repository](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javascriptv3/example_code/bedrock-runtime#code-examples). - -Use the Invoke Model API to send a text message. - - - import { fileURLToPath } from "node:url"; - - import { FoundationModels } from "../../config/foundation_models.js"; - import { - BedrockRuntimeClient, - InvokeModelCommand, - } from "@aws-sdk/client-bedrock-runtime"; - - /** - * @typedef {Object} ResponseBody - * @property {Object[]} results - */ - - /** - * Invokes an Amazon Titan Text generation model. - * - * @param {string} prompt - The input text prompt for the model to complete. - * @param {string} [modelId] - The ID of the model to use. Defaults to "amazon.titan-text-express-v1". - */ - export const invokeModel = async ( - prompt, - modelId = "amazon.titan-text-express-v1", - ) => { - // Create a new Bedrock Runtime client instance. - const client = new BedrockRuntimeClient({ region: "us-east-1" }); - - // Prepare the payload for the model. - const payload = { - inputText: prompt, - textGenerationConfig: { - maxTokenCount: 4096, - stopSequences: [],