|
12 | 12 | "\n",
|
13 | 13 | ">[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",
|
14 | 14 | "\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", |
16 | 16 | "\n",
|
17 | 17 | "## Setting up\n",
|
18 | 18 | "\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" |
20 | 22 | ]
|
21 | 23 | },
|
22 | 24 | {
|
23 | 25 | "cell_type": "code",
|
24 |
| - "execution_count": 2, |
| 26 | + "execution_count": 11, |
25 | 27 | "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 | + }, |
27 | 34 | "outputs": [],
|
28 | 35 | "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", |
30 | 41 | "\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\")" |
33 | 45 | ]
|
34 | 46 | },
|
35 | 47 | {
|
|
42 | 54 | },
|
43 | 55 | {
|
44 | 56 | "cell_type": "code",
|
45 |
| - "execution_count": 1, |
| 57 | + "execution_count": 12, |
46 | 58 | "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 | + }, |
48 | 65 | "outputs": [],
|
49 | 66 | "source": [
|
50 | 67 | "from langchain_cloudflare.embeddings import (\n",
|
|
54 | 71 | },
|
55 | 72 | {
|
56 | 73 | "cell_type": "code",
|
57 |
| - "execution_count": 3, |
| 74 | + "execution_count": 13, |
58 | 75 | "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 | + }, |
60 | 82 | "outputs": [
|
61 | 83 | {
|
62 | 84 | "data": {
|
63 |
| - "text/plain": [ |
64 |
| - "(384, [-0.033627357333898544, 0.03982774540781975, 0.03559349477291107])" |
65 |
| - ] |
| 85 | + "text/plain": "(384, [-0.033660888671875, 0.039764404296875, 0.03558349609375])" |
66 | 86 | },
|
67 |
| - "execution_count": 3, |
| 87 | + "execution_count": 13, |
68 | 88 | "metadata": {},
|
69 | 89 | "output_type": "execute_result"
|
70 | 90 | }
|
71 | 91 | ],
|
72 | 92 | "source": [
|
73 | 93 | "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", |
76 | 96 | " model_name=\"@cf/baai/bge-small-en-v1.5\",\n",
|
77 | 97 | ")\n",
|
78 | 98 | "# single string embeddings\n",
|
|
82 | 102 | },
|
83 | 103 | {
|
84 | 104 | "cell_type": "code",
|
85 |
| - "execution_count": 4, |
| 105 | + "execution_count": 14, |
86 | 106 | "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 | + }, |
88 | 113 | "outputs": [
|
89 | 114 | {
|
90 | 115 | "data": {
|
91 |
| - "text/plain": [ |
92 |
| - "(3, 384)" |
93 |
| - ] |
| 116 | + "text/plain": "(3, 384)" |
94 | 117 | },
|
95 |
| - "execution_count": 4, |
| 118 | + "execution_count": 14, |
96 | 119 | "metadata": {},
|
97 | 120 | "output_type": "execute_result"
|
98 | 121 | }
|
|
102 | 125 | "batch_query_result = embeddings.embed_documents([\"test1\", \"test2\", \"test3\"])\n",
|
103 | 126 | "len(batch_query_result), len(batch_query_result[0])"
|
104 | 127 | ]
|
105 |
| - }, |
106 |
| - { |
107 |
| - "cell_type": "code", |
108 |
| - "execution_count": null, |
109 |
| - "id": "52de8b88", |
110 |
| - "metadata": {}, |
111 |
| - "outputs": [], |
112 |
| - "source": [] |
113 | 128 | }
|
114 | 129 | ],
|
115 | 130 | "metadata": {
|
|
0 commit comments