主题
设置
通过 pip 安装库
%pip install llama-index-embeddings-cloudflare-workersai
# %pip install -e ~/llama_index/llama-index-integrations/embeddings/llama-index-embeddings-cloudflare-workersai1
2
2
要访问 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")1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
文本嵌入示例
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])1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
384
[-0.04786296561360359, -0.030788540840148926, -0.07126234471797943, -0.04107927531003952, 0.02904760278761387]1
2
2
批量嵌入
至于批大小,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])1
2
3
4
5
6
7
2
3
4
5
6
7
2
384
[-0.04786296561360359, -0.030788540840148926, -0.07126234471797943, -0.04107927531003952, 0.02904760278761387]
[-0.08951402455568314, -0.015274363569915295, 0.04728245735168457, 0.05478525161743164, 0.05978189781308174]1
2
3
4
2
3
4