

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# Cohere Command 模型
<a name="model-parameters-cohere-command"></a>

您可以使用 [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) (串流)對 Cohere Command 模型進行推論呼叫。您需要您想要使用的模型的模型 ID。若要取得模型 ID，請參閱 [Amazon Bedrock 中支援的基礎模型](models-supported.md)。

**Topics**
+ [請求與回應](#model-parameters-cohere-command-request-response)
+ [程式碼範例](#api-inference-examples-cohere-command)

## 請求與回應
<a name="model-parameters-cohere-command-request-response"></a>

------
#### [ Request ]

這些 Cohere Command 模型具有下列推論參數。

```
{
    "prompt": string,
    "temperature": float,
    "p": float,
    "k": float,
    "max_tokens": int,
    "stop_sequences": [string],
    "return_likelihoods": "GENERATION|ALL|NONE",
    "stream": boolean,
    "num_generations": int,
    "logit_bias": {token_id: bias},
    "truncate": "NONE|START|END"
}
```

以下是必要的參數。
+ **prompt** – (必要) 作為產生回應起點的輸入文字。

  以下是每個呼叫的文字和字元限制。

**每個呼叫的文字**  
    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/model-parameters-cohere-command.html)

**字元**  
    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/model-parameters-cohere-command.html)

以下是選用參數。
+ **return\$1likelihoods** – 指定如何以及是否隨回應一起傳回字符概似值。您可以指定下列選項：
  + `GENERATION` - 僅傳回產生的記號的概似值。
  + `ALL` - 傳回所有記號的概似值。
  + `NONE` - (預設)不傳回任何概似值。
+ **stream** – (支援串流所需) 指定 `true` 以即時傳回逐段回應，並指定 `false` 以在程序完成後傳回完整回應。
+ **logit\$1bias** – 防止模型產生自不需要的字符，或激勵模型包含所需的字符。格式是 `{token_id: bias}`，其中偏差是介於 -10 和 10 之間的浮點數。可以使用任何字符化服務從文字中獲得字符，例如 Cohere 的字符化端點。如需詳細資訊，請參閱 [Cohere 文件](https://docs.cohere.com/docs)。    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/model-parameters-cohere-command.html)
+  **num\$1generations** – 模型應傳回的產生數目上限。    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/model-parameters-cohere-command.html)
+  **truncate** – 指定 API 如何處理超過字符長度上限的輸入。請使用下列其中一個：
  + `NONE` - 當輸入超過輸入記號長度上限時傳回錯誤。
  + `START` - 捨棄輸入開端。
  + `END` - (預設) 捨棄輸入結尾。

  如果您指定 `START` 或 `END`，則模型會捨棄輸入，直到剩餘的輸入完全符合模型的輸入記號長度上限。
+ **temperature** — 使用較低的值來降低回應中的隨機性。    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/model-parameters-cohere-command.html)
+ **p** – Top P。使用較低的值來忽略較不可能的選項。設定為 0 或 1.0 以停用。如果 `p` 和 `k` 兩者都已啟用，則 `p` 會在 `k` 之後執行。    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/model-parameters-cohere-command.html)
+ **k** – Top K。指定模型用來產生下一個字符的字符選擇數。如果 `p` 和 `k` 兩者都已啟用，則 `p` 會在 `k` 之後執行。    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/model-parameters-cohere-command.html)
+ **max\$1tokens** – 指定產生的回應中使用的字符數目上限。    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/model-parameters-cohere-command.html)
+ **stop\$1sequences** – 最多設定四個模型辨識的序列。停止序列後，模型停止產生進一步的記號。傳回的文字不包含停止序列。

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

回應具有以下可能的欄位：

