a
    bgs                     @  sZ  d dl m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mZmZmZmZmZmZ d dlZd dlmZ d dlmZ d dlmZmZmZ d dlmZmZmZm Z  d dl!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z' d d	l(m)Z) e*e+Z,d
ddddZ-d
ddddZ.d"ddddddZ/d
dddddZ0d
dddddZ1edddd G d!d
 d
eeZ2dS )#    )annotationsN)AnyCallableDictListLiteralMappingOptionalSequenceSetTupleUnioncast)
deprecated)
Embeddings)get_from_dict_or_envget_pydantic_field_namespre_init)	BaseModel
ConfigDictFieldmodel_validator)AsyncRetryingbefore_sleep_logretryretry_if_exception_typestop_after_attemptwait_exponential)is_openai_v1OpenAIEmbeddingszCallable[[Any], Any])
embeddingsreturnc                 C  sn   dd l }tdt| jtd| j| jdt|jj	t|jj
B t|jjB t|jjB t|jjB tttjdS )Nr   T   
multiplierminmaxreraisestopwaitr   before_sleep)openair   r   max_retriesr   retry_min_secondsretry_max_secondsr   errorTimeoutAPIErrorAPIConnectionErrorRateLimitErrorServiceUnavailableErrorr   loggerloggingWARNING)r    r,    r9   s/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/embeddings/openai.py_create_retry_decorator,   s(    





r;   r   c                   s   dd l }tdt| jtd| j| jdt|jj	t|jj
B t|jjB t|jjB t|jjB tttjd ddd fdd	}|S )
Nr   Tr"   r#   r'   r   )funcr!   c                   s   dddd fdd}|S )Nr   r   )argskwargsr!   c                    s6    2 z"3 d H W }| i |I d H   S 6 t dd S )Nzthis is unreachable)AssertionError)r=   r>   _)async_retryingr<   r9   r:   	wrapped_fb   s    z7_async_retry_decorator.<locals>.wrap.<locals>.wrapped_fr9   )r<   rB   rA   )r<   r:   wrapa   s    z$_async_retry_decorator.<locals>.wrap)r,   r   r   r-   r   r.   r/   r   r0   r1   r2   r3   r4   r5   r   r6   r7   r8   )r    r,   rD   r9   rC   r:   _async_retry_decoratorG   s,    





rE   Fdictbool)response
skip_emptyr!   c                 C  s2   t dd | d D r.|s.dd l}|jd| S )Nc                 s  s   | ]}t |d  dkV  qdS )	embeddingr"   N)len).0dr9   r9   r:   	<genexpr>n       z"_check_response.<locals>.<genexpr>datar   z&OpenAI API returned an empty embedding)anyr,   r0   r2   )rH   rI   r,   r9   r9   r:   _check_responsem   s    rR   )r    r>   r!   c                   sF   t  r jjf i |S t }|ddd fdd}|f i |S ))Use tenacity to retry the embedding call.r   r>   r!   c                    s     j jf i | }t| jdS N)rI   )clientcreaterR   rI   r>   rH   r    r9   r:   _embed_with_retry{   s    z+embed_with_retry.<locals>._embed_with_retry)r   rV   rW   r;   )r    r>   Zretry_decoratorrZ   r9   rY   r:   embed_with_retryu   s    r[   c                   sN   t  r jjf i |I dH S t ddd fdd}|f i |I dH S )rS   Nr   rT   c                    s&    j jf i | I d H }t| jdS rU   )rV   ZacreaterR   rI   rX   rY   r9   r:   _async_embed_with_retry   s    z7async_embed_with_retry.<locals>._async_embed_with_retry)r   async_clientrW   rE   )r    r>   r\   r9   rY   r:   async_embed_with_retry   s
    r^   z0.0.9z1.0z!langchain_openai.OpenAIEmbeddings)ZsinceZremovalZalternative_importc                   @  sd  e Zd ZU dZedddZded< edddZded< dZd	ed
