
    Q
i'                     z    d dl mZmZmZmZmZ d dlmZ erd dlm	Z	 d dl
mZ dededdfd	Z G d
 de          ZdS )    )TYPE_CHECKINGAnyCallableListOptional)
ConfigDict)EmbeddingsCache)BaseVectorizerresultmethod_namereturnNc                    |                     d          rt          | t                    r| st          | d          t          | d         t                    r| d         st          | d          t          | d         d         t                    st          | d          dS t          | t                    r| st          | d          t          | d         t                    st          | d          dS )	a  
    Validates the structure of returned embeddings.

    - For methods named "*_many", expects a list of lists of floats.
    - For single methods, expects a list of floats.

    Raises:
        ValueError: If the embeddings do not match the expected structure.
    _manyz' must return a non-empty list of lists.r   z' must return a list of non-empty lists.z' must return a list of lists of floats.z must return a non-empty list.z must return a list of floats.N)endswith
isinstancelist
ValueErrorfloat)r   r   s     C:\Users\Dell Inspiron 16\Desktop\tws\AgrotaPowerBi\back-agrota-powerbi\mcp-client-agrota\venv\Lib\site-packages\redisvl/utils/vectorize/custom.py_check_vectorr      s3    G$$ M&$'' 	Vv 	VTTTUUU&)T** 	V&) 	VTTTUUU&)A,.. 	VTTTUUU	V 	V &$'' 	Mv 	MKKKLLL&)U++ 	MKKKLLL	M 	M    c                   j    e Zd ZdZ ed          Z	 	 	 	 	 ddedee         dee         d	ee         d
eded         f fdZ	d Z
edefd            ZddZdedee         fdZ	 ddee         dedeee                  fdZdedee         fdZ	 ddee         dedeee                  fdZ xZS )CustomVectorizera  The CustomVectorizer class wraps user-defined embedding methods to create
    embeddings for data.

    This vectorizer is designed to accept a provided callable vectorizer and
    provides a class definition to allow for compatibility with RedisVL.
    The vectorizer may support both synchronous and asynchronous operations which
    allows for batch processing of inputs, but at a minimum only synchronous embedding
    is required to satisfy the 'embed()' method.

    You can optionally enable caching to improve performance when generating
    embeddings for repeated inputs.

    .. code-block:: python

        # Basic usage with a custom embedding function
        vectorizer = CustomVectorizer(
            embed = my_vectorizer.generate_embedding
        )
        embedding = vectorizer.embed("Hello, world!")

        # With caching enabled
        from redisvl.extensions.cache.embeddings import EmbeddingsCache
        cache = EmbeddingsCache(name="my_embeddings_cache")

        vectorizer = CustomVectorizer(
            embed=my_vectorizer.generate_embedding,
            cache=cache
        )

        # First call will compute and cache the embedding
        embedding1 = vectorizer.embed("Hello, world!")

        # Second call will retrieve from cache
        embedding2 = vectorizer.embed("Hello, world!")

        # Asynchronous batch embedding of multiple texts
        embeddings = await vectorizer.aembed_many(
            ["Hello, world!", "How are you?"],
            batch_size=2
        )

    T)arbitrary_types_allowedNfloat32embed
embed_manyaembedaembed_manydtypecacher	   c                 "   	  |d          }t          |d           t          |          }n$# t          $ r}	t          d|	           d}	~	ww xY wt	                                          d|||           |                     ||||           dS )aZ  Initialize the Custom vectorizer.

        Args:
            embed (Callable): a Callable function that accepts an object and returns a list of floats.
            embed_many (Optional[Callable]): a Callable function that accepts a list of objects and returns a list containing lists of floats. Defaults to None.
            aembed (Optional[Callable]): an asynchronous Callable function that accepts a object and returns a lists of floats. Defaults to None.
            aembed_many (Optional[Callable]): an asynchronous Callable function that accepts a list of objects and returns a list containing lists of floats. Defaults to None.
            dtype (str): the default datatype to use when embedding inputs as byte arrays.
                Used when setting `as_buffer=True` in calls to embed() and embed_many().
                Defaults to 'float32'.
            cache (Optional[EmbeddingsCache]): Optional EmbeddingsCache instance to cache embeddings for
                better performance with repeated inputs. Defaults to None.

        Raises:
            ValueError: if embedding validation fails.
        zdimension testr   z!Failed to validate embed method: Ncustom)modelr    dimsr!   )r   len	Exceptionr   super__init___setup_functions)selfr   r   r   r   r    r!   test_resultr%   e	__class__s             r   r)   zCustomVectorizer.__init__S   s    4	F% 011K+w///{##DD 	F 	F 	FDDDEEE	F 	xu4uMMM 	eZEEEEEs   *. 
AA

