Skip to content

Commit e53c10e

Browse files
authored
docs: update Cloudflare examples for env references (#31205)
- [ ] **Docs Update**: "langchain-cloudflare: add env var references in example notebooks" - We've updated our Cloudflare integration example notebooks with examples showing environmental variables to initialize the class instances.
1 parent 395f057 commit e53c10e

File tree

3 files changed

+286
-330
lines changed

3 files changed

+286
-330
lines changed

docs/docs/integrations/chat/cloudflare_workersai.ipynb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"### Credentials\n",
4242
"\n",
4343
"\n",
44-
"Head to https://www.cloudflare.com/developer-platform/products/workers-ai/ to sign up to CloudflareWorkersAI and generate an API key. Once you've done this set the CF_API_KEY environment variable and the CF_ACCOUNT_ID environment variable:"
44+
"Head to https://www.cloudflare.com/developer-platform/products/workers-ai/ to sign up to CloudflareWorkersAI and generate an API key. Once you've done this set the CF_AI_API_KEY environment variable and the CF_ACCOUNT_ID environment variable:"
4545
]
4646
},
4747
{
@@ -56,8 +56,8 @@
5656
"import getpass\n",
5757
"import os\n",
5858
"\n",
59-
"if not os.getenv(\"CF_API_KEY\"):\n",
60-
" os.environ[\"CF_API_KEY\"] = getpass.getpass(\n",
59+
"if not os.getenv(\"CF_AI_API_KEY\"):\n",
60+
" os.environ[\"CF_AI_API_KEY\"] = getpass.getpass(\n",
6161
" \"Enter your CloudflareWorkersAI API key: \"\n",
6262
" )\n",
6363
"\n",

docs/docs/integrations/text_embedding/cloudflare_workersai.ipynb

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,36 @@
1212
"\n",
1313
">[Cloudflare Workers AI](https://developers.cloudflare.com/workers-ai/) allows you to run machine learning models, on the `Cloudflare` network, from your code via REST API.\n",
1414
"\n",
15-
">[Cloudflare AI document](https://developers.cloudflare.com/workers-ai/models/text-embeddings/) listed all text embeddings models available.\n",
15+
">[Workers AI Developer Docs](https://developers.cloudflare.com/workers-ai/models/text-embeddings/) lists all text embeddings models available.\n",
1616
"\n",
1717
"## Setting up\n",
1818
"\n",
19-
"Both Cloudflare account ID and API token are required. Find how to obtain them from [this document](https://developers.cloudflare.com/workers-ai/get-started/rest-api/).\n"
19+
"Both a Cloudflare Account ID and Workers AI API token are required. Find how to obtain them from [this document](https://developers.cloudflare.com/workers-ai/get-started/rest-api/).\n",
20+
"\n",
21+
"You can pass these parameters explicitly or define as environmental variables.\n"
2022
]
2123
},
2224
{
2325
"cell_type": "code",
24-
"execution_count": 2,
26+
"execution_count": 11,
2527
"id": "f60023b8",
26-
"metadata": {},
28+
"metadata": {
29+
"ExecuteTime": {
30+
"end_time": "2025-05-13T06:00:30.121204Z",
31+
"start_time": "2025-05-13T06:00:30.117936Z"
32+
}
33+
},
2734
"outputs": [],
2835
"source": [
29-
"import getpass\n",
36+
"import os\n",
37+
"\n",
38+
"from dotenv import load_dotenv\n",
39+
"\n",
40+
"load_dotenv(\".env\")\n",
3041
"\n",
31-
"my_account_id = getpass.getpass(\"Enter your Cloudflare account ID:\\n\\n\")\n",
32-
"my_api_token = getpass.getpass(\"Enter your Cloudflare API token:\\n\\n\")"
42+
"cf_acct_id = os.getenv(\"CF_ACCOUNT_ID\")\n",
43+
"\n",
44+
"cf_ai_token = os.getenv(\"CF_AI_API_TOKEN\")"
3345
]
3446
},
3547
{
@@ -42,9 +54,14 @@
4254
},
4355
{
4456
"cell_type": "code",
45-
"execution_count": 1,
57+
"execution_count": 12,
4658
"id": "92c5b61e",
47-
"metadata": {},
59+
"metadata": {
60+
"ExecuteTime": {
61+
"end_time": "2025-05-13T06:00:31.224996Z",
62+
"start_time": "2025-05-13T06:00:31.222981Z"
63+
}
64+
},
4865
"outputs": [],
4966
"source": [
5067
"from langchain_cloudflare.embeddings import (\n",
@@ -54,25 +71,28 @@
5471
},
5572
{
5673
"cell_type": "code",
57-
"execution_count": 3,
74+
"execution_count": 13,
5875
"id": "062547b9",
59-
"metadata": {},
76+
"metadata": {
77+
"ExecuteTime": {
78+
"end_time": "2025-05-13T06:00:32.515031Z",
79+
"start_time": "2025-05-13T06:00:31.798590Z"
80+
}
81+
},
6082
"outputs": [
6183
{
6284
"data": {
63-
"text/plain": [
64-
"(384, [-0.033627357333898544, 0.03982774540781975, 0.03559349477291107])"
65-
]
85+
"text/plain": "(384, [-0.033660888671875, 0.039764404296875, 0.03558349609375])"
6686
},
67-
"execution_count": 3,
87+
"execution_count": 13,
6888
"metadata": {},
6989
"output_type": "execute_result"
7090
}
7191
],
7292
"source": [
7393
"embeddings = CloudflareWorkersAIEmbeddings(\n",
74-
" account_id=my_account_id,\n",
75-
" api_token=my_api_token,\n",
94+
" account_id=cf_acct_id,\n",
95+
" api_token=cf_ai_token,\n",
7696
" model_name=\"@cf/baai/bge-small-en-v1.5\",\n",
7797
")\n",
7898
"# single string embeddings\n",
@@ -82,17 +102,20 @@
82102
},
83103
{
84104
"cell_type": "code",
85-
"execution_count": 4,
105+
"execution_count": 14,
86106
"id": "e1dcc4bd",
87-
"metadata": {},
107+
"metadata": {
108+
"ExecuteTime": {
109+
"end_time": "2025-05-13T06:00:33.106160Z",
110+
"start_time": "2025-05-13T06:00:32.847232Z"
111+
}
112+
},
88113
"outputs": [
89114
{
90115
"data": {
91-
"text/plain": [
92-
"(3, 384)"
93-
]
116+
"text/plain": "(3, 384)"
94117
},
95-
"execution_count": 4,
118+
"execution_count": 14,
96119
"metadata": {},
97120
"output_type": "execute_result"
98121
}
@@ -102,14 +125,6 @@
102125
"batch_query_result = embeddings.embed_documents([\"test1\", \"test2\", \"test3\"])\n",
103126
"len(batch_query_result), len(batch_query_result[0])"
104127
]
105-
},
106-
{
107-
"cell_type": "code",
108-
"execution_count": null,
109-
"id": "52de8b88",
110-
"metadata": {},
111-
"outputs": [],
112-
"source": []
113128
}
114129
],
115130
"metadata": {

0 commit comments

Comments
 (0)