Ë
    ³ŒjH#  ã                   ó  — d Z ddlZddlZddlZddlZddlmZ ddlmZm	Z	 ddl
mZ ddl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	ed
edefd„Zdej:                  dedee   fd„Zedddddœdeej:                     de dee    dee   de!de!dejD                  fd„«       Z#de dedeej:                     fd„Z$ e	d«      Z% e	d«      Z&dee%   dee&   dee'e%e&f      fd„Z( ed «      ed!dd"œde d#ed$ee)   dee   ddf
d%„«       «       Z*y)&zfBeta utility functions to assist in common eval workflows.

These functions may change in the future.
é    N)ÚSequence)ÚOptionalÚTypeVar)Ú
evaluation)Ú_v2_migration_utils)Ú
deprecatedÚsuppress_deprecation_warningÚ	warn_beta)ÚClientÚrun_dictÚid_mapÚreturnc                 óö   — | d   }|j                  «       D ])  \  }}|j                  t        |«      t        |«      «      }Œ+ || d<   | j                  d«      r|| d      | d<   | j                  d«      si | d<   | S )zøConvert the IDs in the run dictionary using the provided ID map.

    Parameters:
    - run_dict: The dictionary representing a run.
    - id_map: The dictionary mapping old IDs to new IDs.

    Returns:
    - dict: The updated run dictionary.
    Údotted_orderÚparent_run_idÚextra)ÚitemsÚreplaceÚstrÚget)r   r   ÚdoÚkÚvs        ú_/var/www/html/Fitness-lenito-AI-main/venv/lib/python3.12/site-packages/langsmith/beta/_evals.pyÚ_convert_idsr      s   € ð 
�.Ñ	!€BØ—‘–‰ˆˆ1Ø�Z‰Zœ˜A›¤ A£Ó'‰ð à!€Hˆ^Ñà‡|�|�OÔ$Ø$*¨8°OÑ+DÑ$Eˆ�Ñ!Ø�<‰<˜Ô Øˆ�ÑØ€Oó    ÚrootÚrun_to_example_mapc                 ó  — | g}t        j                  «       }| j                  |i}g }|r¢|j                  «       }|j	                  h d£¬«      }|j                  |d   t        j                  «       «      ||d   <   ||d      |d<   ||d      |d<   |j                  r|j                  |j                  «       |j                  |«       |rŒ¢|D �cg c]  }t        ||«      ‘Œ }	}|| j                     |	d   d<   |	S c c}w )a  Convert the root run and its child runs to a list of dictionaries.

    Parameters:
    - root: The root run to convert.
    - run_to_example_map: The dictionary mapping run IDs to example IDs.

    Returns:
    - The list of converted run dictionaries.
    >   Ú
session_idÚchild_run_idsÚparent_run_ids)ÚexcludeÚidÚtrace_idr   Úreference_example_id)ÚuuidÚuuid4r%   ÚpopÚdictr   Ú
child_runsÚextendÚappendr   r$   )
r   r   Úruns_r%   r   ÚresultsÚsrcÚsrc_dictÚrÚresults
             r   Ú_convert_root_runr4   /   sø   € ð ˆF€EÜ�z‰z‹|€HØ�m‰m˜XÐ&€FØ€GÙ
Ø�i‰i‹kˆØ—8‘8Ò$U�8ÓVˆØ!'§¡¨H°T©N¼D¿J¹J»LÓ!Iˆˆx˜‰~ÑØ ¨¡Ñ/ˆ�‰Ø% h¨zÑ&:Ñ;ˆ�ÑØ�>Š>Ø�L‰L˜Ÿ™Ô(Ø�‰�xÔ ò ñ 07Ó7©w¨!Œl˜1˜fÕ%¨w€FÐ7Ø(:¸4¿7¹7Ñ(C€Fˆ1�IÐ$Ñ%Ø€Mùò 8s   ÃC<F)Útest_project_nameÚclientÚload_child_runsÚinclude_outputsÚrunsÚdataset_namer5   r6   r7   r8   c                ó´  — | st        d| › �«      ‚|xs t        j                  «       }|j                  |¬«      }|r| D �cg c]  }|j                  ‘Œ c}nd}|j                  | D �cg c]  }|j                  ‘Œ c}|| D �cg c]  }|j                  ‘Œ c}|j                  ¬«       |s| }	n| D �cg c]  }|j                  |«      ‘Œ }	}|xs$ dt        j                  «       j                  dd › �}t        |j                  |¬«      «      }
|
D �ci c]  }|j                  |j                  “Œ }}|
d   j                  r|
d   j                  n|
d   j                   }|	D ��cg c]  }t#        ||«      D ]  }|‘Œ Œ }}}|j%                  ||j                  d|j'                  «       d	œ¬
«      }|D ]i  }|d   |d   z
  }t(        j(                  j+                  t(        j,                  j.                  ¬«      |d<   |d   |z   |d<    |j0                  di |¤d|i¤Ž Œk |j3                  |j                  «      }|S c c}w c c}w c c}w c c}w c c}w c c}}w )aÔ  Convert the following runs to a dataset + test.

    This makes it easy to sample prod runs into a new regression testing
    workflow and compare against a candidate system.

    Internally, this function does the following:
        1. Create a dataset from the provided production run inputs.
        2. Create a new test project.
        3. Clone the production runs and re-upload against the dataset.

    Parameters:
    - runs: A sequence of runs to be executed as a test.
    - dataset_name: The name of the dataset to associate with the test runs.
    - client: An optional LangSmith client instance. If not provided, a new client will
        be created.
    - load_child_runs: Whether to load child runs when copying runs.

    Returns:
    - The project containing the cloned runs.

    Example:
    --------
    ```python
    import langsmith
    import random

    client = langsmith.Client()

    # Randomly sample 100 runs from a prod project
    runs = list(client.list_runs(project_name="My Project", execution_order=1))
    sampled_runs = random.sample(runs, min(len(runs), 100))

    runs_as_test(runs, dataset_name="Random Runs")

    # Select runs named "extractor" whose root traces received good feedback
    runs = client.list_runs(
        project_name="<your_project>",
        filter='eq(name, "extractor")',
        trace_filter='and(eq(feedback_key, "user_score"), eq(feedback_score, 1))',
    )
    runs_as_test(runs, dataset_name="Extraction Good")
    ```
    z1Expected a non-empty sequence of runs. Received: )r:   N)ÚinputsÚoutputsÚsource_run_idsÚ
