Yago Bolivar commited on
Commit
577039e
·
1 Parent(s): 27568a1

recover app.py and prompts.yaml from f3d56b

Browse files
Files changed (2) hide show
  1. app.py +104 -26
  2. prompts.yaml +89 -27
app.py CHANGED
@@ -26,21 +26,36 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
26
 
27
  # --- Basic Agent Definition ---
28
 
29
- # # Configure the Language Model. Overloaded or not available
30
- # model = HfApiModel(
31
- # max_tokens=2096,
32
- # temperature=0.5,
33
- # # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud/', # nope
34
- # # model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
35
- # custom_role_conversions=None,
36
- # )
37
-
38
- # Try to use OpenAI API
39
- model = OpenAIServerModel(
40
- model_id="gpt-4o",
41
- api_base="https://api.openai.com/v1",
42
- api_key=os.environ["OPENAI_API_KEY"],
43
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
  # Instantiate Tools
46
  final_answer_tool = FinalAnswerTool()
@@ -54,24 +69,86 @@ spreadsheet_tool = SpreadsheetTool()
54
  text_reversal_tool = TextReversalTool()
55
  video_processing_tool = VideoProcessingTool()
56
 
 
 
 
 
57
  # Load Prompts
58
  try:
59
  with open("prompts.yaml", 'r') as stream:
60
  prompt_templates = yaml.safe_load(stream)
 
 
 
 
 
61
  except FileNotFoundError:
62
- print("Error: prompts.yaml not found. Please ensure it is in the root project directory.")
63
- prompt_templates = {} # Fallback to empty templates
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  except yaml.YAMLError as e:
65
  print(f"Error parsing prompts.yaml: {e}")
66
- prompt_templates = {}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
68
 
69
- # Enhance the agent class to conform to the template's interface
70
  class EnhancedCodeAgent(CodeAgent):
71
  def __call__(self, question: str) -> str:
72
- response = self.run(question)
73
- return response
74
-
 
 
 
 
 
75
  # Create the Agent
76
  agent_tools = [
77
  final_answer_tool,
@@ -86,13 +163,14 @@ agent_tools = [
86
  video_processing_tool
87
  ]
88
 
 
89
  agent = EnhancedCodeAgent(
90
  model=model,
91
  tools=agent_tools,
92
- max_steps=15,
93
- verbosity_level=1,
94
- name="ComprehensiveQuestionAgent",
95
- description="An agent equipped with a suite of tools to answer diverse questions from the common_questions.json set.",
96
  prompt_templates=prompt_templates
97
  )
98
 
 
26
 
27
  # --- Basic Agent Definition ---
28
 
29
+ # Enhanced Phase 1: Lightweight Model and Token Management for HF Spaces
30
+ try:
31
+ # Try OpenAI first (if API key available) - Use mini version for better token management
32
+ model = OpenAIServerModel(
33
+ model_id="gpt-4o-mini", # Use mini version for better token management
34
+ api_base="https://api.openai.com/v1",
35
+ api_key=os.environ.get("OPENAI_API_KEY"),
36
+ max_tokens=2000, # Increased from 1000 for better reasoning capability
37
+ temperature=0.1, # Lower temperature for more consistent outputs
38
+ )
39
+ print("Using OpenAI gpt-4o-mini model")
40
+ except Exception as e:
41
+ print(f"OpenAI model initialization failed: {e}")
42
+ # Fallback to HF model - More capable than DialoGPT-medium
43
+ try:
44
+ model = HfApiModel(
45
+ model_id="microsoft/DialoGPT-large", # Upgraded from medium for better capability
46
+ max_tokens=2000,
47
+ temperature=0.1,
48
+ custom_role_conversions=None,
49
+ )
50
+ print("Using fallback HF DialoGPT-large model")
51
+ except Exception as fallback_error:
52
+ print(f"Fallback model initialization failed: {fallback_error}")
53
+ # Final fallback to basic HF model
54
+ model = HfApiModel(
55
+ max_tokens=2000,
56
+ temperature=0.1,
57
+ )
58
+ print("Using basic HF model as final fallback")
59
 
60
  # Instantiate Tools
61
  final_answer_tool = FinalAnswerTool()
 
69
  text_reversal_tool = TextReversalTool()
70
  video_processing_tool = VideoProcessingTool()
71
 
72
+ # Add debug prints for file paths
73
+ print("Current directory:", os.getcwd())
74
+ print("prompts.yaml exists:", os.path.exists("prompts.yaml"))
75
+
76
  # Load Prompts
77
  try:
78
  with open("prompts.yaml", 'r') as stream:
79
  prompt_templates = yaml.safe_load(stream)
80
+ print("Loaded prompts.yaml successfully. Structure:", type(prompt_templates)) # Debug
81
+ if isinstance(prompt_templates, dict):
82
+ print("Keys:", prompt_templates.keys()) # Debug
83
+ else:
84
+ print("Loaded prompt_templates is not a dictionary.")
85
  except FileNotFoundError:
86
+ print("Error: prompts.yaml not found. Using default templates.")
87
+ prompt_templates = {
88
+ "system_prompt": { # This was a single string, now a dict
89
+ "base": "You are an expert assistant...", # Default value
90
+ "with_tools": "At each step...", # Default value
91
+ },
92
+ "system": { # This section was already a dict, kept for consistency
93
+ "base": "You are a GAIA benchmark agent running in HF Spaces. Be concise and efficient in your responses.",
94
+ "with_tools": "Think briefly, act decisively. Use tools efficiently to solve GAIA benchmark tasks."
95
+ },
96
+ "human": {
97
+ "base": "Here is your task: {{task}}\\\\nProvide exact answer. Be concise and efficient.", # Updated base
98
+ "with_tools": "Here is your task: {{task}}\\\\nUse available tools strategically. Be direct and resource-conscious: {{tools}}" # Updated with_tools
99
+ },
100
+ "planning": {
101
+ "initial_facts": "Task: {{task}}. Identify key facts and missing information concisely.",
102
+ "initial_plan": "Develop an efficient 3-5 step plan for this GAIA task using available tools."
103
+ # etc...
104
+ },
105
+ "managed_agent": {
106
+ "task": "Managed agent task: {{task}}",
107
+ "report": "Managed agent report: {{final_answer}}"
108
+ },
109
+ "final_answer": {
110
+ "base": "The final answer is: {{answer}}"
111
+ }
112
+ # Include all other required sections as per your YAML structure if they exist
113
+ }
114
  except yaml.YAMLError as e:
115
  print(f"Error parsing prompts.yaml: {e}")
116
+ print("Using default templates optimized for HF Spaces")
117
+ prompt_templates = {
118
+ "system_prompt": "You are a helpful AI assistant. Please be concise and efficient.",
119
+ "system": {
120
+ "base": "You are a GAIA benchmark agent running in HF Spaces. Be concise and efficient in your responses.",
121
+ "with_tools": "Think briefly, act decisively. Use tools efficiently to solve GAIA benchmark tasks."
122
+ },
123
+ "human": {
124
+ "base": "GAIA Task: {{task}}\\\\nProvide exact answer. Be concise and efficient.",
125
+ "with_tools": "GAIA Task: {{task}}\\\\nUse available tools strategically. Be direct and resource-conscious: {{tools}}"
126
+ },
127
+ "planning": {
128
+ "initial_facts": "Task: {{task}}. Identify key facts and missing information concisely.",
129
+ "initial_plan": "Develop an efficient 3-5 step plan for this GAIA task using available tools."
130
+ },
131
+ "managed_agent": {
132
+ "task": "Managed agent task: {{task}}",
133
+ "report": "Managed agent report: {{final_answer}}"
134
+ },
135
+ "final_answer": { # Placeholder, structure might need refinement based on agent's specific use
136
+ "base": "The final answer is: {{answer}}"
137
+ }
138
+ }
139
 
140
 
141
+ # Enhanced agent configuration for HF Spaces optimization
142
  class EnhancedCodeAgent(CodeAgent):
143
  def __call__(self, question: str) -> str:
144
+ try:
145
+ response = self.run(question)
146
+ return response
147
+ except Exception as e:
148
+ print(f"Agent execution error: {e}")
149
+ # Provide a graceful fallback response
150
+ return f"I encountered an issue while processing your request. Here's what I know: {str(e)}"
151
+
152
  # Create the Agent
153
  agent_tools = [
154
  final_answer_tool,
 
163
  video_processing_tool
164
  ]
165
 
166
+ # Enhanced agent configuration for HF Spaces optimization
167
  agent = EnhancedCodeAgent(
168
  model=model,
169
  tools=agent_tools,
170
+ max_steps=8, # Increased from 5 to handle multi-step reasoning while staying efficient
171
+ verbosity_level=1, # Keep some verbosity for debugging in HF Spaces
172
+ name="GAIAAgent", # Updated name to reflect GAIA benchmark focus
173
+ description="Efficient GAIA benchmark agent optimized for HF Spaces with enhanced token management",
174
  prompt_templates=prompt_templates
175
  )
176
 
prompts.yaml CHANGED
@@ -1,4 +1,4 @@
1
- system:
2
  base: |-
3
  You are an expert assistant who can solve any task using code blobs. You will be given a task to solve as best you can.
4
  To do so, you have been given access to a list of tools: these tools are basically Python functions which you can call with code.
@@ -9,20 +9,18 @@ system:
9
  During each intermediate step, you can use 'print()' to save whatever important information you will then need.
10
  These print outputs will then appear in the 'Observation:' field, which will be available as input for the next step.
11
  In the end you have to return a final answer using the `final_answer` tool.
12
-
13
  You have access to these tools:
14
- {% raw %}{%- for tool in tools.values() %}{% endraw %}
15
- - {{ tool.name }}: {{ tool.description }}
16
- Takes inputs: {{tool.inputs}}
17
- Returns an output of type: {{tool.output_type}}
18
- {% raw %}{%- endfor %}{% endraw %}# filepath: /Users/yagoairm2/Desktop/agents/final project/HF_Agents_Final_Project/prompts.yaml
19
-
20
- Current subtask: {{subtask}}
21
- {% if context %}
22
- Additional context: {{context}}
23
- {% endif %}
24
 
25
- Provide your response in a clear and structured format that the manager agent can use.
 
 
26
 
27
  human:
28
  base: |-
@@ -38,13 +36,80 @@ human:
38
  planning:
39
  initial_facts: |-
40
  Below I will present you a task.
 
41
  You will now build a comprehensive preparatory survey of which facts we have at our disposal and which ones we still need.
42
  To do so, you will have to read the task and identify things that must be discovered in order to successfully complete it.
43
- Don't make any assumptions. For each item, provide a thorough reasoning.
 
 
 
 
 
 
 
 
 
 
 
44
 
45
  initial_plan: |-
46
  You are a world expert at making efficient plans to solve any task using a set of carefully crafted tools.
47
  Now for the given task, develop a step-by-step high-level plan taking into account the above inputs and list of facts.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
  managed_agent:
50
  task: |-
@@ -54,23 +119,20 @@ managed_agent:
54
  Task:
55
  {{task}}
56
  ---
57
- You're helping your manager solve a wider task: so make sure to not provide a one-line answer.
 
 
 
 
 
 
 
 
58
 
59
  report: |-
60
  Here is the final answer from your managed agent '{{name}}':
61
  {{final_answer}}
62
 
63
- Current subtask: {{subtask}}
64
- {% if context %}
65
- Additional context: {{context}}
66
- {% endif %}
67
-
68
- Provide your response in a clear and structured format that the manager agent can use.
69
-
70
- planning: |-
71
- Here's my plan to solve this task:
72
- {{plan}}
73
-
74
  manager_prompt: |
75
  Task: {{task_description}}
76
  {% if file_url %}
@@ -88,4 +150,4 @@ manager_prompt: |
88
 
89
  Based on the task and any provided file, devise a plan and call the appropriate agent(s) to gather information and formulate an answer.
90
  Generate the Python code to call these agents and produce the final answer.
91
- Your final response should be the answer to the task.
 
1
+ system_prompt:
2
  base: |-
3
  You are an expert assistant who can solve any task using code blobs. You will be given a task to solve as best you can.
4
  To do so, you have been given access to a list of tools: these tools are basically Python functions which you can call with code.
 
9
  During each intermediate step, you can use 'print()' to save whatever important information you will then need.
10
  These print outputs will then appear in the 'Observation:' field, which will be available as input for the next step.
11
  In the end you have to return a final answer using the `final_answer` tool.
12
+
13
  You have access to these tools:
14
+ - WebSearchAgent: Call this agent for web browsing and fetching URL content.
15
+ - FileProcessorAgent: Call this agent for identifying file types, parsing spreadsheets, transcribing audio, and parsing markdown tables.
16
+ - VisionAgent: Call this agent for image processing, OCR, and chess image analysis.
17
+ - VideoAgent: Call this agent for video processing tasks.
18
+ - CodeInterpreterAgent: Call this agent to execute Python code.
19
+ - TextToolAgent: Call this agent for simple text manipulations like reversing text.
 
 
 
 
20
 
21
+ final_answer: |-
22
+ Here is the final answer to the task:
23
+ {{answer}}
24
 
25
  human:
26
  base: |-
 
36
  planning:
37
  initial_facts: |-
38
  Below I will present you a task.
39
+
40
  You will now build a comprehensive preparatory survey of which facts we have at our disposal and which ones we still need.
41
  To do so, you will have to read the task and identify things that must be discovered in order to successfully complete it.
42
+ Don't make any assumptions. For each item, provide a thorough reasoning. Here is how you will structure this survey:
43
+
44
+ ---
45
+ ### 1. Facts given in the task
46
+ List here the specific facts given in the task that could help you (there might be nothing here).
47
+
48
+ ### 2. Facts to look up
49
+ List here any facts that we may need to look up.
50
+ Also list where to find each of these, for instance a website, a file... - maybe the task contains some sources that you should re-use here.
51
+
52
+ ### 3. Facts to derive
53
+ List here anything that we want to derive from the above by logical reasoning, for instance computation or simulation.
54
 
55
  initial_plan: |-
56
  You are a world expert at making efficient plans to solve any task using a set of carefully crafted tools.
57
  Now for the given task, develop a step-by-step high-level plan taking into account the above inputs and list of facts.
58
+ This plan should involve individual tasks based on the available tools, that if executed correctly will yield the correct answer.
59
+ Do not skip steps, do not add any superfluous steps. Only write the high-level plan, DO NOT DETAIL INDIVIDUAL TOOL CALLS.
60
+ After writing the final step of the plan, write the '\n<end_plan>' tag and stop there.
61
+
62
+ update_facts_pre_messages: |-
63
+ You are a world expert at gathering known and unknown facts based on a conversation.
64
+ Below you will find a task, and a history of attempts made to solve the task. You will have to produce a list of these:
65
+ ### 1. Facts given in the task
66
+ ### 2. Facts that we have learned
67
+ ### 3. Facts still to look up
68
+ ### 4. Facts still to derive
69
+ Find the task and history below:
70
+
71
+ update_facts_post_messages: |-
72
+ Earlier we've built a list of facts.
73
+ But since in your previous steps you may have learned useful new facts or invalidated some false ones.
74
+ Please update your list of facts based on the previous history, and provide these headings:
75
+ ### 1. Facts given in the task
76
+ ### 2. Facts that we have learned
77
+ ### 3. Facts still to look up
78
+ ### 4. Facts still to derive
79
+
80
+ Now write your new list of facts below.
81
+
82
+ update_plan_pre_messages: |-
83
+ You are a world expert at making efficient plans to solve any task using a set of carefully crafted tools.
84
+
85
+ You have been given a task:
86
+ ```
87
+ {{task}}
88
+ ```
89
+
90
+ Find below the record of what has been tried so far to solve it. Then you will be asked to make an updated plan to solve the task.
91
+ If the previous tries so far have met some success, you can make an updated plan based on these actions.
92
+ If you are stalled, you can make a completely new plan starting from scratch.
93
+
94
+ update_plan_post_messages: |-
95
+ You're still working towards solving this task:
96
+ ```
97
+ {{task}}
98
+ ```
99
+
100
+ You can leverage these tools:
101
+ {{tools}}
102
+
103
+ Here is the up to date list of facts that you know:
104
+ ```
105
+ {{facts_update}}
106
+ ```
107
+
108
+ Now for the given task, develop a step-by-step high-level plan taking into account the above inputs and list of facts.
109
+ This plan should involve individual tasks based on the available tools, that if executed correctly will yield the correct answer.
110
+ Beware that you have {remaining_steps} steps remaining.
111
+ Do not skip steps, do not add any superfluous steps. Only write the high-level plan, DO NOT DETAIL INDIVIDUAL TOOL CALLS.
112
+ After writing the final step of the plan, write the '\n<end_plan>' tag and stop there.
113
 
114
  managed_agent:
115
  task: |-
 
119
  Task:
120
  {{task}}
121
  ---
122
+ You're helping your manager solve a wider task: so make sure to not provide a one-line answer, but give as much information as possible to give them a clear understanding of the answer.
123
+
124
+ Your final_answer WILL HAVE to contain these parts:
125
+ ### 1. Task outcome (short version):
126
+ ### 2. Task outcome (extremely detailed version):
127
+ ### 3. Additional context (if relevant):
128
+
129
+ Put all these in your final_answer tool, everything that you do not pass as an argument to final_answer will be lost.
130
+ And even if your task resolution is not successful, please return as much context as possible, so that your manager can act upon this feedback.
131
 
132
  report: |-
133
  Here is the final answer from your managed agent '{{name}}':
134
  {{final_answer}}
135
 
 
 
 
 
 
 
 
 
 
 
 
136
  manager_prompt: |
137
  Task: {{task_description}}
138
  {% if file_url %}
 
150
 
151
  Based on the task and any provided file, devise a plan and call the appropriate agent(s) to gather information and formulate an answer.
152
  Generate the Python code to call these agents and produce the final answer.
153
+ Your final response should be the answer to the task.