
    @'h7                     d   d Z ddlZddlZddlZddlmZmZmZmZm	Z	m
Z
 ddlmZ ee	e   geee      f   Z	 ee	e   geeee         f   Z	 de
eeeedf   defdZ G d d	e      Zd
ede
eee   f   dee   fdZdedee   fdZdedefdZej0                  deeegef      fd       Zg dZy)an  Utilities for working with embedding functions and LangChain's Embeddings interface.

This module provides tools to wrap arbitrary embedding functions (both sync and async)
into LangChain's Embeddings interface. This enables using custom embedding functions
with LangChain-compatible tools while maintaining support for both synchronous and
asynchronous operations.
    N)Any	AwaitableCallableOptionalSequenceUnion)
Embeddingsembedreturnc                 
   | t        d      t        | t              r=t               }|)ddlm}m} 	  |d      }d| d}t        d|  d	| d
       ||       S t        | t              r| S t        |       S # |$ r d}Y @w xY w)a  Ensure that an embedding function conforms to LangChain's Embeddings interface.

    This function wraps arbitrary embedding functions to make them compatible with
    LangChain's Embeddings interface. It handles both synchronous and asynchronous
    functions.

    Args:
        embed: Either an existing Embeddings instance, or a function that converts
            text to embeddings. If the function is async, it will be used for both
            sync and async operations.

    Returns:
        An Embeddings instance that wraps the provided function(s).

    ??? example "Examples"
        Wrap a synchronous embedding function:
        ```python
        def my_embed_fn(texts):
            return [[0.1, 0.2] for _ in texts]

        embeddings = ensure_embeddings(my_embed_fn)
        result = embeddings.embed_query("hello")  # Returns [0.1, 0.2]
        ```

        Wrap an asynchronous embedding function:
        ```python
        async def my_async_fn(texts):
            return [[0.1, 0.2] for _ in texts]

        embeddings = ensure_embeddings(my_async_fn)
        result = await embeddings.aembed_query("hello")  # Returns [0.1, 0.2]
        ```

        Initialize embeddings using a provider string:
        ```python
        # Requires langchain>=0.3.9 and langgraph-checkpoint>=2.0.11
        embeddings = ensure_embeddings("openai:text-embedding-3-small")
        result = embeddings.embed_query("hello")
        ```
    zembed must be providedr   )PackageNotFoundErrorversion	langchainzFound langchain version z, butzlangchain is not installed;z'Could not load embeddings from string 'z'. aF   loading embeddings by provider:identifier string requires langchain>=0.3.9 as well as the provider-specific package. Install LangChain with: pip install 'langchain>=0.3.9' and the provider-specific package (e.g., 'langchain-openai>=0.3.0'). Alternatively, specify 'embed' as a compatible Embeddings object or python function.)	
ValueError
isinstancestr_get_init_embeddingsimportlib.metadatar   r   r	   EmbeddingsLambda)r
   init_embeddingsr   r   
