
    -j                        d dl mZ d dlZd dlmZ d dlmZ ej        rd dl	m
Z d dlmZ 	 d dlmZ d dlmZ n# e$ r  ed	          w xY w	 dddZ	 	 	 d d!dZdS )"    )annotationsN)RunEvalConfigEvaluatorChain)Dataset)TestDataset)Client)LangSmithNotFoundErrorzZPlease install langsmith to use this feature. You can install it via pip install langsmith datasetr   dataset_namestrdataset_descreturnLangsmithDatasetc                6   t                      }	 |                    |          } t          d| d|  d          # t          $ rR |                    |                                 |dgdg|          } t          d| j         d	| j                    | cY S w xY w)
a  
    Uploads a new dataset to LangSmith, converting it from a TestDataset object to a
    pandas DataFrame before upload. If a dataset with the specified name already
    exists, the function raises an error.

    Parameters
    ----------
    dataset : TestDataset
        The dataset to be uploaded.
    dataset_name : str
        The name for the new dataset in LangSmith.
    dataset_desc : str, optional
        A description for the new dataset. The default is an empty string.

    Returns
    -------
    LangsmithDataset
        The dataset object as stored in LangSmith after upload.

    Raises
    ------
    ValueError
        If a dataset with the specified name already exists in LangSmith.

    Notes
    -----
    The function attempts to read a dataset by the given name to check its existence.
    If not found, it proceeds to upload the dataset after converting it to a pandas
    DataFrame. This involves specifying input and output keys for the dataset being
    uploaded.
    r   Dataset z already exists in langsmith. []questionground_truth)dfname
input_keysoutput_keysdescriptionzCreated a new dataset 'z'. Dataset is accessible at )	r	   read_dataset
ValueErrorr
   upload_dataframe	to_pandasprintr   url)r   r   r   clients       f/lsinfo/ai/hellotax_ai/base_platform/venv/lib/python3.11/site-packages/ragas/integrations/langsmith.pyupload_datasetr%      s    D XXF%%<%@@N|NNGNNN
 
 	
 "   ))  """|'($ * 
 
 	]gl]]PWP[]]	
 	
 	
 s   ,< ABBFllm_or_chain_factoryt.Anyexperiment_namet.Optional[str]metricst.Optional[list]verboseboolt.Dict[str, t.Any]c                (   t                      }	 |                    |           }n!# t          $ r t          d|  d          w xY w|ddlm}m}m}	m}
 |||
|	g}d |D             }t          |          }|
                    | ||||	          }|S )
aP  
    Evaluates a language model or a chain factory on a specified dataset using
    LangSmith, with the option to customize metrics and verbosity.

    Parameters
    ----------
    dataset_name : str
        The name of the dataset to use for evaluation. This dataset must exist in
        LangSmith.
    llm_or_chain_factory : Any
        The language model or chain factory to be evaluated. This parameter is
        flexible and can accept a variety of objects depending on the implementation.
    experiment_name : Optional[str], optional
        The name of the experiment. This can be used to categorize or identify the
        evaluation run within LangSmith. The default is None.
    metrics : Optional[list], optional
        A list of custom metrics (functions or evaluators) to be used for the
        evaluation. If None, a default set of metrics (answer relevancy, context
        precision, context recall, and faithfulness) are used.
        The default is None.
    verbose : bool, optional
        If True, detailed progress and results will be printed during the evaluation
        process.
        The default is False.

    Returns
    -------
    Dict[str, Any]
        A dictionary containing the results of the evaluation.

    Raises
    ------
    ValueError
        If the specified dataset does not exist in LangSmith.

    See Also
    --------
    Client.read_dataset : Method to read an existing dataset.
    Client.run_on_dataset : Method to run the evaluation on the specified dataset.

    Examples
    --------
    >>> results = evaluate(
    ...     dataset_name="MyDataset",
    ...     llm_or_chain_factory=my_llm,
    ...     experiment_name="experiment_1_with_vanila_rag",
    ...     verbose=True
    ... )
    >>> print(results)
    {'evaluation_result': ...}

    Notes
    -----
    The function initializes a client to interact with LangSmith, validates the existence
    of the specified dataset, prepares evaluation metrics, and runs the evaluation,
    returning the results. Custom evaluation metrics can be specified, or a default set
    will be used if none are provided.
    r   r   z9 not found in langsmith, make sure it exists in langsmithNr   )answer_relevancycontext_precisioncontext_recallfaithfulnessc                ,    g | ]}t          |          S  r   ).0ms     r$   
<listcomp>zevaluate.<locals>.<listcomp>   s     222Q~a  222    )custom_evaluators)r   r&   
evaluationr,   project_name)r	   r   r
   r   ragas.metricsr0   r1   r2   r3   r   run_on_dataset)r   r&   r(   r*   r,   r#   _r0   r1   r2   r3   eval_configruns                r$   evaluaterB   P   s   D XXF
\::! 
 
 
^|^^^
 
 	

 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 $%6nU22'222G!  K
 

!1$    C Js	   ' A)r   )r   r   r   r   r   r   r   r   )NNF)r   r   r&   r'   r(   r)   r*   r+   r,   r-   r   r.   )
__future__r   typingtlangchain.smithr   ragas.integrations.langchainr   TYPE_CHECKINGlangsmith.schemasr   r   ragas.testset.generatorr   	langsmithr	   langsmith.utilsr
   ImportErrorr%   rB   r5   r9   r$   <module>rN      s)   " " " " " "     ) ) ) ) ) ) 7 7 7 7 7 7? 4======333333      6666666   
+d   BD6 6 6 6 6x (, $d d d d d d ds	   8 A	