Invoque modelos Anthropic Claude no Amazon Bedrock usando a API Invoke Model com um fluxo de resposta - Amazon Bedrock

As traduções são geradas por tradução automática. Em caso de conflito entre o conteúdo da tradução e da versão original em inglês, a versão em inglês prevalecerá.

Invoque modelos Anthropic Claude no Amazon Bedrock usando a API Invoke Model com um fluxo de resposta

Os exemplos de código a seguir mostram como enviar uma mensagem de texto para modelos da Anthropic Claude usando a API Invoke Model e imprimir o fluxo de resposta.

.NET
AWS SDK for .NET
nota

Tem mais sobre GitHub. Encontre o exemplo completo e veja como configurar e executar no AWS Code Examples Repository.

Use a API Invoke Model para enviar uma mensagem de texto e imprimir o fluxo de resposta.

/// <summary> /// Asynchronously invokes the Anthropic Claude 2 model to run an inference based on the provided input and process the response stream. /// </summary> /// <param name="prompt">The prompt that you want Claude to complete.</param> /// <returns>The inference response from the model</returns> /// <remarks> /// The different model providers have individual request and response formats. /// For the format, ranges, and default values for Anthropic Claude, refer to: /// https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-claude.html /// </remarks> public static async IAsyncEnumerable<string> InvokeClaudeWithResponseStreamAsync(string prompt, [EnumeratorCancellation] CancellationToken cancellationToken = default) { string claudeModelId = "anthropic.claude-v2"; // Claude requires you to enclose the prompt as follows: string enclosedPrompt = "Human: " + prompt + "\n\nAssistant:"; AmazonBedrockRuntimeClient client = new(RegionEndpoint.USEast1); string payload = new JsonObject() { { "prompt", enclosedPrompt }, { "max_tokens_to_sample", 200 }, { "temperature", 0.5 }, { "stop_sequences", new JsonArray("\n\nHuman:") } }.ToJsonString(); InvokeModelWithResponseStreamResponse? response = null; try { response = await client.InvokeModelWithResponseStreamAsync(new InvokeModelWithResponseStreamRequest() { ModelId = claudeModelId, Body = AWSSDKUtils.GenerateMemoryStreamFromString(payload), ContentType = "application/json", Accept = "application/json" }); } catch (AmazonBedrockRuntimeException e) { Console.WriteLine(e.Message); } if (response is not null && response.HttpStatusCode == System.Net.HttpStatusCode.OK) { // create a buffer to write the event in to move from a push mode to a pull mode Channel<string> buffer = Channel.CreateUnbounded<string>(); bool isStreaming = true; response.Body.ChunkReceived += BodyOnChunkReceived; response.Body.StartProcessing(); while ((!cancellationToken.IsCancellationRequested && isStreaming) || (!cancellationToken.IsCancellationRequested && buffer.Reader.Count > 0)) { // pull the completion from the buffer and add it to the IAsyncEnumerable collection yield return await buffer.Reader.ReadAsync(cancellationToken); } response.Body.ChunkReceived -= BodyOnChunkReceived; yield break; // handle the ChunkReceived events async void BodyOnChunkReceived(object? sender, EventStreamEventReceivedArgs<PayloadPart> e) { var streamResponse = JsonSerializer.Deserialize<JsonObject>(e.EventStreamEvent.Bytes) ?? throw new NullReferenceException($"Unable to deserialize {nameof(e.EventStreamEvent.Bytes)}"); if (streamResponse["stop_reason"]?.GetValue<string?>() != null) { isStreaming = false; } // write the received completion chunk into the buffer await buffer.Writer.WriteAsync(streamResponse["completion"]?.GetValue<string>(), cancellationToken); } } else if (response is not null) { Console.WriteLine("InvokeModelAsync failed with status code " + response.HttpStatusCode); } yield break; }
Go
SDK for Go V2
nota

Tem mais sobre GitHub. Encontre o exemplo completo e veja como configurar e executar no AWS Code Examples Repository.

Use a API Invoke Model para enviar uma mensagem de texto e imprimir o fluxo de resposta.

