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à.
Questa sezione fornisce i formati del corpo di richiesta e risposta ed esempi di codice per l'utilizzo di Amazon. Titan Multimodal Embeddings G1.
Richiesta e risposta
Il corpo della richiesta viene passato nel body
campo di una InvokeModelrichiesta.
Il corpo della richiesta per Amazon Titan Multimodal Embeddings G1 include i seguenti campi.
{
"inputText": string,
"inputImage": base64-encoded string,
"embeddingConfig": {
"outputEmbeddingLength": 256 | 384 | 1024
}
}
Almeno uno dei seguenti campi è obbligatorio. Includi entrambi per generare un vettore di incorporamento che calcoli la media dei vettori di incorporamento di testo e di immagini risultanti.
-
inputText— Inserisci il testo da convertire in incorporamenti.
-
inputImage— Codifica l'immagine che desideri convertire in incorporamenti in base64 e inserisci la stringa in questo campo. Per esempi su come codificare un'immagine con base64 e decodificare una stringa con codifica base64 e trasformarla in un'immagine, consulta gli esempi di codice.
Il campo seguente è facoltativo.
-
embeddingConfig— Contiene un
outputEmbeddingLength
campo in cui si specifica una delle seguenti lunghezze per il vettore di incorporamento dell'output.-
256
-
384
-
1024 (impostazione predefinita)
-
Codice di esempio
I seguenti esempi mostrano come richiamare Amazon Titan Multimodal Embeddings G1 modello con throughput su richiesta in Python. SDK Seleziona una scheda per visualizzare un esempio per ogni caso d'uso.
Questo esempio mostra come chiamare Amazon Titan Multimodal Embeddings G1 modello per generare incorporamenti di testo.
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Shows how to generate embeddings from text with the Amazon Titan Multimodal Embeddings G1 model (on demand).
"""
import json
import logging
import boto3
from botocore.exceptions import ClientError
class EmbedError(Exception):
"Custom exception for errors returned by Amazon Titan Multimodal Embeddings G1"
def __init__(self, message):
self.message = message
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
def generate_embeddings(model_id, body):
"""
Generate a vector of embeddings for a text input using Amazon Titan Multimodal Embeddings G1 on demand.
Args:
model_id (str): The model ID to use.
body (str) : The request body to use.
Returns:
response (JSON): The embeddings that the model generated, token information, and the
reason the model stopped generating embeddings.
"""
logger.info("Generating embeddings with Amazon Titan Multimodal Embeddings G1 model %s", model_id)
bedrock = boto3.client(service_name='bedrock-runtime')
accept = "application/json"
content_type = "application/json"
response = bedrock.invoke_model(
body=body, modelId=model_id, accept=accept, contentType=content_type
)
response_body = json.loads(response.get('body').read())
finish_reason = response_body.get("message")
if finish_reason is not None:
raise EmbedError(f"Embeddings generation error: {finish_reason}")
return response_body
def main():
"""
Entrypoint for Amazon Titan Multimodal Embeddings G1 example.
"""
logging.basicConfig(level=logging.INFO,
format="%(levelname)s: %(message)s")
model_id = "amazon.titan-embed-image-v1"
input_text = "What are the different services that you offer?"
output_embedding_length = 256
# Create request body.
body = json.dumps({
"inputText": input_text,
"embeddingConfig": {
"outputEmbeddingLength": output_embedding_length
}
})
try:
response = generate_embeddings(model_id, body)
print(f"Generated text embeddings of length {output_embedding_length}: {response['embedding']}")
print(f"Input text token count: {response['inputTextTokenCount']}")
except ClientError as err:
message = err.response["Error"]["Message"]
logger.error("A client error occurred: %s", message)
print("A client error occured: " +
format(message))
except EmbedError as err:
logger.error(err.message)
print(err.message)
else:
print(f"Finished generating text embeddings with Amazon Titan Multimodal Embeddings G1 model {model_id}.")
if __name__ == "__main__":
main()