lc_versionversion_infos         a/home/kushmeetdev/Regenta/Chatbot/venv/lib/python3.12/site-packages/langgraph/store/base/embed.pyensure_embeddingsr      s    V }122%.0"H=$[1
!9*UK 9%L> Rg g  u%%%$E"" ( =<=s   A8 8BBc                        e Zd ZdZdeeef   ddfdZdee	   deee
      fdZde	dee
   fd	Zdee	   deee
      f fd
Zde	dee
   f fdZ xZS )r   a#  Wrapper to convert embedding functions into LangChain's Embeddings interface.

    This class allows arbitrary embedding functions to be used with LangChain-compatible
    tools. It supports both synchronous and asynchronous operations, and can handle:
    1. A synchronous function for sync operations (async operations will use sync function)
    2. An async function for both sync/async operations (sync operations will raise an error)

    The embedding functions should convert text into fixed-dimensional vectors that
    capture the semantic meaning of the text.

    Args:
        func: Function that converts text to embeddings. Can be sync or async.
            If async, it will be used for async operations, but sync operations
            will raise an error. If sync, it will be used for both sync and async operations.

    ??? example "Examples"
        With a sync function:
        ```python
        def my_embed_fn(texts):
            # Return 2D embeddings for each text
            return [[0.1, 0.2] for _ in texts]

        embeddings = EmbeddingsLambda(my_embed_fn)
        result = embeddings.embed_query("hello")  # Returns [0.1, 0.2]
        await embeddings.aembed_query("hello")  # Also returns [0.1, 0.2]
        ```

        With an async function:
        ```python
        async def my_async_fn(texts):
            return [[0.1, 0.2] for _ in texts]

        embeddings = EmbeddingsLambda(my_async_fn)
        await embeddings.aembed_query("hello")  # Returns [0.1, 0.2]
        # Note: embed_query() would raise an error
        ```
    funcr   Nc                 R    |t        d      t        |      r|| _        y || _        y )Nzfunc must be provided)r   _is_async_callableafuncr   )selfr   s     r   __init__zEmbeddingsLambda.__init__   s+     <455d#DJDI    textsc                 F    t        | dd      }|t        d       ||      S )aF  Embed a list of texts into vectors.

        Args:
            texts: list of texts to convert to embeddings.

        Returns:
            list of embeddings, one per input text. Each embedding is a list of floats.

        Raises:
            ValueError: If the instance was initialized with only an async function.
        r   NzEmbeddingsLambda was initialized with an async function but no sync function. Use aembed_documents for async operation or provide a sync function.)getattrr   )r    r#   r   s      r   embed_documentsz EmbeddingsLambda.embed_documents   s6     tVT*<W  E{r"   textc                 ,    | j                  |g      d   S )a1  Embed a single piece of text.

        Args:
            text: Text to convert to an embedding.

        Returns:
            Embedding vector as a list of floats.

        Note:
            This is equivalent to calling embed_documents with a single text
            and taking the first result.
        r   )r&   )r    r'   s     r   embed_queryzEmbeddingsLambda.embed_query   s     ##TF+A..r"   c                    K   t        | dd      }|t        | 	  |       d{   S  ||       d{   S 7 7 w)aY  Asynchronously embed a list of texts into vectors.

        Args:
            texts: list of texts to convert to embeddings.

        Returns:
            list of embeddings, one per input text. Each embedding is a list of floats.

        Note:
            If no async function was provided, this falls back to the sync implementation.
        r   N)r%   superaembed_documents)r    r#   r   	__class__s      r   r,   z!EmbeddingsLambda.aembed_documents   sE      gt,=1%8885\!! 9!s   ">:><>>c                    K   t        | dd      }|t        | 	  |       d{   S  ||g       d{   d   S 7 7 	w)aA  Asynchronously embed a single piece of text.

        Args:
            text: Text to convert to an embedding.

        Returns:
            Embedding vector as a list of floats.

        Note:
            This is equivalent to calling aembed_documents with a single text
            and taking the first result.
        r   Nr   )r%   r+   aembed_query)r    r'   r   r-   s      r   r/   zEmbeddingsLambda.aembed_query   sL      gt,=-d333TFm#Q'' 4#s   "A>AA A A)__name__
__module____qualname____doc__r   EmbeddingsFuncAEmbeddingsFuncr!   listr   floatr&   r)   r,   r/   __classcell__)r-   s   @r   r   r   f   s    $L	NO34	 
	T#Y 4U3D (/ /U /"DI "$tE{:K ""(s (tE{ ( (r"   r   objpathc           	          |r|dk(  rt        j                  | d      gS t        |t              rt	        |      n|}dt
        dt        t           dt        dt        t           ffd | |d	      S )
