
    =Ki.                         S r SSKJrJrJrJrJr  SSKJr  SSK	J
r
  SSKJrJrJr  SSKJrJr   " S S\5      r " S	 S
\5      rS\\\4   S\4S jr " S S\5      rg)zIContains the `LLMEvaluator` class for building LLM-as-a-judge evaluators.    )AnyCallableOptionalUnioncast)	BaseModel)	warn_beta)EvaluationResultEvaluationResultsRunEvaluator)ExampleRunc                   `    \ rS rSr% Sr\\S'   \\   \S'   \\S'   Sr\	\S'   Sr
\\   \S	'   S
rg)CategoricalScoreConfig   z&Configuration for a categorical score.keychoicesdescriptionFinclude_explanationNexplanation_description )__name__
__module____qualname____firstlineno____doc__str__annotations__listr   boolr   r   __static_attributes__r       d/var/www/html/dynamic-report/venv/lib/python3.13/site-packages/langsmith/evaluation/llm_evaluator.pyr   r      s4    0	H#Y %%-1Xc]1r"   r   c                   l    \ rS rSr% Sr\\S'   Sr\\S'   Sr	\\S'   \\S'   S	r
\\S
'   Sr\\   \S'   Srg)ContinuousScoreConfig   z%Configuration for a continuous score.r   r   min   maxr   Fr   Nr   r   )r   r   r   r   r   r   r   r'   floatr)   r   r    r   r   r!   r   r"   r#   r%   r%      s<    /	HCNCN %%-1Xc]1r"   r%   score_configreturnc                    0 n[        U [        5      (       a1  SU R                  SSR                  U R                  5       S3S.US'   OZ[        U [        5      (       a:  SU R
                  U R                  SU R
                   S	U R                   S
3S.US'   O[        S5      eU R                  (       a!  SU R                  c  SOU R                  S.US'   U R                  U R                  SUU R                  (       a  SS/S.$ S/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.)r0   minimummaximumr   z9Invalid score type. Must be 'categorical' or 'continuous'zThe explanation for the score.)r0   r   explanationobject)titler   r0   
propertiesrequired)
isinstancer   r   joinr%   r'   r)   
ValueErrorr   r   r   r   )r+   r9   s     r#   _create_score_json_schemar>   !   s,    "$J, 677 ((Byy--./q2

7 
L"7	8	8#''#''C l&6&6%7|E	

7 TUU''  77? 1!99%

=! !!#// (4(H(HWm$  PWi r"   c                      \ rS rSrSrSSSS.S\\\\\\4      4   S\\	\
4   S	\\\\\   /\4      S
\S\4
S jjr\SS.S\S\\\\\\4      4   S\\	\
4   S	\\\\\   /\4      4S jj5       rS\\\\\\4      4   S\\	\
4   S	\\\\\   /\4      S\4S jr\ SS\S\\   S\\\4   4S jj5       r\ SS\S\\   S\\\4   4S jj5       rS\S\\   S\4S jrS\S\\\4   4S jrSrg)LLMEvaluatorL   zA 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+   rC   rD   rE   c                     SSK Jn  U" SXES.UD6n	U R                  XX95        g! [         a  n[        S5      UeSnAff = f)a5  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)modelrE   r   )langchain.chat_modelsrH   ImportError_initialize)
selfrF   r+   rC   rD   rE   kwargsrH   e
chat_models
             r#   __init__LLMEvaluator.__init__T   sd    8	 % 

?E

 	R  	O 	s   % 
