Spaces:
Sleeping
Sleeping
feat: updated app for tool-use agent
Browse files
app.py
CHANGED
@@ -5,33 +5,62 @@ import inspect
|
|
5 |
import pandas as pd
|
6 |
import json
|
7 |
import copy
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
|
11 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
12 |
|
|
|
|
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
|
32 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
33 |
"""
|
34 |
-
Fetches all questions, runs the
|
35 |
and displays the results.
|
36 |
"""
|
37 |
|
@@ -51,7 +80,16 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
51 |
|
52 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
53 |
try:
|
54 |
-
agent =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
except Exception as e:
|
57 |
print(f"Error instantiating agent: {e}")
|
@@ -107,7 +145,12 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
107 |
continue
|
108 |
|
109 |
try:
|
110 |
-
|
|
|
|
|
|
|
|
|
|
|
111 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
112 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
113 |
|
|
|
5 |
import pandas as pd
|
6 |
import json
|
7 |
import copy
|
8 |
+
|
9 |
+
from basic_agent import ToolAgent
|
10 |
+
from tools import (
|
11 |
+
search_and_extract,
|
12 |
+
load_youtube_transcript,
|
13 |
+
search_and_extract_from_wikipedia,
|
14 |
+
image_query_tool,
|
15 |
+
)
|
16 |
|
17 |
|
18 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
19 |
|
20 |
+
TOOL_USE_SYS_PROMPT = """
|
21 |
+
You are a helpful AI assistant operating in a structured reasoning and action loop using the ReAct pattern.
|
22 |
|
23 |
+
Your reasoning loop consists of:
|
24 |
+
- Question: the input task you must solve
|
25 |
+
- Thought: Reflect on the task and decide what to do next.
|
26 |
+
- Action: Choose one of the following actions:
|
27 |
+
- Solve it directly using your own knowledge
|
28 |
+
- Break the problem into smaller steps
|
29 |
+
- Use a tool to get more information
|
30 |
+
- Action Input: Provide input for the selected action
|
31 |
+
- Observation: Record the result of the action and/or aggregate information from previous observations (summarize, count, analyse, ...).
|
32 |
+
(Repeat Thought/Action/Action Input/Observation as needed)
|
33 |
+
|
34 |
+
Terminate your loop with:
|
35 |
+
- Thought: I now know the final answer
|
36 |
+
- Final Answer: [your best answer to the original question]
|
37 |
+
|
38 |
+
**General Execution Rules:**
|
39 |
+
- If you can answer using only your trained knowledge, do so directly without using tools.
|
40 |
+
- If the question involves image content, use the `image_query_tool`:
|
41 |
+
- Action: image_query_tool
|
42 |
+
- Action Input: 'image_path': [image_path], 'question': [user's question about the image]
|
43 |
+
|
44 |
+
**Tool Use Constraints:**
|
45 |
+
- Never use any tool more than **2 consecutive times** without either:
|
46 |
+
- Reasoning about the information received so far: aggregate and analyze the tool outputs to answer the question.
|
47 |
+
- If you need more information, use a different tool or break the problem down further, but do not return a final answer yet.
|
48 |
+
- Do not exceed **3 total calls** to *search-type tools* per query (e.g. `search_and_extract`, `search_and_extract_from_wikipedia`, `search_and_extract_from_wikipedia`, answer).
|
49 |
+
- Do not ask the user for additional clarification or input. Work with only what is already provided.
|
50 |
+
|
51 |
+
**If you are unable to answer:**
|
52 |
+
- If neither your knowledge nor tool outputs yield useful information, say:
|
53 |
+
> Final Answer: I could not find any useful information to answer your query.
|
54 |
+
- If the question is unanswerable due to lack of input (e.g., missing attachment) or is fundamentally outside your scope, say:
|
55 |
+
> Final Answer: I don't have the ability to answer this query: [brief reason]
|
56 |
+
|
57 |
+
Always aim to provide the **best and most complete** answer based on your trained knowledge and the tools available.
|
58 |
+
"""
|
59 |
|
60 |
|
61 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
62 |
"""
|
63 |
+
Fetches all questions, runs the ToolAgent on them, submits all answers,
|
64 |
and displays the results.
|
65 |
"""
|
66 |
|
|
|
80 |
|
81 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
82 |
try:
|
83 |
+
agent = ToolAgent(
|
84 |
+
tools=[
|
85 |
+
search_and_extract,
|
86 |
+
load_youtube_transcript,
|
87 |
+
search_and_extract_from_wikipedia,
|
88 |
+
image_query_tool,
|
89 |
+
]
|
90 |
+
backstory=TOOL_USE_SYS_PROMPT
|
91 |
+
)
|
92 |
+
agent.agent.initialize()
|
93 |
|
94 |
except Exception as e:
|
95 |
print(f"Error instantiating agent: {e}")
|
|
|
145 |
continue
|
146 |
|
147 |
try:
|
148 |
+
image_path = question_file_name if ".png" in question_file_name else ""
|
149 |
+
metadata = {}
|
150 |
+
if image_path:
|
151 |
+
metadata = {'image_path': image_path}
|
152 |
+
q_data {'query': question_text, 'metadata': metadata}
|
153 |
+
submitted_answer = agent(q_data) # todo: send more data (files)
|
154 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
155 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
156 |
|