< eZ	ded< edddZ
ded< edddZded< dZded< dZded< dZded< edddZded< edddZded< e Zded< dZded< d Zded!< d"Zded#< edd$dZd%ed&< dZded'< dZd(ed)< dZded*< d+Zd(ed,< eed-Zd.ed/< d+Zd(ed0< dZd1ed2< dZd3ed4< d5Z ded6< d7Z!ded8< dZ"d9ed:< e#dd;d<d=Z$e%d>d?e&d.dd@dAdBZ'e(dCdCd@dDdEZ)e*d.dFdGdHZ+ddIdJd	dKdLdMdNdOZ,ddIdJd	dKdLdMdPdQZ-d^dJdKdLdSdTdUZ.d_dJdKdLdSdVdWZ/d	dXdYdZd[Z0d	dXdYd\d]Z1dS )`r   aj  OpenAI embedding models.

    To use, you should have the ``openai`` python package installed, and the
    environment variable ``OPENAI_API_KEY`` set with your API key or pass it
    as a named parameter to the constructor.

    Example:
        .. code-block:: python

            from langchain_community.embeddings import OpenAIEmbeddings
            openai = OpenAIEmbeddings(openai_api_key="my-api-key")

    In order to use the library with Microsoft Azure endpoints, you need to set
    the OPENAI_API_TYPE, OPENAI_API_BASE, OPENAI_API_KEY and OPENAI_API_VERSION.
    The OPENAI_API_TYPE must be set to 'azure' and the others correspond to
    the properties of your endpoint.
    In addition, the deployment name must be passed as the model parameter.

    Example:
        .. code-block:: python

            import os

            os.environ["OPENAI_API_TYPE"] = "azure"
            os.environ["OPENAI_API_BASE"] = "https://<your-endpoint.openai.azure.com/"
            os.environ["OPENAI_API_KEY"] = "your AzureOpenAI key"
            os.environ["OPENAI_API_VERSION"] = "2023-05-15"
            os.environ["OPENAI_PROXY"] = "http://your-corporate-proxy:8080"

            from langchain_community.embeddings.openai import OpenAIEmbeddings
            embeddings = OpenAIEmbeddings(
                deployment="your-embeddings-deployment-name",
                model="your-embeddings-model-name",
                openai_api_base="https://your-endpoint.openai.azure.com/",
                openai_api_type="azure",
            )
            text = "This is a test query."
            query_result = embeddings.embed_query(text)

    NT)defaultexcluder   rV   r]   ztext-embedding-ada-002strmodelzOptional[str]
deploymentapi_version)r_   aliasopenai_api_versionbase_urlopenai_api_baseopenai_api_typeopenai_proxyi  intembedding_ctx_lengthapi_keyopenai_api_keyorganizationopenai_organizationzUnion[Literal['all'], Set[str]]allowed_specialallz.Union[Literal['all'], Set[str], Sequence[str]]disallowed_speciali  
chunk_size   r-   timeoutz0Optional[Union[float, Tuple[float, float], Any]]request_timeoutheadersrG   tiktoken_enabledtiktoken_model_nameFshow_progress_bar)default_factoryzDict[str, Any]model_kwargsrI   zUnion[Mapping[str, str], None]default_headersz!Union[Mapping[str, object], None]default_query   r.      r/   zUnion[Any, None]http_clientZforbidr9   )Zpopulate_by_nameextraZprotected_namespacesbefore)mode)valuesr!   c              
   C  s   t | }|di }t|D ]P}||v r8td| d||vrtd| d| d| d ||||< q|| }|rtd| d	||d< |S )
z>Build extra kwargs from additional params that were passed in.r}   zFound z supplied twice.z	WARNING! z/ is not default parameter.
                    zJ was transferred to model_kwargs.
                    Please confirm that z is what you intended.zParameters za should be specified explicitly. Instead they were passed in as part of `model_kwargs` parameter.)	r   getlist
ValueErrorwarningswarnpopintersectionkeys)clsr   Zall_required_field_namesr   
field_nameZinvalid_model_kwargsr9   r9   r:   build_extra  s*    
zOpenAIEmbeddings.build_extrar   c              	   C  s  t |dd|d< |d p td|d< t |dddd|d< t |d	d
dd|d	< |d dv rrd}t|d d|d< nd}t |dd|d|d< |d ptdptd|d< zddl}W n ty   tdY n0 t rl|d dv rtd |d |d |d |d |d |d |d |d d}|	dsH|j
f i |j|d< |	ds|jf i |j|d< n|	ds|j|d< n |S ) z?Validate that api key and python package exists in environment.rn   ZOPENAI_API_KEYrh   ZOPENAI_API_BASEri   ZOPENAI_API_TYPE )r_   rj   ZOPENAI_PROXYZazureZazure_adZazureadz
2023-05-15rt   i   rf   ZOPENAI_API_VERSIONrp   ZOPENAI_ORG_IDZOPENAI_ORGANIZATIONr   NTCould not import openai python package. Please install it with `pip install openai`.zfIf you have openai>=1.0.0 installed and are using Azure, please use the `AzureOpenAIEmbeddings` class.rw   r-   r~   r   r   )rm   ro   rg   rv   r-   r~   r   r   rV   r]   )r   osgetenvr%   r,   ImportErrorr   r   r   r   ZOpenAIr    ZAsyncOpenAIZ	Embedding)r   r   Zdefault_api_versionr,   Zclient_paramsr9   r9   r:   validate_environment   s|    