```
{
    "generations": [
        {
            "finish_reason": "COMPLETE | MAX_TOKENS | ERROR | ERROR_TOXIC",
            "id": string,
            "text": string,
            "likelihood" : float,
            "token_likelihoods" : [{"token" : string, "likelihood": float}],
            "is_finished" : true | false,
            "index" : integer
           
        }
    ],
    "id": string,
    "prompt": string
}
```
+ `generations` - 產生結果清單以及請求的記號的概似值。(始終傳回)。清單中的每個世代物件都包含下列欄位。
  + `id` - 產生的識別符。(始終傳回)。
  + `likelihood` - 輸出的可能性。該值是 `token_likelihoods` 中字符類似性的平均值。如果您指定 `return_likelihoods` 輸入參數，則傳回。
  + `token_likelihoods` - 每個記號概似值陣列。如果您指定 `return_likelihoods` 輸入參數，則傳回。
  + `finish_reason` - 模型完成產生字符的原因。`COMPLETE` - 模型傳回完成的回覆。`MAX_TOKENS` - 回復被切斷，因為模型達到了其內容長度的最大字符數。`ERROR ` - 產生回覆時發生錯誤。`ERROR_TOXIC` – 該模型產生了一個被認為有毒的回覆。只有當 `is_finished` = `true` 時才會傳回 `finish_reason`。(不一定會傳回)。
  + `is_finished` - 僅在 `stream` 為 `true` 時使用的布林值欄位，表示是否有其他記號會以串流回應的一部分的形式產生。(不一定會傳回)
  + `text` - 產生的文字。
  + `index` - 在串流回應中，用於判斷指定的記號屬於哪一世代。當僅傳輸一個回應時，所有記號都屬於同一世代，並且不傳回索引。因此，僅會在 `num_generations` 的值大於 1 的串流請求中傳回 `index`。
+ `prompt` — 來自輸入請求的提示 (一律傳回)。
+ `id` - 請求的識別符 (始終傳回)。

如需詳細資訊，請參閱 Cohere 文件中的[產生](https://docs.cohere.com/reference/generate-1)。

------

## 程式碼範例
<a name="api-inference-examples-cohere-command"></a>

此範例展示如何呼叫 *Cohere Command* 模型。

```
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Shows how to generate text using a Cohere 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 Cohere model.
    Args:
        model_id (str): The model ID to use.
        body (str) : The reqest body to use.
    Returns:
        dict: The response from the model.
    """

    logger.info("Generating text with Cohere model %s", model_id)

    accept = 'application/json'
    content_type = 'application/json'

    bedrock = boto3.client(service_name='bedrock-runtime')

    response = bedrock.invoke_model(
        body=body,
        modelId=model_id,
        accept=accept,
        contentType=content_type
    )

    logger.info("Successfully generated text with Cohere model %s", model_id)

    return response


def main():
    """
    Entrypoint for Cohere example.
    """

    logging.basicConfig(level=logging.INFO,
                        format="%(levelname)s: %(message)s")

    model_id = 'cohere.command-text-v14'
    prompt = """Summarize this dialogue: 
"Customer: Please connect me with a support agent.
AI: Hi there, how can I assist you today?
Customer: I forgot my password and lost access to the email affiliated to my account. Can you please help me?
AI: Yes of course. First I'll need to confirm your identity and then I can connect you with one of our support agents.
"""
    try:
        body = json.dumps({
            "prompt": prompt,
            "max_tokens": 200,
            "temperature": 0.6,
            "p": 1,
            "k": 0,
            "num_generations": 2,
            "return_likelihoods": "GENERATION"
        })
        response = generate_text(model_id=model_id,
                                 body=body)

        response_body = json.loads(response.get('body').read())
        generations = response_body.get('generations')

        for index, generation in enumerate(generations):

            print(f"Generation {index + 1}\n------------")
            print(f"Text:\n {generation['text']}\n")
            if 'likelihood' in generation:
                print(f"Likelihood:\n {generation['likelihood']}\n")
            
            print(f"Reason: {generation['finish_reason']}\n\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 Cohere model {model_id}.")


if __name__ == "__main__":
    main()
```