dataset_idzprod-baseline-é   r   zprod-baseline)ÚwhichÚdataset_version)Úproject_nameÚreference_dataset_idÚmetadataÚend_timeÚ
start_time)ÚtzrC   © )Ú
ValueErrorÚrtÚget_cached_clientÚcreate_datasetr=   Úcreate_examplesr<   r$   Ú_load_child_runsr'   r(   ÚhexÚlistÚlist_examplesÚsource_run_idÚmodified_atÚ
created_atr4   Úcreate_projectÚ	isoformatÚdatetimeÚnowÚtimezoneÚutcÚ
create_runÚupdate_project)r9   r:   r5   r6   r7   r8   Údsr2   r=   Úruns_to_copyÚexamplesÚer   rB   Úroot_runr   Ú	to_createÚprojectÚnew_runÚlatencyÚ_s                        r   Úconvert_runs_to_testrh   K   sz  € ñj ÜÐNÈtÈfÐWÓXÐXØÒ-”r×+Ñ+Ó-€FØ	×	Ñ	¨LÐ	Ó	9€BÙ+:¡$Ó'¡$˜Qˆq�y‹y $Ò'À€GØ
×ÑÙ"&Ó'¡$˜Q�—“ $Ñ'ØÙ&*Ó+¡d ˜Ÿ› dÑ+Ø—5‘5ð	 ô ñ Ø‰á<@ÓA¹D°q˜×/Ñ/°Õ2¸DˆÐAà)ÒT¨~¼d¿j¹j»l×>NÑ>NÈrÐPQÐ>RÐ=SÐ-TÐä�F×(Ñ(°lÐ(ÓCÓD€HÙ9AÓB¹°A˜!Ÿ/™/¨1¯4©4Ñ/¸ÐÐBà#+¨A¡;×#:Ò#:ˆ�‰×ÒÀÈÁ×@VÑ@Vð ñ %ôá$ˆHÜ)¨(Ð4FÖGˆHò 	àGð 	Ø$ð ñ ð ×#Ñ#Ø&ØŸU™Uà$Ø.×8Ñ8Ó:ñ
ð $ó €Gó ˆØ˜*Ñ%¨°Ñ(=Ñ=ˆÜ (× 1Ñ 1× 5Ñ 5¼×9JÑ9J×9NÑ9NÐ 5Ó Oˆ�ÑØ% lÑ3°gÑ=ˆ�
ÑØˆ×ÑÑD˜GÑDÐ2CÔDð	 ð 	×ÑØ�
‰
ó	€Að €Nùò[ (ùâ'ùâ+ùò Bùò
 Cùó
s$   ÁH;Á(I ÂIÂ1I
ÄIÅ$IrC   c                 óZ  — t        j                  |j                  j                  «      }|t         j                  j
                  k(  rt        j                  | |«      S t        «       5  |j                  | ¬«      }d d d «       t        j                  t        «      }g }i }D ]M  }|j                  �||j                     j                  |«       n|j                  |«       |||j                  <   ŒO |j                  «       D ]  \  }}	t!        |	d„ ¬«      ||   _        Œ |S # 1 sw Y   Œ¨xY w)N)rC   c                 ó   — | j                   S ©N)r   )r2   s    r   Ú<lambda>z%_load_nested_traces.<locals>.<lambda>Ë   s   € ÀqÇ~Â~r   )Úkey)r   Úget_query_backendÚinfoÚinstance_flagsÚQueryBackendÚSMITHDB_ONLYÚ_load_nested_traces_v2r	   Ú	list_runsÚcollectionsÚdefaultdictrQ   r   r-   r$   r   Úsortedr+   )
