§
    æ˜ºh"  ã                  ó  — d dl mZ d dlZd dlmZmZmZmZmZ d dl	m
Z
m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 d d	lmZmZmZmZmZ d d
lmZ  ej        e¦  «        Z dd„Z!dd„Z"dd„Z# G d„ de¦  «        Z$ G d„ dee$¦  «        Z%dS )é    )ÚannotationsN)ÚAnyÚCallableÚDictÚListÚOptional)ÚAsyncCallbackManagerForLLMRunÚCallbackManagerForLLMRun)ÚLLM)ÚSerializable)ÚExtraÚFieldÚroot_validator)Úget_from_dict_or_env)Úbefore_sleep_logÚretryÚretry_if_exception_typeÚstop_after_attemptÚwait_exponential)Úenforce_stop_tokensÚllmÚCohereÚreturnúCallable[[Any], Any]c           
     óæ   — dd l }d}d}t          dt          | j        ¦  «        t	          d||¬¦  «        t          |j        j        ¦  «        t          t          t          j        ¦  «        ¬¦  «        S )Nr   é   é
   Té   )Ú
multiplierÚminÚmax)ÚreraiseÚstopÚwaitr   Úbefore_sleep)Úcoherer   r   Úmax_retriesr   r   ÚerrorÚCohereErrorr   ÚloggerÚloggingÚWARNING)r   r&   Úmin_secondsÚmax_secondss       úg/var/www/html/services/rag-service/venv/lib/python3.11/site-packages/langchain_community/llms/cohere.pyÚ_create_retry_decoratorr0      so   € Ø€M€M€Mà€KØ€Kõ ØÝ ¤Ñ0Ô0Ý¨°ÀÐMÑMÔMÝ& v¤|Ô'?Ñ@Ô@Ý%¥f­g¬oÑ>Ô>ðñ ô ð ó    Úkwargsr   c                óN   ‡ — t          ‰ ¦  «        }|dˆ fd„¦   «         } |di |¤ŽS )ú*Use tenacity to retry the completion call.r2   r   r   c                 ó(   •—  ‰j         j        di | ¤ŽS ©N© )ÚclientÚgenerate©r2   r   s    €r/   Ú_completion_with_retryz5completion_with_retry.<locals>._completion_with_retry/   s   ø€ à"ˆsŒzÔ"Ð,Ð, VÐ,Ð,Ð,r1   ©r2   r   r   r   r7   ©r0   ©r   r2   Úretry_decoratorr;   s   `   r/   Úcompletion_with_retryr@   +   sQ   ø€ å-¨cÑ2Ô2€Oàð-ð -ð -ð -ð -ñ „_ð-ð "Ð!Ð+Ð+ FÐ+Ð+Ð+r1   c                óN   ‡ — t          ‰ ¦  «        }|dˆ fd„¦   «         } |di |¤ŽS )r4   r2   r   r   c               ›  ó8   •K  —  ‰j         j        di | ¤Žƒ d {V —†S r6   )Úasync_clientr9   r:   s    €r/   r;   z6acompletion_with_retry.<locals>._completion_with_retry:   s5   øè è € à.�SÔ%Ô.Ð8Ð8°Ð8Ð8Ð8Ð8Ð8Ð8Ð8Ð8Ð8r1   r<   r7   r=   r>   s   `   r/   Úacompletion_with_retryrD   6   sQ   ø€ å-¨cÑ2Ô2€Oàð9ð 9ð 9ð 9ð 9ñ „_ð9ð "Ð!Ð+Ð+ FÐ+Ð+Ð+r1   c                  óÔ   — e Zd ZU dZded<   ded<    ed¬¦  «        Zded<   	 d	Zd
ed<   	 dZded<   dZ	ded<    ed¬¦  «        Z
ded<   	 dZded<   	  e¦   «         dd„¦   «         ZdS )Ú
BaseCoherezBase class for Cohere models.r   r8   rC   N)ÚdefaultúOptional[str]Úmodelg      è?ÚfloatÚtemperatureÚcohere_api_keyúOptional[List[str]]r#   FÚboolÚ	streamingÚ	langchainÚstrÚ
user_agentÚvaluesr   r   c                óæ   — 	 ddl }t          |dd¦  «        }|d         }|                     ||¬¦  «        |d<   |                     ||¬¦  «        |d<   n# t          $ r t	          d	¦  «        ‚w xY w|S )
z?Validate that api key and python package exists in environment.r   NrL   ÚCOHERE_API_KEYrR   )Úclient_namer8   rC   zTCould not import cohere python package. Please install it with `pip install cohere`.)r&   r   ÚClientÚAsyncClientÚImportError)ÚclsrS   r&   rL   rV   s        r/   Úvalidate_environmentzBaseCohere.validate_environmentV   s²   € ð	ØˆMˆMˆMõ 2ØÐ(Ð*:ñô ˆNð ! Ô.ˆKØ%Ÿ}š}¨^È˜}ÑUÔUˆF�8ÑØ%+×%7Ò%7Ø¨Kð &8ñ &ô &ˆF�>Ñ"Ð"øõ ð 	ð 	ð 	Ýð?ñô ð ð	øøøð ˆs   ‚A ÁA.)rS   r   r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__r   rI   rK   rL   r#   rO   rR   r   r[   r7   r1   r/   rF   rF   A   sä   € € € € € € Ø'Ð'à€K€K�KØÐÐÑØ ˜5¨Ð.Ñ.Ô.€EÐ.Ð.Ð.Ñ.Øà€KÐÐÐÑØQà$(€NÐ(Ð(Ð(Ñ(à $€DÐ$Ð$Ð$Ñ$à�e EÐ*Ñ*Ô*€IÐ*Ð*Ð*Ñ*Ø(à!€JÐ!Ð!Ð!Ñ!Ø<à€^ÑÔðð ð ñ Ôðð ð r1   rF   c                  ó*  — e Zd ZU dZ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<   	 dZ
ded<   	 dZded<   	  G d„ d¦  «        Zed*d„¦   «         Zed+d„¦   «         Zed*d„¦   «         Zed,d„¦   «         Zd-d!„Zd.d#„Z	 	 d/d0d'„Z	 	 d/d1d)„ZdS )2r   a¦  Cohere large language models.

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

    Example:
        .. code-block:: python

            from langchain_community.llms import Cohere

            cohere = Cohere(model="gptd-instruct-tft", cohere_api_key="my-api-key")
    é   ÚintÚ
