11from __future__ import annotations
22
33from enum import Enum
4- from typing import List , Optional
4+ from typing import (List ,
5+ Optional ,
6+ Union )
57
68from couchbase .exceptions import InvalidArgumentException
79from couchbase .options import VectorSearchOptions
@@ -37,30 +39,29 @@ class VectorQuery:
3739
3840 Args:
3941 field_name (str): The name of the field in the search index that stores the vector.
40- vector (List[float]): The vector to use in the query.
42+ vector (Union[ List[float], str ]): The vector to use in the query.
4143 num_candidates (int, optional): Specifies the number of results returned. If provided, must be greater or equal to 1.
4244 boost (float, optional): Add boost to query.
4345
4446 Raises:
4547 :class:`~couchbase.exceptions.InvalidArgumentException`: If the vector is not provided.
46- :class:`~couchbase.exceptions.InvalidArgumentException`: If all values of the provided vector are not instances of float.
48+ :class:`~couchbase.exceptions.InvalidArgumentException`: If the vector is not a list or str.
49+ :class:`~couchbase.exceptions.InvalidArgumentException`: If vector is a list and all values of the provided vector are not instances of float.
4750
4851 Returns:
4952 :class:`~couchbase.vector_search.VectorQuery`: The created vector query.
5053 """ # noqa: E501
5154
5255 def __init__ (self ,
5356 field_name , # type: str
54- vector , # type: List[float]
57+ vector , # type: Union[ List[float], str ]
5558 num_candidates = None , # type: Optional[int]
5659 boost = None , # type: Optional[float]
5760 ):
5861 self ._field_name = field_name
59- if vector is None or len (vector ) == 0 :
60- raise InvalidArgumentException ('Provided vector cannot be empty.' )
61- if not all (map (lambda q : isinstance (q , float ), vector )):
62- raise InvalidArgumentException ('All vector values must be a float.' )
63- self ._vector = vector
62+ self ._vector = None
63+ self ._vector_base64 = None
64+ self ._validate_and_set_vector (vector )
6465 self ._num_candidates = self ._boost = None
6566 if num_candidates is not None :
6667 self .num_candidates = num_candidates
@@ -116,19 +117,46 @@ def num_candidates(self,
116117 self ._num_candidates = value
117118
118119 @property
119- def vector (self ) -> List [float ]:
120+ def vector (self ) -> Optional [ List [float ] ]:
120121 """
121122 **UNCOMMITTED** This API is unlikely to change,
122123 but may still change as final consensus on its behavior has not yet been reached.
123124
124- List[float]: Returns the vector query's vector.
125+ Optional[ List[float] ]: Returns the vector query's vector.
125126 """
126127 return self ._vector
127128
129+ @property
130+ def vector_base64 (self ) -> Optional [str ]:
131+ """
132+ **UNCOMMITTED** This API is unlikely to change,
133+ but may still change as final consensus on its behavior has not yet been reached.
134+
135+ Optional[str]: Returns the vector query's base64 vector str.
136+ """
137+ return self ._vector_base64
138+
139+ def _validate_and_set_vector (self ,
140+ vector , # type: Union[List[float], str]
141+ ) -> None :
142+ if vector is None :
143+ raise InvalidArgumentException ('Provided vector cannot be empty.' )
144+ if isinstance (vector , list ):
145+ if len (vector ) == 0 :
146+ raise InvalidArgumentException ('Provided vector cannot be empty.' )
147+ if not all (map (lambda q : isinstance (q , float ), vector )):
148+ raise InvalidArgumentException ('All vector values must be a float.' )
149+ self ._vector = vector
150+ return
151+ elif not isinstance (vector , str ):
152+ raise InvalidArgumentException ('Provided vector must be either a List[float] or base64 encoded str.' )
153+
154+ self ._vector_base64 = vector
155+
128156 @classmethod
129157 def create (cls ,
130158 field_name , # type: str
131- vector , # type: List[float]
159+ vector , # type: Union[ List[float], str ]
132160 num_candidates = None , # type: Optional[int]
133161 boost = None , # type: Optional[float]
134162 ) -> VectorQuery :
@@ -139,13 +167,14 @@ def create(cls,
139167
140168 Args:
141169 field_name (str): The name of the field in the search index that stores the vector.
142- vector (List[float]): The vector to use in the query.
170+ vector (Union[ List[float], str ]): The vector to use in the query.
143171 num_candidates (int, optional): Specifies the number of results returned. If provided, must be greater or equal to 1.
144172 boost (float, optional): Add boost to query.
145173
146174 Raises:
147- :class:`~couchbase.exceptions.InvalidArgumentException`: If the vector is not provided.
148- :class:`~couchbase.exceptions.InvalidArgumentException`: If all values of the provided vector are not instances of float.
175+ :class:`~couchbase.exceptions.InvalidArgumentException`: If the vector is not provided.
176+ :class:`~couchbase.exceptions.InvalidArgumentException`: If the vector is not a list or str.
177+ :class:`~couchbase.exceptions.InvalidArgumentException`: If vector is a list and all values of the provided vector are not instances of float.
149178
150179 Returns:
151180 :class:`~couchbase.vector_search.VectorQuery`: The created vector query.
0 commit comments