rC   r6   Úbackendr9   Útreemapr/   Úall_runsÚrunÚrun_idr+   s
             r   Ú_load_nested_tracesr}   ´   s
  € ô "×3Ñ3°F·K±K×4NÑ4NÓO€GØÔ%×2Ñ2×?Ñ?Ò?Ü"×9Ñ9¸,ÈÓOÐOô 
&Õ	'Ø×Ñ¨\ÐÓ:ˆ÷ 
(ô 	×Ñ¤Ó%ð ð €GØ€HÛˆØ×ÑÐ(Ø�C×%Ñ%Ñ&×-Ñ-¨cÕ2à�N‰N˜3ÔØˆ�—‘Òð ð &Ÿm™mžoÑˆ�
Ü&,¨ZÑ=UÔ&Vˆ�ÑÕ#ð .à€N÷ 
(Ð	'ús   Á'D!Ä!D*ÚTÚUÚlist1Úlist2c                 ó@   — t        t        j                  | |«      «      S rk   )rQ   Ú	itertoolsÚproduct)r€   r�   s     r   Ú_outer_productr…   Ó   s   € Ü”	×!Ñ! %¨Ó/Ó0Ð0r   z´compute_test_metrics() is deprecated and will be removed after Jan 31, 2027. There is no replacement: run the evaluators yourself and log the results with client.create_feedback().é
   )Úmax_concurrencyr6   Ú
evaluatorsr‡   c          
      óò  — ddl m} g }|D ]t  }t        |t        j                  «      r|j                  |«       Œ/t        |«      r%|j                  t        j                  |«      «       Œ_t        dt        |«      › �«      ‚ |xs t        j                  «       }t        | |«      } ||¬«      5 } |j                  |j                  gt        t!        ||«      Ž ¢­Ž }	ddd«       	D ]  }
Œ y# 1 sw Y   ŒxY w)aê  Compute test metrics for a given test name using a list of evaluators.

    .. admonition:: Deprecated

        There is no replacement: run the evaluators yourself and log the results
        with :meth:`langsmith.Client.create_feedback`.
        Will be removed after Jan 31, 2027.

    Args:
        project_name (str): The name of the test project to evaluate.
        evaluators (list): A list of evaluators to compute metrics with.
        max_concurrency (Optional[int], optional): The maximum number of concurrent
            evaluations. Defaults to 10.
        client (Optional[Client], optional): The client to use for evaluations.
            Defaults to None.

    Returns:
        None: This function does not return any value.
    r   )ÚContextThreadPoolExecutorz5Evaluation not yet implemented for evaluator of type )Úmax_workersN)Ú	langsmithrŠ   Ú
isinstanceÚls_evalÚRunEvaluatorr-   ÚcallableÚrun_evaluatorÚNotImplementedErrorÚtyperK   rL   r}   ÚmapÚevaluate_runÚzipr…   )rC   rˆ   r‡   r6   rŠ   Úevaluators_ÚfuncÚtracesÚexecutorr/   rg   s              r   Úcompute_test_metricsr›   ×   sç   € õ@ 4à.0€KÛˆÜ�dœG×0Ñ0Ô1Ø×Ñ˜tÕ$Ü�dŒ^Ø×Ñœw×4Ñ4°TÓ:Õ;ä%ØGÌÈTË
À|ÐTóð ð ð Ò-”r×+Ñ+Ó-€FÜ  ¨vÓ6€FÙ	"¨Õ	?À8Ø�(—,‘,Ø×Ñð
Ü"%¤~°f¸kÓ'JÐ"Kò
ˆ÷ 
@ó ˆØñ ÷	 
@Ð	?ús   Â/.C-Ã-C6)+Ú__doc__ru   rX   rƒ   r'   Úcollections.abcr   Útypingr   r   Úlangsmith.run_treesÚ	run_treesrK   Úlangsmith.schemasÚschemasÚ
ls_schemasrŒ   r   rŽ   Úlangsmith._internalr   Ú#langsmith._internal._beta_decoratorr   r	   r
   Úlangsmith.clientr   r*   r   ÚRunrQ   r4   r   ÚboolÚTracerSessionrh   r}   r~   r   Útupler…   Úintr›   rI   r   r   Ú<module>r¬      sÌ  ðñó
 Û Û Û Ý $ß $å  Ý &Ý +Ý 3÷ñ õ
 $ð˜4ð ¨ð °$ó ð,˜JŸN™Nð Àð ÈÈdÉó ð8 ð
 (,Ø#Ø!Ø!òeØ
�:—>‘>Ñ
"ðeð ðeð   ‘}ð	eð
 �VÑðeð ðeð ðeð ×Ñòeó ðeðP cð °6ð ¸dÀ:Ç>Á>Ñ>Ró ñ6 ˆCƒL€ÙˆCƒL€ð1˜$˜q™'ð 1¨$¨q©'ð 1°d¸5ÀÀAÀ¹;Ñ6Gó 1ñ ð%óð
 ð
 &(Ø#ò-Øð-ð ð-ð ˜c‘]ð	-ð
 �VÑð-ð 
ò-ó óñ-r   