z%OpenAIEmbeddings.validate_environment)r!   c              	   C  s   t  rd| ji| j}n~| j| j| j| j| j| j| j| j	d| j}| jdv rX| j
|d< | jrzdd l}W n ty   tdY n0 | j| jd|_|S )Nrb   )rb   rw   rx   rm   ro   Zapi_baseZapi_typerd   r   enginer   r   )httphttps)r   rb   r}   rw   rx   rn   rp   rh   ri   rf   rc   rj   r,   r   proxy)selfZopenai_argsr,   r9   r9   r:   _invocation_paramsm  s4    	


z#OpenAIEmbeddings._invocation_params)rt   z	List[str]zOptional[int]zList[List[float]])textsr   rt   r!   c                C  s  g }g }| j p| j}|p| j}| jszddlm} W n tyN   tdY n0 |j|d}	t|D ]^\}
}|	j	|dd}t
dt|| jD ]4}|||| j  }|	|}|| ||
 qqdnzddl}W n ty   tdY n0 z||}W n, ty(   td	 d
}||}Y n0 t|D ]t\}
}| jdrT|dd}|j	|| j| jd}t
dt|| jD ](}||||| j   ||
 qzq2| jrz$ddlm} |t
dt||}W n$ ty   t
dt||}Y n0 nt
dt||}g }|D ]T}
t| fd||
|
|  i| j}t|tsN| }| dd |d D  qdd t
t|D }dd t
t|D }t
t|D ]T}
| j!rt||
 dkrĐq|||
  ||
  |||
  t||
  qdd t
t|D }t
t|D ]}
||
 }t|dkrpt| fddi| j}t|ts^| }|d d d }nt"j#|d||
 d}|t"j$%| & ||
< q|S )al  
        Generate length-safe embeddings for a list of texts.

        This method handles tokenization and embedding generation, respecting the
        set embedding context length and chunk size. It supports both tiktoken
        and HuggingFace tokenizer based on the tiktoken_enabled flag.

        Args:
            texts (List[str]): A list of texts to embed.
            engine (str): The engine or model to use for embeddings.
            chunk_size (Optional[int]): The size of chunks for processing embeddings.

        Returns:
            List[List[float]]: A list of embeddings for each input text.
        r   AutoTokenizerzCould not import transformers python package. This is needed in order to for OpenAIEmbeddings without `tiktoken`. Please install it with `pip install transformers`. Zpretrained_model_name_or_pathFZadd_special_tokensNCould not import tiktoken python package. This is needed in order to for OpenAIEmbeddings. Please install it with `pip install tiktoken`.5Warning: model not found. Using cl100k_base encoding.cl100k_base001
 textrq   rs   )tqdminputc                 s  s   | ]}|d  V  qdS rJ   Nr9   rL   rr9   r9   r:   rN     rO   z<OpenAIEmbeddings._get_len_safe_embeddings.<locals>.<genexpr>rP   c                 S  s   g | ]}g qS r9   r9   rL   r@   r9   r9   r:   
<listcomp>  rO   z=OpenAIEmbeddings._get_len_safe_embeddings.<locals>.<listcomp>c                 S  s   g | ]}g qS r9   r9   r   r9   r9   r:   r     rO   r"   c                 S  s   g | ]}g qS r9   r9   r   r9   r9   r:   r     rO   r   rJ   Zaxisweights)'rz   rb   rt   ry   transformersr   r   from_pretrained	enumerateencoderangerK   rl   decodeappendtiktokenencoding_for_modelKeyErrorr6   warningget_encodingendswithreplacerq   rs   r{   Z	tqdm.autor   r[   r   
isinstancerF   extendrI   npaveragelinalgnormtolist)r   r   r   rt   tokensindices
model_name_chunk_sizer   	tokenizerir   	tokenizedjtoken_chunk
chunk_textr   encodingrb   tokenr   Z_iterbatched_embeddingsrH   resultsnum_tokens_in_batchr    _resultaverage_embeddedr   r9   r9   r:   _get_len_safe_embeddings  s    





z)OpenAIEmbeddings._get_len_safe_embeddingsc                  sF  g }g }| j p| j}|p| j}| jszddlm} W n tyN   tdY n0 |j|d}	t|D ]^\}
}|	j	|dd}t
dt|| jD ]4}|||| j  }|	|}|| ||
 qqdnzddl}W n ty   tdY n0 z||}W n, ty(   td	 d
}||}Y n0 t|D ]t\}
}| jdrT|dd}|j	|| j| jd}t
dt|| jD ](}||||| j   ||
 qzq2g }|p| j}t
