Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.
Penyematan Teks Amazon Titan
Titan Embeddings G1 - Text tidak mendukung penggunaan parameter inferensi. Bagian berikut merinci format permintaan dan respons dan memberikan contoh kode.
Permintaan dan tanggapan
Badan permintaan diteruskan di body
bidang InvokeModelpermintaan.
- V2 Request
-
Parameter InputText diperlukan. Parameter normalisasi dan dimensi bersifat opsional.
-
InputText - Masukkan teks untuk dikonversi ke embedding.
-
normalize - (opsional) Bendera yang menunjukkan apakah akan menormalkan penyematan output atau tidak. Default ke true.
-
dimensi - (opsional) Jumlah dimensi yang harus dimiliki oleh penyematan keluaran. Nilai-nilai berikut diterima: 1024 (default), 512, 256.
-
EmbeddingTypes — (opsional) Menerima daftar yang berisi “float”, “binary”, atau keduanya. Default ke
float
.
{ "inputText": string, "dimensions": int, "normalize": boolean, "embeddingTypes": list }
-
- V2 Response
-
Bidang dijelaskan di bawah ini.
-
embedding — Sebuah array yang mewakili vektor embedding dari input yang Anda berikan. Ini akan selalu menjadi tipe
float
. -
inputTextTokenHitung — Jumlah token dalam input.
-
embeddingsByType — Kamus atau peta daftar penyematan. Tergantung pada input, daftar “float”, “biner”, atau keduanya.
-
Contoh:
"embeddingsByType": {"binary": [int,..], "float": [float,...]}
-
Bidang ini akan selalu muncul. Bahkan jika Anda tidak menentukan
embeddingTypes
input Anda, masih akan ada “float”. Contoh:"embeddingsByType": {"float": [float,...]}
-
{ "embedding": [float, float, ...], "inputTextTokenCount": int, "embeddingsByType": {"binary": [int,..], "float": [float,...]} }
-
- G1 Request
-
Satu-satunya bidang yang tersedia adalah
inputText
, di mana Anda dapat menyertakan teks untuk dikonversi menjadi penyematan.{ "inputText": string }
- G1 Response
-
Respons berisi bidang-bidang berikut.
body
{ "embedding": [float, float, ...], "inputTextTokenCount": int }
Bidang dijelaskan di bawah ini.
-
embedding — Sebuah array yang mewakili vektor embedding dari input yang Anda berikan.
-
inputTextTokenHitung — Jumlah token dalam input.
-
Contoh kode
Contoh berikut menunjukkan cara memanggil model Amazon Titan Embeddings untuk menghasilkan penyematan. Pilih tab yang sesuai dengan model yang Anda gunakan:
- Amazon Titan Embeddings G1 - Text
-
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 """ Shows how to generate an embedding with the Amazon Titan Embeddings G1 - Text model (on demand). """ import json import logging import boto3 from botocore.exceptions import ClientError logger = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) def generate_embedding(model_id, body): """ Generate an embedding with the vector representation of a text input using Amazon Titan Embeddings G1 - Text on demand. Args: model_id (str): The model ID to use. body (str) : The request body to use. Returns: response (JSON): The embedding created by the model and the number of input tokens. """ logger.info("Generating an embedding with Amazon Titan Embeddings G1 - Text 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()) return response_body def main(): """ Entrypoint for Amazon Titan Embeddings G1 - Text example. """ logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") model_id = "amazon.titan-embed-text-v1" input_text = "What are the different services that you offer?" # Create request body. body = json.dumps({ "inputText": input_text, }) try: response = generate_embedding(model_id, body) print(f"Generated an embedding: {response['embedding']}") print(f"Input 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)) else: print(f"Finished generating an embedding with Amazon Titan Embeddings G1 - Text model {model_id}.") if __name__ == "__main__": main()
- Amazon Titan Text Embeddings V2
-
Saat menggunakan Titan Text Embeddings V2,
embedding
bidang tidak dalam respons jikaembeddingTypes
hanya berisibinary
.# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 """ Shows how to generate an embedding with the Amazon Titan Text Embeddings V2 Model """ import json import logging import boto3 from botocore.exceptions import ClientError logger = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) def generate_embedding(model_id, body): """ Generate an embedding with the vector representation of a text input using Amazon Titan Text Embeddings G1 on demand. Args: model_id (str): The model ID to use. body (str) : The request body to use. Returns: response (JSON): The embedding created by the model and the number of input tokens. """ logger.info("Generating an embedding with Amazon Titan Text Embeddings V2 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()) return response_body def main(): """ Entrypoint for Amazon Titan Embeddings V2 - Text example. """ logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") model_id = "amazon.titan-embed-text-v2:0" input_text = "What are the different services that you offer?" # Create request body. body = json.dumps({ "inputText": input_text, "embeddingTypes": ["binary"] }) try: response = generate_embedding(model_id, body) print(f"Generated an embedding: {response['embeddingsByType']['binary']}") # returns binary embedding print(f"Input 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)) else: print(f"Finished generating an embedding with Amazon Titan Text Embeddings V2 model {model_id}.") if __name__ == "__main__": main()