

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

# Mistral AI Text Completion
<a name="model-parameters-mistral-text-completion"></a>

Mistral AI Text Completion API를 사용하면 Mistral AI 모델로 텍스트를 생성할 수 있습니다.

[InvokeModel](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModel.html) 또는 [InvokeModelWithResponseStream](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_InvokeModelWithResponseStream.html)(스트리밍)을 사용하여 Mistral AI 모델에 대한 추론 요청을 수행합니다.

Mistral AI 모델은 [Apache 2.0 라이선스](https://www.apache.org/licenses/LICENSE-2.0.txt)에서 사용할 수 있습니다. Mistral AI 모델 사용에 대한 자세한 내용은 [Mistral AI 설명서](https://docs.mistral.ai/)를 참조하세요.

**Topics**
+ [지원되는 모델](#mistral--text-completion-supported-models)
+ [요청 및 응답](#model-parameters-mistral-text-completion-request-response)
+ [코드 예제](#api-inference-examples-mistral-text-completion)

## 지원되는 모델
<a name="mistral--text-completion-supported-models"></a>

다음 Mistral AI 모델을 사용할 수 있습니다.
+ Mistral 7B Instruct
+ Mixtral 8X7B Instruct
+ Mistral Large
+ Mistral Small

사용하려는 모델의 모델 ID가 필요합니다. 모델 ID를 가져오려면 [Amazon Bedrock에서 지원되는 파운데이션 모델](models-supported.md) 섹션을 참조하세요.

## 요청 및 응답
<a name="model-parameters-mistral-text-completion-request-response"></a>

------
#### [ 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 설명서의 [채팅 템플릿](https://docs.mistral.ai/models/#chat-template)을 참조하세요.

다음 파라미터는 선택 사항입니다.
+ **max\$1tokens** - 생성된 응답에서 사용할 최대 토큰 수를 지정합니다. 생성된 텍스트가 `max_tokens`을 초과하면 모델은 응답을 잘라냅니다.    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ko_kr/bedrock/latest/userguide/model-parameters-mistral-text-completion.html)
+ **stop** - 모델에 의해 생성되는 경우 모델의 추가 출력을 생성하지 못하도록 하는 중지 시퀀스 목록입니다.    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ko_kr/bedrock/latest/userguide/model-parameters-mistral-text-completion.html)
+ **temperature** - 모델이 수행한 예측의 무작위성을 제어합니다. 자세한 내용은 [추론 파라미터를 사용하여 응답 생성에 영향을 주는 방법](inference-parameters.md) 섹션을 참조하세요.    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ko_kr/bedrock/latest/userguide/model-parameters-mistral-text-completion.html)
+ **top\$1p** - 모델이 다음번 토큰에 대해 고려하는 가장 가능성이 높은 후보의 백분율을 설정하여 모델이 생성하는 텍스트의 다양성을 제어합니다. 자세한 내용은 [추론 파라미터를 사용하여 응답 생성에 영향을 주는 방법](inference-parameters.md) 섹션을 참조하세요.    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ko_kr/bedrock/latest/userguide/model-parameters-mistral-text-completion.html)
+ **top\$1k** - 모델이 다음번 토큰으로 고려할 가능성이 가장 높은 후보의 수입니다. 자세한 내용은 [추론 파라미터를 사용하여 응답 생성에 영향을 주는 방법](inference-parameters.md) 섹션을 참조하세요.    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ko_kr/bedrock/latest/userguide/model-parameters-mistral-text-completion.html)

------
#### [ Response ]

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

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

`body` 응답에는 다음과 같은 필드가 포함됩니다.
+ **outputs** - 모델의 출력 목록입니다. 각 출력에는 다음과 같은 필드가 있습니다.
  + **text** - 모델이 생성한 텍스트입니다.
  + **stop\$1reason** – 응답이 텍스트 생성을 중지한 이유입니다. 가능한 값은 다음과 같습니다.
    + **중지** - 모델이 입력 프롬프트에 대한 텍스트 생성을 완료했습니다. 생성할 콘텐츠가 더 이상 없거나 모델이 `stop` 요청 파라미터에서 정의한 중지 시퀀스 중 하나를 생성하는 경우 모델이 중지됩니다.
    + **길이** - 생성된 텍스트의 토큰 길이가 `InvokeModel`(`InvokeModelWithResponseStream`, 출력을 스트리밍하는 경우)에 대한 호출에서 `max_tokens`의 값을 초과합니다. 응답은 `max_tokens` 토큰 수로 잘립니다.

------

## 코드 예제
<a name="api-inference-examples-mistral-text-completion"></a>

이 예제는 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()
```