forked from redis/redis-vl-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.py
More file actions
577 lines (494 loc) · 20.8 KB
/
Copy pathbase.py
File metadata and controls
577 lines (494 loc) · 20.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
import io
import logging
from enum import Enum
from pathlib import Path
from typing import Annotated, Any, Callable
from pydantic import BaseModel, ConfigDict, Field, field_validator
from redisvl.extensions.cache.embeddings import EmbeddingsCache
from redisvl.redis.utils import array_to_buffer
from redisvl.schema.fields import VectorDataType
from redisvl.utils.utils import deprecated_argument
try:
from PIL.Image import Image
except ImportError:
_PILLOW_INSTALLED = False
else:
_PILLOW_INSTALLED = True
logger = logging.getLogger(__name__)
class Vectorizers(Enum):
azure_openai = "azure_openai"
openai = "openai"
cohere = "cohere"
mistral = "mistral"
ollama = "ollama"
vertexai = "vertexai"
hf = "hf"
voyageai = "voyageai"
class BaseVectorizer(BaseModel):
"""Base RedisVL vectorizer interface.
This class defines the interface for vectorization with an optional
caching layer to improve performance by avoiding redundant API calls.
Attributes:
model: The name of the embedding model.
dtype: The data type of the embeddings, defaults to "float32".
dims: The dimensionality of the vectors.
cache: Optional embedding cache to store and retrieve embeddings.
"""
model: str
dtype: str = "float32"
dims: Annotated[int | None, Field(strict=True, gt=0)] = None
cache: EmbeddingsCache | None = Field(default=None)
model_config = ConfigDict(arbitrary_types_allowed=True)
@property
def type(self) -> str:
"""Return the type of vectorizer."""
return "base"
@field_validator("dtype")
@classmethod
def check_dtype(cls, dtype):
"""Validate the data type is supported."""
try:
VectorDataType(dtype.upper())
except ValueError:
raise ValueError(
f"Invalid data type: {dtype}. Supported types are: {[t.lower() for t in VectorDataType]}"
)
return dtype
@deprecated_argument("text", "content")
def embed(
self,
content: Any = None,
text: Any = None,
preprocess: Callable | None = None,
as_buffer: bool = False,
skip_cache: bool = False,
**kwargs,
) -> list[float] | bytes:
"""Generate a vector embedding for content.
Args:
content: The content to convert to a vector embedding
text: The text to convert to a vector embedding (deprecated - use `content` instead)
preprocess: Function to apply to the content before embedding
as_buffer: Return the embedding as a binary buffer instead of a list
skip_cache: Bypass the cache for this request
**kwargs: Additional model-specific parameters
Returns:
The vector embedding as either a list of floats or binary buffer
Examples:
>>> embedding = text_vectorizer.embed("Hello world")
>>> embedding = image_vectorizer.embed(Image.open("test.png"))
"""
content = content or text
if not content:
raise ValueError("No content provided to embed.")
# Apply preprocessing if provided
if preprocess is not None:
content = preprocess(content)
# Check cache if available and not skipped
if self.cache is not None and not skip_cache:
try:
cache_result = self.cache.get(
content=self._serialize_for_cache(content), model_name=self.model
)
if cache_result:
logger.debug(f"Cache hit for content with model {self.model}")
return self._process_embedding(
cache_result["embedding"], as_buffer, self.dtype
)
except Exception as e:
logger.warning(f"Error accessing embedding cache: {str(e)}")
# Generate embedding using provider-specific implementation
cache_metadata = kwargs.pop("metadata", {})
embedding = self._embed(content, **kwargs)
# Store in cache if available and not skipped
if self.cache is not None and not skip_cache:
try:
self.cache.set(
content=self._serialize_for_cache(content),
model_name=self.model,
embedding=embedding,
metadata=cache_metadata,
)
except Exception as e:
logger.warning(f"Error storing in embedding cache: {str(e)}")
# Process and return result
return self._process_embedding(embedding, as_buffer, self.dtype)
@deprecated_argument("texts", "contents")
def embed_many(
self,
contents: list[Any] | None = None,
texts: list[Any] | None = None,
preprocess: Callable | None = None,
batch_size: int = 10,
as_buffer: bool = False,
skip_cache: bool = False,
**kwargs,
) -> list[list[float]] | list[bytes]:
"""Generate vector embeddings for multiple items efficiently.
Args:
contents: List of content to convert to vector embeddings
texts: List of texts to convert to vector embeddings (deprecated - use `contents` instead)
preprocess: Function to apply to each item before embedding
batch_size: Number of items to process in each API call
as_buffer: Return embeddings as binary buffers instead of lists
skip_cache: Bypass the cache for this request
**kwargs: Additional model-specific parameters
Returns:
List of vector embeddings in the same order as the inputs
Examples:
>>> embeddings = vectorizer.embed_many(["Hello", "World"], batch_size=2)
"""
contents = contents or texts
if not contents:
return []
# Apply preprocessing if provided
if preprocess is not None:
processed_contents = [preprocess(item) for item in contents]
else:
processed_contents = contents
# Get cached embeddings and identify misses
results, cache_misses, cache_miss_indices = self._get_from_cache_batch(
processed_contents, skip_cache
)
# Generate embeddings for cache misses
if cache_misses:
cache_metadata = kwargs.pop("metadata", {})
new_embeddings = self._embed_many(
contents=cache_misses, batch_size=batch_size, **kwargs
)
# Store new embeddings in cache
self._store_in_cache_batch(
cache_misses, new_embeddings, cache_metadata, skip_cache
)
# Insert new embeddings into results array
for idx, embedding in zip(cache_miss_indices, new_embeddings):
results[idx] = embedding
# Process and return results
return [self._process_embedding(emb, as_buffer, self.dtype) for emb in results]
@deprecated_argument("text", "content")
async def aembed(
self,
content: Any = None,
text: Any = None,
preprocess: Callable | None = None,
as_buffer: bool = False,
skip_cache: bool = False,
**kwargs,
) -> list[float] | bytes:
"""Asynchronously generate a vector embedding for an item of content.
Args:
content: The content to convert to a vector embedding
text: The text to convert to a vector embedding (deprecated - use `content` instead)
preprocess: Function to apply to the content before embedding
as_buffer: Return the embedding as a binary buffer instead of a list
skip_cache: Bypass the cache for this request
**kwargs: Additional model-specific parameters
Returns:
The vector embedding as either a list of floats or binary buffer
Examples:
>>> embedding = await vectorizer.aembed("Hello world")
"""
content = content or text
if not content:
raise ValueError("No content provided to embed.")
# Apply preprocessing if provided
if preprocess is not None:
content = preprocess(content)
# Check cache if available and not skipped
if self.cache is not None and not skip_cache:
try:
cache_result = await self.cache.aget(
content=self._serialize_for_cache(content), model_name=self.model
)
if cache_result:
logger.debug(f"Async cache hit for content with model {self.model}")
return self._process_embedding(
cache_result["embedding"], as_buffer, self.dtype
)
except Exception as e:
logger.warning(
f"Error accessing embedding cache asynchronously: {str(e)}"
)
# Generate embedding using provider-specific implementation
cache_metadata = kwargs.pop("metadata", {})
embedding = await self._aembed(content, **kwargs)
# Store in cache if available and not skipped
if self.cache is not None and not skip_cache:
try:
await self.cache.aset(
content=self._serialize_for_cache(content),
model_name=self.model,
embedding=embedding,
metadata=cache_metadata,
)
except Exception as e:
logger.warning(
f"Error storing in embedding cache asynchronously: {str(e)}"
)
# Process and return result
return self._process_embedding(embedding, as_buffer, self.dtype)
@deprecated_argument("texts", "contents")
async def aembed_many(
self,
contents: list[Any] | None = None,
texts: list[Any] | None = None,
preprocess: Callable | None = None,
batch_size: int = 10,
as_buffer: bool = False,
skip_cache: bool = False,
**kwargs,
) -> list[list[float]] | list[bytes]:
"""Asynchronously generate vector embeddings for multiple items efficiently.
Args:
contents: List of content to convert to vector embeddings
texts: List of texts to convert to vector embeddings (deprecated - use `contents` instead)
preprocess: Function to apply to each item before embedding
batch_size: Number of texts to process in each API call
as_buffer: Return embeddings as binary buffers instead of lists
skip_cache: Bypass the cache for this request
**kwargs: Additional model-specific parameters
Returns:
List of vector embeddings in the same order as the inputs
Examples:
>>> embeddings = await vectorizer.aembed_many(["Hello", "World"], batch_size=2)
"""
contents = contents or texts
if not contents:
return []
# Apply preprocessing if provided
if preprocess is not None:
processed_contents = [preprocess(item) for item in contents]
else:
processed_contents = contents
# Get cached embeddings and identify misses
results, cache_misses, cache_miss_indices = await self._aget_from_cache_batch(
processed_contents, skip_cache
)
# Generate embeddings for cache misses
if cache_misses:
cache_metadata = kwargs.pop("metadata", {})
new_embeddings = await self._aembed_many(
contents=cache_misses, batch_size=batch_size, **kwargs
)
# Store new embeddings in cache
await self._astore_in_cache_batch(
cache_misses, new_embeddings, cache_metadata, skip_cache
)
# Insert new embeddings into results array
for idx, embedding in zip(cache_miss_indices, new_embeddings):
results[idx] = embedding
# Process and return results
return [self._process_embedding(emb, as_buffer, self.dtype) for emb in results]
@deprecated_argument("text", "content")
def _embed(self, text: Any = "", content: Any = "", **kwargs) -> list[float]:
"""Generate a vector embedding for a single item."""
raise NotImplementedError
@deprecated_argument("texts", "contents")
def _embed_many(
self,
contents: list[Any] | None = None,
texts: list[Any] | None = None,
batch_size: int = 10,
**kwargs,
) -> list[list[float]]:
"""Generate vector embeddings for a batch of items."""
raise NotImplementedError
@deprecated_argument("text", "content")
async def _aembed(self, content: Any = "", text: Any = "", **kwargs) -> list[float]:
"""Asynchronously generate a vector embedding for a single item."""
logger.warning(
"This vectorizer has no async embed method. Falling back to sync."
)
return self._embed(content=content or text, **kwargs)
@deprecated_argument("texts", "contents")
async def _aembed_many(
self,
contents: list[Any] | None = None,
texts: list[Any] | None = None,
batch_size: int = 10,
**kwargs,
) -> list[list[float]]:
"""Asynchronously generate vector embeddings for a batch of items."""
logger.warning(
"This vectorizer has no async embed_many method. Falling back to sync."
)
return self._embed_many(
contents=contents or texts, batch_size=batch_size, **kwargs
)
def _get_from_cache_batch(
self, contents: list[Any], skip_cache: bool
) -> tuple[list[list[float] | None], list[str], list[int]]:
"""Get vector embeddings from cache and track cache misses.
Args:
contents: List of content to get from cache
skip_cache: Whether to skip cache lookup
Returns:
Tuple of (results, cache_misses, cache_miss_indices)
"""
results = [None] * len(contents)
cache_misses = []
cache_miss_indices = []
# Skip cache if requested or no cache available
if skip_cache or self.cache is None:
return results, contents, list(range(len(contents))) # type: ignore
try:
# Efficient batch cache lookup
cache_results = self.cache.mget(
contents=(self._serialize_for_cache(c) for c in contents),
model_name=self.model,
)
# Process cache hits and collect misses
for i, (content, cache_result) in enumerate(zip(contents, cache_results)):
if cache_result:
results[i] = cache_result["embedding"]
else:
cache_misses.append(content)
cache_miss_indices.append(i)
logger.debug(
f"Cache hits: {len(contents) - len(cache_misses)}, misses: {len(cache_misses)}"
)
except Exception as e:
logger.warning(f"Error accessing embedding cache in batch: {str(e)}")
# On cache error, process all data
cache_misses = contents
cache_miss_indices = list(range(len(contents)))
return results, cache_misses, cache_miss_indices # type: ignore
async def _aget_from_cache_batch(
self, contents: list[Any], skip_cache: bool
) -> tuple[list[list[float] | None], list[str], list[int]]:
"""Asynchronously get vector embeddings from cache and track cache misses.
Args:
contents: List of content to get from cache
skip_cache: Whether to skip cache lookup
Returns:
Tuple of (results, cache_misses, cache_miss_indices)
"""
results = [None] * len(contents)
cache_misses = []
cache_miss_indices = []
# Skip cache if requested or no cache available
if skip_cache or self.cache is None:
return results, contents, list(range(len(contents))) # type: ignore
try:
# Efficient batch cache lookup
cache_results = await self.cache.amget(
contents=(self._serialize_for_cache(c) for c in contents),
model_name=self.model,
)
# Process cache hits and collect misses
for i, (content, cache_result) in enumerate(zip(contents, cache_results)):
if cache_result:
results[i] = cache_result["embedding"]
else:
cache_misses.append(content)
cache_miss_indices.append(i)
logger.debug(
f"Async cache hits: {len(contents) - len(cache_misses)}, misses: {len(cache_misses)}"
)
except Exception as e:
logger.warning(
f"Error accessing embedding cache in batch asynchronously: {str(e)}"
)
# On cache error, process all data
cache_misses = contents
cache_miss_indices = list(range(len(contents)))
return results, cache_misses, cache_miss_indices # type: ignore
def _store_in_cache_batch(
self,
contents: list[Any],
embeddings: list[list[float]],
metadata: dict[str, Any],
skip_cache: bool,
) -> None:
"""Store a batch of vector embeddings in the cache.
Args:
contents: List of content that was embedded
embeddings: List of vector embeddings
metadata: Metadata to store with the embeddings
skip_cache: Whether to skip cache storage
"""
if skip_cache or self.cache is None:
return
try:
# Prepare batch cache storage items
cache_items = [
{
"content": self._serialize_for_cache(content),
"model_name": self.model,
"embedding": emb,
"metadata": metadata,
}
for content, emb in zip(contents, embeddings)
]
self.cache.mset(items=cache_items)
except Exception as e:
logger.warning(f"Error storing batch in embedding cache: {str(e)}")
async def _astore_in_cache_batch(
self,
contents: list[Any],
embeddings: list[list[float]],
metadata: dict[str, Any],
skip_cache: bool,
) -> None:
"""Asynchronously store a batch of vector embeddings in the cache.
Args:
contents: List of content that was embedded
embeddings: List of vector embeddings
metadata: Metadata to store with the embeddings
skip_cache: Whether to skip cache storage
"""
if skip_cache or self.cache is None:
return
try:
# Prepare batch cache storage items
cache_items = [
{
"content": self._serialize_for_cache(content),
"model_name": self.model,
"embedding": emb,
"metadata": metadata,
}
for content, emb in zip(contents, embeddings)
]
await self.cache.amset(items=cache_items)
except Exception as e:
logger.warning(
f"Error storing batch in embedding cache asynchronously: {str(e)}"
)
def batchify(self, seq: list, size: int, preprocess: Callable | None = None):
"""Split a sequence into batches of specified size.
Args:
seq: Sequence to split into batches
size: Batch size
preprocess: Optional function to preprocess each item
Yields:
Batches of the sequence
"""
for pos in range(0, len(seq), size):
if preprocess is not None:
yield [preprocess(chunk) for chunk in seq[pos : pos + size]]
else:
yield seq[pos : pos + size]
def _process_embedding(
self, embedding: list[float] | None, as_buffer: bool, dtype: str
):
"""Process the vector embedding format based on the as_buffer flag."""
if embedding is not None:
if as_buffer:
return array_to_buffer(embedding, dtype)
return embedding
def _serialize_for_cache(self, content: Any) -> bytes | str:
"""Convert content to a cacheable format."""
if isinstance(content, str):
return content
elif isinstance(content, bytes):
return content
elif isinstance(content, Path):
return content.read_bytes()
elif _PILLOW_INSTALLED and isinstance(content, Image):
buffer = io.BytesIO()
content.save(buffer, format="PNG")
return buffer.getvalue()
raise NotImplementedError(
f"Content type {type(content)} is not supported for caching."
)