Mistral AI텍스트 완성 - Amazon Bedrock

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

Mistral AI텍스트 완성

Mistral AI텍스트 완성 API를 사용하면 Mistral AI 모델을 사용하여 텍스트를 생성할 수 있습니다.

InvokeModel또는 InvokeModelWithResponseStream (스트리밍) 을 사용하여 Mistral AI 모델에 추론 요청을 합니다.

Mistral AI모델은 Apache 2.0 라이선스에 따라 사용할 수 있습니다. Mistral AI모델 사용에 대한 자세한 내용은 Mistral AI설명서를 참조하십시오.

지원되는 모델

다음 Mistral AI 모델을 사용할 수 있습니다.

  • Mistral 7B Instruct

  • Mixtral 8X7B Instruct

  • Mistral Large

  • Mistral Small

사용하려는 모델의 모델 ID가 필요합니다. 모델 ID를 가져오려면 을 참조하십시오아마존 베드락 모델 IDs.

요청 및 응답

Request

Mistral AI모델에는 다음과 같은 추론 매개변수가 있습니다.

{ "prompt": string, "max_tokens" : int, "stop" : [string], "temperature": float, "top_p": float, "top_k": int }

다음은 필수 파라미터입니다.

  • prompt — (필수) 다음 예제와 같이 모델에 전달하려는 프롬프트입니다.

    <s>[INST] What is your favourite condiment? [/INST]

    다음 예제는 멀티턴 프롬프트를 포맷하는 방법을 보여줍니다.

    <s>[INST] What is your favourite condiment? [/INST] Well, I'm quite partial to a good squeeze of fresh lemon juice. It adds just the right amount of zesty flavour to whatever I'm cooking up in the kitchen!</s> [INST] Do you have mayonnaise recipes? [/INST]

    사용자 역할 텍스트는 [INST]...[/INST] 토큰 내부에 있고, 외부 텍스트는 어시스턴트 역할입니다. 문자열의 시작과 끝은 (문자열의 시작) 및 <s> </s> (문자열의 끝) 토큰으로 표시됩니다. 채팅 프롬프트를 올바른 형식으로 보내는 방법에 대한 자세한 내용은 Mistral AI 설명서의 채팅 템플릿을 참조하십시오.

다음 파라미터는 선택 사항입니다.

  • max_token — 생성된 응답에 사용할 최대 토큰 수를 지정합니다. 생성된 텍스트가 max_tokens을 초과하면 모델은 응답을 잘라냅니다.

    기본값 최소 Maximum

    Mistral 7B Instruct— 512

    Mixtral 8X7B Instruct— 512

    Mistral Large— 8,192

    Mistral Small— 8,192

    1

    Mistral 7B Instruct— 8,192

    Mixtral 8X7B Instruct— 4,096

    Mistral Large— 8,192

    Mistral Small— 8,192

  • stop — 모델에서 생성된 경우 모델이 추가 출력을 생성하지 못하도록 하는 중지 시퀀스 목록입니다.

    기본값 최소 Maximum

    0

    0

    10

  • 온도 — 모델에 의한 예측의 무작위성을 제어합니다. 자세한 정보는 추론 파라미터를 사용한 영향 응답 생성을 참조하세요.

    기본값 최소 Maximum

    Mistral 7B Instruct— 0.5

    Mixtral 8X7B Instruct— 0.5

    Mistral Large— 0.7

    Mistral Small— 0.7

    0

    1

  • top_p — 모델이 다음 토큰으로 고려할 가능성이 가장 높은 후보의 비율을 설정하여 모델이 생성하는 텍스트의 다양성을 제어합니다. 자세한 정보는 추론 파라미터를 사용한 영향 응답 생성을 참조하세요.

    기본값 최소 Maximum

    Mistral 7B Instruct— 0.9

    Mixtral 8X7B Instruct— 0.9

    Mistral Large— 1

    Mistral Small— 1

    0

    1

  • top_k — 모델이 다음 토큰으로 고려할 가능성이 가장 높은 후보의 수를 제어합니다. 자세한 정보는 추론 파라미터를 사용한 영향 응답 생성을 참조하세요.

    기본값 최소 Maximum

    Mistral 7B Instruct— 50

    Mixtral 8X7B Instruct— 50

    Mistral Large— 장애인

    Mistral Small— 비활성화됨

    1

    200

Response

InvokeModel에 대한 직접 호출의 body 응답은 다음과 같습니다.

{ "outputs": [ { "text": string, "stop_reason": string } ] }

body 응답에는 다음과 같은 필드가 포함됩니다.

  • 출력 — 모델의 출력 목록입니다. 각 출력에는 다음 필드가 있습니다.

    • text — 모델이 생성한 텍스트입니다.

    • stop_reason — 응답에서 텍스트 생성이 중단된 이유. 가능한 값은 다음과 같습니다.

      • 중지 - 모델이 입력 프롬프트에 대한 텍스트 생성을 완료했습니다. 더 이상 생성할 콘텐츠가 없거나 stop 요청 매개변수에 정의한 중지 시퀀스 중 하나를 모델이 생성하는 경우 모델이 중지됩니다.

      • 길이 - 생성된 텍스트의 토큰 길이가 InvokeModel(InvokeModelWithResponseStream, 출력을 스트리밍하는 경우)에 대한 호출에서 max_tokens의 값을 초과합니다. 응답은 max_tokens 토큰 수로 잘립니다.

코드 예제

이 예제는 Mistral 7B Instruct 모델을 호출하는 방법을 보여줍니다.

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 """ Shows how to generate text using a Mistral AI model. """ import json import logging import boto3 from botocore.exceptions import ClientError logger = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) def generate_text(model_id, body): """ Generate text using a Mistral AI model. Args: model_id (str): The model ID to use. body (str) : The request body to use. Returns: JSON: The response from the model. """ logger.info("Generating text with Mistral AI model %s", model_id) bedrock = boto3.client(service_name='bedrock-runtime') response = bedrock.invoke_model( body=body, modelId=model_id ) logger.info("Successfully generated text with Mistral AI model %s", model_id) return response def main(): """ Entrypoint for Mistral AI example. """ logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") try: model_id = 'mistral.mistral-7b-instruct-v0:2' prompt = """<s>[INST] In Bash, how do I list all text files in the current directory (excluding subdirectories) that have been modified in the last month? [/INST]""" body = json.dumps({ "prompt": prompt, "max_tokens": 400, "temperature": 0.7, "top_p": 0.7, "top_k": 50 }) response = generate_text(model_id=model_id, body=body) response_body = json.loads(response.get('body').read()) outputs = response_body.get('outputs') for index, output in enumerate(outputs): print(f"Output {index + 1}\n----------") print(f"Text:\n{output['text']}\n") print(f"Stop reason: {output['stop_reason']}\n") except ClientError as err: message = err.response["Error"]["Message"] logger.error("A client error occurred: %s", message) print("A client error occured: " + format(message)) else: print(f"Finished generating text with Mistral AI model {model_id}.") if __name__ == "__main__": main()