U
    DAf.                     @   s   d dl Z d dlmZmZmZmZ d dlmZ d dlm	Z	m
Z
mZ d dlmZmZ ee jddZee jdd	Ze	G d
d dZdS )    N)AnyDictListOptional)OpenAI)	componentdefault_from_dictdefault_to_dict)Secretdeserialize_secrets_inplaceOPENAI_TIMEOUT   OPENAI_MAX_RETRIES   c                   @   s   e Zd ZdZedddddddddf	eeee ee ee eeee	 ee d	ddZ
eeef d	d
dZeeef d	ddZeeeef d dddZejee	 eeef dedddZdS )OpenAITextEmbeddera  
    A component for embedding strings using OpenAI models.

    Usage example:
    ```python
    from haystack.components.embedders import OpenAITextEmbedder

    text_to_embed = "I love pizza!"

    text_embedder = OpenAITextEmbedder()

    print(text_embedder.run(text_to_embed))

    # {'embedding': [0.017020374536514282, -0.023255806416273117, ...],
    # 'meta': {'model': 'text-embedding-ada-002-v2',
    #          'usage': {'prompt_tokens': 4, 'total_tokens': 4}}}
    ```
    ZOPENAI_API_KEYztext-embedding-ada-002N )	api_keymodel
dimensionsapi_base_urlorganizationprefixsuffixtimeoutmax_retriesc
           
      C   sz   || _ || _|| _|| _|| _|| _|| _|dkrDttj	
dd}|	dkr^ttj	
dd}	t| ||||	d| _dS )an  
        Create an OpenAITextEmbedder component.

        By setting the 'OPENAI_TIMEOUT' and 'OPENAI_MAX_RETRIES' you can change the timeout and max_retries parameters
        in the OpenAI client.

        :param api_key:
            The OpenAI API key.
        :param model:
            The name of the model to use.
        :param dimensions:
            The number of dimensions the resulting output embeddings should have. Only supported in `text-embedding-3` a
            nd later models.
        :param api_base_url:
            Overrides default base url for all HTTP requests.
        :param organization:
            The Organization ID. See OpenAI's
            [production best practices](https://platform.openai.com/docs/guides/production-best-practices/setting-up-your-organization)
            for more information.
        :param prefix:
            A string to add at the beginning of each text.
        :param suffix:
            A string to add at the end of each text.
        :param timeout:
            Timeout for OpenAI Client calls, if not set it is inferred from the `OPENAI_TIMEOUT` environment variable
            or set to 30.
        :param max_retries:
            Maximum retries to stablish contact with OpenAI if it returns an internal error, if not set it is inferred
            from the `OPENAI_MAX_RETRIES` environment variable or set to 5.
        Nr   g      >@r   r   )r   r   base_urlr   r   )r   r   r   r   r   r   r   floatosenvirongetintr   Zresolve_valueclient)
selfr   r   r   r   r   r   r   r   r    r#   V/tmp/pip-unpacked-wheel-z752163x/haystack/components/embedders/openai_text_embedder.py__init__&   s$    *zOpenAITextEmbedder.__init__)returnc                 C   s
   d| j iS )zC
        Data that is sent to Posthog for usage analytics.
        r   )r   r"   r#   r#   r$   _get_telemetry_datae   s    z&OpenAITextEmbedder._get_telemetry_datac              
   C   s*   t | | j| j| j| j| j| j| j dS )z{
        Serializes the component to a dictionary.

        :returns:
            Dictionary with serialized data.
        )r   r   r   r   r   r   r   )	r	   r   r   r   r   r   r   r   to_dictr'   r#   r#   r$   r)   k   s    zOpenAITextEmbedder.to_dict)datar&   c                 C   s   t |d dgd t| |S )z
        Deserializes the component from a dictionary.

        :param data:
            Dictionary to deserialize from.
        :returns:
            Deserialized component.
        Zinit_parametersr   )keys)r   r   )clsr*   r#   r#   r$   	from_dict}   s    
zOpenAITextEmbedder.from_dict	embeddingmeta)textc                 C   s   t |tstd| j| | j }|dd}| jdk	rR| jjj	| j
| j|d}n| jjj	| j
|d}|j
t|jd}|jd j|d	S )
a  
        Embed a single string.

        :param text:
            Text to embed.

        :returns:
            A dictionary with the following keys:
            - `embedding`: The embedding of the input text.
            - `meta`: Information about the usage of the model.
        zOpenAITextEmbedder expects a string as an input.In case you want to embed a list of Documents, please use the OpenAIDocumentEmbedder.
 N)r   r   input)r   r4   )r   usager   r.   )
isinstancestr	TypeErrorr   r   replacer   r!   Z
embeddingscreater   dictr5   r*   r/   )r"   r1   Ztext_to_embedresponser0   r#   r#   r$   run   s    

zOpenAITextEmbedder.run)__name__
__module____qualname____doc__r
   Zfrom_env_varr7   r   r    r   r%   r   r   r(   r)   classmethodr-   r   Zoutput_typesr   r=   r#   r#   r#   r$   r      s6   ?r   )r   typingr   r   r   r   Zopenair   Zhaystackr   r   r	   Zhaystack.utilsr
   r   r   r   r   r   r    r   r   r#   r#   r#   r$   <module>   s   