CohereEmbed模型 - Amazon Bedrock

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

CohereEmbed模型

您可以通过以下方式向Embed模型发出推理请求:InvokeModel您需要要使用的模型的模型 ID。要获取模型 ID,请参阅Amazon Bedrock 模型 IDs

注意

Amazon Bedrock 不支持来自CohereEmbed模型的直播回复。

请求和响应

Request

这些CohereEmbed模型具有以下推理参数。

{ "texts":[string], "input_type": "search_document|search_query|classification|clustering", "truncate": "NONE|START|END", "embedding_types": embedding_types }

以下是必要参数。

  • texts-模型要嵌入的字符串数组。为了获得最佳性能,我们建议将每个文本的长度减小到 512 个以下令牌。1 个令牌约含 4 个字符。

    以下是每次通话的短信数和字符数限制。

    每次通话发短信

    最低 最高

    0 条文本

    96 篇文本

    人物

    最低 最高

    0 个字符

    2048 个字符

  • input_type — 在前面添加特殊标记,以区分每种类型。除非混合使用不同类型进行搜索和检索,否则不应将不同的类型混合在一起。在这种情况下,请在语料库中嵌入 search_document 类型,并在嵌入式查询中嵌入 search_query 类型。

    • search_document — 在搜索使用案例中,使用 search_document 对存储在向量数据库中的嵌入内容的文档进行编码。

    • search_query — 使用 search_query 查询向量数据库以查找相关文档。

    • classification — 使用 classification 将嵌入内容用作文本分类器的输入。

    • clustering — 使用 clustering 对嵌入内容进行聚类。

以下是可选参数:

  • truncate — 指定 API 如何处理长度超过最大令牌长度的输入。使用以下值之一:

    • NONE —(默认)当输入超过最大输入令牌长度时返回错误。

    • START— 丢弃输入的开头。

    • END — 丢弃输入的结尾。

    如果指定 STARTEND,则模型会丢弃输入,直到剩余的输入正好达到模型的最大输入令牌长度。

  • embedding_t ypes — 指定要返回的嵌入类型。可选,默认为None,返回Embed Floats响应类型。可以是以下一种或多种类型:

    • float— 使用此值返回默认的浮点嵌入。

    • int8— 使用此值返回带符号的 int8 嵌入。

    • uint8— 使用此值返回无符号的 int8 嵌入。

    • binary— 使用此值返回带符号的二进制嵌入。

    • ubinary— 使用此值返回无符号的二进制嵌入。

欲了解更多信息,请参阅Cohere文档中的 https://docs.cohere.com/reference/embed

Response

以下是来自对 InvokeModel 的调用的 body 响应。

{ "embeddings": [ [ <array of 1024 floats> ] ], "id": string, "response_type" : "embeddings_floats, "texts": [string] }

body 响应含有以下值:

  • id — 响应的标识符。

  • r@@ esponse_type — 响应类型。此值始终为 embeddings_floats

  • embeddings — 嵌入内容数组,其中每个嵌入内容是包含 1024 个元素的浮点数数组。embeddings 数组的长度将与原 texts 数组的长度相同。

  • texts – 包含为其返回嵌入内容的文本条目的数组。

有关更多信息,请参阅 https://docs.cohere.com/reference/embed

代码示例

此示例说明如何调用CohereEmbed English模型。

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 """ Shows how to generate text embeddings using the Cohere Embed English model. """ import json import logging import boto3 from botocore.exceptions import ClientError logger = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) def generate_text_embeddings(model_id, body): """ Generate text embedding by using the Cohere Embed 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 emdeddings with the Cohere Embed model %s", model_id) accept = '*/*' 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 Embed example. """ logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") model_id = 'cohere.embed-english-v3' text1 = "hello world" text2 = "this is a test" input_type = "search_document" embedding_types = ["int8", "float"] try: body = json.dumps({ "texts": [ text1, text2], "input_type": input_type, "embedding_types": embedding_types} ) response = generate_text_embeddings(model_id=model_id, body=body) response_body = json.loads(response.get('body').read()) print(f"ID: {response_body.get('id')}") print(f"Response type: {response_body.get('response_type')}") print("Embeddings") for i, embedding in enumerate(response_body.get('embeddings')): print(f"\tEmbedding {i}") print(*embedding) print("Texts") for i, text in enumerate(response_body.get('texts')): print(f"\tText {i}: {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 embeddings with Cohere model {model_id}.") if __name__ == "__main__": main()