mirror of
https://github.com/hwchase17/langchain.git
synced 2025-08-10 21:35:08 +00:00
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:
parent
395f057243
commit
e53c10e546
@ -41,7 +41,7 @@
|
|||||||
"### Credentials\n",
|
"### Credentials\n",
|
||||||
"\n",
|
"\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 getpass\n",
|
||||||
"import os\n",
|
"import os\n",
|
||||||
"\n",
|
"\n",
|
||||||
"if not os.getenv(\"CF_API_KEY\"):\n",
|
"if not os.getenv(\"CF_AI_API_KEY\"):\n",
|
||||||
" os.environ[\"CF_API_KEY\"] = getpass.getpass(\n",
|
" os.environ[\"CF_AI_API_KEY\"] = getpass.getpass(\n",
|
||||||
" \"Enter your CloudflareWorkersAI API key: \"\n",
|
" \"Enter your CloudflareWorkersAI API key: \"\n",
|
||||||
" )\n",
|
" )\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
@ -12,24 +12,36 @@
|
|||||||
"\n",
|
"\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",
|
">[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",
|
"\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",
|
"\n",
|
||||||
"## Setting up\n",
|
"## Setting up\n",
|
||||||
"\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",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": 11,
|
||||||
"id": "f60023b8",
|
"id": "f60023b8",
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"ExecuteTime": {
|
||||||
|
"end_time": "2025-05-13T06:00:30.121204Z",
|
||||||
|
"start_time": "2025-05-13T06:00:30.117936Z"
|
||||||
|
}
|
||||||
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"import getpass\n",
|
"import os\n",
|
||||||
"\n",
|
"\n",
|
||||||
"my_account_id = getpass.getpass(\"Enter your Cloudflare account ID:\\n\\n\")\n",
|
"from dotenv import load_dotenv\n",
|
||||||
"my_api_token = getpass.getpass(\"Enter your Cloudflare API token:\\n\\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",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": 12,
|
||||||
"id": "92c5b61e",
|
"id": "92c5b61e",
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"ExecuteTime": {
|
||||||
|
"end_time": "2025-05-13T06:00:31.224996Z",
|
||||||
|
"start_time": "2025-05-13T06:00:31.222981Z"
|
||||||
|
}
|
||||||
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain_cloudflare.embeddings import (\n",
|
"from langchain_cloudflare.embeddings import (\n",
|
||||||
@ -54,25 +71,28 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": 13,
|
||||||
"id": "062547b9",
|
"id": "062547b9",
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"ExecuteTime": {
|
||||||
|
"end_time": "2025-05-13T06:00:32.515031Z",
|
||||||
|
"start_time": "2025-05-13T06:00:31.798590Z"
|
||||||
|
}
|
||||||
|
},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"data": {
|
"data": {
|
||||||
"text/plain": [
|
"text/plain": "(384, [-0.033660888671875, 0.039764404296875, 0.03558349609375])"
|
||||||
"(384, [-0.033627357333898544, 0.03982774540781975, 0.03559349477291107])"
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
"execution_count": 3,
|
"execution_count": 13,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"output_type": "execute_result"
|
"output_type": "execute_result"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"embeddings = CloudflareWorkersAIEmbeddings(\n",
|
"embeddings = CloudflareWorkersAIEmbeddings(\n",
|
||||||
" account_id=my_account_id,\n",
|
" account_id=cf_acct_id,\n",
|
||||||
" api_token=my_api_token,\n",
|
" api_token=cf_ai_token,\n",
|
||||||
" model_name=\"@cf/baai/bge-small-en-v1.5\",\n",
|
" model_name=\"@cf/baai/bge-small-en-v1.5\",\n",
|
||||||
")\n",
|
")\n",
|
||||||
"# single string embeddings\n",
|
"# single string embeddings\n",
|
||||||
@ -82,17 +102,20 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 14,
|
||||||
"id": "e1dcc4bd",
|
"id": "e1dcc4bd",
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"ExecuteTime": {
|
||||||
|
"end_time": "2025-05-13T06:00:33.106160Z",
|
||||||
|
"start_time": "2025-05-13T06:00:32.847232Z"
|
||||||
|
}
|
||||||
|
},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"data": {
|
"data": {
|
||||||
"text/plain": [
|
"text/plain": "(3, 384)"
|
||||||
"(3, 384)"
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
"execution_count": 4,
|
"execution_count": 14,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"output_type": "execute_result"
|
"output_type": "execute_result"
|
||||||
}
|
}
|
||||||
@ -102,14 +125,6 @@
|
|||||||
"batch_query_result = embeddings.embed_documents([\"test1\", \"test2\", \"test3\"])\n",
|
"batch_query_result = embeddings.embed_documents([\"test1\", \"test2\", \"test3\"])\n",
|
||||||
"len(batch_query_result), len(batch_query_result[0])"
|
"len(batch_query_result), len(batch_query_result[0])"
|
||||||
]
|
]
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"id": "52de8b88",
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": []
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user