Ë
    ÂXÕi,  ã                   óî  — 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 ded	ed
efd„Zdej2                  ded
ee   fd„Zedddddœdeej2                     dedee   dee   deded
ej<                  fd„«       Zdeded
eej2                     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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)Ú	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        úa/root/rosshost/parking-map/parking-api/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_runr1   *   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¨!Œl˜1˜fÕ%Ð7€FÐ7Ø(:¸4¿7¹7Ñ(C€Fˆ1�IÐ$Ñ%Ø€Mùò 8s   ÃC<F)Útest_project_nameÚclientÚload_child_runsÚinclude_outputsÚrunsÚdataset_namer2   r3   r4   r5   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                  |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: )r7   N)ÚinputsÚoutputsÚsource_run_idsÚ
dataset_id)r4   zprod-baseline-é   r   zprod-baseline)ÚwhichÚdataset_version)Úproject_nameÚreference_dataset_idÚmetadataÚend_timeÚ
start_time)Útzr@   © )Ú
ValueErrorÚrtÚget_cached_clientÚcreate_datasetr:   Úcreate_examplesr9   r!   Úread_runr$   r%   ÚhexÚlistÚlist_examplesÚsource_run_idÚmodified_atÚ
created_atr1   Úcreate_projectÚ	isoformatÚdatetimeÚnowÚtimezoneÚutcÚ
create_runÚupdate_project)r6   r7   r2   r3   r4   r5   Údsr/   r:   Úruns_to_copyÚexamplesÚer   r?   Úroot_runr	   Ú	to_createÚprojectÚnew_runÚlatencyÚ_s                        r   Úconvert_runs_to_testre   F   sŠ  € ñj ÜÐNÈtÈfÐWÓXÐXØÒ-”r×+Ñ+Ó-€FØ	×	Ñ	¨LÐ	Ó	9€BÙ+: $Ö'˜Qˆq�y‹yÓ'À€GØ
×ÑØ"&Ö'˜Q�—“Ò'ØØ&*Ö+ ˜Ÿ›Ò+Ø—5‘5ð	 ô ñ Ø‰ð MQö
ØGHˆF�O‰O˜AŸD™D°/ˆOÕBð
ˆð 
ð *ÒT¨~¼d¿j¹j»l×>NÑ>NÈrÐPQÐ>RÐ=SÐ-TÐä�F×(Ñ(°lÐ(ÓCÓD€HØ9AÖB°A˜!Ÿ/™/¨1¯4©4Ñ/ÐBÐÐBà#+¨A¡;×#:Ò#:ˆ�‰×ÒÀÈÁ×@VÑ@Vð ð %÷àÜ)¨(Ð4FÓGòð ò 	ðØð€Iñ ð ×#Ñ#Ø&ØŸU™Uà$Ø.×8Ñ8Ó:ñ
ð $ó €Gð ò EˆØ˜*Ñ%¨°Ñ(=Ñ=ˆÜ (× 1Ñ 1× 5Ñ 5¼×9JÑ9J×9NÑ9NÐ 5Ó Oˆ�ÑØ% lÑ3°gÑ=ˆ�
ÑØˆ×ÑÑD˜GÑDÐ2CÔDð	Eð 	×ÑØ�
‰
ó	€Að €Nùò_ (ùâ'ùâ+ùò
ùò Cùó
s$   ÁIÁ(IÂIÂ1$IÄIÅ0I r@   c                 ód  — |j                  | ¬«      }t        j                  t        «      }g }i }|D ]M  }|j                  �||j                     j                  |«       n|j                  |«       |||j                  <   ŒO |j                  «       D ]  \  }}t        |d„ ¬«      ||   _	        Œ |S )N)r@   c                 ó   — | j                   S ©N)r   )r/   s    r   ú<lambda>z%_load_nested_traces.<locals>.<lambda>¿   s
   € ÀqÇ~Á~€ r   )Úkey)
Ú	list_runsÚcollectionsÚdefaultdictrN   r   r*   r!   r   Úsortedr(   )	r@   r3   r6   Útreemapr,   Úall_runsÚrunÚrun_idr(   s	            r   Ú_load_nested_tracesrs   ±   sº   € Ø×Ñ¨ÐÓ6€Dä×Ñ¤Ó%ð ð €GØ€HØò ˆØ×ÑÐ(Ø�C×%Ñ%Ñ&×-Ñ-¨cÕ2à�N‰N˜3ÔØˆ�—‘Òðð &Ÿm™m›oò WÑˆ�
Ü&,¨ZÑ=UÔ&Vˆ�ÑÕ#ðWà€Nr   ÚTÚUÚlist1Úlist2c                 ó@   — t        t        j                  | |«      «      S rh   )rN   Ú	itertoolsÚproduct)rv   rw   s     r   Ú_outer_productr{   Ç   s   € Ü”	×!Ñ! %¨Ó/Ó0Ð0r   é
   )Úmax_concurrencyr3   Ú
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.

    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ÚtyperH   rI   rs   ÚmapÚevaluate_runÚzipr{   )r@   r~   r}   r3   r€   Úevaluators_ÚfuncÚtracesÚexecutorr,   rd   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__rl   rU   ry   r$   Úcollections.abcr   Útypingr   r   Úlangsmith.run_treesÚ	run_treesrH   Úlangsmith.schemasÚschemasÚ
ls_schemasr‚   r   r„   Ú#langsmith._internal._beta_decoratorr   Úlangsmith.clientr   r'   r   ÚRunrN   r1   r   ÚboolÚTracerSessionre   rs   rt   ru   Útupler{   Úintr‘   rF   r   r   ú<module>r¡      s¬  ðñó
 Û Û Û Ý $ß $å  Ý &Ý +Ý 9Ý #ð˜4ð ¨ð °$ó ð,˜JŸN™Nð Àð ÈÈdÉó ð8 ð
 (,Ø#Ø!Ø!ògØ
�:—>‘>Ñ
"ðgð ðgð   ‘}ð	gð
 �VÑðgð ðgð ðgð ×Ñògó ðgðT cð °6ð ¸dÀ:Ç>Á>Ñ>Ró ñ$ ˆCƒL€ÙˆCƒL€ð1˜$˜q™'ð 1¨$¨q©'ð 1°d¸5ÀÀAÀ¹;Ñ6Gó 1ð ð
 &(Ø#ò'Øð'ð ð'ð ˜c‘]ð	'ð
 �VÑð'ð 
ò'ó ñ'r   