テキスト生成のために Amazon Bedrock で Anthropic Claude Instant モデルを呼び出す - AWS SDK コード例

Doc AWS SDK Examples リポジトリには、他にも SDK の例があります。 AWS GitHub

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

テキスト生成のために Amazon Bedrock で Anthropic Claude Instant モデルを呼び出す

次のコード例は、Amazon Bedrock で Anthropic Claude Instant モデルを呼び出してテキストを生成する方法を示しています。

JavaScript
SDK for JavaScript (v3)
注記

の詳細については、「」を参照してください GitHub。AWS コード例リポジトリ で全く同じ例を見つけて、設定と実行の方法を確認してください。

Anthropic Claude Instant 基盤モデルを呼び出してテキストを生成します。

// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 import { fileURLToPath } from "url"; import { FoundationModels } from "../../config/foundation_models.js"; import { BedrockRuntimeClient, InvokeModelCommand, } from "@aws-sdk/client-bedrock-runtime"; /** * @typedef {Object} Content * @property {string} text * * @typedef {Object} MessageApiResponse * @property {Content[]} content */ /** * @typedef {Object} TextCompletionApiResponse * @property {string} completion */ /** * Invokes Anthropic Claude Instant using the Messages API. * * To learn more about the Anthropic Messages API, go to: * https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-anthropic-claude-messages.html * * @param {string} prompt - The input text prompt for the model to complete. * @param {string} [modelId] - The ID of the model to use. Defaults to "anthropic.claude-instant-v1". */ export const invokeModel = async ( prompt, modelId = "anthropic.claude-instant-v1", ) => { // Create a new Bedrock Runtime client instance. const client = new BedrockRuntimeClient({ region: "us-east-1" }); // Prepare the payload for the Messages API request. const payload = { anthropic_version: "bedrock-2023-05-31", max_tokens: 1000, messages: [ { role: "user", content: [{ type: "text", text: prompt }], }, ], }; // Invoke Claude with the payload and wait for the response. const command = new InvokeModelCommand({ contentType: "application/json", body: JSON.stringify(payload), modelId, }); const apiResponse = await client.send(command); // Decode and return the response(s) const decodedResponseBody = new TextDecoder().decode(apiResponse.body); /** @type {MessageApiResponse} */ const responseBody = JSON.parse(decodedResponseBody); return responseBody.content[0].text; }; /** * Invokes Anthropic Claude Instant using the Text Completions API. * * To learn more about the Anthropic Text Completions API, go to: * https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-anthropic-claude-text-completion.html * * @param {string} prompt - The input text prompt for the model to complete. * @param {string} [modelId] - The ID of the model to use. Defaults to "anthropic.claude-instant-v1". */ export const invokeTextCompletionsApi = async ( prompt, modelId = "anthropic.claude-instant-v1", ) => { // Create a new Bedrock Runtime client instance. const client = new BedrockRuntimeClient({ region: "us-east-1" }); // Prepare the payload for the Text Completions API, using the required prompt template. const enclosedPrompt = `Human: ${prompt}\n\nAssistant:`; const payload = { prompt: enclosedPrompt, max_tokens_to_sample: 500, temperature: 0.5, stop_sequences: ["\n\nHuman:"], }; // Invoke Claude with the payload and wait for the response. const command = new InvokeModelCommand({ contentType: "application/json", body: JSON.stringify(payload), modelId, }); const apiResponse = await client.send(command); // Decode and return the response. const decoded = new TextDecoder().decode(apiResponse.body); /** @type {TextCompletionApiResponse} */ const responseBody = JSON.parse(decoded); return responseBody.completion; }; // Invoke the function if this file was run directly. if (process.argv[1] === fileURLToPath(import.meta.url)) { const prompt = 'Complete the following in one sentence: "Once upon a time..."'; const modelId = FoundationModels.CLAUDE_INSTANT.modelId; console.log(`Prompt: ${prompt}`); console.log(`Model ID: ${modelId}`); try { console.log("-".repeat(53)); console.log("Using the Messages API:"); const response = await invokeModel(prompt, modelId); console.log(response); } catch (err) { console.log(err); } try { console.log("-".repeat(53)); console.log("Using the Text Completions API:"); const response = await invokeTextCompletionsApi(prompt, modelId); console.log(response); } catch (err) { console.log(err); } }
  • API の詳細については、「 API リファレンスInvokeModel」の「」を参照してください。 AWS SDK for JavaScript