a  Extract text from an object using a path expression or pre-tokenized path.

    Args:
        obj: The object to extract text from
        path: Either a path string or pre-tokenized path list.

    !!! info "Path types handled"
        - Simple paths: "field1.field2"
        - Array indexing: "[0]", "[*]", "[-1]"
        - Wildcards: "*"
        - Multi-field selection: "{field1,field2}"
        - Nested paths in multi-field: "{field1,nested.field2}"
    $T	sort_keysr9   tokensposr   c           	      6   |t        |      k\  r`t        | t        t        t        t
        f      rt        |       gS | g S t        | t        t        f      rt        j                  | d      gS g S ||   }g }|j                  d      r|j                  d      rt        | t              sg S |dd }|dk(  r%| D ]  }|j                   |||dz                  |S 	 t        |      }|dk  rt        |       |z   }d|cxk  rt        |       k  r"n n|j                   | |   ||dz                |S |j                  d	      r
|j                  d
      rt        | t              sg S |dd j                  d      D cg c]  }|j!                          }	}|	D ]  }
t#        |
      }|s| }|D ]  }t        |t              r
||v r||   }d } n |:t        |t        t        t        t
        f      r|j%                  t        |             ut        |t        t        f      s|j%                  t        j                  |d              |S |dk(  rxt        | t              r3| j'                         D ]  }|j                   |||dz                  |S t        | t              r#| D ]  }|j                   |||dz                  |S t        | t              r#|| v r|j                   | |   ||dz                |S # t        t        f$ r g cY S w xY wc c}w )NTr=   []   *r   {},)lenr   r   intr7   boolr6   dictjsondumps
startswithendswithextendr   
IndexErrorsplitstriptokenize_pathappendvalues)r9   r?   r@   tokenresultsindexitemidxffieldsfieldnested_tokenscurrent_objnested_tokenvalue_extract_from_objs                  r   re   z+get_text_at_path.<locals>._extract_from_obj   s   #f+#S%67Cz!	C$.

3$788IsC U^^C%8c4(	!BKE| MDNN#4T637#KLMd _e*CQw!#hnC*#c(*'8S63QR7'STT M c"u~~c':c4(	).q):):3)?@Aaggi@F@ T -e 4 25K(5 "&{D9 , ;*5l*CK*.K!" #.%kCeT3JK#NN3{+;<'dD\B#NN4::kT+RS#TB  c\#t$ ZZ\ NENN#4UFC!G#LMN  C& MDNN#4T637#KLM  #t$#0UVS1WMNS #J/ I As   AK? <L?LLr   )rN   rO   r   r   rV   r   r6   rK   )r9   r:   r?   re   s      @r   get_text_at_pathrf      sw     43;

3$/00$.tS$9]4 tFEs EDI EC EDI EN S&!,,r"   c                    | sg S g }g }d}|t        |       k  r| |   }|dk(  r|r"|j                  dj                  |             g }d}dg}|dz  }|t        |       k  rM|dkD  rH| |   dk(  r|dz  }n| |   dk(  r|dz  }|j                  | |          |dz  }|t        |       k  r|dkD  rH|j                  dj                  |             |dk(  r|r"|j                  dj                  |             g }d}dg}|dz  }|t        |       k  rM|dkD  rH| |   dk(  r|dz  }n| |   dk(  r|dz  }|j                  | |          |dz  }|t        |       k  r|dkD  rH|j                  dj                  |             s|dk(  r%|r4|j                  dj                  |             g }n|j                  |       |dz  }|t        |       k  r|r |j                  dj                  |             |S )	zTokenize a path into components.

    !!! info "Types handled"
        - Simple paths: "field1.field2"
        - Array indexing: "[0]", "[*]", "[-1]"
        - Wildcards: "*"
        - Multi-field selection: "{field1,field2}"
    r   rB    rD   rC   rG   rH   .)rJ   rW   join)	r:   r?   currenticharbracket_countindex_charsbrace_countfield_charss	            r   rV   rV   ?  s    	FG	A
c$i-Aw3;bggg./M%KFAc$i-MA$57c>!Q&M!W^!Q&M""47+Q c$i-MA$5 MM"''+./S[bggg./K%KFAc$i-K!O7c>1$K!W^1$K""47+Q c$i-K!O MM"''+./S[bggg./NN4 	QW c$i-Z bggg&'Mr"   r   c                     t        j                  |       xs- t        | d      xr t        j                  | j                        S )zCheck if a function is async.

    This includes both async def functions and classes with async __call__ methods.

    Args:
        func: Function or callable object to check.

    Returns:
        True if the function is async, False otherwise.
    __call__)asyncioiscoroutinefunctionhasattrrs   )r   s    r   r   r     s=     	##D) 	74$ 7''6r"   c                  2    	 ddl m}  | S # t        $ r Y y w xY w)Nr   r   )langchain.embeddingsr   ImportErrorrx   s    r   r   r     s"    8 s   
 	)r   r4   r5   )r3   rt   	functoolsrN   typingr   r   r   r   r   r   langchain_core.embeddingsr	   r   r6   r7   r4   r5   r   r   rf   rV   rL   r   	lru_cacher   __all__ r"   r   <module>r      s9      F F 08C=/4U+<<= HSM?Id4;6G,HHID#^_c4GHD#D#Nv(z v(rZ-# Z-U3S	>%: Z-tCy Z-@? ?S	 ?D
	( hxz0A'BC  r"   