CohereCommand模型 - Amazon Bedrock

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

CohereCommand模型

您可以使用InvokeModelInvokeModelWithResponseStream(串流) 向CohereCommand模型提出推論請求。您需要您想要使用的模型的模型 ID。若要取得模型 ID,請參閱Amazon 基岩模型 ID

請求與回應

Request

這些CohereCommand模型具有以下推論參數。

{ "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 — (必要) 作為產生回應起點的輸入文字。

    以下是每次通話的文字和字元限制。

以下是選用參數。

  • return _likelihoods-指定如何以及是否隨響應一起返回令牌類似性。您可以指定下列選項:

    • GENERATION - 僅傳回產生的記號的概似值。

    • ALL - 傳回所有記號的概似值。

    • NONE - (預設)不傳回任何概似值。

  • stream — (支援串流所需) 指定true以即時傳回回應,並 piece-by-piece false在處理程序完成後傳回完整回應。

  • logit_bias — 防止模型生成不需要的令牌或激勵模型包含所需的令牌。格式是 {token_id: bias},其中偏差是介於 -10 和 10 之間的浮點數。令牌可以使用任何標記化服務,如Cohere的標記化端點從文本中獲得。如需詳細資訊,請參閱Cohere文件

    預設 下限 最大

    N/A

    -10(用於記號偏差)

    10(用於記號偏差)

  • num_S — 模型應傳回的層代數目上限。

    預設 下限 最大

    1

    1

    5

  • 截斷 — 指定 API 如何處理超過最大令牌長度的輸入。請使用下列其中一個:

    • NONE - 當輸入超過輸入記號長度上限時傳回錯誤。

    • START - 捨棄輸入開端。

    • END - (預設) 捨棄輸入結尾。

    如果您指定 STARTEND,則模型會捨棄輸入,直到剩餘的輸入完全符合模型的輸入記號長度上限。

  • 溫度 — 使用較低的值來降低回應中的隨機性。

    預設 下限 最大

    0.9

    0

    5

  • p-頂部 P. 使用較低的值忽略較少的選項。設定為 0 或 1.0 以停用。如果 pk 兩者都已啟用,則 p 會在 k 之後執行。

    預設 下限 最大

    0.75

    0

    1

  • k — 前 K。指定模型用來產生下一個權杖的權杖選擇數目。如果 pk 兩者都已啟用,則 p 會在 k 之後執行。

    預設 下限 最大

    0

    0

    500

  • max_token — 指定要在產生的回應中使用的記號數目上限。

    預設 下限 最大

    20

    1

    4096

  • stop_序列-配置模型可識取的最多四個序列。停止序列後,模型停止產生進一步的記號。傳回的文字不包含停止序列。

Response

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

{ "generations": [ { "finish_reason": "COMPLETE | MAX_TOKENS | ERROR | ERROR_TOXIC", "id": string, "text": string, "likelihood" : float, "token_likelihoods" : [{"token" : 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— 該模型產生了一個被認為是有毒的答复。 finish_reason只有當 is_finished = 時才會傳回true。(不一定會傳回)。

    • is_finished - 僅在 streamtrue 時使用的布林值欄位,表示是否有其他記號會以串流回應的一部分的形式產生。(不一定會傳回)

    • text - 產生的文字。

    • index - 在串流回應中,用於判斷指定的記號屬於哪一世代。當僅傳輸一個回應時,所有記號都屬於同一世代,並且不傳回索引。因此,僅會在 num_generations 的值大於 1 的串流請求中傳回 index

  • prompt— 來自輸入要求的提示 (永遠傳回)。

  • id - 請求的識別符 (始終傳回)。

如需詳細資訊,請參閱Cohere文件中的 https://docs.cohere.com/reference/generate

程式碼範例

此範例顯示如何呼叫CohereCommand模型。

# 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()