Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -95,6 +95,61 @@ class KeywordsExtractorTool(Tool):
|
|
95 |
except Exception as e:
|
96 |
return f"Error during extracting most common words: {e}"
|
97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
@tool
|
99 |
def parse_excel_to_json(task_id: str) -> dict:
|
100 |
"""
|
@@ -197,19 +252,39 @@ class BasicAgent:
|
|
197 |
video_transcription_tool = VideoTranscriptionTool()
|
198 |
|
199 |
system_prompt = f"""
|
200 |
-
You are
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
207 |
"""
|
208 |
self.agent = CodeAgent(
|
209 |
model=model,
|
210 |
tools=[search_tool, wiki_search_tool, str_reverse_tool, keywords_extract_tool, speech_to_text_tool, visit_webpage_tool, \
|
211 |
final_answer_tool, parse_excel_to_json, video_transcription_tool],
|
212 |
-
add_base_tools=True
|
|
|
213 |
)
|
214 |
self.agent.prompt_templates["system_prompt"] = self.agent.prompt_templates["system_prompt"] + system_prompt
|
215 |
|
|
|
95 |
except Exception as e:
|
96 |
return f"Error during extracting most common words: {e}"
|
97 |
|
98 |
+
|
99 |
+
@tool
|
100 |
+
def calculator(inputs: Union[str, dict]):
|
101 |
+
"""
|
102 |
+
Perform mathematical operations based on the operation provided.
|
103 |
+
Supports both binary (a, b) operations and list operations.
|
104 |
+
"""
|
105 |
+
|
106 |
+
# If input is a JSON string, parse it
|
107 |
+
if isinstance(inputs, str):
|
108 |
+
try:
|
109 |
+
import json
|
110 |
+
inputs = json.loads(inputs)
|
111 |
+
except Exception as e:
|
112 |
+
return f"Invalid input format: {e}"
|
113 |
+
|
114 |
+
# Handle list-based operations like SUM
|
115 |
+
if "list" in inputs:
|
116 |
+
nums = inputs.get("list", [])
|
117 |
+
op = inputs.get("operation", "").lower()
|
118 |
+
|
119 |
+
if not isinstance(nums, list) or not all(isinstance(n, (int, float)) for n in nums):
|
120 |
+
return "Invalid list input. Must be a list of numbers."
|
121 |
+
|
122 |
+
if op == "sum":
|
123 |
+
return sum(nums)
|
124 |
+
elif op == "multiply":
|
125 |
+
return reduce(operator.mul, nums, 1)
|
126 |
+
else:
|
127 |
+
return f"Unsupported list operation: {op}"
|
128 |
+
|
129 |
+
# Handle basic two-number operations
|
130 |
+
a = inputs.get("a")
|
131 |
+
b = inputs.get("b")
|
132 |
+
operation = inputs.get("operation", "").lower()
|
133 |
+
|
134 |
+
if a is None or b is None or not isinstance(a, (int, float)) or not isinstance(b, (int, float)):
|
135 |
+
return "Both 'a' and 'b' must be numbers."
|
136 |
+
|
137 |
+
if operation == "add":
|
138 |
+
return a + b
|
139 |
+
elif operation == "subtract":
|
140 |
+
return a - b
|
141 |
+
elif operation == "multiply":
|
142 |
+
return a * b
|
143 |
+
elif operation == "divide":
|
144 |
+
if b == 0:
|
145 |
+
return "Error: Division by zero"
|
146 |
+
return a / b
|
147 |
+
elif operation == "modulus":
|
148 |
+
return a % b
|
149 |
+
else:
|
150 |
+
return f"Unknown operation: {operation}"
|
151 |
+
|
152 |
+
|
153 |
@tool
|
154 |
def parse_excel_to_json(task_id: str) -> dict:
|
155 |
"""
|
|
|
252 |
video_transcription_tool = VideoTranscriptionTool()
|
253 |
|
254 |
system_prompt = f"""
|
255 |
+
You are an advanced, helpful, and highly analytical research assistant. Your goal is to provide accurate, comprehensive, and well-structured answers to user queries, leveraging all available tools efficiently.
|
256 |
+
|
257 |
+
**Follow this robust process:**
|
258 |
+
|
259 |
+
1. **Understand the User's Need:** Carefully analyze the user's question, including any attached files or specific requests (e.g., "summarize," "analyze data," "find facts").
|
260 |
+
2. **Formulate a Detailed Plan:** Before acting, create a clear, step-by-step plan. This plan should outline:
|
261 |
+
* What information needs to be gathered.
|
262 |
+
* Which tools are most appropriate for each step (e.g., `duckduckgo_search` for general web search, `wiki_search` for encyclopedic facts, `transcript_video` for YouTube, `file_analysis` or `data_analysis` for local files).
|
263 |
+
* How you will combine information from different sources.
|
264 |
+
* How you will verify or synthesize the findings.
|
265 |
+
3. **Execute the Plan Using Tools:** Call the necessary tools, providing clear and correct arguments. If a tool fails, try to understand why and adapt your plan (e.g., try a different search query or tool).
|
266 |
+
4. **Synthesize and Verify Information:** Once you have gathered sufficient information, synthesize it into a coherent answer. Do not just list facts; explain their significance and how they relate to the original question. If there are contradictions or uncertainties, mention them.
|
267 |
+
5. **Formulate the Final Answer:**
|
268 |
+
* Present your answer clearly and concisely.
|
269 |
+
* Always begin your ultimate response with "FINAL ANSWER:".
|
270 |
+
* If the answer is a single number, provide only the number.
|
271 |
+
* If the answer is a list, provide comma-separated values.
|
272 |
+
* For complex answers, use structured formats like bullet points or JSON where appropriate to enhance readability.
|
273 |
+
* **Crucially, always include sources or references (e.g., URLs, Wikipedia titles, file names) where you obtained the information.** This builds trust and allows for verification.
|
274 |
+
* If you used `file_analysis` or `data_analysis` tools on an uploaded file, explicitly state that you analyzed the provided file.
|
275 |
+
|
276 |
+
**Important Considerations:**
|
277 |
+
* **Prioritize:** If the query involves a specific file, start by analyzing that file if appropriate.
|
278 |
+
* **Ambiguity:** If the question is ambiguous, ask for clarification.
|
279 |
+
* **Limitations:** If you cannot answer a question with the available tools, state that clearly.
|
280 |
+
* **Conciseness:** Be as concise as possible while providing a complete and accurate answer.
|
281 |
"""
|
282 |
self.agent = CodeAgent(
|
283 |
model=model,
|
284 |
tools=[search_tool, wiki_search_tool, str_reverse_tool, keywords_extract_tool, speech_to_text_tool, visit_webpage_tool, \
|
285 |
final_answer_tool, parse_excel_to_json, video_transcription_tool],
|
286 |
+
add_base_tools=True,
|
287 |
+
max_steps=15
|
288 |
)
|
289 |
self.agent.prompt_templates["system_prompt"] = self.agent.prompt_templates["system_prompt"] + system_prompt
|
290 |
|