本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
Cohere 內嵌 v4
Cohere Embed v4 是一種多模式內嵌模型,支援文字和影像輸入。它可以處理交錯的文字和影像內容,因此非常適合文件理解、視覺化搜尋和多模式擷取應用程式。此模型支援各種內嵌類型,包括 float、int8、uint8、binary 和 ubinary 格式,可設定的輸出維度從 256 到 1536。
的模型 ID Cohere Embed v4是 cohere.embed-v4。
其他用量備註
-
內容長度:支援高達 ~128k 個字符;對於 RAG,較小的區塊通常會改善擷取和成本。
-
影像大小:> 2,458,624 像素的影像會縮減取樣至該大小;< 3,136 像素的影像會向上取樣。
-
交錯輸入:偏好用於類似頁面的多模態內容的 input.content【】,因此文字內容 (例如檔案名稱、實體) 會隨影像一起移動。
請求與回應
- Request
-
內容類型:Application/json
{
"input_type": "search_document | search_query | classification | clustering",
"texts": ["..."], // optional; text-only
"images": ["data:<mime>;base64,..."], // optional; image-only
"inputs": [
{ "content": [
{ "type": "text", "text": "..." },
{ "type": "image_url", "image_url": "data:<mime>;base64,..." }
]
}
], // optional; mixed (interleaved) text+image
"embedding_types": ["float" | "int8" | "uint8" | "binary" | "ubinary"],
"output_dimension": 256 | 512 | 1024 | 1536,
"max_tokens": 128000,
"truncate": "NONE | LEFT | RIGHT"
}
參數
-
input_type (必要) – 新增特殊字符以區分使用案例。允許:search_document、search_query、classification、clustering。對於搜尋/RAG,使用 內嵌您的 corpus search_document和使用 內嵌查詢search_query。
-
texts (選用) – 要內嵌的字串陣列。每次呼叫最多 96 個。如果您使用 texts,請勿在相同的通話images中傳送 。
-
image (選用) – 要內嵌的資料 URL base64 映像陣列。每次呼叫最多 96 個。請勿同時傳送 texts和 images。(inputs用於交錯。)
-
input (選用;混合/融合模式) – 每個項目都有組件內容清單的清單。每個部分都是 { "type": "text", "text": ... }或 { "type": "image_url", "image_url": "data:<mime>;base64,..." }。在此處傳送交錯的類似頁面內容 (例如,PDF 頁面影像 + 字幕/中繼資料)。最多 96 個項目。
-
embeddedding_types (選用) – 一或多個:float、int8、uint8、binary、ubinary。如果省略,則傳回浮點數內嵌。
-
output_dimension (選用) – 選取向量長度。允許:256、512、1024、 1536(如果未指定,1536則預設為預設值)。
-
max_tokens (選用) – 每個輸入物件的截斷預算。此模型最多支援 ~128,000 個權杖;適合用於 RAG 的區塊較小。
-
截斷 (選用) – 如何處理超長輸入:從開頭LEFT捨棄權杖;從結尾RIGHT捨棄權杖;如果輸入超過限制,則NONE傳回錯誤。
限制和調整大小
-
每個請求的項目:最多 96 個映像。原始映像檔案類型必須是 png、jpeg、Webp 或 gif 格式,大小最多可達 5 MB。
-
請求大小上限:總承載約 20 MB。
-
最大輸入字符:最大 128k 字符。 影像檔案會轉換為字符,且字符總數應小於 128k。
-
影像:減少取樣前最多 2,458,624 像素;小於 3,136 像素的影像會向上取樣。提供影像做為 data:<mime>;base64,....
-
權杖會計 (每個inputs項目):來自影像輸入的權杖 ≈ (影像像素 ÷ 784) x 4 交錯文字和影像輸入的權杖 = (影像像素 ÷ 784) x 4 + (文字權杖)
提示:對於 PDFs,將每個頁面轉換為影像,並透過 與相鄰文字部分中的頁面中繼資料 (例如 file_name、實體) inputs一起傳送。
- Response
-
內容類型:Application/json
如果您請求單一內嵌類型 (例如,僅限 float):
{
"id": "string",
"embeddings": [[ /* length = output_dimension */ ]],
"response_type": "embeddings_floats",
"texts": ["..."], // present if text was provided
"inputs": [ { "content": [ ... ] } ] // present if 'inputs' was used
}
如果您請求多個內嵌類型 (例如 ["float","int8"]):
{
"id": "string",
"embeddings": {
"float": [[ ... ]],
"int8": [[ ... ]]
},
"response_type": "embeddings_by_type",
"texts": ["..."], // when text used
"inputs": [ { "content": [ ... ] } ] // when 'inputs' used
}
不同 input_types 的請求和回應
A) 具有精簡 int8 向量的交錯頁面 (影像 + 字幕)
請求
{
"input_type": "search_document",
"inputs": [
{
"content": [
{ "type": "text", "text": "Quarterly ARR growth chart; outlier in Q3." },
{ "type": "image_url", "image_url": "data:image/png;base64,{{BASE64_PAGE_IMG}}" }
]
}
],
"embedding_types": ["int8"],
"output_dimension": 512,
"truncate": "RIGHT",
"max_tokens": 128000
}
回應 (截斷)
{
"id": "836a33cc-61ec-4e65-afaf-c4628171a315",
"embeddings": { "int8": [[ 7, -3, ... ]] },
"response_type": "embeddings_by_type",
"inputs": [
{ "content": [
{ "type": "text", "text": "Quarterly ARR growth chart; outlier in Q3." },
{ "type": "image_url", "image_url": "data:image/png;base64,{{...}}" }
] }
]
}
B) 純文字 corpus 索引 (預設浮點數,1536-dim)
請求
{
"input_type": "search_document",
"texts": [
"RAG system design patterns for insurance claims",
"Actuarial loss triangles and reserving primer"
]
}
回應 (範例)
{
"response_type": "embeddings_floats",
"embeddings": [
[0.0135, -0.0272, ...], // length 1536
[0.0047, 0.0189, ...]
]
}
程式碼範例
- Text input
-
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Shows how to generate embeddings using the Cohere Embed v4 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-v4:0'
text1 = "hello world"
text2 = "this is a test"
input_type = "search_document"
embedding_types = ["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()
- Mixed modalities
-
# 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 v4 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_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"
text = "hello world"
model_id = 'cohere.embed-v4:0'
input_type = "search_document"
image_base64_uri = get_base64_image_uri(image_file_path, image_mime_type)
embedding_types = ["int8","float"]
try:
body = json.dumps({
"inputs": [
{
"content": [
{ "type": "text", "text": text },
{ "type": "image_url", "image_url": "data:image/png;base64,{{image_base64_uri}}" }
]
}
],
"input_type": input_type,
"embedding_types": embedding_types
})
response = generate_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("inputs")
for i, input in enumerate(response_body.get('inputs')):
print(f"\tinput {i}: {input}")
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 embeddings with Cohere model {model_id}.")
if __name__ == "__main__":
main()