A ;A )rC   rI   c                L    U R                  U 5      nUR                  X#XA5        U$ )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__rL   )clsrI   rF   r+   rC   instances         r#   
from_modelLLMEvaluator.from_model   s'    8 ;;s#_MQr"   rP   c                 \    SSK Jn  SSKJn  [        XE5      (       a  [        US5      (       d  [        S5      e[        U[        5      (       a  UR                  SU4/5      U l
        OUR                  U5      U l
        [        U R                  R                  5      1 S	k-
  (       a  U(       d  [        S
5      eX0l        X l        [        U R                  5      U l        UR#                  U R                   5      nU R                  U-  U l        g! [         a  n[	        S5      UeSnAff = f)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_modelsrZ   langchain_core.promptsr[   rK   r;   hasattrr=   r   from_messagespromptsetinput_variablesrC   r+   r>   score_schemar\   runnable)rM   rF   r+   rC   rP   rZ   r[   rO   s           r#   rL   LLMEvaluator._initialize   s   "	PA z11
$<==C 
 os++,::Wo<V;WXDK,::?KDKt{{**+.MM  M  +(5d6G6GH66t7H7HI
j0A  	Y 	s   D 
D+D&&D+runexampler,   c                     U R                  X5      n[        [        U R                  R	                  U5      5      nU R                  U5      $ )zEvaluate a run.)_prepare_variablesr   dictri   invoke_parse_outputrM   rk   rl   	variablesr_   s        r#   evaluate_runLLMEvaluator.evaluate_run   s@    
 ++C9	D$--"6"6y"AB!!&))r"   c                    #    U R                  X5      n[        [        U R                  R	                  U5      I Sh  vN 5      nU R                  U5      $  N7f)zAsynchronously evaluate a run.N)rn   r   ro   ri   ainvokerq   rr   s        r#   aevaluate_runLLMEvaluator.aevaluate_run   sL     
 ++C9	D(=(=i(H"HI!!&)) #Is   :AA
Ac                    U R                   (       a  U R                  X5      $ 0 nSU R                  R                  ;   aq  [        UR                  5      S:X  a  [        S5      e[        UR                  5      S:w  a  [        S5      e[        UR                  R                  5       5      S   US'   SU R                  R                  ;   a  UR                  (       d  [        S5      e[        UR                  5      S:X  a  [        S5      e[        UR                  5      S:w  a  [        S5      e[        UR                  R                  5       5      S   US'   S	U R                  R                  ;   a  U(       a  UR                  (       d  [        S
5      e[        UR                  5      S:X  a  [        S5      e[        UR                  5      S:w  a  [        S5      e[        UR                  R                  5       5      S   US	'   U$ )z'Prepare variables for model invocation.r^   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.r_   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.r`   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.)	rC   re   rg   leninputsr=   r   valuesoutputs)rM   rk   rl   rs   s       r#   rn   LLMEvaluator._prepare_variables   s   %%c33	dkk1113::!# (  3::!# 0  "&cjj&7&7&9!:1!=Igt{{222;; )  3;;1$ )  3;;1$ 8  #'s{{'9'9';"<Q"?Ih444'// +  7??#q( +  7??#q( 8  %))?)?)A$B1$EIj!r"   r_   c                 T   [        U R                  [        5      (       a5  US   nUR                  SS5      n[	        U R                  R
                  X#S9$ [        U R                  [        5      (       a5  US   nUR                  SS5      n[	        U R                  R
                  XCS9$ g)z1Parse the model output into an evaluation result.r2   r6   N)r   valuecomment)r   r2   r   )r;   r+   r   getr
   r   r%   )rM   r_   r   r6   r2   s        r#   rq   LLMEvaluator._parse_output!  s    d'')?@@7OE **]D9K#%%))  ))+@AA7OE **]D9K#%%))  Br"   )rC   re   ri   r+   rh   )N)r   r   r   r   r   r   r   r   tupler   r%   r   r   r   r   ro   rQ   classmethodr   rW   rL   r	   r
   r   rt   rx   rn   rq   r!   r   r"   r#   r@   r@   L   s2    MQ"&*S sDsCx$99:*S 24IIJ	*S
  #x/@)A4)G HI*S *S *SX  MQ sDsCx$99:	
 24IIJ  #x/@)A4)G HI >41sDsCx$99:41 24IIJ41  #x/@)A4)G HI	41
 41l 59**!)'!2*	!22	3* * 59**!)'!2*	!22	3* *7c 7HW4E 7$ 7rD U3CEV3V-W r"   r@   N)r   typingr   r   r   r   r   pydanticr   #langsmith._internal._beta_decoratorr	   langsmith.evaluationr
   r   r   langsmith.schemasr   r   r   r%   ro   r>   r@   r   r"   r#   <module>r      se    O 7 7  9 R R *2Y 22I 2(.0EEF(	(Vb< br"   