Invoquez Meta Llama 3 sur Amazon Bedrock à l'aide de l'API Invoke Model avec un flux de réponse - Amazon Bedrock

Les traductions sont fournies par des outils de traduction automatique. En cas de conflit entre le contenu d'une traduction et celui de la version originale en anglais, la version anglaise prévaudra.

Invoquez Meta Llama 3 sur Amazon Bedrock à l'aide de l'API Invoke Model avec un flux de réponse

Les exemples de code suivants montrent comment envoyer un message texte à Meta Llama 3 à l'aide de l'API Invoke Model et comment imprimer le flux de réponse.

Java
SDK pour Java 2.x
Note

Il y en a plus sur GitHub. Trouvez l'exemple complet et découvrez comment le configurer et l'exécuter dans le référentiel d'exemples de code AWS.

Utilisez l'API Invoke Model pour envoyer un message texte et imprimer le flux de réponses.

// Send a prompt to Meta Llama 3 and print the response stream in real-time. public class InvokeModelWithResponseStreamQuickstart { public static void main(String[] args) { // Create a Bedrock Runtime client in the AWS Region of your choice. var client = BedrockRuntimeAsyncClient.builder() .region(Region.US_WEST_2) .build(); // Set the model ID, e.g., Llama 3 8B Instruct. var modelId = "meta.llama3-8b-instruct-v1:0"; // Define the user message to send. var userMessage = "Describe the purpose of a 'hello world' program in one line."; // Embed the message in Llama 3's prompt format. var prompt = MessageFormat.format(""" <|begin_of_text|> <|start_header_id|>user<|end_header_id|> {0} <|eot_id|> <|start_header_id|>assistant<|end_header_id|> """, userMessage); // Create a JSON payload using the model's native structure. var request = new JSONObject() .put("prompt", prompt) // Optional inference parameters: .put("max_gen_len", 512) .put("temperature", 0.5F) .put("top_p", 0.9F); // Create a handler to extract and print the response text in real-time. var streamHandler = InvokeModelWithResponseStreamResponseHandler.builder() .subscriber(event -> event.accept( InvokeModelWithResponseStreamResponseHandler.Visitor.builder() .onChunk(c -> { var chunk = new JSONObject(c.bytes().asUtf8String()); if (chunk.has("generation")) { System.out.print(chunk.getString("generation")); } }).build()) ).build(); // Encode and send the request. Let the stream handler process the response. client.invokeModelWithResponseStream(req -> req .body(SdkBytes.fromUtf8String(request.toString())) .modelId(modelId), streamHandler ).join(); } } // 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
JavaScript
SDK pour JavaScript (v3)
Note

Il y en a plus sur GitHub. Trouvez l'exemple complet et découvrez comment le configurer et l'exécuter dans le référentiel d'exemples de code AWS.

Utilisez l'API Invoke Model pour envoyer un message texte et imprimer le flux de réponses.

// Send a prompt to Meta Llama 3 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 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 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
Python
SDK pour Python (Boto3)
Note

Il y en a plus sur GitHub. Trouvez l'exemple complet et découvrez comment le configurer et l'exécuter dans le référentiel d'exemples de code AWS.

Utilisez l'API Invoke Model pour envoyer un message texte et imprimer le flux de réponses.

# Use the native inference API to send a text message to Meta Llama 3 # and print the response stream. import boto3 import json # Create a Bedrock Runtime client in the AWS Region of your choice. client = boto3.client("bedrock-runtime", region_name="us-east-1") # Set the model ID, e.g., Llama 3 8b Instruct. model_id = "meta.llama3-8b-instruct-v1:0" # Define the message to send. user_message = "Describe the purpose of a 'hello world' program in one line." # Embed the message in Llama 3's prompt format. prompt = f""" <|begin_of_text|> <|start_header_id|>user<|end_header_id|> {user_message} <|eot_id|> <|start_header_id|>assistant<|end_header_id|> """ # Format the request payload using the model's native structure. native_request = { "prompt": prompt, "max_gen_len": 512, "temperature": 0.5, } # Convert the native request to JSON. request = json.dumps(native_request) # Invoke the model with the request. streaming_response = client.invoke_model_with_response_stream( modelId=model_id, body=request ) # Extract and print the response text in real-time. for event in streaming_response["body"]: chunk = json.loads(event["chunk"]["bytes"]) if "generation" in chunk: print(chunk["generation"], end="")

Pour obtenir la liste complète des guides de développement du AWS SDK et des exemples de code, consultezUtilisation de ce service avec un AWS SDK. Cette rubrique comprend également des informations sur le démarrage et sur les versions précédentes de SDK.