dt||D ]Z}
t| fd||
|
|  i| jI dH }t|ts| }|dd |d D  qdd t
t|D }dd t
t|D }t
t|D ]6}
|||
  ||
  |||
  t||
  q\dd t
t|D }t
t|D ]}
||
 }t|dkrt| fddi| jI dH }t|ts | }|d d d }ntj|d||
 d}|tj !| " ||
< q|S )a  
        Asynchronously generate length-safe embeddings for a list of texts.

        This method handles tokenization and asynchronous embedding generation,
        respecting the set embedding context length and chunk size. It supports both
        `tiktoken` and HuggingFace `tokenizer` based on the tiktoken_enabled flag.

        Args:
            texts (List[str]): A list of texts to embed.
            engine (str): The engine or model to use for embeddings.
            chunk_size (Optional[int]): The size of chunks for processing embeddings.

        Returns:
            List[List[float]]: A list of embeddings for each input text.
        r   r   zCould not import transformers python package. This is needed in order to for OpenAIEmbeddings without  `tiktoken`. Please install it with `pip install transformers`.r   Fr   Nr   r   r   r   r   r   r   r   c                 s  s   | ]}|d  V  qdS r   r9   r   r9   r9   r:   rN   u  rO   z=OpenAIEmbeddings._aget_len_safe_embeddings.<locals>.<genexpr>rP   c                 S  s   g | ]}g qS r9   r9   r   r9   r9   r:   r   w  rO   z>OpenAIEmbeddings._aget_len_safe_embeddings.<locals>.<listcomp>c                 S  s   g | ]}g qS r9   r9   r   r9   r9   r:   r   x  rO   c                 S  s   g | ]}g qS r9   r9   r   r9   r9   r:   r   }  rO   r   rJ   r   )#rz   rb   rt   ry   r   r   r   r   r   r   r   rK   rl   r   r   r   r   r   r6   r   r   r   r   rq   rs   r^   r   r   rF   r   r   r   r   r   r   )r   r   r   rt   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rb   r   r   rH   r   r   r    r   r   r   r9   r9   r:   _aget_len_safe_embeddings  s    





z*OpenAIEmbeddings._aget_len_safe_embeddingsr   )r   rt   r!   c                 C  s   t t| j}| j||dS )aM  Call out to OpenAI's embedding endpoint for embedding search docs.

        Args:
            texts: The list of texts to embed.
            chunk_size: The chunk size of embeddings. If None, will use the chunk size
                specified by the class.

        Returns:
            List of embeddings, one for each text.
        r   )r   ra   rc   r   r   r   rt   r   r9   r9   r:   embed_documents  s    z OpenAIEmbeddings.embed_documentsc                   s    t t| j}| j||dI dH S )aS  Call out to OpenAI's embedding endpoint async for embedding search docs.

        Args:
            texts: The list of texts to embed.
            chunk_size: The chunk size of embeddings. If None, will use the chunk size
                specified by the class.

        Returns:
            List of embeddings, one for each text.
        r   N)r   ra   rc   r   r   r9   r9   r:   aembed_documents  s    z!OpenAIEmbeddings.aembed_documentszList[float])r   r!   c                 C  s   |  |gd S )zCall out to OpenAI's embedding endpoint for embedding query text.

        Args:
            text: The text to embed.

        Returns:
            Embedding for the text.
        r   )r   )r   r   r9   r9   r:   embed_query  s    	zOpenAIEmbeddings.embed_queryc                   s   |  |gI dH }|d S )zCall out to OpenAI's embedding endpoint async for embedding query text.

        Args:
            text: The text to embed.

        Returns:
            Embedding for the text.
        Nr   )r   )r   r   r    r9   r9   r:   aembed_query  s    	zOpenAIEmbeddings.aembed_query)r   )r   )2__name__
__module____qualname____doc__r   rV   __annotations__r]   rb   rc   rf   rh   ri   rj   rl   rn   rp   setrq   rs   rt   r-   rw   rx   ry   rz   r{   rF   r}   rI   r~   r   r.   r/   r   r   Zmodel_configr   classmethodr   r   r   propertyr   r   r   r   r   r   r   r9   r9   r9   r:   r      sf   
)
L$ z  )F)3
__future__r   r7   r   r   typingr   r   r   r   r   r   r	   r
   r   r   r   r   numpyr   Zlangchain_core._api.deprecationr   Zlangchain_core.embeddingsr   Zlangchain_core.utilsr   r   r   Zpydanticr   r   r   r   tenacityr   r   r   r   r   r   Z langchain_community.utils.openair   	getLoggerr   r6   r;   rE   rR   r[   r^   r   r9   r9   r9   r:   <module>   s.   8 	
&