本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
Cohere Command 模型
你向一个人提出推理请求 Cohere Command 带InvokeModel或的模型 InvokeModelWithResponseStream(直播)。您需要获得希望使用的模型的模型 ID。要获取模型 ID,请参阅Amazon Bedrock 模型 IDs。
请求和响应
代码示例
此示例说明如何调用 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()