

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

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

**Topics**
+ [Richiesta e risposta](#model-parameters-embed-v3-request-response)
+ [esempio di codice](#api-inference-examples-cohere-embed-v3)

## Richiesta e risposta
<a name="model-parameters-embed-v3-request-response"></a>

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

I modelli Cohere Embed hanno i seguenti parametri di inferenza. 

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

I seguenti sono parametri obbligatori.
+ **texts**: un array di stringhe da incorporare nel modello. Per prestazioni ottimali, consigliamo di ridurre la lunghezza di ogni testo a meno di 512 token. 1 token corrisponde a circa 4 caratteri.

  Di seguito sono riportati i limiti di testo per chiamata e per carattere.

**Testi per chiamata**  
    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/it_it/bedrock/latest/userguide/model-parameters-embed-v3.html)

**Caratteri**  
    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/it_it/bedrock/latest/userguide/model-parameters-embed-v3.html)
+ **input\$1type**: aggiunto all’inizio dei token speciali per differenziare i vari tipi. Non conviene mischiare tipi diversi, tranne quelli per la ricerca e il recupero. In questo caso, incorpora il corpus con il tipo `search_document` e le query incorporate con il tipo `search_query`. 
  + `search_document`: nei casi d’uso legati alla ricerca, utilizza `search_document` per codificare i documenti per gli embedding archiviati in un database vettoriale.
  + `search_query`: utilizza `search_query` per interrogare il database vettoriale al fine di trovare i documenti pertinenti.
  + `classification`: utilizza `classification` quando gli incorporamenti sono usati come input per un classificatore di testo.
  + `clustering`: utilizza `clustering` per raggruppare gli incorporamenti.
  + `images`: array di immagini.
    + Un array di URI di dati immagine che il modello deve incorporare. Il numero massimo di immagini per chiamata è 1 (ovvero, il modello supporta solo un’input dell’immagine).
    + L’immagine deve essere un URI dati valido. L’immagine deve essere in formato image/jpeg o image/png e avere una dimensione massima di 5 MB.
    + È necessario fornire solo uno dei due elementi: “immagini” o “testi”.

I seguenti sono parametri opzionali:
+  **truncate**: specifica in che modo l’API gestisce gli input maggiori della lunghezza massima del token. Utilizzare una delle seguenti operazioni:
  + `NONE`: (impostazione predefinita) restituisce un errore quando l’input supera la lunghezza massima del token di input. 
  + `START`: elimina l’inizio dell’input. 
  + `END`: elimina la fine dell’input.

  Se specifichi `START` o`END`, il modello elimina l’input finché quello rimanente non raggiunge esattamente la lunghezza massima del token di input per il modello.
+  **embedding\$1types**: specifica i tipi di embedding che devono essere restituiti. Facoltativo e predefinito è `None`, che restituisce il tipo di risposta `Embed Floats`. Può essere uno o più dei seguenti tipi:
  + `float`: utilizza questo valore per restituire gli embedding float predefiniti. 
  + `int8`: utilizza questo valore per restituire gli embedding int8 firmati. 
  + `uint8`: utilizza questo valore per restituire gli embedding int8 firmati. 
  + `binary`: utilizza questo valore per restituire gli embedding binari firmati. 
  + `ubinary`: utilizza questo valore per restituire gli embedding binari non firmati. 

Per ulteriori informazioni, consulta [https://docs.cohere.com/reference/embed](https://docs.cohere.com/reference/embed) nella documentazione Cohere.

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

Di seguito è riportata la risposta `body` da una chiamata `InvokeModel`.

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

La risposta `body` ha i seguenti campi possibili:
+ **id**: un identificatore per la risposta. 
+ **response\$1type**: il tipo di risposta. Questo valore è sempre `embeddings_floats`. 
+ **embeddings**: un array di incorporamenti, ognuno dei quali è un array di numeri a virgola mobile con 1024 elementi. La lunghezza dell’array `embeddings` sarà uguale alla lunghezza dell’array `texts` originale. 
+ **texts**: un array contenente le voci di testo per le quali sono stati restituiti incorporamenti. 
+ **images**: un array di una descrizione per ogni input dell’immagine.

  Un `image_description`image\$1description ha il seguente formato:

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

  Se l’immagine è stata utilizzata come input, il campo di risposta `“texts”` sarà un array vuoto. Il contrario non è vero (ovvero, quando si utilizzano testi, `“images”` non saranno nella risposta)

Per ulteriori informazioni, consulta [https://docs.cohere.com/reference/embed](https://docs.cohere.com/reference/embed).

------

## esempio di codice
<a name="api-inference-examples-cohere-embed-v3"></a>

Questo esempio mostra come chiamare il modello *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()
```

**Input dell’immagine**

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