|
5 | 5 | "id": "b90e936b724dae40", |
6 | 6 | "metadata": {}, |
7 | 7 | "source": [ |
8 | | - "# TiDB Vector SDK V2\n", |
| 8 | + "# TiDB Python SDK V2\n", |
9 | 9 | "\n", |
10 | 10 | "A powerful Python SDK for vector storage and retrieval operations with TiDB.\n", |
11 | 11 | "\n", |
|
25 | 25 | }, |
26 | 26 | { |
27 | 27 | "cell_type": "code", |
28 | | - "execution_count": null, |
29 | 28 | "id": "b81aa3ab", |
30 | 29 | "metadata": {}, |
31 | | - "outputs": [], |
32 | 30 | "source": [ |
33 | 31 | "%pip install autoflow-ai==0.0.1.dev10\n", |
34 | 32 | "%pip install dotenv ipywidgets pymysql sqlmodel" |
35 | | - ] |
| 33 | + ], |
| 34 | + "outputs": [], |
| 35 | + "execution_count": null |
36 | 36 | }, |
37 | 37 | { |
38 | 38 | "cell_type": "markdown", |
|
41 | 41 | "source": [ |
42 | 42 | "#### Configure environment variable\n", |
43 | 43 | "\n", |
44 | | - "Go [tidbcloud.com](http://tidbcloud.com/) or using [tiup playground](https://docs.pingcap.com/tidb/stable/tiup-playground/) to create a free TiDB database cluster\n", |
| 44 | + "Go [tidbcloud.com](http://tidbcloud.com/) or using [tiup playground](https://docs.pingcap.com/tidb/stable/tiup-playground/) to create a free TiDB database cluster.\n", |
45 | 45 | "\n", |
46 | 46 | "Configuration can be provided through environment variables, or using `.env`:" |
47 | 47 | ] |
|
78 | 78 | }, |
79 | 79 | { |
80 | 80 | "cell_type": "code", |
81 | | - "execution_count": 39, |
82 | 81 | "id": "4995a54f311c4b1c", |
83 | | - "metadata": {}, |
84 | | - "outputs": [], |
| 82 | + "metadata": { |
| 83 | + "ExecuteTime": { |
| 84 | + "end_time": "2025-03-10T02:09:23.700467Z", |
| 85 | + "start_time": "2025-03-10T02:09:06.319163Z" |
| 86 | + } |
| 87 | + }, |
85 | 88 | "source": [ |
86 | 89 | "import os\n", |
87 | 90 | "from autoflow.storage.tidb import TiDBClient\n", |
88 | 91 | "\n", |
89 | 92 | "# Format: mysql+pymysql://<username>:<password>@<host>:4000/<database>\n", |
90 | 93 | "db = TiDBClient.connect(os.getenv(\"DATABASE_URL\"))" |
91 | | - ] |
| 94 | + ], |
| 95 | + "outputs": [], |
| 96 | + "execution_count": 3 |
92 | 97 | }, |
93 | 98 | { |
94 | 99 | "cell_type": "markdown", |
|
100 | 105 | }, |
101 | 106 | { |
102 | 107 | "cell_type": "code", |
103 | | - "execution_count": 40, |
104 | 108 | "id": "bdddb9f0a005b74d", |
105 | | - "metadata": {}, |
106 | | - "outputs": [], |
| 109 | + "metadata": { |
| 110 | + "ExecuteTime": { |
| 111 | + "end_time": "2025-03-10T02:09:37.844382Z", |
| 112 | + "start_time": "2025-03-10T02:09:37.823858Z" |
| 113 | + } |
| 114 | + }, |
107 | 115 | "source": [ |
108 | 116 | "from typing import Optional, Any\n", |
109 | 117 | "from autoflow.storage.tidb.base import TiDBModel\n", |
|
127 | 135 | "\n", |
128 | 136 | "\n", |
129 | 137 | "table = db.create_table(schema=Chunk)" |
130 | | - ] |
| 138 | + ], |
| 139 | + "outputs": [], |
| 140 | + "execution_count": 4 |
131 | 141 | }, |
132 | 142 | { |
133 | 143 | "cell_type": "markdown", |
|
141 | 151 | }, |
142 | 152 | { |
143 | 153 | "cell_type": "code", |
144 | | - "execution_count": 46, |
145 | 154 | "id": "baec9a5ae06231be", |
146 | | - "metadata": {}, |
| 155 | + "metadata": { |
| 156 | + "ExecuteTime": { |
| 157 | + "end_time": "2025-03-10T02:09:46.528201Z", |
| 158 | + "start_time": "2025-03-10T02:09:46.490530Z" |
| 159 | + } |
| 160 | + }, |
| 161 | + "source": [ |
| 162 | + "table.insert(\n", |
| 163 | + " Chunk(text=\"The quick brown fox jumps over the lazy dog\", user_id=1),\n", |
| 164 | + ")\n", |
| 165 | + "table.bulk_insert(\n", |
| 166 | + " [\n", |
| 167 | + " Chunk(text=\"A quick brown dog runs in the park\", user_id=2),\n", |
| 168 | + " Chunk(text=\"The lazy fox sleeps under the tree\", user_id=2),\n", |
| 169 | + " Chunk(text=\"A dog and a fox play in the park\", user_id=3),\n", |
| 170 | + " ]\n", |
| 171 | + ")\n", |
| 172 | + "table.rows()" |
| 173 | + ], |
147 | 174 | "outputs": [ |
148 | 175 | { |
149 | 176 | "data": { |
150 | 177 | "text/plain": [ |
151 | | - "4" |
| 178 | + "8" |
152 | 179 | ] |
153 | 180 | }, |
154 | | - "execution_count": 46, |
| 181 | + "execution_count": 5, |
155 | 182 | "metadata": {}, |
156 | 183 | "output_type": "execute_result" |
157 | 184 | } |
158 | 185 | ], |
159 | | - "source": [ |
160 | | - "table.insert(\n", |
161 | | - " Chunk(id=1, text=\"The quick brown fox jumps over the lazy dog\", user_id=1),\n", |
162 | | - ")\n", |
163 | | - "table.bulk_insert(\n", |
164 | | - " [\n", |
165 | | - " Chunk(id=2, text=\"A quick brown dog runs in the park\", user_id=2),\n", |
166 | | - " Chunk(id=3, text=\"The lazy fox sleeps under the tree\", user_id=2),\n", |
167 | | - " Chunk(id=4, text=\"A dog and a fox play in the park\", user_id=3),\n", |
168 | | - " ]\n", |
169 | | - ")\n", |
170 | | - "table.rows()" |
171 | | - ] |
| 186 | + "execution_count": 5 |
172 | 187 | }, |
173 | 188 | { |
174 | 189 | "cell_type": "markdown", |
|
180 | 195 | }, |
181 | 196 | { |
182 | 197 | "cell_type": "code", |
183 | | - "execution_count": 53, |
184 | 198 | "id": "3c4313022f06bd3e", |
185 | | - "metadata": {}, |
186 | | - "outputs": [ |
187 | | - { |
188 | | - "data": { |
189 | | - "text/plain": [ |
190 | | - "[('A quick brown dog runs in the park', 0.665493189763966),\n", |
191 | | - " ('The lazy fox sleeps under the tree', 0.554631888866523)]" |
192 | | - ] |
193 | | - }, |
194 | | - "execution_count": 53, |
195 | | - "metadata": {}, |
196 | | - "output_type": "execute_result" |
| 199 | + "metadata": { |
| 200 | + "ExecuteTime": { |
| 201 | + "end_time": "2025-03-10T02:09:56.362012Z", |
| 202 | + "start_time": "2025-03-10T02:09:56.330090Z" |
197 | 203 | } |
198 | | - ], |
| 204 | + }, |
199 | 205 | "source": [ |
200 | 206 | "chunks = (\n", |
201 | 207 | " table.search(\n", |
|
206 | 212 | " .to_pydantic()\n", |
207 | 213 | ")\n", |
208 | 214 | "[(c.text, c.score) for c in chunks]" |
209 | | - ] |
| 215 | + ], |
| 216 | + "outputs": [ |
| 217 | + { |
| 218 | + "data": { |
| 219 | + "text/plain": [ |
| 220 | + "[('A quick brown dog runs in the park', 0.665493189763966),\n", |
| 221 | + " ('A quick brown dog runs in the park', 0.665493189763966)]" |
| 222 | + ] |
| 223 | + }, |
| 224 | + "execution_count": 6, |
| 225 | + "metadata": {}, |
| 226 | + "output_type": "execute_result" |
| 227 | + } |
| 228 | + ], |
| 229 | + "execution_count": 6 |
210 | 230 | }, |
211 | 231 | { |
212 | 232 | "cell_type": "markdown", |
|
232 | 252 | }, |
233 | 253 | { |
234 | 254 | "cell_type": "code", |
235 | | - "execution_count": 49, |
236 | 255 | "id": "ace02b45", |
| 256 | + "metadata": { |
| 257 | + "ExecuteTime": { |
| 258 | + "end_time": "2025-03-10T02:10:03.161292Z", |
| 259 | + "start_time": "2025-03-10T02:10:03.154627Z" |
| 260 | + } |
| 261 | + }, |
| 262 | + "source": [ |
| 263 | + "chunks = table.query({\"user_id\": 1})\n", |
| 264 | + "[(c.id, c.text, c.user_id) for c in chunks]" |
| 265 | + ], |
| 266 | + "outputs": [ |
| 267 | + { |
| 268 | + "data": { |
| 269 | + "text/plain": [ |
| 270 | + "[(1, 'The quick brown fox jumps over the lazy dog', 1),\n", |
| 271 | + " (5, 'The quick brown fox jumps over the lazy dog', 1)]" |
| 272 | + ] |
| 273 | + }, |
| 274 | + "execution_count": 7, |
| 275 | + "metadata": {}, |
| 276 | + "output_type": "execute_result" |
| 277 | + } |
| 278 | + ], |
| 279 | + "execution_count": 7 |
| 280 | + }, |
| 281 | + { |
237 | 282 | "metadata": {}, |
| 283 | + "cell_type": "markdown", |
| 284 | + "source": "### Execute raw SQL", |
| 285 | + "id": "f8dd3bc68287ade1" |
| 286 | + }, |
| 287 | + { |
| 288 | + "metadata": { |
| 289 | + "ExecuteTime": { |
| 290 | + "end_time": "2025-03-10T02:13:36.455402Z", |
| 291 | + "start_time": "2025-03-10T02:13:36.445640Z" |
| 292 | + } |
| 293 | + }, |
| 294 | + "cell_type": "code", |
| 295 | + "source": "db.execute(\"SELECT COUNT(*) FROM chunks\")", |
| 296 | + "id": "124cc1a2463c474d", |
238 | 297 | "outputs": [ |
239 | 298 | { |
240 | 299 | "data": { |
241 | 300 | "text/plain": [ |
242 | | - "[(1, 'The quick brown fox jumps over the lazy dog', 1)]" |
| 301 | + "{'success': True, 'result': [(0,)], 'error': None}" |
243 | 302 | ] |
244 | 303 | }, |
245 | | - "execution_count": 49, |
| 304 | + "execution_count": 13, |
246 | 305 | "metadata": {}, |
247 | 306 | "output_type": "execute_result" |
248 | 307 | } |
249 | 308 | ], |
250 | | - "source": [ |
251 | | - "chunks = table.query({\"user_id\": 1})\n", |
252 | | - "[(c.id, c.text, c.user_id) for c in chunks]" |
253 | | - ] |
| 309 | + "execution_count": 13 |
254 | 310 | }, |
255 | 311 | { |
256 | 312 | "cell_type": "markdown", |
|
264 | 320 | }, |
265 | 321 | { |
266 | 322 | "cell_type": "code", |
267 | | - "execution_count": 45, |
268 | 323 | "id": "cceb0bf0", |
269 | | - "metadata": {}, |
| 324 | + "metadata": { |
| 325 | + "ExecuteTime": { |
| 326 | + "end_time": "2025-03-10T02:13:11.093835Z", |
| 327 | + "start_time": "2025-03-10T02:13:11.024531Z" |
| 328 | + } |
| 329 | + }, |
| 330 | + "source": [ |
| 331 | + "table.truncate()\n", |
| 332 | + "table.rows()" |
| 333 | + ], |
270 | 334 | "outputs": [ |
271 | 335 | { |
272 | 336 | "data": { |
273 | 337 | "text/plain": [ |
274 | 338 | "0" |
275 | 339 | ] |
276 | 340 | }, |
277 | | - "execution_count": 45, |
| 341 | + "execution_count": 10, |
278 | 342 | "metadata": {}, |
279 | 343 | "output_type": "execute_result" |
280 | 344 | } |
281 | 345 | ], |
282 | | - "source": [ |
283 | | - "table.truncate()\n", |
284 | | - "table.rows()" |
285 | | - ] |
| 346 | + "execution_count": 10 |
286 | 347 | } |
287 | 348 | ], |
288 | 349 | "metadata": { |
|
0 commit comments