멀티모달 임베딩의 힘 풀어내기
멀티모달 임베딩의 힘 풀어내기
멀티모달 임베딩은 텍스트와 이미지를 임베딩으로 변환해 검색과 분류에 활용할 수 있게 해 줘요 (API v2). 이 가이드에서는 embed 엔드포인트를 이용해 일련의 이미지를 임베딩하는 방법을 살펴볼 거예요.
이 가이드는 Embed API를 사용해요.
자세한 내용은 API 레퍼런스를 참고해 주세요.
이미지 기능은
v4.0과v3.0모델에서만 지원되지만,v4.0에는v3.0에 없는 기능이 있어요. 자세한 내용은 임베딩 문서를 참고해 주세요.
이 가이드에서는 embed 엔드포인트로 일련의 이미지를 임베딩하는 방법을 보여드릴게요. 간단한 그래프 데이터셋을 사용해서 Cohere로 이미지에 대한 시맨틱 검색을 어떻게 수행하는지 설명해요. 검색(retrieval)의 end-to-end 예시를 보고 싶다면 이 노트북을 확인해 보세요.
출처: 문서
멀티모달 임베딩 소개
정보는 종종 여러 가지 양식(modality)으로 표현돼요. 예를 들어 문서에는 텍스트, 이미지, 그래프가 함께 들어 있을 수 있고, 제품은 이미지와 제목, 서면 설명을 통해 묘사될 수 있죠. 이런 요소들의 조합은 주제에 대한 종합적인 의미 이해로 이어지는 경우가 많아요. 기존 임베딩 모델은 단일 양식에 국한되어 있었고, 심지어 멀티모달 임베딩 모델조차도 text-to-text나 text-to-image 검색 작업에서 성능 저하를 겪는 경우가 많았어요. 하지만 embed-v4.0과 embed-v3.0 모델 시리즈는 완전한 멀티모달이라서 이미지와 텍스트를 모두 효과적으로 임베딩할 수 있어요. 우리는 텍스트 간 검색 능력을 희생하지 않으면서도 최고 수준(state-of-the-art)의 성능을 달성했답니다.
멀티모달 임베딩 사용 방법
1. 임베딩할 이미지 준비하기
PYTHON
# Import the necessary packages
import os
import base64
# Defining the function to convert an image to a base 64 Data URL
def image_to_base64_data_url(image_path):
_, file_extension = os.path.splitext(image_path)
file_type = file_extension[1:]
with open(image_path, "rb") as f:
enc_img = base64.b64encode(f.read()).decode("utf-8")
enc_img = f"data:image/{file_type};base64,{enc_img}"
return enc_img
image_path = "<YOUR IMAGE PATH>"
base64_url = image_to_base64_data_url(image_path)
2. Embed 엔드포인트 호출하기
PYTHON
# Import the necessary packages
import cohere
co = cohere.ClientV2(api_key="<YOUR API KEY>")
# format the input_object
image_input = {
"content": [
{"type": "image_url", "image_url": {"url": base64_url}}
]
}
co.embed(
model="embed-v4.0",
inputs=[image_input],
input_type="search_document",
embedding_types=["float"],
)
cURL
curl --request POST \
--url https://api.cohere.ai/v2/embed \
--header 'accept: application/json' \
--header 'content-type: application/json' \
--header "Authorization: bearer ***" \
--data '{
"model": "embed-v4.0",
"inputs": [
{
"content": [
{
"type": "image_url",
"image_url": {
"url": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD..."
}
}
]
}
],
"input_type": "search_document",
"embedding_types": ["float"]
}'
샘플 출력
원본 크기가 1080x1350이고 표준 비트 심도(bit-depth)가 24인 jpeg 이미지를 입력했다면 출력은 대략 다음과 같이 나와요.
JSON
{
"id": "d8f2b461-79a4-44ee-82e4-be601bbb07be",
"embeddings": {
"float_": [[-0.025604248, 0.0154418945, ...]],
"int8": null,
"uint8": null,
"binary": null,
"ubinary": null,
},
"texts": [],
"meta": {
"api_version": {"version": "2", "is_deprecated": null, "is_experimental": null},
"billed_units": {
"input_tokens": null,
"output_tokens": null,
"search_units": null,
"classifications": null,
"images": 1,
},
"tokens": null,
"warnings": null,
},
"images": [{"width": 1080, "height": 1080, "format": "jpeg", "bit_depth": 24}],
"response_type": "embeddings_by_type",
}