a
    •˜ëjW  ã                
   @   sþ   d dl Z d dlmZ d dlmZmZmZ d dlmZ d dl	m
Z
 dddd	d
ddddœZedœdd„Zeeeef dœdd„Zddœeee eeef dœdd„Ze jeeƒd�eddœdd„ƒZddœeee eeee
eee f f dœdd„ZddgZdS ) é    N)Úutil)ÚAnyÚOptionalÚUnion)Ú
Embeddings)ÚRunnableÚlangchain_openaiÚlangchain_awsÚlangchain_cohereÚlangchain_google_vertexaiÚlangchain_huggingfaceÚlangchain_mistralaiÚlangchain_ollama)Úazure_openaiÚbedrockÚcohereÚgoogle_vertexaiÚhuggingfaceÚ	mistralaiÚollamaÚopenai)Úreturnc                   C   s   d  dd„ t ¡ D ƒ¡S )z3Get formatted list of providers and their packages.Ú
c                 s   s*   | ]"\}}d |› d|  dd¡› �V  qdS )z  - z: Ú_ú-N)Úreplace)Ú.0ÚpÚpkg© r   úX/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langchain/embeddings/base.pyÚ	<genexpr>   s   z%_get_provider_list.<locals>.<genexpr>)ÚjoinÚ_SUPPORTED_PROVIDERSÚitemsr   r   r   r    Ú_get_provider_list   s    
ÿr%   )Ú
model_namer   c                 C   s‚   d| vr$t }d| › d|› �}t|ƒ‚|  dd¡\}}| ¡  ¡ }| ¡ }|t vrjd|› dtƒ › �}t|ƒ‚|szd}t|ƒ‚||fS )a×  Parse a model string into provider and model name components.

    The model string should be in the format 'provider:model-name', where provider
    is one of the supported providers.

    Args:
        model_name: A model string in the format 'provider:model-name'

    Returns:
        A tuple of (provider, model_name)

    .. code-block:: python

        _parse_model_string("openai:text-embedding-3-small")
        # Returns: ("openai", "text-embedding-3-small")

        _parse_model_string("bedrock:amazon.titan-embed-text-v1")
        # Returns: ("bedrock", "amazon.titan-embed-text-v1")

    Raises:
        ValueError: If the model string is not in the correct format or
            the provider is unsupported

    ú:zInvalid model format 'zÏ'.
Model name must be in format 'provider:model-name'
Example valid model strings:
  - openai:text-embedding-3-small
  - bedrock:amazon.titan-embed-text-v1
  - cohere:embed-english-v3.0
Supported providers: é   ú
Provider 'úE' is not supported.
Supported providers and their required packages:
úModel name cannot be empty)r#   Ú
ValueErrorÚsplitÚlowerÚstripr%   )r&   Ú	providersÚmsgÚproviderÚmodelr   r   r    Ú_parse_model_string   s(    úÿ	þÿr4   ©r2   )r3   r2   r   c                C   sz   |   ¡ sd}t|ƒ‚|d u r2d| v r2t| ƒ\}}n| }|sPt}d|› �}t|ƒ‚|tvrrd|› dtƒ › �}t|ƒ‚||fS )Nr+   r'   z—Must specify either:
1. A model string in format 'provider:model-name'
   Example: 'openai:text-embedding-3-small'
2. Or explicitly set provider from: r)   r*   )r/   r,   r4   r#   r%   )r3   r2   r1   r&   r0   r   r   r    Ú_infer_model_and_providerR   s(    üÿþÿr6   )Úmaxsize)r   r   c                 C   s(   t  | ¡s$d| › d| › d�}t|ƒ‚dS )z Check if a package is installed.zCould not import z5 python package. Please install it with `pip install ú`N)r   Ú	find_specÚImportError)r   r1   r   r   r    Ú
_check_pkgt   s    
ÿÿr;   )r3   r2   Úkwargsr   c                K   sš  | s$t  ¡ }dd |¡› �}t|ƒ‚t| |d�\}}t | }t|ƒ |dkrjddlm} |f d|i|¤ŽS |dkr�dd	lm} |f d|i|¤ŽS |d
kr¶ddl	m
}	 |	f d|i|¤ŽS |dkrÜddlm}
 |
f d|i|¤ŽS |dk�rddlm} |f d|i|¤ŽS |dk�r,ddlm} |f d|i|¤ŽS |dk�rTddlm} |f d|i|¤ŽS |dk�r|ddlm} |f d|i|¤ŽS d|› dtƒ › �}t|ƒ‚dS )aG  Initialize an embeddings model from a model name and optional provider.

    **Note:** Must have the integration package corresponding to the model provider
    installed.

    Args:
        model: Name of the model to use. Can be either:
            - A model string like "openai:text-embedding-3-small"
            - Just the model name if provider is specified
        provider: Optional explicit provider name. If not specified,
            will attempt to parse from the model string. Supported providers
            and their required packages:

            {_get_provider_list()}

        **kwargs: Additional model-specific parameters passed to the embedding model.
            These vary by provider, see the provider-specific documentation for details.

    Returns:
        An Embeddings instance that can generate embeddings for text.

    Raises:
        ValueError: If the model provider is not supported or cannot be determined
        ImportError: If the required provider package is not installed

    .. dropdown:: Example Usage
        :open:

        .. code-block:: python

            # Using a model string
            model = init_embeddings("openai:text-embedding-3-small")
            model.embed_query("Hello, world!")

            # Using explicit provider
            model = init_embeddings(model="text-embedding-3-small", provider="openai")
            model.embed_documents(["Hello, world!", "Goodbye, world!"])

            # With additional parameters
            model = init_embeddings("openai:text-embedding-3-small", api_key="sk-...")

    .. versionadded:: 0.3.9

    z2Must specify model name. Supported providers are: z, r5   r   r   )ÚOpenAIEmbeddingsr3   r   )ÚAzureOpenAIEmbeddingsr   )ÚVertexAIEmbeddingsr   )ÚBedrockEmbeddingsZmodel_idr   )ÚCohereEmbeddingsr   )ÚMistralAIEmbeddingsr   )ÚHuggingFaceEmbeddingsr&   r   )ÚOllamaEmbeddingsr)   r*   N)r#   Úkeysr"   r,   r6   r;   r   r=   r>   r   r?   r	   r@   r
   rA   r   rB   r   rC   r   rD   r%   )r3   r2   r<   r0   r1   r&   r   r=   r>   r?   r@   rA   rB   rC   rD   r   r   r    Úinit_embeddings   sJ    2ÿ



þÿrF   r   )Ú	functoolsÚ	importlibr   Útypingr   r   r   Zlangchain_core.embeddingsr   Zlangchain_core.runnablesr   r#   Ústrr%   Útupler4   r6   Ú	lru_cacheÚlenr;   ÚlistÚfloatrF   Ú__all__r   r   r   r    Ú<module>   sB   ø:ý
ü"ýûfþ