// Each model provider defines their own individual request and response formats. // For the format, ranges, and default values for the different models, refer to: // https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters.html type Request struct { Prompt string `json:"prompt"` MaxTokensToSample int `json:"max_tokens_to_sample"` Temperature float64 `json:"temperature,omitempty"` } type Response struct { Completion string `json:"completion"` } // Invokes Anthropic Claude on Amazon Bedrock to run an inference and asynchronously // process the response stream. func (wrapper InvokeModelWithResponseStreamWrapper) InvokeModelWithResponseStream(prompt string) (string, error) { modelId := "anthropic.claude-v2" // Anthropic Claude requires you to enclose the prompt as follows: prefix := "Human: " postfix := "\n\nAssistant:" prompt = prefix + prompt + postfix request := ClaudeRequest{ Prompt: prompt, MaxTokensToSample: 200, Temperature: 0.5, StopSequences: []string{"\n\nHuman:"}, } body, err := json.Marshal(request) if err != nil { log.Panicln("Couldn't marshal the request: ", err) } output, err := wrapper.BedrockRuntimeClient.InvokeModelWithResponseStream(context.Background(), &bedrockruntime.InvokeModelWithResponseStreamInput{ Body: body, ModelId: aws.String(modelId), ContentType: aws.String("application/json"), }) if err != nil { errMsg := err.Error() if strings.Contains(errMsg, "no such host") { log.Printf("The Bedrock service is not available in the selected region. Please double-check the service availability for your region at https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services/.\n") } else if strings.Contains(errMsg, "Could not resolve the foundation model") { log.Printf("Could not resolve the foundation model from model identifier: \"%v\". Please verify that the requested model exists and is accessible within the specified region.\n", modelId) } else { log.Printf("Couldn't invoke Anthropic Claude. Here's why: %v\n", err) } } resp, err := processStreamingOutput(output, func(ctx context.Context, part []byte) error { fmt.Print(string(part)) return nil }) if err != nil { log.Fatal("streaming output processing error: ", err) } return resp.Completion, nil } type StreamingOutputHandler func(ctx context.Context, part []byte) error func processStreamingOutput(output *bedrockruntime.InvokeModelWithResponseStreamOutput, handler StreamingOutputHandler) (Response, error) { var combinedResult string resp := Response{} for event := range output.GetStream().Events() { switch v := event.(type) { case *types.ResponseStreamMemberChunk: //fmt.Println("payload", string(v.Value.Bytes)) var resp Response err := json.NewDecoder(bytes.NewReader(v.Value.Bytes)).Decode(&resp) if err != nil { return resp, err } err = handler(context.Background(), []byte(resp.Completion)) if err != nil { return resp, err } combinedResult += resp.Completion case *types.UnknownUnionMember: fmt.Println("unknown tag:", v.Tag) default: fmt.Println("union is nil or unknown type") } } resp.Completion = combinedResult return resp, nil }
Java
SDK para Java 2.x
nota

Tem mais sobre GitHub. Encontre o exemplo completo e veja como configurar e executar no AWS Code Examples Repository.

Use a API Invoke Model para enviar uma mensagem de texto e imprimir o fluxo de resposta.

