Ë
    ³Œjà.  ã                   ó¼   — d Z ddlmZmZmZ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mZ  G d„ de«      Z G d	„ d
e«      Zdeeef   defd„Z G d„ de«      Zy)zIContains the `LLMEvaluator` class for building LLM-as-a-judge evaluators.é    )ÚAnyÚCallableÚOptionalÚUnionÚcast)Ú	BaseModel)Ú	warn_beta)ÚEvaluationResultÚEvaluationResultsÚRunEvaluator)ÚExampleÚRunc                   óX   — e Zd ZU dZeed<   ee   ed<   eed<   dZeed<   dZ	e
e   ed<   y)	ÚCategoricalScoreConfigz&Configuration for a categorical score.ÚkeyÚchoicesÚdescriptionFÚinclude_explanationNÚexplanation_description)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚstrÚ__annotations__Úlistr   Úboolr   r   © ó    úl/var/www/html/Fitness-lenito-AI-main/venv/lib/python3.12/site-packages/langsmith/evaluation/llm_evaluator.pyr   r      s4   … Ù0à	ƒHØ�#‰YÓØÓØ %Ð˜Ó%Ø-1Ð˜X c™]Ô1r   r   c                   ód   — e Zd ZU dZeed<   dZeed<   dZeed<   eed<   dZ	e
ed	<   d
Zee   ed<   y
)ÚContinuousScoreConfigz%Configuration for a continuous score.r   r   Úminé   Úmaxr   Fr   Nr   )r   r   r   r   r   r   r#   Úfloatr%   r   r   r   r   r   r   r    r"   r"      s<   … Ù/à	ƒHØ€CˆƒNØ€CˆƒNØÓØ %Ð˜Ó%Ø-1Ð˜X c™]Ô1r   r"   Úscore_configÚreturnc                 óè  — i }t        | t        «      r1d| j                  ddj                  | j                  «      › d�dœ|d<   nUt        | t        «      r:d| j
                  | j                  d| j
                  › d	| j                  › d
�dœ|d<   nt        d«      ‚| j                  r d| j                  €dn| j                  dœ|d<   | j                  | j                  d|| j                  rddgdœS dgdœS )NÚstringz%The score for the evaluation, one of z, Ú.)ÚtypeÚenumr   ÚscoreÚnumberz&The score for the evaluation, between z and z, inclusive.)r,   ÚminimumÚmaximumr   z9Invalid score type. Must be 'categorical' or 'continuous'zThe explanation for the score.)r,   r   ÚexplanationÚobject)Útitler   r,   Ú
propertiesÚrequired)Ú
isinstancer   r   Újoinr"   r#   r%   Ú
ValueErrorr   r   r   r   )r'   r5   s     r    Ú_create_score_json_schemar:   !   s&  € ð "$€JÜ�,Ô 6Ô7àØ ×(Ñ(ØBØ�y‰y˜×-Ñ-Ó.Ð/¨qð2ñ
ˆ
�7Òô 
�LÔ"7Ô	8àØ#×'Ñ'Ø#×'Ñ'ØCØ×ÑÐ   l×&6Ñ&6Ð%7°|ðEñ	
ˆ
�7Òô ÐTÓUÐUà×'Ò'àð  ×7Ñ7Ð?ñ 1à!×9Ñ9ñ%
ˆ
�=Ñ!ð ×!Ñ!Ø#×/Ñ/ØØ à(4×(HÒ(HˆW�mÐ$ñð ð PWÈiñð r   c                   óÈ  — e Zd ZdZddddœdeeeeeef      f   deee	f   de
eee
e   gef      d	ed
ef
d„Zeddœdedeeeeeef      f   deee	f   de
eee
e   gef      fd„«       Zdeeeeeef      f   deee	f   de
eee
e   gef      defd„Ze	 ddede
e   deeef   fd„«       Ze	 ddede
e   deeef   fd„«       Zdede
e   defd„Zdedeeef   fd„Zy)ÚLLMEvaluatorz´A class for building LLM-as-a-judge evaluators.

    .. deprecated:: 0.5.0

       LLMEvaluator is deprecated. Use openevals instead: https://github.com/langchain-ai/openevals
    Nzgpt-4oÚopenai)Úmap_variablesÚ
