本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
Cohere Embed 模型
您向 提出推論請求 Embed 模型InvokeModel,您需要要使用的模型模型 ID。若要取得模型 ID,請參閱 Amazon Bedrock 模型 IDs。
注意
Amazon Bedrock 不支援來自 的串流回應 Cohere Embed 模型。
請求與回應
程式碼範例
此範例示範如何呼叫 Cohere Embed 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()