|
13 | 13 | "3. Vertex AI\n",
|
14 | 14 | "4. Cohere\n",
|
15 | 15 | "5. Mistral AI\n",
|
| 16 | + "6. Bringing your own vectorizer\n", |
16 | 17 | "\n",
|
17 | 18 | "Before running this notebook, be sure to\n",
|
18 | 19 | "1. Have installed ``redisvl`` and have that environment active for this notebook.\n",
|
|
90 | 91 | {
|
91 | 92 | "data": {
|
92 | 93 | "text/plain": [
|
93 |
| - "[-0.001025049015879631,\n", |
94 |
| - " -0.0030993607360869646,\n", |
95 |
| - " 0.0024536605924367905,\n", |
96 |
| - " -0.004484387580305338,\n", |
97 |
| - " -0.010331203229725361,\n", |
98 |
| - " 0.012700922787189484,\n", |
99 |
| - " -0.005368996877223253,\n", |
100 |
| - " -0.0029411641880869865,\n", |
101 |
| - " -0.0070833307690918446,\n", |
102 |
| - " -0.03386051580309868]" |
| 94 | + "[-0.0010508307022973895,\n", |
| 95 | + " -0.0031670420430600643,\n", |
| 96 | + " 0.0023781107738614082,\n", |
| 97 | + " -0.004539588466286659,\n", |
| 98 | + " -0.010320774279534817,\n", |
| 99 | + " 0.012868634425103664,\n", |
| 100 | + " -0.0054513863287866116,\n", |
| 101 | + " -0.002984359161928296,\n", |
| 102 | + " -0.0072814482264220715,\n", |
| 103 | + " -0.033704183995723724]" |
103 | 104 | ]
|
104 | 105 | },
|
105 | 106 | "execution_count": 3,
|
|
129 | 130 | {
|
130 | 131 | "data": {
|
131 | 132 | "text/plain": [
|
132 |
| - "[-0.01747742109000683,\n", |
133 |
| - " -5.228330701356754e-05,\n", |
134 |
| - " 0.0013870716793462634,\n", |
135 |
| - " -0.025637786835432053,\n", |
136 |
| - " -0.01985435001552105,\n", |
137 |
| - " 0.016117358580231667,\n", |
138 |
| - " -0.0037306349258869886,\n", |
139 |
| - " 0.0008945261361077428,\n", |
140 |
| - " 0.006577865686267614,\n", |
141 |
| - " -0.025091219693422318]" |
| 133 | + "[-0.01749197021126747,\n", |
| 134 | + " -5.238811718299985e-05,\n", |
| 135 | + " 0.0013331907102838159,\n", |
| 136 | + " -0.025576923042535782,\n", |
| 137 | + " -0.019907286390662193,\n", |
| 138 | + " 0.016106342896819115,\n", |
| 139 | + " -0.003756451653316617,\n", |
| 140 | + " 0.0009971122490242124,\n", |
| 141 | + " 0.006661186460405588,\n", |
| 142 | + " -0.024954024702310562]" |
142 | 143 | ]
|
143 | 144 | },
|
144 | 145 | "execution_count": 4,
|
|
505 | 506 | "cell_type": "markdown",
|
506 | 507 | "metadata": {},
|
507 | 508 | "source": [
|
508 |
| - "## Mistral AI\n", |
| 509 | + "### Mistral AI\n", |
509 | 510 | "\n",
|
510 |
| - "[Mistral](https://console.mistral.ai/) offers LLM and embedding APIs you to implement into your product. The `MistralAITextVectorizer` makes it simple to use RedisVL with their embeddings model. You will need to install `mistralai`.\n", |
| 511 | + "[Mistral](https://console.mistral.ai/) offers LLM and embedding APIs for you to implement into your product. The `MistralAITextVectorizer` makes it simple to use RedisVL with their embeddings model.\n", |
| 512 | + "You will need to install `mistralai`.\n", |
511 | 513 | "\n",
|
512 | 514 | "```bash\n",
|
513 | 515 | "pip install mistralai\n",
|
|
516 | 518 | },
|
517 | 519 | {
|
518 | 520 | "cell_type": "code",
|
519 |
| - "execution_count": 7, |
| 521 | + "execution_count": 3, |
520 | 522 | "metadata": {},
|
521 | 523 | "outputs": [
|
522 | 524 | {
|
523 | 525 | "name": "stdout",
|
524 | 526 | "output_type": "stream",
|
525 | 527 | "text": [
|
526 |
| - "vector dimensions: 1024\n", |
| 528 | + "Vector dimensions: 1024\n", |
527 | 529 | "[-0.02801513671875, 0.02532958984375, 0.04278564453125, 0.0185699462890625, 0.041015625, 0.006053924560546875, 0.03607177734375, -0.0030155181884765625, 0.0033893585205078125, -0.01390838623046875]\n"
|
528 | 530 | ]
|
529 | 531 | }
|
|
533 | 535 | "\n",
|
534 | 536 | "mistral = MistralAITextVectorizer()\n",
|
535 | 537 | "\n",
|
536 |
| - "# embed a sentence using their asyncronous method\n", |
| 538 | + "# mebed a sentence using their asyncronous method\n", |
537 | 539 | "test = await mistral.aembed(\"This is a test sentence.\")\n",
|
538 |
| - "print(\"vector dimensions:\", len(test))\n", |
| 540 | + "print(\"Vector dimensions: \", len(test))\n", |
539 | 541 | "print(test[:10])"
|
540 | 542 | ]
|
541 | 543 | },
|
| 544 | + { |
| 545 | + "cell_type": "markdown", |
| 546 | + "metadata": {}, |
| 547 | + "source": [ |
| 548 | + "### Custom Vectorizers\n", |
| 549 | + "\n", |
| 550 | + "RedisVL supports the use of other vectorizers and provides a class to enable compatibility with any function that generates a vector or vectors from string data" |
| 551 | + ] |
| 552 | + }, |
| 553 | + { |
| 554 | + "cell_type": "code", |
| 555 | + "execution_count": 6, |
| 556 | + "metadata": {}, |
| 557 | + "outputs": [ |
| 558 | + { |
| 559 | + "data": { |
| 560 | + "text/plain": [ |
| 561 | + "[0.101, 0.101, 0.101, 0.101, 0.101, 0.101, 0.101, 0.101, 0.101, 0.101]" |
| 562 | + ] |
| 563 | + }, |
| 564 | + "execution_count": 6, |
| 565 | + "metadata": {}, |
| 566 | + "output_type": "execute_result" |
| 567 | + } |
| 568 | + ], |
| 569 | + "source": [ |
| 570 | + "from redisvl.utils.vectorize import CustomTextVectorizer\n", |
| 571 | + "\n", |
| 572 | + "def generate_embeddings(text_input):\n", |
| 573 | + " return [0.101] * 768\n", |
| 574 | + "\n", |
| 575 | + "custom_vectorizer = CustomTextVectorizer(generate_embeddings)\n", |
| 576 | + "\n", |
| 577 | + "custom_vectorizer.embed(\"This is a test sentence.\")[:10]" |
| 578 | + ] |
| 579 | + }, |
| 580 | + { |
| 581 | + "cell_type": "markdown", |
| 582 | + "metadata": {}, |
| 583 | + "source": [ |
| 584 | + "This enables the use of custom vectorizers with other RedisVL components" |
| 585 | + ] |
| 586 | + }, |
| 587 | + { |
| 588 | + "cell_type": "code", |
| 589 | + "execution_count": 7, |
| 590 | + "metadata": {}, |
| 591 | + "outputs": [ |
| 592 | + { |
| 593 | + "name": "stdout", |
| 594 | + "output_type": "stream", |
| 595 | + "text": [ |
| 596 | + "11:04:14 redisvl.index.index INFO Index already exists, not overwriting.\n" |
| 597 | + ] |
| 598 | + }, |
| 599 | + { |
| 600 | + "data": { |
| 601 | + "text/plain": [ |
| 602 | + "[{'id': 'llmcache:78bd2446a37a0c6ab62652af9b7e53845145c4471ea83ff9fb4280a528d36bbb',\n", |
| 603 | + " 'vector_distance': '6.13927841187e-06',\n", |
| 604 | + " 'prompt': 'this is a test prompt',\n", |
| 605 | + " 'response': 'this is a test response',\n", |
| 606 | + " 'prompt_vector': '\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17=\\x17='}]" |
| 607 | + ] |
| 608 | + }, |
| 609 | + "execution_count": 7, |
| 610 | + "metadata": {}, |
| 611 | + "output_type": "execute_result" |
| 612 | + } |
| 613 | + ], |
| 614 | + "source": [ |
| 615 | + "from redisvl.extensions.llmcache import SemanticCache\n", |
| 616 | + "\n", |
| 617 | + "cache = SemanticCache(vectorizer=custom_vectorizer)\n", |
| 618 | + "\n", |
| 619 | + "cache.store(\"this is a test prompt\", \"this is a test response\")\n", |
| 620 | + "cache.check(\"this is also a test prompt\")" |
| 621 | + ] |
| 622 | + }, |
542 | 623 | {
|
543 | 624 | "cell_type": "markdown",
|
544 | 625 | "metadata": {},
|
|
0 commit comments