Skip to content

Commit c0b0a27

Browse files
committed
Handle VoyageAI exceptions and format for multimodal
1 parent 09dde8c commit c0b0a27

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

redisvl/utils/vectorize/voyageai.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ def __init__(
117117
ImportError: If the voyageai library is not installed.
118118
ValueError: If the API key is not provided.
119119
120+
Notes:
121+
- Multimodal models require voyageai>=0.3.6 to be installed for video embeddings, as well as
122+
ffmpeg installed on the system. Image embeddings require pillow to be installed.
123+
120124
"""
121125
super().__init__(model=model, dtype=dtype, cache=cache)
122126
# Initialize client and set up the model
@@ -128,7 +132,7 @@ def is_multimodal(self) -> bool:
128132
return "multimodal" in self.model
129133

130134
def embed_image(self, image_path: str, **kwargs) -> Union[List[float], bytes]:
131-
"""Embed an image (from its path on disk) using VoyageAI's multimodal API."""
135+
"""Embed an image (from its path on disk) using VoyageAI's multimodal API. Requires pillow to be installed."""
132136
if not self.is_multimodal:
133137
raise ValueError("Cannot embed image with a non-multimodal model.")
134138

@@ -142,7 +146,10 @@ def embed_image(self, image_path: str, **kwargs) -> Union[List[float], bytes]:
142146
return self.embed(Image.open(image_path), **kwargs)
143147

144148
def embed_video(self, video_path: str, **kwargs) -> Union[List[float], bytes]:
145-
"""Embed a video (from its path on disk) using VoyageAI's multimodal API."""
149+
"""Embed a video (from its path on disk) using VoyageAI's multimodal API.
150+
151+
Requires voyageai>=0.3.6 to be installed, as well as ffmpeg to be installed on the system.
152+
"""
146153
if not self.is_multimodal:
147154
raise ValueError("Cannot embed video with a non-multimodal model.")
148155

@@ -321,8 +328,10 @@ def _embed_many(
321328
TypeError: If `contents` is not a list, or parameters are invalid
322329
ValueError: If embedding fails
323330
"""
331+
from voyageai.error import InvalidRequestError
332+
324333
input_type = kwargs.pop("input_type", None)
325-
truncation = kwargs.pop("truncation", None)
334+
truncation = kwargs.pop("truncation", True)
326335

327336
# Validate inputs
328337
self._validate_input(contents, input_type, truncation)
@@ -335,14 +344,18 @@ def _embed_many(
335344
embeddings: List = []
336345
for batch in self.batchify(contents, batch_size):
337346
response = self._embed_fn(
338-
batch,
347+
(
348+
[batch] if self.is_multimodal else batch
349+
), # Multimodal requires a list of lists/dicts
339350
model=self.model,
340351
input_type=input_type,
341352
truncation=truncation,
342353
**kwargs, # type: ignore
343354
)
344355
embeddings.extend(response.embeddings)
345356
return embeddings
357+
except InvalidRequestError as e:
358+
raise TypeError(f"Invalid input for embedding: {str(e)}") from e
346359
except Exception as e:
347360
raise ValueError(f"Embedding texts failed: {e}")
348361

@@ -390,8 +403,10 @@ async def _aembed_many(
390403
TypeError: If `contents` is not a list, or parameters are invalid
391404
ValueError: If embedding fails
392405
"""
406+
from voyageai.error import InvalidRequestError
407+
393408
input_type = kwargs.pop("input_type", None)
394-
truncation = kwargs.pop("truncation", None)
409+
truncation = kwargs.pop("truncation", True)
395410

396411
# Validate inputs
397412
self._validate_input(contents, input_type, truncation)
@@ -404,14 +419,18 @@ async def _aembed_many(
404419
embeddings: List = []
405420
for batch in self.batchify(contents, batch_size):
406421
response = await self._aembed_fn(
407-
batch,
422+
(
423+
[batch] if self.is_multimodal else batch
424+
), # Multimodal requires a list of lists/dicts
408425
model=self.model,
409426
input_type=input_type,
410427
truncation=truncation,
411428
**kwargs, # type: ignore
412429
)
413430
embeddings.extend(response.embeddings)
414431
return embeddings
432+
except InvalidRequestError as e:
433+
raise TypeError(f"Invalid input for embedding: {str(e)}") from e
415434
except Exception as e:
416435
raise ValueError(f"Embedding texts failed: {e}")
417436

0 commit comments

Comments
 (0)