Text Embeddings on Cloudflare Workers AI
Cloudflare AI document listed all text embeddings models available.
Both Cloudflare account ID and API token are required. Find how to obtain them from this document.
from langchain.embeddings.cloudflare_workersai import CloudflareWorkersAIEmbeddings
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")
embeddings = CloudflareWorkersAIEmbeddings(
account_id=my_account_id,
api_token=my_api_token,
model_name="@cf/baai/bge-small-en-v1.5",
)
# single string embeddings
query_result = embeddings.embed_query("test")
len(query_result), query_result[:3]
(384, [-0.033627357333898544, 0.03982774540781975, 0.03559349477291107])
# string embeddings in batches
batch_query_result = embeddings.embed_documents(["test1", "test2", "test3"])
len(batch_query_result), len(batch_query_result[0])
(3, 384)