Skip to content

Commit 1f8e9c5

Browse files
committed
Add GGUF example to low level API example #973
1 parent d64c8d8 commit 1f8e9c5

File tree

1 file changed

+122
-2
lines changed

1 file changed

+122
-2
lines changed

examples/78_Accessing_Low_Level_Vector_APIs.ipynb

Lines changed: 122 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"outputs": [],
3333
"source": [
3434
"%%capture\n",
35-
"!pip install git+https://github.com/neuml/txtai#egg=txtai[ann]"
35+
"!pip install git+https://github.com/neuml/txtai#egg=txtai[ann] gguf"
3636
]
3737
},
3838
{
@@ -343,6 +343,112 @@
343343
"Same result. Note the score is slightly different but this is expected."
344344
]
345345
},
346+
{
347+
"cell_type": "markdown",
348+
"id": "89abb301",
349+
"metadata": {},
350+
"source": [
351+
"# GGUF Support\n",
352+
"\n",
353+
"`txtai` 9.1 also adds support for [GGML](https://github.com/ggml-org/ggml) / [GGUF](https://huggingface.co/docs/hub/en/gguf) popularized by [llama.cpp](https://github.com/ggml-org/llama.cpp)."
354+
]
355+
},
356+
{
357+
"cell_type": "code",
358+
"execution_count": 19,
359+
"id": "e04498fc",
360+
"metadata": {},
361+
"outputs": [],
362+
"source": [
363+
"ann = ANNFactory.create({\n",
364+
" \"backend\": \"ggml\",\n",
365+
" \"ggml\": {\n",
366+
" \"quantize\": \"Q4_0\"\n",
367+
" }\n",
368+
"})\n",
369+
"ann.index(data)\n",
370+
"ann.save(\"vectors.gguf\")"
371+
]
372+
},
373+
{
374+
"cell_type": "markdown",
375+
"id": "7bea0f58",
376+
"metadata": {},
377+
"source": [
378+
"Now let's check out the generated file using the [gguf](https://github.com/ggml-org/llama.cpp/tree/master/gguf-py) package provided by llama.cpp."
379+
]
380+
},
381+
{
382+
"cell_type": "code",
383+
"execution_count": 20,
384+
"id": "b0e6f85e",
385+
"metadata": {},
386+
"outputs": [
387+
{
388+
"name": "stdout",
389+
"output_type": "stream",
390+
"text": [
391+
"Tensor Name | Shape | Size | Quantization\n",
392+
"--------------------------------------------------------------------------------\n",
393+
"data | 384x1411868 | 258.52 MB | Q4_0\n"
394+
]
395+
}
396+
],
397+
"source": [
398+
"from gguf.gguf_reader import GGUFReader\n",
399+
"\n",
400+
"reader = GGUFReader(\"vectors.gguf\")\n",
401+
"\n",
402+
"# List all tensors\n",
403+
"info = \"{:<30} | {:<15} | {:<12} | {}\"\n",
404+
"print(info.format(\"Tensor Name\", \"Shape\", \"Size\", \"Quantization\"))\n",
405+
"print(\"-\" * 80)\n",
406+
"for tensor in reader.tensors:\n",
407+
" shape = \"x\".join(map(str, tensor.shape))\n",
408+
" size = f\"{tensor.n_elements / 2 / 1024 / 1024:.2f} MB\"\n",
409+
" quantization = tensor.tensor_type.name\n",
410+
" print(info.format(tensor.name, shape, size, quantization))"
411+
]
412+
},
413+
{
414+
"cell_type": "markdown",
415+
"id": "7be79bf7",
416+
"metadata": {},
417+
"source": [
418+
"And search like we did with Torch."
419+
]
420+
},
421+
{
422+
"cell_type": "code",
423+
"execution_count": 18,
424+
"id": "450dc024",
425+
"metadata": {},
426+
"outputs": [
427+
{
428+
"name": "stdout",
429+
"output_type": "stream",
430+
"text": [
431+
"The answer to your question, that how many miles is it from earth to mars, is very easy to know. Because of huge satellites which are being sent to\n",
432+
"mars in search of life from many countries, we have discovered a lot about mars. According to experts, earth and mars reaches to their closest points\n",
433+
"in every 26 months. This situation is considered as opposition of mars as the location of sun and mars in totally opposite to each other in relation\n",
434+
"to earth. When this opposition takes place, the planet is visible with a red tint in the sky from earth. And this also gives mars a name, i.e. the red\n",
435+
"planet. Mars is also the fourth planet from sun, which is located between Jupiter and earth. Its distance from sun is not only opposite but is also\n",
436+
"much further away, than that of the earth and sun. The distance between the sun and mars is said to be 140 million miles. Mars can reach about 128\n",
437+
"million miles closer to the sun whereas it can even travel around 154 million miles away from it. The assumed distance between mars and earth is said\n",
438+
"to be between 40 to 225 million miles. The distance between these two planets keeps on changing throughout the year because of the elliptical path in\n",
439+
"which all the planets rotate. As the distance between mars, sun and earth is so much high, it takes a Martian year, for mars to go around the sun. The\n",
440+
"Martian period includes a time of around 687 earth days. This means that, it takes more than 2 years for the mars to reach its initial rotation point.\n",
441+
"If we talk about one Martian day, it is the total time which is taken by a planet to spin around once. This day usually lasts longer than our regular\n",
442+
"earth days. So this was the actual reason which states the distance between earth and mars. \n",
443+
"\n",
444+
" 0.7043964862823486\n"
445+
]
446+
}
447+
],
448+
"source": [
449+
"search(\"How far is earth from mars?\")"
450+
]
451+
},
346452
{
347453
"cell_type": "markdown",
348454
"id": "af1a344c",
@@ -355,8 +461,22 @@
355461
}
356462
],
357463
"metadata": {
464+
"kernelspec": {
465+
"display_name": "local",
466+
"language": "python",
467+
"name": "python3"
468+
},
358469
"language_info": {
359-
"name": "python"
470+
"codemirror_mode": {
471+
"name": "ipython",
472+
"version": 3
473+
},
474+
"file_extension": ".py",
475+
"mimetype": "text/x-python",
476+
"name": "python",
477+
"nbconvert_exporter": "python",
478+
"pygments_lexer": "ipython3",
479+
"version": "3.10.19"
360480
}
361481
},
362482
"nbformat": 4,

0 commit comments

Comments
 (0)