/** * Invokes Anthropic Claude 2 via the Messages API and processes the response stream. * <p> * 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 prompt The prompt for the model to complete. * @return A JSON object containing the complete response along with some metadata. */ public static JSONObject invokeMessagesApiWithResponseStream(String prompt) { BedrockRuntimeAsyncClient client = BedrockRuntimeAsyncClient.builder() .credentialsProvider(ProfileCredentialsProvider.create()) .region(Region.US_EAST_1) .build(); String modelId = "anthropic.claude-v2"; // Prepare the JSON payload for the Messages API request var payload = new JSONObject() .put("anthropic_version", "bedrock-2023-05-31") .put("max_tokens", 1000) .append("messages", new JSONObject() .put("role", "user") .append("content", new JSONObject() .put("type", "text") .put("text", prompt) )); // Create the request object using the payload and the model ID var request = InvokeModelWithResponseStreamRequest.builder() .contentType("application/json") .body(SdkBytes.fromUtf8String(payload.toString())) .modelId(modelId) .build(); // Create a handler to print the stream in real-time and add metadata to a response object JSONObject structuredResponse = new JSONObject(); var handler = createMessagesApiResponseStreamHandler(structuredResponse); // Invoke the model with the request payload and the response stream handler client.invokeModelWithResponseStream(request, handler).join(); return structuredResponse; } private static InvokeModelWithResponseStreamResponseHandler createMessagesApiResponseStreamHandler(JSONObject structuredResponse) { AtomicReference<String> completeMessage = new AtomicReference<>(""); Consumer<ResponseStream> responseStreamHandler = event -> event.accept(InvokeModelWithResponseStreamResponseHandler.Visitor.builder() .onChunk(c -> { // Decode the chunk var chunk = new JSONObject(c.bytes().asUtf8String()); // The Messages API returns different types: var chunkType = chunk.getString("type"); if ("message_start".equals(chunkType)) { // The first chunk contains information about the message role String role = chunk.optJSONObject("message").optString("role"); structuredResponse.put("role", role); } else if ("content_block_delta".equals(chunkType)) { // These chunks contain the text fragments var text = chunk.optJSONObject("delta").optString("text"); // Print the text fragment to the console ... System.out.print(text); // ... and append it to the complete message completeMessage.getAndUpdate(current -> current + text); } else if ("message_delta".equals(chunkType)) { // This chunk contains the stop reason var stopReason = chunk.optJSONObject("delta").optString("stop_reason"); structuredResponse.put("stop_reason", stopReason); } else if ("message_stop".equals(chunkType)) { // The last chunk contains the metrics JSONObject metrics = chunk.optJSONObject("amazon-bedrock-invocationMetrics"); structuredResponse.put("metrics", new JSONObject() .put("inputTokenCount", metrics.optString("inputTokenCount")) .put("outputTokenCount", metrics.optString("outputTokenCount")) .put("firstByteLatency", metrics.optString("firstByteLatency")) .put("invocationLatency", metrics.optString("invocationLatency"))); } }) .build()); return InvokeModelWithResponseStreamResponseHandler.builder() .onEventStream(stream -> stream.subscribe(responseStreamHandler)) .onComplete(() -> // Add the complete message to the response object structuredResponse.append("content", new JSONObject() .put("type", "text") .put("text", completeMessage.get()))) .build(); }
JavaScript
SDK para JavaScript (v3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e veja como configurar e executar no AWS Code Examples Repository.

Use a API Invoke Model para enviar uma mensagem de texto e imprimir o fluxo de resposta.

// 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, InvokeModelWithResponseStreamCommand, } from "@aws-sdk/client-bedrock-runtime"; /** * @typedef {Object} ResponseContent * @property {string} text * * @typedef {Object} MessagesResponseBody * @property {ResponseContent[]} content * * @typedef {Object} Delta * @property {string} text * * @typedef {Object} Message * @property {string} role * * @typedef {Object} Chunk * @property {string} type * @property {Delta} delta * @property {Message} message */ /** * Invokes Anthropic Claude 3 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-3-haiku-20240307-v1:0". */ export const invokeModel = async ( prompt, modelId = "anthropic.claude-3-haiku-20240307-v1:0", ) => { // Create a new Bedrock Runtime client instance. const client = new BedrockRuntimeClient({ region: "us-east-1" }); // Prepare the payload for the model. 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 {MessagesResponseBody} */ const responseBody = JSON.parse(decodedResponseBody); return responseBody.content[0].text; }; /** * Invokes Anthropic Claude 3 and processes the response stream. * * 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-3-haiku-20240307-v1:0". */ export const invokeModelWithResponseStream = async ( prompt, modelId = "anthropic.claude-3-haiku-20240307-v1:0", ) => { // Create a new Bedrock Runtime client instance. const client = new BedrockRuntimeClient({ region: "us-east-1" }); // Prepare the payload for the model. 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 API to respond. const command = new InvokeModelWithResponseStreamCommand({ contentType: "application/json", body: JSON.stringify(payload), modelId, }); const apiResponse = await client.send(command); let completeMessage = ""; // Decode and process the response stream for await (const item of apiResponse.body) { /** @type Chunk */ const chunk = JSON.parse(new TextDecoder().decode(item.chunk.bytes)); const chunk_type = chunk.type; if (chunk_type === "content_block_delta") { const text = chunk.delta.text; completeMessage = completeMessage + text; process.stdout.write(text); } } // Return the final response return completeMessage; }; // Invoke the function if this file was run directly. if (process.argv[1] === fileURLToPath(import.meta.url)) { const prompt = 'Write a paragraph starting with: "Once upon a time..."'; const modelId = FoundationModels.CLAUDE_3_HAIKU.modelId; console.log(`Prompt: ${prompt}`); console.log(`Model ID: ${modelId}`); try { console.log("-".repeat(53)); const response = await invokeModel(prompt, modelId); console.log("\n" + "-".repeat(53)); console.log("Final structured response:"); console.log(response); } catch (err) { console.log(`\n${err}`); } }
Python
SDK para Python (Boto3).
nota

Tem mais sobre GitHub. Encontre o exemplo completo e veja como configurar e executar no AWS Code Examples Repository.

Use a API Invoke Model para enviar uma mensagem de texto e imprimir o fluxo de resposta.

# Use the native inference API to send a text message to Anthropic Claude # 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., Claude 3 Haiku. model_id = "anthropic.claude-3-haiku-20240307-v1:0" # Define the prompt for the model. prompt = "Describe the purpose of a 'hello world' program in one line." # Format the request payload using the model's native structure. native_request = { "anthropic_version": "bedrock-2023-05-31", "max_tokens": 512, "temperature": 0.5, "messages": [ { "role": "user", "content": [{"type": "text", "text": prompt}], } ], } # 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 chunk["type"] == "content_block_delta": print(chunk["delta"].get("text", ""), end="")

Para obter uma lista completa dos guias do desenvolvedor do AWS SDK e exemplos de código, consulteUsando esse serviço com um AWS SDK. Este tópico também inclui informações sobre como começar e detalhes sobre versões anteriores do SDK.