Saptadip Sarkar
commited on
Commit
·
1da91a9
1
Parent(s):
d7f3625
fix: updated tool list
Browse files- agent_course_tools.py +28 -11
- app.py +12 -8
- requirements.txt +5 -1
agent_course_tools.py
CHANGED
@@ -1,18 +1,35 @@
|
|
|
|
1 |
from llama_index.tools.wikipedia import WikipediaToolSpec
|
2 |
from llama_index.tools.duckduckgo import DuckDuckGoSearchToolSpec
|
3 |
-
from llama_index.
|
4 |
-
|
5 |
-
)
|
6 |
|
|
|
7 |
wiki_spec = WikipediaToolSpec()
|
8 |
-
|
9 |
-
wiki_tool = wiki_spec.to_tool_list()[1]
|
10 |
|
|
|
11 |
ddgo_spec = DuckDuckGoSearchToolSpec()
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
-
#
|
16 |
-
|
17 |
-
# LoadAndSearchToolSpec.from_defaults(wiki_tool).to_tool_list(), verbose=True
|
18 |
-
#)
|
|
|
1 |
+
import requests
|
2 |
from llama_index.tools.wikipedia import WikipediaToolSpec
|
3 |
from llama_index.tools.duckduckgo import DuckDuckGoSearchToolSpec
|
4 |
+
from llama_index.tools.code_interpreter import CodeInterpreterToolSpec
|
5 |
+
from llama_index.core.tools import FunctionTool
|
|
|
6 |
|
7 |
+
# wikipedia search tool
|
8 |
wiki_spec = WikipediaToolSpec()
|
9 |
+
wiki_tool = wiki_spec.to_tool_list()
|
|
|
10 |
|
11 |
+
# duckduckgo search tool
|
12 |
ddgo_spec = DuckDuckGoSearchToolSpec()
|
13 |
+
ddgo_tool = ddgo_spec.to_tool_list()
|
14 |
+
|
15 |
+
# execute python code and return the stdout and stderr.
|
16 |
+
code_spec = CodeInterpreterToolSpec()
|
17 |
+
code_tool = code_spec.to_tool_list()
|
18 |
+
|
19 |
+
|
20 |
+
def download_file(url: str, save_path: str) -> str:
|
21 |
+
"""Download a file from a given URL and save it to the specified path."""
|
22 |
+
response = requests.get(url)
|
23 |
+
with open(save_path, 'wb') as file:
|
24 |
+
file.write(response.content)
|
25 |
+
return f"File downloaded to (save_path)"
|
26 |
+
|
27 |
+
download_tool = FunctionTool.from_defaults(
|
28 |
+
fn=download_file,
|
29 |
+
name="download_file_tool",
|
30 |
+
description="A tool to download files from an HTTPS API endpoint. The tool accepts two arguments: The https url path to download the file and the location to save the downloaded file. If no auguments are passed then use the default values. Default values: For 'url' use 'https://agents-course-unit4-scoring.hf.space/files/{task_id}' and for 'save_path' use the current path."
|
31 |
+
)
|
32 |
+
|
33 |
|
34 |
+
# agent complete tool list
|
35 |
+
agent_tools_list = wiki_tool + ddgo_tool + code_tool + [download_tool]
|
|
|
|
app.py
CHANGED
@@ -17,7 +17,8 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
17 |
class BasicAgent:
|
18 |
def __init__(self):
|
19 |
print("BasicAgent initialized.")
|
20 |
-
llm = HuggingFaceLLM(model_name="Qwen/Qwen2.5-Coder-32B-Instruct")
|
|
|
21 |
self.agent = AgentWorkflow.from_tools_or_functions([
|
22 |
tools.wiki_tool,
|
23 |
tools.ddgo_tool
|
@@ -26,13 +27,13 @@ class BasicAgent:
|
|
26 |
system_prompt="You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.",
|
27 |
)
|
28 |
|
29 |
-
def __call__(self, question: str) -> str:
|
30 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
31 |
-
fixed_answer = self.agent.run(question)
|
32 |
print(f"Agent returning fixed answer: {fixed_answer}")
|
33 |
return fixed_answer
|
34 |
|
35 |
-
def run_and_submit_all(
|
36 |
"""
|
37 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
38 |
and displays the results.
|
@@ -93,7 +94,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
93 |
print(f"Skipping item with missing task_id or question: {item}")
|
94 |
continue
|
95 |
try:
|
96 |
-
submitted_answer = agent(question_text)
|
97 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
98 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
99 |
except Exception as e:
|
@@ -106,7 +107,9 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
106 |
|
107 |
# 4. Prepare Submission
|
108 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
109 |
-
status_update =
|
|
|
|
|
110 |
print(status_update)
|
111 |
|
112 |
# 5. Submit
|
@@ -180,7 +183,8 @@ with gr.Blocks() as demo:
|
|
180 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
181 |
|
182 |
run_button.click(
|
183 |
-
fn=run_and_submit_all,
|
|
|
184 |
outputs=[status_output, results_table]
|
185 |
)
|
186 |
|
@@ -206,4 +210,4 @@ if __name__ == "__main__":
|
|
206 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
207 |
|
208 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
209 |
-
demo.launch(debug=True, share=False)
|
|
|
17 |
class BasicAgent:
|
18 |
def __init__(self):
|
19 |
print("BasicAgent initialized.")
|
20 |
+
#llm = HuggingFaceLLM(model_name="Qwen/Qwen2.5-Coder-32B-Instruct")
|
21 |
+
llm = HuggingFaceLLM(model_name="Qwen/Qwen2.5-7B")
|
22 |
self.agent = AgentWorkflow.from_tools_or_functions([
|
23 |
tools.wiki_tool,
|
24 |
tools.ddgo_tool
|
|
|
27 |
system_prompt="You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.",
|
28 |
)
|
29 |
|
30 |
+
async def __call__(self, question: str) -> str:
|
31 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
32 |
+
fixed_answer = await self.agent.run(question)
|
33 |
print(f"Agent returning fixed answer: {fixed_answer}")
|
34 |
return fixed_answer
|
35 |
|
36 |
+
async def run_and_submit_all(profile: gr.OAuthProfile | None):
|
37 |
"""
|
38 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
39 |
and displays the results.
|
|
|
94 |
print(f"Skipping item with missing task_id or question: {item}")
|
95 |
continue
|
96 |
try:
|
97 |
+
submitted_answer = await agent(question_text)
|
98 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
99 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
100 |
except Exception as e:
|
|
|
107 |
|
108 |
# 4. Prepare Submission
|
109 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
110 |
+
status_update = (
|
111 |
+
f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
112 |
+
)
|
113 |
print(status_update)
|
114 |
|
115 |
# 5. Submit
|
|
|
183 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
184 |
|
185 |
run_button.click(
|
186 |
+
fn=lambda profile: asyncio.run(run_and_submit_all(profile)),
|
187 |
+
inputs=gr.OAuth(),
|
188 |
outputs=[status_output, results_table]
|
189 |
)
|
190 |
|
|
|
210 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
211 |
|
212 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
213 |
+
demo.launch(debug=True, share=False)
|
requirements.txt
CHANGED
@@ -8,4 +8,8 @@ llama-index-tools-wikipedia
|
|
8 |
llama-index-llms-litellm
|
9 |
llama-index-readers-file
|
10 |
llama-index-tools-duckduckgo
|
11 |
-
llama-index-llms-huggingface
|
|
|
|
|
|
|
|
|
|
8 |
llama-index-llms-litellm
|
9 |
llama-index-readers-file
|
10 |
llama-index-tools-duckduckgo
|
11 |
+
llama-index-llms-huggingface
|
12 |
+
llama-index-llms-ollama
|
13 |
+
llama-index-tools-code_interpreter
|
14 |
+
#
|
15 |
+
gradio[oauth]
|