Richiama Meta Llama 3 su Amazon Bedrock utilizzando l'API Invoke Model con un flusso di risposta - Amazon Bedrock

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

Richiama Meta Llama 3 su Amazon Bedrock utilizzando l'API Invoke Model con un flusso di risposta

I seguenti esempi di codice mostrano come inviare un messaggio di testo a Meta Llama 3, utilizzando l'API Invoke Model, e stampare il flusso di risposta.

Java
SDK per Java 2.x
Nota

C'è altro su. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

Utilizza l'API Invoke Model per inviare un messaggio di testo e stampare il flusso di risposta.

// 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 per JavaScript (v3)
Nota

C'è altro da fare. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

Utilizza l'API Invoke Model per inviare un messaggio di testo e stampare il flusso di risposta.

// 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 per Python (Boto3)
Nota

C'è di più su GitHub. Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

Utilizza l'API Invoke Model per inviare un messaggio di testo e stampare il flusso di risposta.

# 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="")

Per un elenco completo delle guide per sviluppatori AWS SDK e degli esempi di codice, consulta. Utilizzo di questo servizio con un AWS SDK Questo argomento include anche informazioni su come iniziare e dettagli sulle versioni precedenti dell'SDK.