

Las traducciones son generadas a través de traducción automática. En caso de conflicto entre la traducción y la version original de inglés, prevalecerá la version en inglés.

# Cohere Embed v3
<a name="model-parameters-embed-v3"></a>

**Topics**
+ [Solicitud y respuesta](#model-parameters-embed-v3-request-response)
+ [Ejemplo de código](#api-inference-examples-cohere-embed-v3)

## Solicitud y respuesta
<a name="model-parameters-embed-v3-request-response"></a>

------
#### [ Request ]

Los modelos Cohere Embed tienen los siguientes parámetros de inferencia. 

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

Los siguientes parámetros son obligatorios.
+ **texts**: una matriz de cadenas para que el modelo las incruste. Para un rendimiento óptimo, recomendamos reducir la longitud de cada texto a menos de 512 tokens. 1 token tiene unos 4 caracteres.

  Los siguientes son los límites de texto por llamada y de caracteres.

**Textos por llamada**  
    
[See the AWS documentation website for more details](http://docs.aws.amazon.com/es_es/bedrock/latest/userguide/model-parameters-embed-v3.html)

**Caracteres**  
    
[See the AWS documentation website for more details](http://docs.aws.amazon.com/es_es/bedrock/latest/userguide/model-parameters-embed-v3.html)
+ **input\_type**: antepone tokens especiales para diferenciar cada tipo entre sí. No se deben mezclar tipos diferentes, excepto cuando se mezclan tipos para la búsqueda y la recuperación. En este caso, incruste el corpus con el tipo `search_document` y las consultas incrustadas con el tipo `search_query`. 
  + `search_document`: en los casos de uso de búsquedas, use `search_document` cuando codifique documentos para incrustarlos y guardarlos en una base de datos vectorial.
  + `search_query`: use `search_query` al consultar su base de datos vectorial para encontrar documentos relevantes.
  + `classification`: use `classification` cuando utilice incrustaciones como entrada a un clasificador de texto.
  + `clustering`: use `clustering` para agrupar las incrustaciones.
  + `images`: es una matriz de imágenes.
    + Una matriz de URI de datos de imagen para que el modelo los incruste. El número máximo de imágenes por llamada es 1 (es decir, el modelo solo admite una entrada de imagen).
    + La imagen debe ser un URI de datos válido. La imagen debe estar en formato image/jpeg o image/png y tener un tamaño máximo de 5 MB.
    + Solo se deben proporcionar “imágenes” o “textos”.

Los siguientes parámetros son opcionales:
+  **truncate**: especifica cómo gestiona la API las entradas que superen la longitud máxima del token. Utilice una de las siguientes:
  + `NONE`: (predeterminado) devuelve un error cuando la entrada supera la longitud máxima del token de entrada. 
  + `START`: descarta el inicio de la entrada. 
  + `END`: descarta el final de la entrada.

  Si especifica `START` o`END`, el modelo descarta la entrada hasta que la entrada restante tenga exactamente la longitud máxima del token de entrada para el modelo.
+  **embedding\_types**: especifica los tipos de incrustaciones que desea que se devuelvan. Es opcional pero el valor predeterminado es `None`, que devuelve el tipo de respuesta `Embed Floats`. Puede tratarse de uno o más de los siguientes tipos:
  + `float`: utilice este valor para devolver las incrustaciones flotantes predeterminadas. 
  + `int8`: utilice este valor para devolver las incrustaciones int8 firmadas. 
  + `uint8`: utilice este valor para devolver las incrustaciones int8 no firmadas. 
  + `binary`: utilice este valor para devolver las incrustaciones binarias firmadas. 
  + `ubinary`: utilice este valor para devolver las incrustaciones binarias no firmadas. 

Para obtener más información, consulte [https://docs.cohere.com/reference/embed](https://docs.cohere.com/reference/embed) en la documentación de Cohere.

------
#### [ Response ]

La respuesta `body` de una llamada a `InvokeModel` es la siguiente:

```
{
    "embeddings": [
        [ {{array of 1024 floats.}} ]
    ],
    "id": string,
    "response_type" : "embeddings_floats,
    "texts": [string],
    "images": [image_description]
}
```

La respuesta `body` tiene los siguientes campos posibles:
+ **id**: identificador de la respuesta. 
+ **response\_type**: es el tipo de respuesta. Este valor siempre es `embeddings_floats`. 
+ **incrustaciones**: una matriz de incrustaciones, en la que cada incrustación es una matriz de elementos flotantes con 1024 elementos. La longitud de la matriz `embeddings` será la misma que la longitud de la matriz `texts` original. 
+ **textos**: una matriz que contiene las entradas de texto para las que se devolvieron las incrustaciones. 
+ **images**: una matriz de descripciones para cada entrada de imagen.

  `image_description`image\_description tiene este formato:

  ```
  {
      "width": long,
      "height": long,
      "format": string,
      "bit_depth": long
  }
  ```

  Si se utilizó una imagen como entrada, el campo `“texts”` de la respuesta será una matriz vacía. No ocurre así al revés: cuando se usen textos, las `“images”` no estarán en la respuesta.

Para obtener más información, consulte [https://docs.cohere.com/reference/embed](https://docs.cohere.com/reference/embed).

------

## Ejemplo de código
<a name="api-inference-examples-cohere-embed-v3"></a>

En este ejemplo se muestra cómo llamar al modelo *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, region_name):
    """
    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.
        region_name (str): The AWS region to invoke the model on
    Returns:
        dict: The response from the model.
    """

    logger.info("Generating text embeddings with the Cohere Embed model %s", model_id)

    accept = '*/*'
    content_type = 'application/json'

    bedrock = boto3.client(service_name='bedrock-runtime', region_name=region_name)

    response = bedrock.invoke_model(
        body=body,
        modelId=model_id,
        accept=accept,
        contentType=content_type
    )

    logger.info("Successfully generated embeddings 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")
    
    region_name = 'us-east-1'

    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, region_name=region_name)

        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")
        embeddings = response_body.get('embeddings')
        for i, embedding_type in enumerate(embeddings):
            print(f"\t{embedding_type} Embeddings:")
            print(f"\t{embeddings[embedding_type]}")

        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()
```

**Imagen de entrada**

```
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Shows how to generate image embeddings using the Cohere Embed English model.
"""
import json
import logging
import boto3
import base64


from botocore.exceptions import ClientError

logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)

def get_base64_image_uri(image_file_path: str, image_mime_type: str):
    with open(image_file_path, "rb") as image_file:
        image_bytes = image_file.read()
        base64_image = base64.b64encode(image_bytes).decode("utf-8")
    return f"data:{image_mime_type};base64,{base64_image}"


def generate_image_embeddings(model_id, body, region_name):
    """
    Generate image embedding by using the Cohere Embed model.
    Args:
        model_id (str): The model ID to use.
        body (str) : The reqest body to use.
        region_name (str): The AWS region to invoke the model on
    Returns:
        dict: The response from the model.
    """

    logger.info("Generating image embeddings with the Cohere Embed model %s", model_id)

    accept = '*/*'
    content_type = 'application/json'

    bedrock = boto3.client(service_name='bedrock-runtime', region_name=region_name)

    response = bedrock.invoke_model(
        body=body,
        modelId=model_id,
        accept=accept,
        contentType=content_type
    )

    logger.info("Successfully generated embeddings 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")
    
    region_name = 'us-east-1'

    image_file_path = "image.jpg"
    image_mime_type = "image/jpg"

    model_id = 'cohere.embed-english-v3'
    input_type = "image"
    images = [get_base64_image_uri(image_file_path, image_mime_type)]
    embedding_types = ["int8", "float"]

    try:
        body = json.dumps({
            "images": images,
            "input_type": input_type,
            "embedding_types": embedding_types
        })
        
        response = generate_image_embeddings(model_id=model_id, body=body, region_name=region_name)

        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")
        embeddings = response_body.get('embeddings')
        for i, embedding_type in enumerate(embeddings):
            print(f"\t{embedding_type} Embeddings:")
            print(f"\t{embeddings[embedding_type]}")

        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()
```