CohereCommand R和Command R+模型 - Amazon Bedrock

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

CohereCommand R和Command R+模型

您可以通过InvokeModelInvokeModelWithResponseStream(流式传输)向CohereCommand R和CohereCommand R+模型发出推理请求。您需要获得希望使用的模型的模型 ID。要获取模型 ID,请参阅Amazon Bedrock 模型 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 }

以下是必要参数。

  • mes sage —(必填)模型要响应的文本输入。

以下是可选参数。

  • 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 s — 模型在响应中应生成的最大标记数。请注意,设置较低的值可能会导致生成不完整。与或documents字段一起使用时,设置max_tokens可能会导致生成不完整tools或没有世代。

  • 温度-使用较低的值来降低响应中的随机性。通过增加参数的值,可以进一步最大限度地提高随机性。p

    默认 最低 最高

    0.3

    0

    1

  • p — Top P。使用较低的值忽略可能性较小的选项。

    默认 最低 最高

    0.75

    0.01

    0.99

  • k — Top K. 指定模型用于生成下一个令牌的代币选择数。

    默认 最低 最高

    0

    0

    500

  • prompt_truncation — 默认为。 OFF规定提示的构造方式。prompt_truncation设置为后AUTO_PRESERVE_ORDERdocuments将删除chat_history和中的一些元素,以构造符合模型上下文长度限制的提示。在此过程中,将保留文档和聊天记录的顺序。如果将 prompt_truncation `设置为OFF,则不会删除任何元素。

  • f@@ requency_penalt y — 用于减少生成令牌的重复性。值越高,对先前存在的代币施加的惩罚就越大,这与它们在提示符或上一代中已经出现的次数成正比。

    默认 最低 最高

    0

    0

    1

  • p@@ resence_pen alty — 用于减少生成令牌的重复性。与之类似frequency_penalty,唯一的不同是这种惩罚同样适用于所有已经出现的代币,无论它们的确切频率如何。

    默认 最低 最高

    0

    0

    1

  • s@@ eed — 如果指定,则后端将尽最大努力确定性地对令牌进行采样,这样具有相同种子和参数的重复请求应返回相同的结果。但是,不能完全保证决定性。

  • return_promp true t — 指定返回发送给模型的完整提示。默认值为 false。在响应中,prompt字段中的提示。

  • t@@ ools-模型可能建议在生成文本响应之前调用的可用工具(函数)列表。当tools通过(不通过tool_results)时,响应中的texttool_calls字段将为"",响应中的字段将填充需要进行的工具调用的列表。如果不需要进行任何调用,则tool_calls数组将为空。

    有关更多信息,请参阅Cohere文档中的工具使用

    提示

    我们建议您使用 Converse API 将工具的使用集成到您的应用程序中。有关更多信息,请参阅 使用工具完成 Amazon Bedrock 模型回复

    以下是该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" } ] } ]
  • stop_sequences — 停止序列列表。检测到停止序列后,模型将停止生成更多标记。

  • raw_prompting — 指定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 } } }
  • r@@ esponse_id — 聊天完成的唯一标识符

  • text — 模特对聊天消息输入的回应。

  • generation_id — 聊天完成的唯一标识符,与 Cohere 平台上的反馈端点一起使用。

  • 引文 — 生成的回复的内联引文和相关元数据的数组。包含以下字段:

    • start — 引文开始的索引,从 0 开始。

    • end — 引文结束后的索引,从 0 开始。

    • 文本 — 引文所涉及的文本。

    • document_id s — 一组文档 ID,对应于文本中引用的文档。

  • p rompt — 发送给模型的完整提示。指定要返回此return_prompt字段的字段。

  • finish_re ason — 模型停止生成输出的原因。可以是以下任一种:

    • complete — 完成已到达代币的末尾,请确保这是获得最佳性能的完成原因。

    • error_to xic — 由于我们的内容过滤器,无法完成生成。

    • error_limi t — 无法完成生成,因为已达到模型的上下文限制。

    • error-由于出错,无法完成生成。

    • user_cancel — 无法完成生成,因为它已被用户停止。

    • max_token s — 无法完成生成,因为用户在请求中指定了max_tokens限制,并且已达到此限制。可能无法获得最佳性能。

  • tool_calls — 适合调用的工具列表。仅在您指定tools输入字段时才会返回。

    有关更多信息,请参阅Cohere文档中的工具使用

    提示

    我们建议您使用 Converse API 将工具的使用集成到您的应用程序中。有关更多信息,请参阅 使用工具完成 Amazon Bedrock 模型回复

    以下是该tool_calls字段的 JSON 示例。

    [ { "name": "top_song", "parameters": { "sign": "WZPZ" } } ]
  • meta — API 使用情况数据(仅用于流式传输)。

    • api_version— API 版本。该版本在现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()