
    +#h<%                     f    d dl 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 ddl	m
Z
  G d de      Zy)    N)Example   )Teleprompter)LabeledFewShotc                   V    e Zd Zddi ddddfdZddddZd	 Zd
 ZdddZddZd Z	y)BootstrapFewShotN      r      c                     || _         || _        || _        || _        || _        || _        || _        d| _        t        j                         | _
        y )Nr   )metricmetric_thresholdteacher_settingsmax_bootstrapped_demosmax_labeled_demos
max_rounds
max_errorserror_count	threadingLock
error_lock)selfr   r   r   r   r   r   r   s           V/var/www/html/sandstorm/venv/lib/python3.12/site-packages/dspy/teleprompt/bootstrap.py__init__zBootstrapFewShot.__init__"   sO      0 0&<#!2$$#..*    )teachervalsetc                *   || _         || _        | j                  ||       | j                          | j	                          | j                         | _        d| j                  _        d| j                  _        d| j                  _	        | j                  S )NTr   )
trainsetr   _prepare_student_and_teacher_prepare_predictor_mappings
_bootstrap_trainstudent	_compiled_assert_failures_suggest_failures)r   r$   r   r   r   s        r   compilezBootstrapFewShot.compile7   ss     ))'7;((*{{}!% )*%)*&||r   c                    |j                         | _        ||j                         n|j                         | _        t	        | j                  dd      du sJ d       | j
                  rkt	        | j                  dd      du rQt        | j
                        }|j                  | j                  j                         | j                        | _        y y y )Nr%   FzStudent must be uncompiled.)k)r   )	
reset_copyr$   deepcopyr   getattrr   r   r(   r   )r   r$   r   teleprompters       r   r    z-BootstrapFewShot._prepare_student_and_teacherH   s    ))+-4-@w'')gFXFXFZt||[%8EA`C``A!!gdllK&OSX&X)D,B,BCL'//0G0G0ITXTaTa/bDL 'Y!r   c                 ~   i i }}| j                   | j                  }}t        |j                               t        |j                               k(  sJ d       t	        |j                         |j                               D ]  \  \  }}\  }}||k(  sJ d       |j                  j                  |j                        s3J dt        |j                         dt        |j                                t        |      t        |      k7  sJ d       d ||<   ||t        |      <   ||t        |      <    || _
        || _        y )Nz<Student and teacher must have the same number of predictors.z9Student and teacher must have the same program structure.z3Student and teacher must have the same signatures. z != z.Student and teacher must be different objects.)r$   r   len
predictorszipnamed_predictors	signatureequalstypeidname2predictorpredictor2name)	r   r8   r9   r$   r   name1
predictor1name2
predictor2s	            r   r!   z,BootstrapFewShot._prepare_predictor_mappingsR   s]   )+R<<7%%'(C -
 
 	JI	J 
 9<G<T<T<VX_XpXpXr8s 	34UJ!4%E>^#^^>''..$$ BDT*J^J^E_D``deijtj~j~e  eA  BB  j>R
^3e5ee3$(N5!-2N2j>* .3N2j>*	3" -,r   )max_bootstrapsc          	         |xs | j                   }i }| j                  D ci c]  }|g  c}| _        t        | j                        D ]a  }t        t        j                  | j                              D ]4  \  }}t        |      |k\  r A||vs| j                  ||      }|s0d||<   6 c t        j                  j                  dt        |       ddz    d d       t        | j                        D 	cg c]  \  }}	||vs|	 c}	}| _        t        j                  d      j!                  | j                         | j"                  xs | j                  | _        y c c}w c c}	}w )NTzBootstrapped z full traces after r   z examples in round .r   )r   r8   name2tracesranger   	enumeratetqdmr   r0   _bootstrap_one_exampledspyloggerinfo
validationrandomRandomshuffler   )
r   r>   bootstrappedname	round_idxexample_idxexamplesuccessidxxs
             r   r"   zBootstrapFewShot._bootstrapn   sJ   'F4+F+F151D1DED"HEt/ 		9I(1$))DMM2J(K 9$W|$6l2"99'9MG48[19		9 	C-..A+PQ/ARRefoeppqr	
 ,5T]]+C_asR^G^1_a  1++8- F& `s   
E!9E&E&c           
         | j                   }| j                  }i }	 t        j                  j                  ddg i| j
                  5  t        j                  j                  }|dkD  r|j                  dd|z  z         n|}|dkD  rt        |      ni }t        j                  j                  di |5  |j                         D ]9  \  }}	|	j                  ||<   |	j                  D 
