Mixedbread AI 임베딩

Mixedbread AI 임베딩

MixedBread AI의 임베딩 모델은 커스텀 인코딩 포맷(binary, int, float, base64 등), 임베딩 차원(Matryoshka), 컨텍스트 프롬프트를 두루 지원해요. 이 페이지를 따라 하다 보면 LlamaIndex에서 Mixedbread AI 임베딩을 손쉽게 활용할 수 있게 돼요.

출처: 문서

본문

Colab에서 이 노트북을 여는 경우라면 LlamaIndex 🦙 설치가 필요할 거예요.

%pip install llama-index-embeddings-mixedbreadai
!pip install llama-index
import os
from llama_index.embeddings.mixedbreadai import MixedbreadAIEmbedding
# API Key and Embedding Initialization


# You can visit https://www.mixedbread.ai/api-reference#quick-start-guide
# to get an api key
mixedbread_api_key = os.environ.get("MXBAI_API_KEY", "your-api-key")


# Please check https://www.mixedbread.ai/docs/embeddings/models#whats-new-in-the-mixedbread-embed-model-family
# for our embedding models
model_name = "mixedbread-ai/mxbai-embed-large-v1"
oven = MixedbreadAIEmbedding(api_key=mixedbread_api_key, model_name=model_name)


embeddings = oven.get_query_embedding("Why bread is so tasty?")


print(len(embeddings))
print(embeddings[:5])
1024
[0.01128387451171875, 0.031097412109375, -0.00606536865234375, 0.0291748046875, -0.038604736328125]

컨텍스트 임베딩용 프롬프트 사용하기

프롬프트를 주면 이후 작업에서 임베딩이 어떻게 활용될지 모델이 더 잘 이해할 수 있어 성능이 높아져요. Mixedbread AI의 실험 결과, 도메인 특화 프롬프트를 쓰면 성능이 올라간다고 해요.

prompt_for_retrieval = (
    "Represent this sentence for searching relevant passages:"
)


contextual_oven = MixedbreadAIEmbedding(
    api_key=mixedbread_api_key,
    model_name=model_name,
    prompt=prompt_for_retrieval,
)


contextual_embeddings = contextual_oven.get_query_embedding(
    "What bread is invented in Germany?"
)


print(len(contextual_embeddings))
print(contextual_embeddings[:5])
1024
[-0.0235443115234375, -0.0152435302734375, 0.008392333984375, 0.00336456298828125, -0.044647216796875]

양자화(Quantization)와 Matryoshka 지원

Mixedbread AI 임베딩은 임베딩 크기를 줄여 저장 공간을 절약하면서도 대부분의 성능을 유지할 수 있게 양자화와 matryoshka를 지원해요. 자세한 내용은 아래 포스트를 참고하세요.

다양한 인코딩 포맷 사용하기

기본 encoding_format은 float예요. 그 외에도 float16, binary, ubinary, int8, uint8, base64를 지원해요.

# with `binary` embedding types
binary_oven = MixedbreadAIEmbedding(
    api_key=mixedbread_api_key,
    model_name=model_name,
    encoding_format="binary",
)


binary_embeddings = binary_oven.get_text_embedding(
    "The bread is tiny but still filling!"
)


print(len(binary_embeddings))
print(binary_embeddings[:5])
128
[-121.0, 96.0, -108.0, 111.0, 110.0]

다양한 임베딩 차원 사용하기

Mixedbread AI 임베딩 모델은 Matryoshka 차원 절단을 지원해요. 기본 차원은 모델의 최대값으로 설정돼요. 어떤 모델이 Matryoshka를 지원하는지는 웹사이트에서 확인해 보세요.

# with truncated dimension
half_oven = MixedbreadAIEmbedding(
    api_key=mixedbread_api_key,
    model_name=model_name,
    dimensions=512,  # 1024 is the maximum of `mxbai-embed-large-v1`
)


half_embeddings = half_oven.get_text_embedding(
    "I want the better half of my bread."
)


print(len(half_embeddings))
print(half_embeddings[:5])
512
[-0.014221191, -0.013671875, -0.03314209, 0.025909424, -0.035095215]

더 알아보기 (Learn more)