max_tokensr   Úkr   Úpg        rJ   Úfrequency_penaltyÚpresence_penaltyNrH   Útruncater   r'   c                  ó    — e Zd ZdZej        ZdS )úCohere.Configz'Configuration for this pydantic object.N)r\   r]   r^   r_   r   ÚforbidÚextrar7   r1   r/   ÚConfigrk   ‘   s   € € € € € Ø5Ð5à”ˆˆˆr1   rn   r   úDict[str, Any]c                ó\   — | j         | j        | j        | j        | j        | j        | j        dœS )z2Get the default parameters for calling Cohere API.©rd   rK   re   rf   rg   rh   ri   rq   ©Úselfs    r/   Ú_default_paramszCohere._default_params–   s9   € ð œ/ØÔ+Ø”Ø”Ø!%Ô!7Ø $Ô 5Øœð
ð 
ð 	
r1   úDict[str, str]c                ó
   — ddiS )NrL   rU   r7   rr   s    r/   Ú
lc_secretszCohere.lc_secrets£   s   € à Ð"2Ð3Ð3r1   c                ó&   — i d| j         i¥| j        ¥S )zGet the identifying parameters.rI   )rI   rt   rr   s    r/   Ú_identifying_paramszCohere._identifying_params§   s   € ð A�7˜DœJÐ'Ð@¨4Ô+?Ð@Ð@r1   rQ   c                ó   — dS )zReturn type of llm.r&   r7   rr   s    r/   Ú	_llm_typezCohere._llm_type¬   s	   € ð ˆxr1   r#   rM   r2   r   Údictc                óz   — | j         }| j        �|�t          d¦  «        ‚| j        �| j        |d<   n||d<   i |¥|¥S )Nz2`stop` found in both the input and default params.Ústop_sequences)rt   r#   Ú
ValueError)rs   r#   r2   Úparamss       r/   Ú_invocation_paramszCohere._invocation_params±   sY   € ØÔ%ˆØŒ9Ð  TÐ%5ÝÐQÑRÔRÐRØŒYÐ"Ø'+¤yˆFÐ#Ñ$Ð$à'+ˆFÐ#Ñ$Ø#�&Ð#˜FÐ#Ð#r1   Úresponsec                óN   — |j         d         j        }|rt          ||¦  «        }|S )Nr   )ÚgenerationsÚtextr   )rs   r‚   r#   r…   s       r/   Ú_process_responsezCohere._process_response»   s0   € ØÔ# AÔ&Ô+ˆð ð 	3Ý& t¨TÑ2Ô2ˆDØˆr1   ÚpromptÚrun_managerú"Optional[CallbackManagerForLLMRun]c                óž   —  | j         |fi |¤Ž}t          | f| j        |dœ|¤Ž}|                     d¦  «        }|                      ||¦  «        S )ah  Call out to Cohere's generate endpoint.

        Args:
            prompt: The prompt to pass into the model.
            stop: Optional list of stop words to use when generating.

        Returns:
            The string generated by the model.

        Example:
            .. code-block:: python

                response = cohere("Tell me a joke.")
        ©rI   r‡   r~   )r�   r@   rI   Úgetr†   ©rs   r‡   r#   rˆ   r2   r€   r‚   Ú_stops           r/   Ú_callzCohere._callÃ   ss   € ð* )�Ô(¨Ð8Ð8°Ð8Ð8ˆÝ(Øð
