本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
您使用 InvokeModel 或 InvokeModelWithResponseStream (串流) 對CohereCommand模型提出推論請求。您需要您想要使用的模型的模型 ID。若要取得模型 ID,請參閱 Amazon Bedrock 中支援的基礎模型。
請求與回應
這些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
- (預設)不傳回任何概似值。
-
-
串流 – (支援串流的必要項目) 指定
true
以即時piece-by-piece傳回回應,並在程序完成後false
傳回完整回應。 -
logit_bias – 防止模型產生不需要的字符或激勵模型包含所需的字符。格式是
{token_id: bias}
,其中偏差是介於 -10 和 10 之間的浮點數。權杖可以使用任何權杖化服務從文字取得,例如 Cohere的權杖化端點。如需詳細資訊,請參閱 Cohere 文件。 預設 下限 最大 N/A
-10(用於記號偏差)
10(用於記號偏差)
-
num_generations – 模型應傳回的代數上限。
預設 下限 最大 1
1
5
-
截斷 – 指定 API 如何處理超過字符長度上限的輸入。請使用下列其中一個:
-
NONE
- 當輸入超過輸入記號長度上限時傳回錯誤。 -
START
- 捨棄輸入開端。 -
END
- (預設) 捨棄輸入結尾。
如果您指定
START
或END
,則模型會捨棄輸入,直到剩餘的輸入完全符合模型的輸入記號長度上限。 -
-
temperature – 使用較低的值來減少回應中的隨機性。
預設 下限 最大 0.9
0
5
-
p – Top P。使用較低的值來忽略可能性較低的選項。設定為 0 或 1.0 以停用。如果
p
和k
兩者都已啟用,則p
會在k
之後執行。預設 下限 最大 0.75
0
1
-
k – Top K。指定模型用來產生下一個權杖的權杖選擇數目。如果
p
和k
兩者都已啟用,則p
會在k
之後執行。預設 下限 最大 0
0
500
-
max_tokens – 指定要在產生的回應中使用的字符數目上限。
預設 下限 最大 20
1
4096
-
stop_sequences – 設定最多四個模型辨識的序列。停止序列後,模型停止產生進一步的記號。傳回的文字不包含停止序列。
程式碼範例
此範例示範如何呼叫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()