Ac                     || _         || _        || _        || _        d| _        d| _        |                                  dS )z,Setup the user-provided embedding functions.FN)_embed_func_embed_func_many_aembed_func_aembed_func_many_aembed_validated_aembed_many_validated_validate_optional_funcs)r+   r   r   r   r   s        r   r*   z!CustomVectorizer._setup_functionsz   sN      *"!, "'&+# 	%%'''''r   r   c                     dS )Nr#    )r+   s    r   typezCustomVectorizer.type   s    xr   c                     | j         rM	 |                      dg          }t          |d           dS # t          $ r}t          d|           d}~ww xY wdS )z
        Optionally validate the other user-provided functions if they exist.

        Raises:
            ValueError: If any provided function produces invalid results.
        zdimension test (many)r   zInvalid embed_many function: N)r1   r   r'   r   )r+   
test_batchr-   s      r   r6   z)CustomVectorizer._validate_optional_funcs   s       	FF!224K3LMM
j,77777 F F F !D!D!DEEEF		F 	Fs   &1 
AAAcontentc                 l    	  | j         |fi |}|S # t          $ r}t          d|           d}~ww xY w)aa  Generate a vector embedding for a single input using the provided user function.

        Args:
            content: Input to embed
            **kwargs: Additional parameters to pass to the user function

        Returns:
            List[float]: Vector embedding as a list of floats

        Raises:
            ValueError: If embedding fails
        Embedding input failed: N)r0   r'   r   r+   r<   kwargsr   r-   s        r   _embedzCustomVectorizer._embed   s\    	=%T%g8888FM 	= 	= 	=;;;<<<	=s    
3.3
   contents
batch_sizec                      t          |t                    st          d           j        s fd|D             S 	   j        |fi }|S # t          $ r}t          d|           d}~ww xY w)a  Generate vector embeddings for a batch of inputs using the provided user function.

        Args:
            contents: List of inputs to embed
            batch_size: Number of inputs to process in each batch
            **kwargs: Additional parameters to pass to the user function

        Returns:
            List[List[float]]: List of vector embeddings as lists of floats

        Raises:
            TypeError: If contents is not a list
            ValueError: If embedding fails
        'Must pass in a list of values to embed.c                 ,    g | ]} j         |fi S r8   )rA   ).0r<   r@   r+   s     r   
<listcomp>z0CustomVectorizer._embed_many.<locals>.<listcomp>   s-    KKKwKDK22622KKKr   Embedding inputs failed: N)r   r   	TypeErrorr1   r'   r   r+   rC   rD   r@   resultsr-   s   `  `  r   _embed_manyzCustomVectorizer._embed_many   s    " (D)) 	GEFFF$ 	LKKKKK(KKKK	>+d+H????GN 	> 	> 	><<<===	>s   A 
A/A**A/c                    K   | j         s | j        |fi |S 	  | j         |fi | d{V }| j        st          |d           d| _        |S # t          $ r}t          d|           d}~ww xY w)a  Asynchronously generate a vector embedding for a single input.

        Args:
            content: Input to embed
            **kwargs: Additional parameters to pass to the user async function

        Returns:
            List[float]: Vector embedding as a list of floats

        Raises:
            NotImplementedError: If no aembed function was provided
            ValueError: If embedding fails
        Nr   Tr>   )r2   rA   r4   r   r'   r   r?   s        r   _aembedzCustomVectorizer._aembed   s         	24;w11&111
	=,4,W??????????F ) .fh///)-&M 	= 	= 	=;;;<<<	=s   3A 
A.A))A.c                 ,  K   t          |t                    st          d          | j        s | j        ||fi |S 	  | j        |fi | d{V }| j        st          |d           d| _        |S # t          $ r}t          d|           d}~ww xY w)a+  Asynchronously generate vector embeddings for a batch of inputs.

        Args:
            contents: List of inputs to embed
            batch_size: Number of inputs to process in each batch
            **kwargs: Additional parameters to pass to the user async function

        Returns:
            List[List[float]]: List of vector embeddings as lists of floats

        Raises:
            TypeError: If contents is not a list
            NotImplementedError: If no aembed_many function was provided
            ValueError: If embedding fails
        rF   Nr   TrJ   )	r   r   rK   r3   rN   r5   r   r'   r   rL   s         r   _aembed_manyzCustomVectorizer._aembed_many   s      $ (D)) 	GEFFF% 	D#4#HjCCFCCC
	>2D28FFvFFFFFFFFG . 3g}555.2+N 	> 	> 	><<<===	>s   3A2 2
B<BB)NNNr   N)r   N)rB   )__name__
__module____qualname____doc__r   model_configr   r   strr)   r*   propertyr9   r6   r   r   r   rA   intrN   rP   rR   __classcell__)r.   s   @r   r   r   %   s       ) )V :d;;;L
 *.%)*.-1%F %F%F X&%F "	%F
 h'%F %F )*%F %F %F %F %F %FN( ( ( c    XF F F F=c =U = = = =( 68> >S	>/2>	d5k	> > > ><=S =tE{ = = = =< 68"> ">S	">/2">	d5k	"> "> "> "> "> "> "> ">r   r   )typingr   r   r   r   r   pydanticr   .redisvl.extensions.cache.embeddings.embeddingsr	   redisvl.utils.vectorize.baser
   r   rX   r   r   r8   r   r   <module>r`      s    ? ? ? ? ? ? ? ? ? ? ? ? ? ?       ONNNNNN 7 7 7 7 7 7M$ MS MT M M M M4f> f> f> f> f>~ f> f> f> f> f>r   