Skip to content

Commit e332bea

Browse files
Add custom vectorizer class (#156)
This PR introduces a new CustomTextVectorizer class that allows for users to wrap their own embedding functions and methods to be compatible with RedisVL.
1 parent dc74788 commit e332bea

File tree

5 files changed

+557
-31
lines changed

5 files changed

+557
-31
lines changed

docs/user_guide/vectorizers_04.ipynb

Lines changed: 107 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"3. Vertex AI\n",
1414
"4. Cohere\n",
1515
"5. Mistral AI\n",
16+
"6. Bringing your own vectorizer\n",
1617
"\n",
1718
"Before running this notebook, be sure to\n",
1819
"1. Have installed ``redisvl`` and have that environment active for this notebook.\n",
@@ -90,16 +91,16 @@
9091
{
9192
"data": {
9293
"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]"
103104
]
104105
},
105106
"execution_count": 3,
@@ -129,16 +130,16 @@
129130
{
130131
"data": {
131132
"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]"
142143
]
143144
},
144145
"execution_count": 4,
@@ -505,9 +506,10 @@
505506
"cell_type": "markdown",
506507
"metadata": {},
507508
"source": [
508-
"## Mistral AI\n",
509+
"### Mistral AI\n",
509510
"\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",
511513
"\n",
512514
"```bash\n",
513515
"pip install mistralai\n",
@@ -516,14 +518,14 @@
516518
},
517519
{
518520
"cell_type": "code",
519-
"execution_count": 7,
521+
"execution_count": 3,
520522
"metadata": {},
521523
"outputs": [
522524
{
523525
"name": "stdout",
524526
"output_type": "stream",
525527
"text": [
526-
"vector dimensions: 1024\n",
528+
"Vector dimensions: 1024\n",
527529
"[-0.02801513671875, 0.02532958984375, 0.04278564453125, 0.0185699462890625, 0.041015625, 0.006053924560546875, 0.03607177734375, -0.0030155181884765625, 0.0033893585205078125, -0.01390838623046875]\n"
528530
]
529531
}
@@ -533,12 +535,91 @@
533535
"\n",
534536
"mistral = MistralAITextVectorizer()\n",
535537
"\n",
536-
"# embed a sentence using their asyncronous method\n",
538+
"# mebed a sentence using their asyncronous method\n",
537539
"test = await mistral.aembed(\"This is a test sentence.\")\n",
538-
"print(\"vector dimensions:\", len(test))\n",
540+
"print(\"Vector dimensions: \", len(test))\n",
539541
"print(test[:10])"
540542
]
541543
},
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+
},
542623
{
543624
"cell_type": "markdown",
544625
"metadata": {},

redisvl/utils/vectorize/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from redisvl.utils.vectorize.base import BaseVectorizer
22
from redisvl.utils.vectorize.text.azureopenai import AzureOpenAITextVectorizer
33
from redisvl.utils.vectorize.text.cohere import CohereTextVectorizer
4+
from redisvl.utils.vectorize.text.custom import CustomTextVectorizer
45
from redisvl.utils.vectorize.text.huggingface import HFTextVectorizer
56
from redisvl.utils.vectorize.text.mistral import MistralAITextVectorizer
67
from redisvl.utils.vectorize.text.openai import OpenAITextVectorizer
@@ -14,4 +15,5 @@
1415
"VertexAITextVectorizer",
1516
"AzureOpenAITextVectorizer",
1617
"MistralAITextVectorizer",
18+
"CustomTextVectorizer",
1719
]

0 commit comments

Comments
 (0)