model_nameÚmodel_providerÚprompt_templater'   r>   r?   r@   c                óŠ   — 	 ddl m}  |d||dœ|¤Ž}	| j                  ||||	«       y# t        $ r}t        d«      |‚d}~ww xY w)a­  Initialize the `LLMEvaluator`.

        Args:
            prompt_template (Union[str, List[Tuple[str, str]]): The prompt
                template to use for the evaluation. If a string is provided, it is
                assumed to be a human / user message.
            score_config (Union[CategoricalScoreConfig, ContinuousScoreConfig]):
                The configuration for the score, either categorical or continuous.
            map_variables (Optional[Callable[[Run, Example], dict]], optional):
                A function that maps the run and example to the variables in the
                prompt.

                If `None`, it is assumed that the prompt only requires 'input',
                'output', and 'expected'.
            model_name (Optional[str], optional): The model to use for the evaluation.
            model_provider (Optional[str], optional): The model provider to use
                for the evaluation.
        r   )Úinit_chat_modelzmLLMEvaluator requires langchain to be installed. Please install langchain by running `pip install langchain`.N)Úmodelr@   r   )Úlangchain.chat_modelsrC   ÚImportErrorÚ_initialize)
ÚselfrA   r'   r>   r?   r@   ÚkwargsrC   ÚeÚ
chat_models
             r    Ú__init__zLLMEvaluator.__init__T   sj   € ð8	õñ %ð 
Ø¨^ñ
Ø?Eñ
ˆ
ð 	×Ñ˜¨,¸ÀzÕRøô ò 	ÜðOóð ðûð	ús   ‚( ¨	A±=½A)r>   rD   c                óP   — | j                  | «      }|j                  ||||«       |S )a¢  Create an `LLMEvaluator` instance from a `BaseChatModel` instance.

        Args:
            model (BaseChatModel): The chat model instance to use for the evaluation.
            prompt_template (Union[str, List[Tuple[str, str]]): The prompt
                template to use for the evaluation. If a string is provided, it is
                assumed to be a system message.
            score_config (Union[CategoricalScoreConfig, ContinuousScoreConfig]):
                The configuration for the score, either categorical or continuous.
            map_variables (Optional[Callable[[Run, Example]], dict]], optional):
                A function that maps the run and example to the variables in the
                prompt.

                If `None`, it is assumed that the prompt only requires 'input',
                'output', and 'expected'.

        Returns:
            LLMEvaluator: An instance of `LLMEvaluator`.
        )Ú__new__rG   )ÚclsrD   rA   r'   r>   Úinstances         r    Ú
from_modelzLLMEvaluator.from_model€   s+   € ð8 —;‘;˜sÓ#ˆØ×Ñ˜_¨l¸MÈ5ÔQØˆr   rK   c                 ó.  — 	 ddl m} ddlm} t        ||«      rt        |d«      st        d«      ‚t        |t        «      r|j                  d|fg«      | _
        n|j                  |«      | _
        t        | j                  j                  «      h d	£z
  r|st        d
«      ‚|| _        || _        t        | j                  «      | _        |j#                  | j                   «      }| j                  |z  | _        y# t        $ r}t	        d«      |‚d}~ww xY w)aÕ  Shared initialization code for `__init__` and `from_model`.

        Args:
            prompt_template (Union[str, List[Tuple[str, str]]): The prompt template.
            score_config (Union[CategoricalScoreConfig, ContinuousScoreConfig]):
                The score configuration.
            map_variables (Optional[Callable[[Run, Example]], dict]]):
                Function to map variables.
            chat_model (BaseChatModel): The chat model instance.
        r   )ÚBaseChatModel)ÚChatPromptTemplatez|LLMEvaluator requires langchain-core to be installed. Please install langchain-core by running `pip install langchain-core`.NÚwith_structured_outputzRchat_model must be an instance of BaseLanguageModel and support structured output.Úhuman>   ÚinputÚoutputÚexpectedzrmap_inputs must be provided if the prompt template contains variables other than 'input', 'output', and 'expected')Ú*langchain_core.language_models.chat_modelsrS   Úlangchain_core.promptsrT   rF   r7   Úhasattrr9   r   Úfrom_messagesÚpromptÚsetÚinput_variablesr>   r'   r:   Úscore_schemarU   Úrunnable)rH   rA   r'   r>   rK   rS   rT   rJ   s           r    rG   zLLMEvaluator._initialize    s  € ð"	ÝPÝAô �z =Ô1Ü˜
