Invoque modelos Mistral AI no Amazon Bedrock usando a API Invoke Model - 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 Mistral AI no Amazon Bedrock usando a API Invoke Model

Os exemplos de código a seguir mostram como enviar uma mensagem de texto para os modelos Mistral AI, usando a API Invoke Model.

.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.

/// <summary> /// Asynchronously invokes the Mistral 7B model to run an inference based on the provided input. /// </summary> /// <param name="prompt">The prompt that you want Mistral 7B 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 Mistral 7B, refer to: /// https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-mistral.html /// </remarks> public static async Task<List<string?>> InvokeMistral7BAsync(string prompt) { string mistralModelId = "mistral.mistral-7b-instruct-v0:2"; AmazonBedrockRuntimeClient client = new(RegionEndpoint.USWest2); string payload = new JsonObject() { { "prompt", prompt }, { "max_tokens", 200 }, { "temperature", 0.5 } }.ToJsonString(); List<string?>? generatedText = null; try { InvokeModelResponse response = await client.InvokeModelAsync(new InvokeModelRequest() { ModelId = mistralModelId, Body = AWSSDKUtils.GenerateMemoryStreamFromString(payload), ContentType = "application/json", Accept = "application/json" }); if (response.HttpStatusCode == System.Net.HttpStatusCode.OK) { var results = JsonNode.ParseAsync(response.Body).Result?["outputs"]?.AsArray(); generatedText = results?.Select(x => x?["text"]?.GetValue<string?>())?.ToList(); } else { Console.WriteLine("InvokeModelAsync failed with status code " + response.HttpStatusCode); } } catch (AmazonBedrockRuntimeException e) { Console.WriteLine(e.Message); } return generatedText ?? []; }
  • Para obter detalhes da API, consulte InvokeModela Referência AWS SDK for .NET da API.

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 de forma assíncrona a API Invoke Model para enviar uma mensagem de texto.

/** * Asynchronously invokes the Mistral 7B model to run an inference based on the provided input. * * @param prompt The prompt for Mistral to complete. * @return The generated response. */ public static List<String> invokeMistral7B(String prompt) { BedrockRuntimeAsyncClient client = BedrockRuntimeAsyncClient.builder() .region(Region.US_WEST_2) .credentialsProvider(ProfileCredentialsProvider.create()) .build(); // Mistral instruct models provide optimal results when // embedding the prompt into the following template: String instruction = "<s>[INST] " + prompt + " [/INST]"; String modelId = "mistral.mistral-7b-instruct-v0:2"; String payload = new JSONObject() .put("prompt", instruction) .put("max_tokens", 200) .put("temperature", 0.5) .toString(); CompletableFuture<InvokeModelResponse> completableFuture = client.invokeModel(request -> request .accept("application/json") .contentType("application/json") .body(SdkBytes.fromUtf8String(payload)) .modelId(modelId)) .whenComplete((response, exception) -> { if (exception != null) { System.out.println("Model invocation failed: " + exception); } }); try { InvokeModelResponse response = completableFuture.get(); JSONObject responseBody = new JSONObject(response.body().asUtf8String()); JSONArray outputs = responseBody.getJSONArray("outputs"); return IntStream.range(0, outputs.length()) .mapToObj(i -> outputs.getJSONObject(i).getString("text")) .toList(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); System.err.println(e.getMessage()); } catch (ExecutionException e) { System.err.println(e.getMessage()); } return List.of(); }

Use a API Invoke Model para enviar uma mensagem de texto.

/** * Invokes the Mistral 7B model to run an inference based on the provided input. * * @param prompt The prompt for Mistral to complete. * @return The generated responses. */ public static List<String> invokeMistral7B(String prompt) { BedrockRuntimeClient client = BedrockRuntimeClient.builder() .region(Region.US_WEST_2) .credentialsProvider(ProfileCredentialsProvider.create()) .build(); // Mistral instruct models provide optimal results when // embedding the prompt into the following template: String instruction = "<s>[INST] " + prompt + " [/INST]"; String modelId = "mistral.mistral-7b-instruct-v0:2"; String payload = new JSONObject() .put("prompt", instruction) .put("max_tokens", 200) .put("temperature", 0.5) .toString(); InvokeModelResponse response = client.invokeModel(request -> request .accept("application/json") .contentType("application/json") .body(SdkBytes.fromUtf8String(payload)) .modelId(modelId)); JSONObject responseBody = new JSONObject(response.body().asUtf8String()); JSONArray outputs = responseBody.getJSONArray("outputs"); return IntStream.range(0, outputs.length()) .mapToObj(i -> outputs.getJSONObject(i).getString("text")) .toList(); }
  • Para obter detalhes da API, consulte InvokeModela Referência AWS SDK for Java 2.x da API.

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.

// 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} Output * @property {string} text * * @typedef {Object} ResponseBody * @property {Output[]} outputs */ /** * Invokes a Mistral 7B Instruct model. * * @param {string} prompt - The input text prompt for the model to complete. * @param {string} [modelId] - The ID of the model to use. Defaults to "mistral.mistral-7b-instruct-v0:2". */ export const invokeModel = async ( prompt, modelId = "mistral.mistral-7b-instruct-v0:2", ) => { // Create a new Bedrock Runtime client instance. const client = new BedrockRuntimeClient({ region: "us-east-1" }); // Mistral instruct models provide optimal results when embedding // the prompt into the following template: const instruction = `<s>[INST] ${prompt} [/INST]`; // Prepare the payload. const payload = { prompt: instruction, max_tokens: 500, temperature: 0.5, }; // Invoke the model 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 decodedResponseBody = new TextDecoder().decode(apiResponse.body); /** @type {ResponseBody} */ const responseBody = JSON.parse(decodedResponseBody); return responseBody.outputs[0].text; }; // 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.MISTRAL_7B.modelId; console.log(`Prompt: ${prompt}`); console.log(`Model ID: ${modelId}`); try { console.log("-".repeat(53)); const response = await invokeModel(prompt, modelId); console.log(response); } catch (err) { console.log(err); } }
  • Para obter detalhes da API, consulte InvokeModela Referência AWS SDK for JavaScript da API.

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.

# Use the native inference API to send a text message to Mistral AI. 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., Mistral Large. model_id = "mistral.mistral-large-2402-v1:0" # Define the message to send. user_message = "Describe the purpose of a 'hello world' program in one line." # Embed the message in Mistral's prompt format. prompt = f"<s>[INST] {user_message} [/INST]" # Format the request payload using the model's native structure. native_request = { "prompt": prompt, "max_tokens": 512, "temperature": 0.5, } # Convert the native request to JSON. request = json.dumps(native_request) # Invoke the model with the request. response = client.invoke_model(modelId=model_id, body=request) # Decode the response body. model_response = json.loads(response["body"].read()) # Extract and print the response text. response_text = model_response["outputs"][0]["text"] print(response_text)
  • Para obter detalhes da API, consulte a InvokeModelReferência da API AWS SDK for Python (Boto3).

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.