Aleph Alpha 임베딩 연동

Aleph Alpha 임베딩 연동 (aleph-alpha)

Aleph Alpha는 **멀티모달(multimodal)이면서 다국어(multilingual)**를 지원하는 임베딩 제공자예요. 이들의 API로 텍스트와 이미지 임베딩을 만들 수 있는데, 둘 다 같은 잠재 공간(latent space) 안에 들어와요. 그래서 이미지와 텍스트를 같은 벡터 공간에서 비교할 수 있죠.

공식 Python 클라이언트가 있어서 pip로 설치할 수 있어요.

pip install aleph-alpha-client

동기(synchronous) 클라이언트와 비동기(asynchronous) 클라이언트가 모두 제공돼요. 이미지의 임베딩을 얻어 Qdrant에 저장하는 과정을 코드로 보면 다음과 같아요.

출처: Qdrant 공식 문서 — aleph-alpha

이미지 임베딩을 Qdrant에 저장하기

import qdrant_client
from qdrant_client.models import Batch

from aleph_alpha_client import (
    Prompt,
    AsyncClient,
    SemanticEmbeddingRequest,
    SemanticRepresentation,
    ImagePrompt
)

aa_token = "<< your_token >>"
model = "luminous-base"

qdrant_client = qdrant_client.QdrantClient()
async with AsyncClient(token=aa_token) as client:
    prompt = ImagePrompt.from_file("./path/to/the/image.jpg")
    prompt = Prompt.from_image(prompt)

    query_params = {
        "prompt": prompt,
        "representation": SemanticRepresentation.Symmetric,
        "compress_to_size": 128,
    }
    query_request = SemanticEmbeddingRequest(**query_params)
    query_response = await client.semantic_embed(
        request=query_request, model=model
    )

    qdrant_client.upsert(
        collection_name="MyCollection",
        points=Batch(
            ids=[1],
            vectors=[query_response.embedding],
        )
    )

흐름을 짚어볼게요.

  1. AsyncClient로 Aleph Alpha API에 연결해요. 토큰은 aa_token에 넣어요.
  2. ImagePrompt.from_file(...)로 로컬 이미지 파일을 읽고 Prompt.from_image(...)로 임베딩 요청용 프롬프트로 만들어요.
  3. Representationcompress_to_size(여기서는 128) 같은 파라미터로 SemanticEmbeddingRequest를 구성해요.
  4. semantic_embed로 임베딩을 얻고, 그 결과를 Qdrant의 MyCollection 컬렉션에 upsert해요.

텍스트 임베딩을 만들 때

같은 모델로 텍스트 임베딩을 만들고 싶다면 ImagePrompt.from_file을 쓰지 않고, 입력 텍스트를 Prompt.from_text 메서드에 넣어주면 돼요. 이미지 경로 대신 텍스트를 넘겨주는 것만 다르고 나머지 구성은 같아요.

더 알아보기 (Learn more)