CohereEmbed模型 - Amazon Bedrock

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

CohereEmbed模型

您可以使用「您需要InvokeModel您要使用的模型的模型 ID」向模型提出推論請求。Embed若要取得模型 ID,請參閱Amazon 基岩模型 ID

注意

Amazon 基岩不支援來CohereEmbed自模型的串流回應。

請求與回應

Request

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

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

下列是必要的參數。

  • text — 要嵌入的模型的字串陣列。為獲得最佳效能,我們建議將每個文字的長度減少到 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 叢集內嵌項目。

以下是可選參數:

  • 截斷 — 指定 API 如何處理超過最大令牌長度的輸入。請使用下列其中一個:

    • NONE - (預設) 當輸入超過輸入記號長度上限時傳回錯誤。

    • START— 捨棄輸入的開始。

    • END - 捨棄輸入的結尾。

    如果您指定 STARTEND,則模型會捨棄輸入,直到剩餘的輸入完全符合模型的輸入記號長度上限。

  • 嵌入類型 — 指定要傳回的嵌入類型。可選的默認值是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 — 回應的識別符。

  • 響應類型 — 響應類型。這個值一律為 embeddings_floats

  • 內嵌項目 - 內嵌項目陣列,其中每個內嵌項目都是具有 1024 個元素的浮點數陣列。embeddings 陣列的長度與原始 texts 陣列的長度相同。

  • text — 包含針對其傳回內嵌項目的文字項目的陣列。

如需詳細資訊,請參閱 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()