Ð$<Ô=äðCóð ô
 �o¤sÔ+Ø,×:Ñ:¸WÀoÐ<VÐ;WÓXˆD�Kà,×:Ñ:¸?ÓKˆDŒKäˆt�{‰{×*Ñ*Ó+Ò.MÒMÙ Ü ðMóð ð +ˆÔà(ˆÔÜ5°d×6GÑ6GÓHˆÔà×6Ñ6°t×7HÑ7HÓIˆ
ØŸ™ jÑ0ˆ�øôA ò 	ÜðYóð ðûð	ús   ‚C: Ã:	DÄDÄDÚrunÚexampler(   c                 óš   — | j                  ||«      }t        t        | j                  j	                  |«      «      }| j                  |«      S )zEvaluate a run.)Ú_prepare_variablesr   Údictrb   ÚinvokeÚ_parse_output©rH   rc   rd   Ú	variablesrX   s        r    Úevaluate_runzLLMEvaluator.evaluate_runÖ   sB   € ð
 ×+Ñ+¨C°Ó9ˆ	ÜœD $§-¡-×"6Ñ"6°yÓ"AÓBˆØ×!Ñ! &Ó)Ð)r   c              ƒ   ó¶   K  — | j                  ||«      }t        t        | j                  j	                  |«      ƒ d{  –—† «      }| j                  |«      S 7 Œ­w)zAsynchronously evaluate a run.N)rf   r   rg   rb   Úainvokeri   rj   s        r    Úaevaluate_runzLLMEvaluator.aevaluate_runß   sO   è ø€ ð
 ×+Ñ+¨C°Ó9ˆ	ÜœD¨¯©×(=Ñ(=¸iÓ(H×"HÓIˆØ×!Ñ! &Ó)Ð)ð #Iús   ‚;A½A
¾Ac                 óÐ  — | j                   r| j                  ||«      S i }d| j                  j                  v rot        |j                  «      dk(  rt        d«      ‚t        |j                  «      dk7  rt        d«      ‚t        |j                  j                  «       «      d   |d<   d| j                  j                  v r†|j                  st        d«      ‚t        |j                  «      dk(  rt        d«      ‚t        |j                  «      dk7  rt        d«      ‚t        |j                  j                  «       «      d   |d<   d	| j                  j                  v rˆ|r|j                  st        d
«      ‚t        |j                  «      dk(  rt        d«      ‚t        |j                  «      dk7  rt        d«      ‚t        |j                  j                  «       «      d   |d	<   |S )z'Prepare variables for model invocation.rW   r   zHNo input keys are present in run.inputs but the prompt requires 'input'.r$   zWMultiple input keys are present in run.inputs. Please provide a map_variables function.rX   zKNo output keys are present in run.outputs but the prompt requires 'output'.zYMultiple output keys are present in run.outputs. Please provide a map_variables function.rY   zMNo example or example outputs is provided but the prompt requires 'expected'.zQNo output keys are present in example.outputs but the prompt requires 'expected'.z]Multiple output keys are present in example.outputs. Please provide a map_variables function.)	r>   r^   r`   ÚlenÚinputsr9   r   ÚvaluesÚoutputs)rH   rc   rd   rk   s       r    rf   zLLMEvaluator._prepare_variablesè   sÖ  € à×ÒØ×%Ñ% c¨7Ó3Ð3àˆ	Ø�d—k‘k×1Ñ1Ñ1Ü�3—:‘:‹ !Ò#Ü ð(óð ô �3—:‘:‹ !Ò#Ü ð0óð ô "& c§j¡j×&7Ñ&7Ó&9Ó!:¸1Ñ!=ˆI�gÑà�t—{‘{×2Ñ2Ñ2Ø—;’;Ü ð)óð ô �3—;‘;Ó 1Ò$Ü ð)óð ô �3—;‘;Ó 1Ò$Ü ð8óð ô #' s§{¡{×'9Ñ'9Ó';Ó"<¸QÑ"?ˆI�hÑà˜Ÿ™×4Ñ4Ñ4Ù '§/¢/Ü ð+óð ô �7—?‘?Ó# qÒ(Ü ð+óð ô �7—?‘?Ó# qÒ(Ü ð8óð ô %)¨¯©×)?Ñ)?Ó)AÓ$BÀ1Ñ$EˆI�jÑ!àÐr   rX   c                 óP  — t        | j                  t        «      r9|d   }|j                  dd«      }t	        | j                  j
                  ||¬«      S t        | j                  t        «      r9|d   }|j                  dd«      }t	        | j                  j
                  ||¬«      S y)z1Parse the model output into an evaluation result.r.   r2   N)r   ÚvalueÚcomment)r   r.   rw   )r7   r'   r   Úgetr
   r   r"   )rH   rX   rv   r2   r.   s        r    ri   zLLMEvaluator._parse_output!  sž   € ä�d×'Ñ'Ô)?Ô@Ø˜7‘OˆEØ Ÿ*™* ]°DÓ9ˆKÜ#Ø×%Ñ%×)Ñ)°Àôð ô ˜×)Ñ)Ô+@ÔAØ˜7‘OˆEØ Ÿ*™* ]°DÓ9ˆKÜ#Ø×%Ñ%×)Ñ)°Àôð ð Br   )N)r   r   r   r   r   r   r   Útupler   r"   r   r   r   r   rg   rL   Úclassmethodr   rQ   rG   r	   r
   r   rl   ro   rf   ri   r   r   r    r<   r<   L   s2  „ ñð MQØ"Ø&ò*Sð ˜s D¨¨s°C¨x©Ñ$9Ð9Ñ:ð*Sð Ð2Ð4IÐIÑJð	*Sð
   ¨#¨x¸Ñ/@Ð)AÀ4Ð)GÑ HÑIð*Sð ð*Sð ó*SðX ð MQòàðð ˜s D¨¨s°C¨x©Ñ$9Ð9Ñ:ð	ð
 Ð2Ð4IÐIÑJðð   ¨#¨x¸Ñ/@Ð)AÀ4Ð)GÑ HÑIòó ðð>41à˜s D¨¨s°C¨x©Ñ$9Ð9Ñ:ð41ð Ð2Ð4IÐIÑJð41ð   ¨#¨x¸Ñ/@Ð)AÀ4Ð)GÑ HÑIð	41ð
 ó41ðl à59ñ*Øð*Ø!)¨'Ñ!2ð*à	ÐÐ!2Ð2Ñ	3ò*ó ð*ð à59ñ*Øð*Ø!)¨'Ñ!2ð*à	ÐÐ!2Ð2Ñ	3ò*ó ð*ð7 cð 7°H¸WÑ4Eð 7È$ó 7ðr Dð ¨UÐ3CÐEVÐ3VÑ-Wô r   r<   N)r   Útypingr   r   r   r   r   Úpydanticr   Ú#langsmith._internal._beta_decoratorr	   Úlangsmith.evaluationr
   r   r   Úlangsmith.schemasr   r   r   r"   rg   r:   r<   r   r   r    Ú<module>r€      se   ðÙ Oç 7Õ 7å å 9ß RÑ Rß *ô2˜Yô 2ô2˜Iô 2ð(ØÐ.Ð0EÐEÑFð(à	ó(ôVb�<õ br   