cg c]
  }
|
|k7  s	|
 c}
|	_
        ;  |di |j                         }t        j                  j                  }|j                         D ]  \  }}	||   |	_
         	 d d d        | j                  r2| j                  |      }| j                  r|| j                  k\  }n|}nd}d d d        rhD ]c  }|\  }	}}d|v rt-        dd|j.                  d||}nt-        dddi||}	 | j0                  t3        |	         }||   j7                  |       e |S c c}
w # 1 sw Y   xY w# 1 sw Y   xY w# t        $ r}d}| j                   5  | xj"                  d	z  c_        | j"                  }d d d        n# 1 sw Y   nxY w| j$                  k\  r|t&        j(                  j+                  d
| d| j                   d| d       Y d }~!d }~ww xY w# t4        $ r Y 1w xY w)Ntracer   gffffff?gMbP?)temperature)lmTFr   z%Failed to run or to evaluate example z with z due to r@   	dspy_uuid)	augmentedrY   rZ    )rA   r   dspsettingscontextr   rX   copydictr3   demosinputsrV   r   r   	Exceptionr   r   r   rF   rG   errorr   rY   r9   r7   KeyErrorappend)r   rQ   rO   rA   r   predictor_cacherX   new_settingsrN   	predictorrT   
predictionrV   
metric_valrR   ecurrent_error_countsteprb   outputsdemopredictor_names                         r   rE   z'BootstrapFewShot._bootstrap_one_example   s   &&,, 	p%%HBH$2G2GH #\\__ENQR]RWWuy/@)@WAXZ.7!mtr{\\))9L9 	@+2+C+C+E Wi09-6?oo*Vg1*V	W ")!<7>>+;!<JLL..E+2+C+C+E @i*9$*?	@	@ ;;!%Wj%!HJ,,",0E0E"E","G/#B  9-1*	67')"dTW=N=NdRXd\cdD #GTGVGwGD%)%8%8I%GN N+2248194 i +W	@ 	@# #0  	pG 7  A% &*&6&6#7 7 7 #doo5KK EgYfUYU`U`Taaijkillmnoo	p*   s   +H, A,H 34H'
H
2H
6AHAH H, KHH	H  H)%H, ,	K	5K"I.%	K.I7	3AKK		KKc                    t        j                  d      }| j                  }| j                  j	                         D ]  \  }}| j
                  |   d | j                   }t        | j                  t        |      z
  t        |            }t        d|      }|j                  ||      }t        j                  j                  dk\  r||z   |_        ||z   |_         | j                  S )Nr   i4)rJ   rK   rI   r$   r3   rA   r   minr   r0   maxsamplerF   r]   releasera   )r   rng	raw_demosrN   ri   augmented_demossample_sizes          r   r#   zBootstrapFewShot._train   s    mmAOO	#||<<> 	>OD)"..t45Rt7R7RSOd44s?7KKSQZ^\Ka-K

9k:I}}$$0"+o"=	"1I"=		> ||r   )r   )
__name__
__module____qualname__r   r(   r    r!   r"   rE   r#   r[   r   r   r   r   !   sL      +* +/ "c-8 ,0 9@BHr   r   )rJ   r   rD   r\   rF   dspy.primitivesr   
telepromptr   vanillar   r   r[   r   r   <module>r      s+       
  # $ #,B| Br   