Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
embed.cpython-313.pyc193 linesDownload Raw Back to __pycache__
1�

2��j�8��$�SrSSKJr SSKrSSKrSSKrSSKJrJrJ	r	 SSK3Jr SSKJ
r
 \\	\/\\\4r\\	\/\\\\4rSSjr"SS	\
5rSS4jrSSjrSSjr\R0SS
j5r/SQrg)anUtilities for working with embedding functions and LangChain's Embeddings interface.5 6This module provides tools to wrap arbitrary embedding functions (both sync and async)7into LangChain's Embeddings interface. This enables using custom embedding functions8with LangChain-compatible tools while maintaining support for both synchronous and9asynchronous operations.10�)�annotationsN)�	Awaitable�Callable�Sequence)�Any)�11Embeddingsc�$�Uc[S5e[U[5(a>[5nUc)SSKJnJn U"S5nSUS3n[SUS	US1235eU"U5$[U[5(aU$[U5$!Ua SnNFf=f)a,Ensure that an embedding function conforms to LangChain's Embeddings interface.13 14This function wraps arbitrary embedding functions to make them compatible with15LangChain's Embeddings interface. It handles both synchronous and asynchronous16functions.17 18Args:19    embed: Either an existing Embeddings instance, or a function that converts20        text to embeddings. If the function is async, it will be used for both21        sync and async operations.22 23Returns:24    An Embeddings instance that wraps the provided function(s).25 26??? example "Examples"27 28    Wrap a synchronous embedding function:29 30    ```python31    def my_embed_fn(texts):32        return [[0.1, 0.2] for _ in texts]33 34    embeddings = ensure_embeddings(my_embed_fn)35    result = embeddings.embed_query("hello")  # Returns [0.1, 0.2]36    ```37 38    Wrap an asynchronous embedding function:39 40    ```python41    async def my_async_fn(texts):42        return [[0.1, 0.2] for _ in texts]43 44    embeddings = ensure_embeddings(my_async_fn)45    result = await embeddings.aembed_query("hello")  # Returns [0.1, 0.2]46    ```47 48    Initialize embeddings using a provider string:49 50    ```python51    # Requires langchain>=0.3.9 and langgraph-checkpoint>=2.0.1152    embeddings = ensure_embeddings("openai:text-embedding-3-small")53    result = embeddings.embed_query("hello")54    ```55zembed must be providedr)�PackageNotFoundError�version�	langchainzFound langchain version z, butzlangchain 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.)	�56ValueError�57isinstance�str�_get_init_embeddings�importlib.metadatar58rr�EmbeddingsLambda)�embed�init_embeddingsr59r�60lc_version�version_infos      �`D:\code\apps\devtools\python\user_packages\Python313\site-packages\langgraph/store/base/embed.py�ensure_embeddingsr"s���^
�}��1�2�2��%����.�0���"�H�
=�$�[�1�61�!9�*��U�K���9�%���L�>�Rg�g��
��u�%�%��%��$�$����E�"�"��(�
=�<��
=�s�B�B�Bc�f^�\rSrSrSrS	SjrS62SjrSSjrS63U4SjjrSU4Sjjr	Sr64U=r$)r�ma�Wrapper to convert embedding functions into LangChain's Embeddings interface.65 66This class allows arbitrary embedding functions to be used with LangChain-compatible67tools. It supports both synchronous and asynchronous operations, and can handle:681. A synchronous function for sync operations (async operations will use sync function)692. An async function for both sync/async operations (sync operations will raise an error)70 71The embedding functions should convert text into fixed-dimensional vectors that72capture the semantic meaning of the text.73 74Args:75    func: Function that converts text to embeddings. Can be sync or async.76        If async, it will be used for async operations, but sync operations77        will raise an error. If sync, it will be used for both sync and async operations.78 79??? example "Examples"80 81    With a sync function:82 83    ```python84    def my_embed_fn(texts):85        # Return 2D embeddings for each text86        return [[0.1, 0.2] for _ in texts]87 88    embeddings = EmbeddingsLambda(my_embed_fn)89    result = embeddings.embed_query("hello")  # Returns [0.1, 0.2]90    await embeddings.aembed_query("hello")  # Also returns [0.1, 0.2]91    ```92 93    With an async function:94 95    ```python96    async def my_async_fn(texts):97        return [[0.1, 0.2] for _ in texts]98 99    embeddings = EmbeddingsLambda(my_async_fn)100    await embeddings.aembed_query("hello")  # Returns [0.1, 0.2]101    # Note: embed_query() would raise an error102    ```103c�Z�Uc[S5e[U5(aXlgXlg)Nzfunc must be provided)r
�_is_async_callable�afunc�func)�selfrs  r�__init__�EmbeddingsLambda.__init__�s*���<��4�5�5��d�#�#��J��I�c�H�[USS5nUc[S5eU"U5$)aEmbed a list of texts into vectors.104 105Args:106    texts: list of texts to convert to embeddings.107 108Returns:109    list of embeddings, one per input text. Each embedding is a list of floats.110 111Raises:112    ValueError: If the instance was initialized with only an async function.113rNz�EmbeddingsLambda was initialized with an async function but no sync function. Use aembed_documents for async operation or provide a sync function.)�getattrr
)r�textsrs   r�embed_documents� EmbeddingsLambda.embed_documents�s6���t�V�T�*���<��W��
��E�{�r"c�,�URU/5S$)z�Embed a single piece of text.114 115Args:116    text: Text to convert to an embedding.117 118Returns:119    Embedding vector as a list of floats.120 121Note:122    This is equivalent to calling embed_documents with a single text123    and taking the first result.124r)r&)r�texts  r�embed_query�EmbeddingsLambda.embed_query�s���#�#�T�F�+�A�.�.r"c��># �[USS5nUc[TU]	U5IShv�N$U"U5IShv�N$NN7f)a!Asynchronously embed a list of texts into vectors.125 126Args:127    texts: list of texts to convert to embeddings.128 129Returns:130    list of embeddings, one per input text. Each embedding is a list of floats.131 132Note:133    If no async function was provided, this falls back to the sync implementation.134rN)r$�super�aembed_documents)rr%r�	__class__s   �rr.�!EmbeddingsLambda.aembed_documents�sC������g�t�,���=���1�%�8�8�8��5�\�!�!�9�!�s�#?�;�?�=�?�?c��># �[USS5nUc[TU]	U5IShv�N$U"U/5IShv�NS$NN	7f)aAsynchronously embed a single piece of text.135 136Args:137    text: Text to convert to an embedding.138 139Returns:140    Embedding vector as a list of floats.141 142Note:143    This is equivalent to calling aembed_documents with a single text144    and taking the first result.145rNr)r$r-�aembed_query)rr)rr/s   �rr2�EmbeddingsLambda.aembed_query�sJ������g�t�,���=���-�d�3�3�3��T�F�m�#�Q�'�'�4�#�s�#A�?�A�A�A�A)rr)rz EmbeddingsFunc | AEmbeddingsFunc�return�None)r%�	list[str]r4zlist[list[float]])r)rr4zlist[float])�__name__�146__module__�__qualname__�__firstlineno__�__doc__r r&r*r.r2�__static_attributes__�
__classcell__)r/s@rrrms9���'�R	�.�	�147�	��(
/�"�"(�(r"rc�^�U(aUS:Xa[R"USSS9/$[U[5(a[	U5OUnSU4SjjmT"XS5$)a�Extract text from an object using a path expression or pre-tokenized path.148 149Args:150    obj: The object to extract text from151    path: Either a path string or pre-tokenized path list.152 153!!! info "Path types handled"154    - Simple paths: "field1.field2"155    - Array indexing: "[0]", "[*]", "[-1]"156    - Wildcards: "*"157    - Multi-field selection: "{field1,field2}"158    - Nested paths in multi-field: "{field1,nested.field2}"159�$TF��	sort_keys�ensure_asciic	��>�U[U5:�aj[U[[[[16045(a[U5/$Uc/$[U[[45(a[R"USSS9/$/$Xn/nURS5(a�URS5(a�[U[5(d/$USSnUS:Xa&UHnURT"XaUS-55 M  U$[U5nUS	:a[U5U-nS	Us=::a[U5:a!O U$URT"XXS-55 U$URS1615(Ga3URS5(Ga[U[5(d/$USSRS5Vs/sHo�R!5PM n	nU	H�n162[#U1635nU(dMUnUH%n
[U[5(aX�;aX�nM#Sn O UcMI[U[[[[16445(aUR%[U55 M�[U[[45(dM�UR%[R"USSS95 M� U$US:Xa�[U[5(a4UR'5HnURT"X�US-55 M  U$[U[5(a$UHnURT"XaUS-55 M  U$[U[5(a"X0;aURT"XXS-55 U$![[4a /s$f=fs snf)
NTFr@�[�]�������*r�{�}�,)�lenrr�int�float�bool�list�dict�json�dumps�165startswith�endswith�extendr
�166IndexError�split�strip�
tokenize_path�append�values)�obj�tokens�pos�token�results�index�item�idx�f�fields�field�
nested_tokens�current_obj�nested_token�value�_extract_from_objs               �rrl�+get_text_at_path.<locals>._extract_from_obj�s#����#�f�+���#��S�%��6�7�7��C��z�!����	��C�$���.�.��167�168�3�$�U�K�L�L��I���������C� � �U�^�^�C�%8�%8��c�4�(�(��	��!�B�K�E���|��D��N�N�#4�T�3��7�#K�L� �l��g��e�*�C��Q�w�!�#�h��n���C�*�#�c�(�*�^��] ���'8���6�QR�7�'S�T�\��U�
�
�c�
"�
"�u�~�~�c�':�':��c�4�(�(��	�).�q���):�):�3�)?�@�)?�A�g�g�i�)?�F�@��� -�e� 4�
� �=�/2�K�(5��&�{�D�9�9� ,� ;�*5�*C�K�*.�K�!�)6�#�.�%�k�C��e�T�3J�K�K�#�N�N�3�{�+;�<�'��d�D�\�B�B�#�N�N� $�169�170�$/�4�e�!"��# �J���c�\��#�t�$�$� �Z�Z�\�E��N�N�#4�U�C�!�G�#L�M�*����C��&�&��D��N�N�#4�T�3��7�#K�L� ����#�t�$�$������0���V�1�W�M�N����[#�J�/���I���As�67M�/M�)M3�M0�/M0r)r]rr^r6r_rMr4r6)rRrSrrrZ)r]�pathr^rls   @r�get_text_at_pathro�sR����4�3�;��171�172�3�$�U�C�D�D�$.�t�S�$9�$9�]�4�
 �t�F�I�V�S�!�,�,r"c�4�U(d/$/n/nSnU[U5:Ga�XnUS:Xa�U(a"URSRU55 /nSnS/nUS-
nU[U5:aPUS:�aJXS:XaUS-
nO
XS:XaUS-nURX5 US-
nU[U5:aUS:�aMJURSRU55 M�US:Xa�U(a"URSRU55 /nSnS/nUS-
nU[U5:aPUS:�aJXS:XaUS-
nO
XS:XaUS-nURX5 US-
nU[U5:aUS:�aMJURSRU55 GM�US:Xa*U(a"URSRU55 /nOURU5 US-
nU[U5:aGM�U(a URSRU55 U$)	z�Tokenize a path into components.173 174!!! info "Types handled"175    - Simple paths: "field1.field2"176    - Array indexing: "[0]", "[*]", "[-1]"177    - Wildcards: "*"178    - Multi-field selection: "{field1,field2}"179rrD�rFrErIrJ�.)rLr[�join)	rnr^�current�i�char�
bracket_count�index_chars�brace_count�field_charss	         rrZrZMs����	�
�F��G�	�A�180�c�$�i�-��w���3�;���
�
�b�g�g�g�.�/����M��%�K�
��F�A��c�$�i�-�M�A�$5��7�c�>�!�Q�&�M��W��^�!�Q�&�M��"�"�4�7�+��Q���
�c�$�i�-�M�A�$5�
�M�M�"�'�'�+�.�/��
�S�[���
�
�b�g�g�g�.�/����K��%�K�
��F�A��c�$�i�-�K�!�O��7�c�>��1�$�K��W��^��1�$�K��"�"�4�7�+��Q���
�c�$�i�-�K�!�O�
�M�M�"�'�'�+�.�/��
�S�[���
�
�b�g�g�g�.�/�����N�N�4� �	�Q���W�c�$�i�-�Z��
�
�b�g�g�g�&�'��Mr"c��[R"U5=(d3 [US5=(a  [R"UR5$)z�Check if a function is async.181 182This includes both async def functions and classes with async __call__ methods.183 184Args:185    func: Function or callable object to check.186 187Returns:188    True if the function is async, False otherwise.189�__call__)�asyncio�iscoroutinefunction�hasattrr|)rs rrr�s=��	�#�#�D�)�	7��4��$�7��'�'��
�
�6�r"c�4�SSKJn U$![a gf=f)Nr�r)�langchain.embeddingsr�ImportErrorr�s rrr�s"���8��������s�190�191�)r�EmbeddingsFunc�AEmbeddingsFunc)rz:Embeddings | EmbeddingsFunc | AEmbeddingsFunc | str | Noner4r)r]rrnzstr | list[str]r4r6)rnrr4r6)rrr4rO)r4z"Callable[[str], Embeddings] | None)r;�192__future__rr}�	functoolsrR�collections.abcrrr�typingr�langchain_core.embeddingsrrrPrNr�r�rrrorZr�	lru_cacher�__all__�r"r�<module>r�s����#����9�9��0��8�C�=�/�4��U��+<�<�=����H�S�M�?�I�d�4��;�6G�,H�H�I���H#�E�H#��H#�Vy(�z�y(�x^-�H?�D�193
��	��(�������r"
codekingpro/portable-devtools · Team Ai