CohereCommand R和Command R+模型 - Amazon Bedrock

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

CohereCommand R和Command R+模型

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

提示

對於交談式應用程式,我們建議您使用匡威 API。Converse API 提供了一組統一的參數,可在支持消息的所有模型中工作。如需詳細資訊,請參閱 開展與匡威對話

請求與回應

Request

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

{ "message": string, "chat_history": [ { "role":"USER or CHATBOT", "message": string } ], "documents": [ {"title": string, "snippet": string}, ], "search_queries_only" : boolean, "preamble" : string, "max_tokens": int, "temperature": float, "p": float, "k": float, "prompt_truncation" : string, "frequency_penalty" : float, "presence_penalty" : float, "seed" : int, "return_prompt" : boolean, "tools" : [ { "name": string, "description": string, "parameter_definitions": { "parameter name": { "description": string, "type": string, "required": boolean } } } ], "tool_results" : [ { "call": { "name": string, "parameters": { "parameter name": string } }, "outputs": [ { "text": string } ] } ], "stop_sequences": [string], "raw_prompting" : boolean }

下列是必要的參數。

  • message — (必填) 文字輸入以供模型回應。

以下是選用參數。

  • chat_history-用戶和模型之間先前消息的列表,旨在為模型提供用於響應用戶消息的模型對話上下文。

    以下是必填欄位。

    • role— 訊息的角色。有效值為USERCHATBOT。記號。

    • message— 訊息的文字內容。

    以下是該chat_history欄位的 JSON 範例

    "chat_history": [ {"role": "USER", "message": "Who discovered gravity?"}, {"role": "CHATBOT", "message": "The man who is widely credited with discovering gravity is Sir Isaac Newton"} ]
  • 文件 — 模型可引用的文字清單,以產生更準確的回覆。每個文檔都是一個字符串字典。產生的產生包括參考其中一些文件的引用文獻。我們建議您將字典中字串的總字數保持在 300 字以下。可以選擇性地提供一個字_excludes段(字符串數組),以忽略顯示給模型的一些鍵-值對。如需詳細資訊,請參閱文件中的「文件模式」Cohere 指南

    以下是documents欄位的 JSON 範例。

    "documents": [ {"title": "Tall penguins", "snippet": "Emperor penguins are the tallest."}, {"title": "Penguin habitats", "snippet": "Emperor penguins only live in Antarctica."} ]
  • 搜索查詢-默認為。false何時true,回應只會包含產生的搜尋查詢清單,但不會進行搜尋,也不message會產生模型對使用者的回覆。

  • 起頭訊號 — 覆寫產生搜尋查詢的預設起頭訊號。對工具使用世代沒有影響。

  • max_token — 模型作為響應一部分應生成的令牌的最大數量。請注意,設定較低的值可能會導致層代不完整。與或documents欄位搭配使用時,設定max_tokens可能會導致層代不完整tools或沒有層代。

  • 溫度 — 使用較低的值來降低回應中的隨機性。隨機性可以通過增加參數的值進一步最大化。p

    預設 下限 最大

    0.3

    0

    1

  • p-頂部 P. 使用較低的值忽略較少的選項。

    預設 下限 最大

    0.75

    0.01

    0.99

  • k — 前 K。指定模型用來產生下一個權杖的權杖選擇數目。

    預設 下限 最大

    0

    0

    500

  • 提示截斷 — 預設為。OFF決定提示的構建方式。prompt_truncation設定為時AUTO_PRESERVE_ORDERdocuments將從chat_history和中刪除某些圖元,以建構符合模型上下文長度限制的提示。在此過程中,文檔和聊天記錄的順序將被保留。將 prompt_truncation `設定為時OFF,不會捨棄任何圖元。

  • 頻率罰款 — 用於減少生成令牌的重複性。值越高,對先前存在的令牌施加的懲罰越強,與它們在提示或上一代中已出現的次數成正比。

    預設 下限 最大

    0

    0

    1

  • 存在罰款 — 用於減少生成令牌的重複性。類似於frequency_penalty,不同之處在於,這個懲罰同樣適用於已經出現的所有令牌,而不管它們的確切頻率如何。

    預設 下限 最大

    0

    0

    1

  • seed — 如果指定,後端將盡最大努力確定性地對令牌進行採樣,以便具有相同種子和參數的重複請求應返回相同的結果。但是,決定論不能完全保證。

  • return_prompt — 指定true以傳回傳送至模型的完整提示。預設值為 false。在響應中,字prompt段中的提示。

  • tools-模型可能會建議在產生文本響應之前調用的可用工具(函數)的列表。傳遞 (不含tool_results) 時,回應中的text欄位將會tools是,而""且回應中的tool_calls欄位會填入需要進行的工具呼叫清單。如果不需要進行調用,則tool_calls數組將為空。

    如需詳細資訊,請參閱Cohere文件中的「工具使用」。

    提示

    我們建議您使用 Converse API 將工具使用整合到您的應用程式中。如需詳細資訊,請參閱 使用工具完成 Amazon 基岩模型回應

    以下是tools欄位的 JSON 範例。

    [ { "name": "top_song", "description": "Get the most popular song played on a radio station.", "parameter_definitions": { "sign": { "description": "The call sign for the radio station for which you want the most popular song. Example calls signs are WZPZ and WKRP.", "type": "str", "required": true } } } ]

    如需詳細資訊,請參閱Cohere文件中的單一步驟工具使用 (函數呼叫)

  • tools_results — 調用前一個聊天回合中模型推薦的工具的結果列表。結果可用來產生文字回應,並在引用文獻中參考。使用時tool_results,也tools必須通過。每個都tool_result包含有關如何調用它的信息,以及字典形式的輸出列表。Cohere 獨特的細粒度引用邏輯需要輸出為列表。如果輸出只是一個項目,例如{"status": 200},您仍然應該將其包裝在列表中。

    如需詳細資訊,請參閱Cohere文件中的「工具使用」。

    以下是tools_results欄位的 JSON 範例。

    [ { "call": { "name": "top_song", "parameters": { "sign": "WZPZ" } }, "outputs": [ { "song": "Elemental Hotel" } ] } ]
  • 止序列-停止序列的列表。偵測到停止序列之後,模型會停止產生進一步的 Token。

  • raw_prompt — 指定true,將使用者傳送至模型而不進行任何預處理,否則message為 false。

Response

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

{ "response_id": string, "text": string, "generation_id": string, "citations": [ { "start": int, "end": int, "text": "string", "document_ids": [ "string" ] } ], "finish_reason": string, "tool_calls": [ { "name": string, "parameters": { "parameter name": string } } ], { "meta": { "api_version": { "version": string }, "billed_units": { "input_tokens": int, "output_tokens": int } } }
  • 回應識別碼 — 聊天完成的唯一識別碼

  • text — 模型對聊天訊息輸入的回應。

  • generation_id — 聊天完成的唯一標識符,與 Cohere 平台上的反饋端點一起使用。

  • 用 — 產生之回覆的內嵌引用和相關中繼資料的陣列。包含下列欄位:

    • start — 引用文獻開始的索引,從 0 開始。

    • end — 引用文獻結束後的索引,從 0 開始。

    • text — 引用文獻所屬的文字。

    • document_ids — 文件 ID 陣列,對應於為文字引用的文件。

  • prompt — 傳送至模型的完整提示。指定要傳回此return_prompt欄位的欄位。

  • 完成原因 — 模型停止產生輸出的原因。可為下列任何一項:

    • complete — 完成達到生成令牌的末尾,請確保這是獲得最佳性能的完成原因。

    • error_toxic — 由於我們的內容過濾器,無法完成生成。

    • error_limit — 無法完成產生,因為已達到模型的前後關聯限制。

    • error — 產生錯誤而無法完成產生。

    • user_cancel — 無法完成產生,因為使用者已停止產生。

    • max_token — 產生無法完成,因為使用者在要求中指定了max_tokens限制,且已達到此限制。可能不會產生最佳效能。

  • 具呼叫 — 要呼叫的適當工具清單。只有在指定tools輸入字段時才返回。

    如需詳細資訊,請參閱Cohere文件中的「工具使用」。

    提示

    我們建議您使用 Converse API 將工具使用整合到您的應用程式中。如需詳細資訊,請參閱 使用工具完成 Amazon 基岩模型回應

    以下是tool_calls欄位的 JSON 範例。

    [ { "name": "top_song", "parameters": { "sign": "WZPZ" } } ]
  • meta — API 使用情況數據(僅存在於流式傳輸)。

    • api_version— 應用程式介面版本。版本位於version欄位中。

    • billed_units— 計費單位。可能值為:

      • input_tokens— 已計費的輸入令牌數量。

      • output_tokens— 已計費的輸出令牌數量。

程式碼範例

這個範例說明如何呼叫CohereCommand R模型。

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 """ Shows how to use the Cohere Command R 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 Command R 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) bedrock = boto3.client(service_name='bedrock-runtime') response = bedrock.invoke_model( body=body, modelId=model_id ) logger.info( "Successfully generated text with Cohere Command R 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-r-v1:0' chat_history = [ {"role": "USER", "message": "What is an interesting new role in AI if I don't have an ML background?"}, {"role": "CHATBOT", "message": "You could explore being a prompt engineer!"} ] message = "What are some skills I should have?" try: body = json.dumps({ "message": message, "chat_history": chat_history, "max_tokens": 2000, "temperature": 0.6, "p": 0.5, "k": 250 }) response = generate_text(model_id=model_id, body=body) response_body = json.loads(response.get('body').read()) response_chat_history = response_body.get('chat_history') print('Chat history\n------------') for response_message in response_chat_history: if 'message' in response_message: print(f"Role: {response_message['role']}") print(f"Message: {response_message['message']}\n") print("Generated text\n--------------") print(f"Stop reason: {response_body['finish_reason']}") print(f"Response text: \n{response_body['text']}") 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()