@@ -37,7 +37,12 @@ class HistoryPrompt(DataClass):
3737####################################################################################################
3838# Textual Gradient Descent Optimizer
3939####################################################################################################
40-
40+ # {% if failed_proposals %}
41+ # Here are the past failed proposals:
42+ # {% for failed_proposal in failed_proposals %}
43+ # {{loop.index}}. {{failed_proposal}}
44+ # {% endfor %}
45+ # {% endif %}
4146TEXT_GRAD_DESC_TEMPLATE = r"""<START_OF_SYSTEM_PROMPT>
4247{{optimizer_system_prompt}}
4348<END_OF_SYSTEM_PROMPT>
@@ -51,13 +56,6 @@ class HistoryPrompt(DataClass):
5156{{loop.index}}. {{history}}
5257{% endfor %}
5358IMPORTANT: Your goal is to generate new variable values that score higher than all previous iterations.
54-
55- {% if failed_proposals %}
56- Here are the past failed proposals:
57- {% for failed_proposal in failed_proposals %}
58- {{loop.index}}. {{failed_proposal}}
59- {% endfor %}
60- {% endif %}
6159<END_OF_HISTORY_PERFORMANCE>
6260{% endif %}
6361Here are the context and feedback for the variable:
@@ -176,7 +174,7 @@ class TGDOptimizer(TextOptimizer):
176174 params : ParamsT
177175 constraints : List [str ]
178176 params_history : Dict [str , List [HistoryPrompt ]] = {} # id to history
179- failed_proposals : Dict [str , List [HistoryPrompt ]] = {} # only need the value
177+ # failed_proposals: Dict[str, List[HistoryPrompt]] = {} # only need the value
180178
181179 def __init__ (
182180 self ,
@@ -189,7 +187,7 @@ def __init__(
189187 in_context_examples : List [str ] = None , # TODO: in-context examples
190188 num_gradient_memory : int = 0 , # TODO: gradient memory and momentum, for now it is not useful
191189 max_past_history : int = 3 ,
192- max_failed_proposals : int = 3 ,
190+ # max_failed_proposals: int = 3,
193191 ):
194192 from adalflow .core .generator import Generator
195193 from adalflow .core import Prompt
@@ -229,12 +227,12 @@ def __init__(
229227 )
230228
231229 self .max_past_history = max_past_history
232- self .max_failed_proposals = max_failed_proposals
230+ # self.max_failed_proposals = max_failed_proposals
233231
234232 # initate the past history for each parameter
235233 for param in self .params :
236234 self .params_history [param .id ] = []
237- self .failed_proposals [param .id ] = []
235+ # self.failed_proposals[param.id] = []
238236
239237 @property
240238 def constraint_text (self ):
@@ -289,39 +287,39 @@ def render_history(self, param_id: str) -> List[str]:
289287 history .to_yaml (exclude = ["id" ]) for history in self .params_history [param_id ]
290288 ]
291289
292- def add_failed_proposal (self ):
293- """Save a copy of the current value of the parameter in the failed proposals."""
294- for param in self .params :
295- failed_proposal = HistoryPrompt (
296- id = param .id ,
297- value = param .data ,
298- eval_score = None ,
299- )
300- self .failed_proposals [param .id ].append (failed_proposal )
301- if len (self .failed_proposals [param .id ]) > self .max_failed_proposals :
302- for _ in range (
303- len (self .failed_proposals [param .id ]) - self .max_failed_proposals
304- ):
305- self .failed_proposals [param .id ].pop ()
306- # if param_id not in self.failed_proposals:
307- # self.failed_proposals[param_id] = []
308- # failed_proposal = HistoryPrompt(
309- # id=param_id,
310- # value=value,
311- # eval_score=None,
312- # )
313- # self.failed_proposals[param_id].append(failed_proposal)
314- # if len(self.failed_proposals[param_id]) > self.max_failed_proposals:
315- # for _ in range(len(self.failed_proposals[param_id]) - self.max_failed_proposals):
316- # self.failed_proposals[param_id].pop()
317-
318- def render_failed_proposals (self , param_id : str ) -> List [str ]:
319- if param_id not in self .failed_proposals :
320- return []
321- return [
322- history .to_yaml (exclude = ["id" , "eval_score" ])
323- for history in self .failed_proposals [param_id ]
324- ]
290+ # def add_failed_proposal(self):
291+ # """Save a copy of the current value of the parameter in the failed proposals."""
292+ # for param in self.params:
293+ # failed_proposal = HistoryPrompt(
294+ # id=param.id,
295+ # value=param.data,
296+ # eval_score=None,
297+ # )
298+ # self.failed_proposals[param.id].append(failed_proposal)
299+ # if len(self.failed_proposals[param.id]) > self.max_failed_proposals:
300+ # for _ in range(
301+ # len(self.failed_proposals[param.id]) - self.max_failed_proposals
302+ # ):
303+ # self.failed_proposals[param.id].pop()
304+ # # if param_id not in self.failed_proposals:
305+ # # self.failed_proposals[param_id] = []
306+ # # failed_proposal = HistoryPrompt(
307+ # # id=param_id,
308+ # # value=value,
309+ # # eval_score=None,
310+ # # )
311+ # # self.failed_proposals[param_id].append(failed_proposal)
312+ # # if len(self.failed_proposals[param_id]) > self.max_failed_proposals:
313+ # # for _ in range(len(self.failed_proposals[param_id]) - self.max_failed_proposals):
314+ # # self.failed_proposals[param_id].pop()
315+
316+ # def render_failed_proposals(self, param_id: str) -> List[str]:
317+ # if param_id not in self.failed_proposals:
318+ # return []
319+ # return [
320+ # history.to_yaml(exclude=["id", "eval_score"])
321+ # for history in self.failed_proposals[param_id]
322+ # ]
325323
326324 # TODO: optimize with adalflow template for better readability
327325 def get_gradient_memory_text (self , param : Parameter ) -> str :
@@ -341,7 +339,9 @@ def _get_user_prompt_kwargs(self, param: Parameter) -> Dict[str, str]:
341339
342340 user_prompt_kwargs = {
343341 "variable_and_peers_info" : variable_and_peer_info ,
344- "variable_grad" : param .get_gradient_and_context_text (),
342+ "variable_grad" : param .get_gradient_and_context_text (
343+ skip_correct_sample = True
344+ ),
345345 # constraints
346346 "constraint_text" : self .constraint_text if self .do_constrained else None ,
347347 # in-context examples
@@ -361,19 +361,19 @@ def _get_user_prompt_kwargs(self, param: Parameter) -> Dict[str, str]:
361361 self .render_history (param .id ) if self .max_past_history else None
362362 ),
363363 # failed proposals
364- "failed_proposals" : (
365- self .render_failed_proposals (param .id )
366- if self .max_failed_proposals
367- else None
368- ),
364+ # "failed_proposals": (
365+ # self.render_failed_proposals(param.id)
366+ # if self.max_failed_proposals
367+ # else None
368+ # ),
369369 }
370370
371371 return user_prompt_kwargs
372372
373373 # TODO: better way to update the gradient memory
374374 def update_gradient_memory (self , param : Parameter ):
375375 self .gradient_memory_dict [param .id ].append (
376- {"value" : param .get_gradient_and_context_text ()}
376+ {"value" : param .get_gradient_and_context_text (skip_correct_sample = True )}
377377 )
378378
379379 def zero_grad (self ):
0 commit comments