Cloudflare Workers AI 임베딩
Cloudflare Workers AI 임베딩
Cloudflare Workers AI의 임베딩 모델을 LlamaIndex에서 사용하는 예시예요. Cloudflare 계정 ID와 API 토큰으로 임베딩 모델을 만들어 텍스트(와 배치)를 벡터화하는 방법을 보여줘요.
출처: 문서
본문
설정 (Setup)
pip로 라이브러리를 설치해요.
%pip install llama-index-embeddings-cloudflare-workersai
# %pip install -e ~/llama_index/llama-index-integrations/embeddings/llama-index-embeddings-cloudflare-workersai
Cloudflare Workers AI에 접근하려면 Cloudflare 계정 ID와 API 토큰이 모두 필요해요. 계정 ID와 API 토큰을 얻으려면 이 문서의 지침을 따라 주세요.
# 계정 ID와 API 토큰으로 초기화
# import os
# my_account_id = "example_id"
# my_api_token = "example_token"
# os.environ["CLOUDFLARE_AUTH_TOKEN"] = "my_api_token"
import getpass
my_account_id = getpass.getpass("Enter your Cloudflare account ID:\n\n")
my_api_token = getpass.getpass("Enter your Cloudflare API token:\n\n")
텍스트 임베딩 예시
from llama_index.embeddings.cloudflare_workersai import CloudflareEmbedding
my_embed = CloudflareEmbedding(
account_id=my_account_id,
auth_token=my_api_token,
model="@cf/baai/bge-small-en-v1.5",
)
embeddings = my_embed.get_text_embedding("Why sky is blue")
print(len(embeddings))
print(embeddings[:5])
384
[-0.04786296561360359, -0.030788540840148926, -0.07126234471797943, -0.04107927531003952, 0.02904760278761387]
배치로 임베딩하기
배치 크기와 관련해, Cloudflare의 제한은 최대 100이에요. (2024-03-31 기준)
embeddings = my_embed.get_text_embedding_batch(
["Why sky is blue", "Why roses are red"]
)
print(len(embeddings))
print(len(embeddings[0]))
print(embeddings[0][:5])
print(embeddings[1][:5])
2
384
[-0.04786296561360359, -0.030788540840148926, -0.07126234471797943, -0.04107927531003952, 0.02904760278761387]
[-0.08951402455568314, -0.015274363569915295, 0.04728245735168457, 0.05478525161743164, 0.05978189781308174]