Øœ
¨6ð
ð 
Ø5;ð
ð 
ˆð —
’
Ð+Ñ,Ô,ˆØ×%Ò% h°Ñ6Ô6Ð6r1   ú'Optional[AsyncCallbackManagerForLLMRun]c              ‹  ó®   K  —  | j         |fi |¤Ž}t          | f| j        |dœ|¤Žƒ d{V —†}|                     d¦  «        }|                      ||¦  «        S )at  Async call out to Cohere's generate endpoint.

        Args:
            prompt: The prompt to pass into the model.
            stop: Optional list of stop words to use when generating.

        Returns:
            The string generated by the model.

        Example:
            .. code-block:: python

                response = await cohere("Tell me a joke.")
        r‹   Nr~   )r�   rD   rI   rŒ   r†   r�   s           r/   Ú_acallzCohere._acallß   s•   è è € ð* )�Ô(¨Ð8Ð8°Ð8Ð8ˆÝ/Øð
Øœ
¨6ð
ð 
Ø5;ð
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ˆð —
’
Ð+Ñ,Ô,ˆØ×%Ò% h°Ñ6Ô6Ð6r1   )r   ro   )r   ru   )r   rQ   )r#   rM   r2   r   r   r|   )r‚   r   r#   rM   r   rQ   )NN)
r‡   rQ   r#   rM   rˆ   r‰   r2   r   r   rQ   )
r‡   rQ   r#   rM   rˆ   r�   r2   r   r   rQ   )r\   r]   r^   r_   rd   r`   re   rf   rg   rh   ri   r'   rn   Úpropertyrt   rw   ry   r{   r�   r†   r�   r’   r7   r1   r/   r   r   l   sÂ  € € € € € € ðð ð €JÐÐÐÑØAà€A€J€J€J�JØ@à€A€J€J€J�JØDà"ÐÐ"Ð"Ð"Ñ"ØLà!ÐÐ!Ð!Ð!Ñ!Ø5à"€HÐ"Ð"Ð"Ñ"ð0ð €KÐÐÐÑØ<ðð ð ð ð ñ ô ð ð
 ð

ð 

ð 

ñ „Xð

ð ð4ð 4ð 4ñ „Xð4ð ðAð Að Añ „XðAð ðð ð ñ „Xðð$ð $ð $ð $ðð ð ð ð %)Ø:>ð	7ð 7ð 7ð 7ð 7ð> %)Ø?Cð	7ð 7ð 7ð 7ð 7ð 7ð 7r1   )r   r   r   r   )r   r   r2   r   r   r   )&Ú
__future__r   r+   Útypingr   r   r   r   r   Úlangchain_core.callbacksr	   r
   Ú#langchain_core.language_models.llmsr   Ú langchain_core.load.serializabler   Úlangchain_core.pydantic_v1r   r   r   Úlangchain_core.utilsr   Útenacityr   r   r   r   r   Úlangchain_community.llms.utilsr   Ú	getLoggerr\   r*   r0   r@   rD   rF   r   r7   r1   r/   ú<module>rž      sÛ  ðØ "Ð "Ð "Ð "Ð "Ð "à €€€Ø 6Ð 6Ð 6Ð 6Ð 6Ð 6Ð 6Ð 6Ð 6Ð 6Ð 6Ð 6Ð 6Ð 6ðð ð ð ð ð ð ð ð 4Ð 3Ð 3Ð 3Ð 3Ð 3Ø 9Ð 9Ð 9Ð 9Ð 9Ð 9Ø CÐ CÐ CÐ CÐ CÐ CÐ CÐ CÐ CÐ CØ 5Ð 5Ð 5Ð 5Ð 5Ð 5ðð ð ð ð ð ð ð ð ð ð ð ð ð ð ?Ð >Ð >Ð >Ð >Ð >à	ˆÔ	˜8Ñ	$Ô	$€ðð ð ð ð ,ð ,ð ,ð ,ð,ð ,ð ,ð ,ð(ð (ð (ð (ð (�ñ (ô (ð (ðVM7ð M7ð M7ð M7ð M7ˆS�*ñ M7ô M7ð M7ð M7ð M7r1   