SolshineMisfit commited on
Commit
d94f78e
·
verified ·
1 Parent(s): d98a17f

updated context management and steps to 12 because errored at 14th step

Browse files
Files changed (1) hide show
  1. app.py +42 -2
app.py CHANGED
@@ -585,6 +585,46 @@ image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_co
585
 
586
  with open("prompts.yaml", 'r') as stream:
587
  prompt_templates = yaml.safe_load(stream)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
588
 
589
  # Initialize the agent using standard smolagents patterns
590
  agent = CodeAgent(
@@ -599,10 +639,10 @@ agent = CodeAgent(
599
  Check_Dataset_Validity,
600
  visit_webpage_tool, # This is correctly initialized as VisitWebpageTool()
601
  ],
602
- max_steps=20,
603
  verbosity_level=1,
604
  grammar=None,
605
- planning_interval=3,
606
  name="Research Assistant",
607
  description="""An AI assistant that can search the web, create datasets, and answer questions # Note about working within token limits
608
  # When using with queries that might exceed token limits, consider:
 
585
 
586
  with open("prompts.yaml", 'r') as stream:
587
  prompt_templates = yaml.safe_load(stream)
588
+
589
+ # Override CodeAgent run to automatically clear context periodically
590
+ original_code_agent_run = CodeAgent.run
591
+
592
+ def context_managed_run(self, query, max_steps=None, persist_conversation=None):
593
+ """Override to periodically clear conversation context after steps"""
594
+ # Initialize step counter if not present
595
+ if not hasattr(self, '_context_step_counter'):
596
+ self._context_step_counter = 0
597
+
598
+ # Increment counter
599
+ self._context_step_counter += 1
600
+
601
+ # Every 5 steps, perform aggressive context management
602
+ if self._context_step_counter >= 5:
603
+ print("Performing periodic conversation cleanup")
604
+ self._context_step_counter = 0
605
+
606
+ # Clear most of the conversation history while keeping essential parts
607
+ if hasattr(self, 'conversation') and len(self.conversation) > 8:
608
+ original_length = len(self.conversation)
609
+
610
+ # Find system messages
611
+ system_messages = [msg for msg in self.conversation if msg.get('role') == 'system']
612
+
613
+ # Get the most recent exchanges (last 4 pairs of messages)
614
+ recent_messages = self.conversation[-8:]
615
+
616
+ # Rebuild conversation with system messages + note + recent messages
617
+ self.conversation = system_messages + [
618
+ {'role': 'system', 'content': '[NOTICE: Previous conversation history has been trimmed to manage context size]'}
619
+ ] + recent_messages
620
+
621
+ print(f"Conversation history trimmed from {original_length} to {len(self.conversation)} messages")
622
+
623
+ # Call the original run method
624
+ return original_code_agent_run(self, query, max_steps, persist_conversation)
625
+
626
+ # Apply the monkey patch to CodeAgent class
627
+ CodeAgent.run = context_managed_run
628
 
629
  # Initialize the agent using standard smolagents patterns
630
  agent = CodeAgent(
 
639
  Check_Dataset_Validity,
640
  visit_webpage_tool, # This is correctly initialized as VisitWebpageTool()
641
  ],
642
+ max_steps=12,
643
  verbosity_level=1,
644
  grammar=None,
645
+ planning_interval=2,
646
  name="Research Assistant",
647
  description="""An AI assistant that can search the web, create datasets, and answer questions # Note about working within token limits
648
  # When using with queries that might exceed token limits, consider: