Spaces:
Runtime error
Runtime error
Removed context_managed_run
Browse files
app.py
CHANGED
|
@@ -578,7 +578,7 @@ original_call = model.__call__
|
|
| 578 |
model.__call__ = try_model_call_with_fallbacks
|
| 579 |
|
| 580 |
# Reduce the model's output tokens immediately to improve chances of success
|
| 581 |
-
model.max_tokens =
|
| 582 |
|
| 583 |
# Import tool from Hub
|
| 584 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
|
@@ -586,59 +586,7 @@ image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_co
|
|
| 586 |
with open("prompts.yaml", 'r') as stream:
|
| 587 |
prompt_templates = yaml.safe_load(stream)
|
| 588 |
|
| 589 |
-
#
|
| 590 |
-
original_code_agent_run = CodeAgent.run
|
| 591 |
-
|
| 592 |
-
def context_managed_run(self, task, max_steps=None, persist_conversation=None, stream=False, reset=False, additional_args=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 |
-
# Reset counter if reset is requested
|
| 599 |
-
if reset:
|
| 600 |
-
self._context_step_counter = 0
|
| 601 |
-
else:
|
| 602 |
-
# Increment counter
|
| 603 |
-
self._context_step_counter += 1
|
| 604 |
-
|
| 605 |
-
# Every 5 steps, perform aggressive context management
|
| 606 |
-
if self._context_step_counter >= 5:
|
| 607 |
-
print("Performing periodic conversation cleanup")
|
| 608 |
-
self._context_step_counter = 0
|
| 609 |
-
|
| 610 |
-
# Clear most of the conversation history while keeping essential parts
|
| 611 |
-
if hasattr(self, 'conversation') and len(self.conversation) > 8:
|
| 612 |
-
original_length = len(self.conversation)
|
| 613 |
-
|
| 614 |
-
# Find system messages
|
| 615 |
-
system_messages = [msg for msg in self.conversation if msg.get('role') == 'system']
|
| 616 |
-
|
| 617 |
-
# Get the most recent exchanges (last 4 pairs of messages)
|
| 618 |
-
recent_messages = self.conversation[-8:]
|
| 619 |
-
|
| 620 |
-
# Rebuild conversation with system messages + note + recent messages
|
| 621 |
-
self.conversation = system_messages + [
|
| 622 |
-
{'role': 'system', 'content': '[NOTICE: Previous conversation history has been trimmed to manage context size]'}
|
| 623 |
-
] + recent_messages
|
| 624 |
-
|
| 625 |
-
print(f"Conversation history trimmed from {original_length} to {len(self.conversation)} messages")
|
| 626 |
-
|
| 627 |
-
# Call the original run method with all parameters (as they were received)
|
| 628 |
-
return original_code_agent_run(
|
| 629 |
-
self,
|
| 630 |
-
task,
|
| 631 |
-
max_steps=max_steps,
|
| 632 |
-
persist_conversation=persist_conversation,
|
| 633 |
-
stream=stream,
|
| 634 |
-
reset=reset,
|
| 635 |
-
additional_args=additional_args
|
| 636 |
-
)
|
| 637 |
-
|
| 638 |
-
# Apply the monkey patch to CodeAgent class
|
| 639 |
-
CodeAgent.run = context_managed_run
|
| 640 |
-
|
| 641 |
-
# Initialize the agent using standard smolagents patterns
|
| 642 |
agent = CodeAgent(
|
| 643 |
model=model,
|
| 644 |
tools=[
|
|
@@ -651,16 +599,13 @@ agent = CodeAgent(
|
|
| 651 |
Check_Dataset_Validity,
|
| 652 |
visit_webpage_tool, # This is correctly initialized as VisitWebpageTool()
|
| 653 |
],
|
| 654 |
-
max_steps=12
|
| 655 |
-
verbosity_level=1
|
| 656 |
grammar=None,
|
| 657 |
planning_interval=2,
|
| 658 |
name="Research Assistant",
|
| 659 |
-
description="""An AI assistant that can search the web, create datasets, and answer questions
|
| 660 |
-
|
| 661 |
-
# 1. Breaking tasks into smaller sub-tasks
|
| 662 |
-
# 2. Limiting the amount of data returned by search tools
|
| 663 |
-
# 3. Using the planning_interval to enable more effective reasoning""",
|
| 664 |
prompt_templates=prompt_templates
|
| 665 |
)
|
| 666 |
|
|
@@ -677,7 +622,7 @@ print(f"Available tools: final_answer, Sonar_Web_Search_Tool, {search_tool_name}
|
|
| 677 |
# To fix the TypeError in Gradio_UI.py, you would need to modify that file
|
| 678 |
# For now, we'll just use the agent directly
|
| 679 |
try:
|
| 680 |
-
GradioUI(agent).launch()
|
| 681 |
except TypeError as e:
|
| 682 |
if "unsupported operand type(s) for +=" in str(e):
|
| 683 |
print("Error: Token counting issue in Gradio UI")
|
|
|
|
| 578 |
model.__call__ = try_model_call_with_fallbacks
|
| 579 |
|
| 580 |
# Reduce the model's output tokens immediately to improve chances of success
|
| 581 |
+
model.max_tokens = 750 # Reduce from 2096 to 750 for all outputs by default
|
| 582 |
|
| 583 |
# Import tool from Hub
|
| 584 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
|
|
|
| 586 |
with open("prompts.yaml", 'r') as stream:
|
| 587 |
prompt_templates = yaml.safe_load(stream)
|
| 588 |
|
| 589 |
+
# Update the agent to use more conservative settings
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 590 |
agent = CodeAgent(
|
| 591 |
model=model,
|
| 592 |
tools=[
|
|
|
|
| 599 |
Check_Dataset_Validity,
|
| 600 |
visit_webpage_tool, # This is correctly initialized as VisitWebpageTool()
|
| 601 |
],
|
| 602 |
+
max_steps=8, # Reduce from 12 to 8
|
| 603 |
+
verbosity_level=0, # Reduce from 1 to 0 to minimize internal conversation
|
| 604 |
grammar=None,
|
| 605 |
planning_interval=2,
|
| 606 |
name="Research Assistant",
|
| 607 |
+
description="""An AI assistant that can search the web, create datasets, and answer questions.
|
| 608 |
+
This assistant automatically manages token limits for better stability.""",
|
|
|
|
|
|
|
|
|
|
| 609 |
prompt_templates=prompt_templates
|
| 610 |
)
|
| 611 |
|
|
|
|
| 622 |
# To fix the TypeError in Gradio_UI.py, you would need to modify that file
|
| 623 |
# For now, we'll just use the agent directly
|
| 624 |
try:
|
| 625 |
+
GradioUI(agent).launch(share=True)
|
| 626 |
except TypeError as e:
|
| 627 |
if "unsupported operand type(s) for +=" in str(e):
|
| 628 |
print("Error: Token counting issue in Gradio UI")
|