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.
This commit is contained in:
Collier King 2025-05-13 15:18:27 -05:00 committed by GitHub
parent 395f057243
commit e53c10e546
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 286 additions and 330 deletions

View File

@ -41,7 +41,7 @@
"### Credentials\n",
"\n",
"\n",
"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:"
"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:"
]
},
{
@ -56,8 +56,8 @@
"import getpass\n",
"import os\n",
"\n",
"if not os.getenv(\"CF_API_KEY\"):\n",
" os.environ[\"CF_API_KEY\"] = getpass.getpass(\n",
"if not os.getenv(\"CF_AI_API_KEY\"):\n",
" os.environ[\"CF_AI_API_KEY\"] = getpass.getpass(\n",
" \"Enter your CloudflareWorkersAI API key: \"\n",
" )\n",
"\n",

View File

@ -12,24 +12,36 @@
"\n",
">[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",
"\n",
">[Cloudflare AI document](https://developers.cloudflare.com/workers-ai/models/text-embeddings/) listed all text embeddings models available.\n",
">[Workers AI Developer Docs](https://developers.cloudflare.com/workers-ai/models/text-embeddings/) lists all text embeddings models available.\n",
"\n",
"## Setting up\n",
"\n",
"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"
"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",
"\n",
"You can pass these parameters explicitly or define as environmental variables.\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 11,
"id": "f60023b8",
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2025-05-13T06:00:30.121204Z",
"start_time": "2025-05-13T06:00:30.117936Z"
}
},
"outputs": [],
"source": [
"import getpass\n",
"import os\n",
"\n",
"my_account_id = getpass.getpass(\"Enter your Cloudflare account ID:\\n\\n\")\n",
"my_api_token = getpass.getpass(\"Enter your Cloudflare API token:\\n\\n\")"
"from dotenv import load_dotenv\n",
"\n",
"load_dotenv(\".env\")\n",
"\n",
"cf_acct_id = os.getenv(\"CF_ACCOUNT_ID\")\n",
"\n",
"cf_ai_token = os.getenv(\"CF_AI_API_TOKEN\")"
]
},
{
@ -42,9 +54,14 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 12,
"id": "92c5b61e",
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2025-05-13T06:00:31.224996Z",
"start_time": "2025-05-13T06:00:31.222981Z"
}
},
"outputs": [],
"source": [
"from langchain_cloudflare.embeddings import (\n",
@ -54,25 +71,28 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 13,
"id": "062547b9",
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2025-05-13T06:00:32.515031Z",
"start_time": "2025-05-13T06:00:31.798590Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"(384, [-0.033627357333898544, 0.03982774540781975, 0.03559349477291107])"
]
"text/plain": "(384, [-0.033660888671875, 0.039764404296875, 0.03558349609375])"
},
"execution_count": 3,
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"embeddings = CloudflareWorkersAIEmbeddings(\n",
" account_id=my_account_id,\n",
" api_token=my_api_token,\n",
" account_id=cf_acct_id,\n",
" api_token=cf_ai_token,\n",
" model_name=\"@cf/baai/bge-small-en-v1.5\",\n",
")\n",
"# single string embeddings\n",
@ -82,17 +102,20 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 14,
"id": "e1dcc4bd",
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2025-05-13T06:00:33.106160Z",
"start_time": "2025-05-13T06:00:32.847232Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"(3, 384)"
]
"text/plain": "(3, 384)"
},
"execution_count": 4,
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
@ -102,14 +125,6 @@
"batch_query_result = embeddings.embed_documents([\"test1\", \"test2\", \"test3\"])\n",
"len(batch_query_result), len(batch_query_result[0])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "52de8b88",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {