
    +#h                     Z    d dl Z d dlZd dlmZ d dlmZ ddlmZ ddlm	Z	  G d de      Z
y)	    N)Evaluate)Teleprompter   )BootstrapFewShot)LabeledFewShotc            	       8    e Zd Zi ddddddddf	dZddddd	d
Zy) BootstrapFewShotWithRandomSearch      r      
   Nc                 b   || _         || _        || _        || _        |	| _        |
| _        d| _        || _        || _        || _	        || _
        t        j                  j                  d| j                  d| j                  d       t        j                  j                  d| j                  d       y )Nr   zGoing to sample betweenandztraces per predictor.zWill attempt to trainzcandidate sets.)metricteacher_settings
max_roundsnum_threadsstop_at_scoremetric_thresholdmin_num_samplesmax_num_samples
max_errorsnum_candidate_setsmax_labeled_demosdspyloggerinfo)selfr   r   max_bootstrapped_demosr   r   num_candidate_programsr   r   r   r   s              Z/var/www/html/sandstorm/venv/lib/python3.12/site-packages/dspy/teleprompt/random_search.py__init__z)BootstrapFewShotWithRandomSearch.__init__   s      0$&* 0 5$"8
 "3%t';';UDDXDXZq	
 	0$2I2IK\]    T)teachervalsetrestrictlabeled_samplec                4	   || _         |xs || _        g }g }g }	t        d| j                        D ]   }
||
|vrt	        | j                         }|
dk(  r|j                         }nX|
dk(  r,t        | j                        }|j                  |||      }n'|
dk(  rbt        | j                  | j                  | j                  | j                  | j                  | j                        }|j                  |||      }n|
dk\  sJ |
       t        j                   |
      j#                  |       t        j                   |
      j%                  | j&                  | j                        }t        | j                  | j                  || j                  | j                  | j                        }|j                  |||      }t)        | j                  | j                  | j*                  | j,                  d	d
      } ||d
      \  }}|j/                  |       t1        |d      r||j2                  dz  z
  }t1        |d      r|j4                  dkD  rdn|}t6        j8                  j;                  d|d|j=                         D cg c]  }t?        |j@                         c}       t?        |      dk(  s|tC        |      kD  r$t6        j8                  j;                  d|d|
       |}|j/                  |       t6        j8                  j;                  d|        t6        j8                  j;                  dtC        |             |	j/                  |||
|f       t?        |	      dkD  rdD ]{  }tE        |	d d
      d | }tG        |D cg c]
  ^}}}|s	| c}} }tI        d |D              t?        |d   d         z  }t6        j8                  j;                  d| d|        } | jJ                  || jJ                  k\  st6        j8                  j;                  d| d| jJ                           n |	_&        tE        |jL                  d  d
      |_&        t6        j8                  j;                  t?        |jL                         d!       |S c c}w c c}}w )"N)k)trainsetsample)r   r   r   r   r   r   )r$   r,   r   FT)devsetr   r   r   display_tabledisplay_progress)return_all_scores_suggest_failuresg?_assert_failureszScore:zfor set:zNew best sscore:zfor seedzScores so far: zBest score:   )r   r5            i'  c                     | d   S Nr    xs    r!   <lambda>z:BootstrapFewShotWithRandomSearch.compile.<locals>.<lambda>   s
    AaD r#   )keyreversec              3   2   K   | ]  }t        |        y w)N)max).0entrys     r!   	<genexpr>z;BootstrapFewShotWithRandomSearch.compile.<locals>.<genexpr>   s     .\es5z.\s   r   z$Average of max per entry across top z	 scores: zStopping early because score z is >= stop_at_score c                     | d   S r:   r;   r<   s    r!   r>   z:BootstrapFewShotWithRandomSearch.compile.<locals>.<lambda>   s    `abc`d r#   z candidate programs found.)'r,   r%   ranger   list
reset_copyr   r   compiler   r   r   r   r   r   randomRandomshufflerandintr   r   r   r   appendhasattrr3   r4   r   r   r   
predictorslendemosrB   sortedzipsumr   candidate_programs)r   studentr$   r,   r%   r&   r'   scoresall_subscores
score_dataseed	trainset2program2teleprompterprogramsizeevaluatescore	subscores	predictorbest_programr+   top_3_scores_transposed_subscoresavg_of_max_per_entrys                             r!   rJ   z(BootstrapFewShotWithRandomSearch.compile?   s9    (
"d556 Y	D$4x+?T]]+Irz"--/-0F0FG'//)Tb/c*;;%)%:%:+/+?+?&*&<&<%)%:%:# #??7Gi?X qy&$&yd#++I6}}T*2243G3GI]I]^/;;%)%:%:+/&*&<&<%)%:%:#  (//S\/]{{{{ ,,??#!%H  (DIE9  + x!45 : :S @@x!34%66: KKXujaiatatav:wT]3y;O:wx6{a53v;#6  !3UJM'MM% KKvh78KK]CK8uix@A:". pA#)*.RV#WXZYZ#[L ,/S_0m?Oq)acl0m+n(+..\G[.\+\_bcopqcrstcu_v+v(KK$$'KA3iXlWm%nop !!-%4;M;M2M  #@G\]a]o]o\p!qrsY	x +5'*01P1PVdnr*s'C ? ?@AA[\]C ;x$ 1ns    R
RR)__name__
__module____qualname__r"   rJ   r;   r#   r!   r	   r	      s9      !!^F +/PTei ir#   r	   )rK   r   dspy.evaluate.evaluater   dspy.teleprompt.telepromptr   	bootstrapr   vanillar   r	   r;   r#   r!   <module>rr      s%      + 3 ' #&M| Mr#   