Skip to content
LlamaIndex 文档版本 跟随上游 源快照 2603984d 更新于 2026-10-18

设置

通过 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 令牌,请按照本文档中的说明操作。

# Initilise with account ID and API token

# 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]

BytesRouter 是社区翻译站点,所有译文仅供参考,一切以官方英文原文为准。