모델 호출 API를 사용하여 Amazon Bedrock에서 미스트랄 AI 모델을 호출할 수 있습니다. - Amazon Bedrock

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

모델 호출 API를 사용하여 Amazon Bedrock에서 미스트랄 AI 모델을 호출할 수 있습니다.

다음 코드 예제는 Invoke Model API를 사용하여 Mistral AI 모델에 문자 메시지를 보내는 방법을 보여줍니다.

.NET
AWS SDK for .NET
참고

더 많은 정보가 있습니다 GitHub. AWS 코드 예제 리포지토리에서 전체 예제를 찾고 설정 및 실행하는 방법을 배워보세요.

Invoke Model API를 사용하여 문자 메시지를 보내세요.

/// <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 ?? []; }
  • API 세부 정보는 AWS SDK for .NET API InvokeModel참조를 참조하십시오.

Java
SDK for Java 2.x
참고

자세한 내용은 다음과 같습니다 GitHub. AWS 코드 예제 리포지토리에서 전체 예제를 찾고 설정 및 실행하는 방법을 배워보세요.

Invoke Model API를 비동기적으로 사용하여 문자 메시지를 보내세요.

/** * 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(); }

Invoke Model API를 사용하여 문자 메시지를 보낼 수 있습니다.

/** * 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(); }
  • API 세부 정보는 AWS SDK for Java 2.x API InvokeModel참조를 참조하십시오.

JavaScript
JavaScript (v3) 용 SDK
참고

더 많은 내용이 있습니다. GitHub AWS 코드 예제 리포지토리에서 전체 예제를 찾고 설정 및 실행하는 방법을 배워보세요.

Invoke Model API를 사용하여 문자 메시지를 보내세요.

// 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); } }
  • API 세부 정보는 AWS SDK for JavaScript API InvokeModel참조를 참조하십시오.

Python
SDK for Python(Boto3)
참고

자세한 내용은 다음과 같습니다 GitHub. AWS 코드 예제 리포지토리에서 전체 예제를 찾고 설정 및 실행하는 방법을 배워보세요.

Invoke Model API를 사용하여 문자 메시지를 보내세요.

# 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)
  • API에 대한 자세한 내용은 파이썬용AWS SDK (Boto3) API 레퍼런스를 참조하십시오 InvokeModel.

AWS SDK 개발자 가이드 및 코드 예제의 전체 목록은 을 참조하십시오. AWS SDK와 함께 이 서비스 사용 이 주제에는 시작하기에 대한 정보와 이전 SDK 버전에 대한 세부 정보도 포함되어 있습니다.