forked from awsdocs/aws-doc-sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Python, Java, JavaScript: Bedrock getting started examples for Llama …
…2 and 3 (awsdocs#6375) Co-authored-by: Dennis Traub <[email protected]>
- Loading branch information
1 parent
7592456
commit 62677d3
Showing
32 changed files
with
969 additions
and
333 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
javascriptv3/example_code/bedrock-runtime/models/meta/llama2/invoke_model_quickstart.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// snippet-start:[javascript.v3.bedrock-runtime.InvokeModel_Llama2_Quickstart] | ||
// Send a prompt to Meta Llama 2 and print the response. | ||
|
||
import { | ||
BedrockRuntimeClient, | ||
InvokeModelCommand, | ||
} from "@aws-sdk/client-bedrock-runtime"; | ||
|
||
// Create a Bedrock Runtime client in the AWS Region of your choice. | ||
const client = new BedrockRuntimeClient({ region: "us-west-2" }); | ||
|
||
// Set the model ID, e.g., Llama 2 Chat 13B. | ||
const modelId = "meta.llama2-13b-chat-v1"; | ||
|
||
// Define the user message to send. | ||
const userMessage = | ||
"Describe the purpose of a 'hello world' program in one sentence."; | ||
|
||
// Embed the message in Llama 2's prompt format. | ||
const prompt = `<s>[INST] ${userMessage} [/INST]`; | ||
|
||
// Format the request payload using the model's native structure. | ||
const request = { | ||
prompt, | ||
// Optional inference parameters: | ||
max_gen_len: 512, | ||
temperature: 0.5, | ||
top_p: 0.9, | ||
}; | ||
|
||
// Encode and send the request. | ||
const response = await client.send( | ||
new InvokeModelCommand({ | ||
contentType: "application/json", | ||
body: JSON.stringify(request), | ||
modelId, | ||
}), | ||
); | ||
|
||
// Decode the native response body. | ||
/** @type {{ generation: string }} */ | ||
const nativeResponse = JSON.parse(new TextDecoder().decode(response.body)); | ||
|
||
// Extract and print the generated text. | ||
const responseText = nativeResponse.generation; | ||
console.log(responseText); | ||
|
||
// Learn more about the Llama 2 prompt format at: | ||
// https://llama.meta.com/docs/model-cards-and-prompt-formats/meta-llama-2 | ||
|
||
// snippet-end:[javascript.v3.bedrock-runtime.InvokeModel_Llama2_Quickstart] |
54 changes: 54 additions & 0 deletions
54
...e_code/bedrock-runtime/models/meta/llama2/invoke_model_with_response_stream_quickstart.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// snippet-start:[javascript.v3.bedrock-runtime.InvokeModelWithResponseStream_Llama2_Quickstart] | ||
// Send a prompt to Meta Llama 2 and print the response stream in real-time. | ||
|
||
import { | ||
BedrockRuntimeClient, | ||
InvokeModelWithResponseStreamCommand, | ||
} from "@aws-sdk/client-bedrock-runtime"; | ||
|
||
// Create a Bedrock Runtime client in the AWS Region of your choice. | ||
const client = new BedrockRuntimeClient({ region: "us-west-2" }); | ||
|
||
// Set the model ID, e.g., Llama 2 Chat 13B. | ||
const modelId = "meta.llama2-13b-chat-v1"; | ||
|
||
// Define the user message to send. | ||
const userMessage = | ||
"Describe the purpose of a 'hello world' program in one sentence."; | ||
|
||
// Embed the message in Llama 2's prompt format. | ||
const prompt = `<s>[INST] ${userMessage} [/INST]`; | ||
|
||
// Format the request payload using the model's native structure. | ||
const request = { | ||
prompt, | ||
// Optional inference parameters: | ||
max_gen_len: 512, | ||
temperature: 0.5, | ||
top_p: 0.9, | ||
}; | ||
|
||
// Encode and send the request. | ||
const responseStream = await client.send( | ||
new InvokeModelWithResponseStreamCommand({ | ||
contentType: "application/json", | ||
body: JSON.stringify(request), | ||
modelId, | ||
}), | ||
); | ||
|
||
// Extract and print the response stream in real-time. | ||
for await (const event of responseStream.body) { | ||
/** @type {{ generation: string }} */ | ||
const chunk = JSON.parse(new TextDecoder().decode(event.chunk.bytes)); | ||
if (chunk.generation) { | ||
process.stdout.write(chunk.generation); | ||
} | ||
} | ||
|
||
// Learn more about the Llama 3 prompt format at: | ||
// https://llama.meta.com/docs/model-cards-and-prompt-formats/meta-llama-3/#special-tokens-used-with-meta-llama-3 | ||
// snippet-end:[javascript.v3.bedrock-runtime.InvokeModelWithResponseStream_Llama2_Quickstart] |
59 changes: 59 additions & 0 deletions
59
javascriptv3/example_code/bedrock-runtime/models/meta/llama3/invoke_model_quickstart.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// snippet-start:[javascript.v3.bedrock-runtime.InvokeModel_Llama3_Quickstart] | ||
// Send a prompt to Meta Llama 3 and print the response. | ||
|
||
import { | ||
BedrockRuntimeClient, | ||
InvokeModelCommand, | ||
} from "@aws-sdk/client-bedrock-runtime"; | ||
|
||
// Create a Bedrock Runtime client in the AWS Region of your choice. | ||
const client = new BedrockRuntimeClient({ region: "us-west-2" }); | ||
|
||
// Set the model ID, e.g., Llama 3 8B Instruct. | ||
const modelId = "meta.llama3-8b-instruct-v1:0"; | ||
|
||
// Define the user message to send. | ||
const userMessage = | ||
"Describe the purpose of a 'hello world' program in one sentence."; | ||
|
||
// Embed the message in Llama 3's prompt format. | ||
const prompt = ` | ||
<|begin_of_text|> | ||
<|start_header_id|>user<|end_header_id|> | ||
${userMessage} | ||
<|eot_id|> | ||
<|start_header_id|>assistant<|end_header_id|> | ||
`; | ||
|
||
// Format the request payload using the model's native structure. | ||
const request = { | ||
prompt, | ||
// Optional inference parameters: | ||
max_gen_len: 512, | ||
temperature: 0.5, | ||
top_p: 0.9, | ||
}; | ||
|
||
// Encode and send the request. | ||
const response = await client.send( | ||
new InvokeModelCommand({ | ||
contentType: "application/json", | ||
body: JSON.stringify(request), | ||
modelId, | ||
}), | ||
); | ||
|
||
// Decode the native response body. | ||
/** @type {{ generation: string }} */ | ||
const nativeResponse = JSON.parse(new TextDecoder().decode(response.body)); | ||
|
||
// Extract and print the generated text. | ||
const responseText = nativeResponse.generation; | ||
console.log(responseText); | ||
|
||
// Learn more about the Llama 3 prompt format at: | ||
// https://llama.meta.com/docs/model-cards-and-prompt-formats/meta-llama-3/#special-tokens-used-with-meta-llama-3 | ||
// snippet-end:[javascript.v3.bedrock-runtime.InvokeModel_Llama3_Quickstart] |
Oops, something went wrong.