Spaces:
Sleeping
Sleeping
organize project
Browse files- __pycache__/agents.cpython-310.pyc +0 -0
- __pycache__/multi_agent.cpython-310.pyc +0 -0
- __pycache__/prompts.cpython-310.pyc +0 -0
- __pycache__/tools.cpython-310.pyc +0 -0
- agents.py +18 -115
- app.py +1 -10
- multi_agent.py +29 -0
- prompts.py +53 -0
- tools.py +34 -0
__pycache__/agents.cpython-310.pyc
CHANGED
Binary files a/__pycache__/agents.cpython-310.pyc and b/__pycache__/agents.cpython-310.pyc differ
|
|
__pycache__/multi_agent.cpython-310.pyc
ADDED
Binary file (727 Bytes). View file
|
|
__pycache__/prompts.cpython-310.pyc
ADDED
Binary file (3.12 kB). View file
|
|
__pycache__/tools.cpython-310.pyc
ADDED
Binary file (1.13 kB). View file
|
|
agents.py
CHANGED
@@ -1,10 +1,9 @@
|
|
1 |
-
import os
|
2 |
-
import pandas as pd
|
3 |
-
import requests
|
4 |
from smolagents import OpenAIServerModel, CodeAgent, InferenceClientModel, DuckDuckGoSearchTool, VisitWebpageTool
|
5 |
-
from smolagents.tools import tool
|
6 |
import markdownify
|
7 |
|
|
|
|
|
|
|
8 |
MANAGER_MODEL = "deepseek-ai/DeepSeek-R1"
|
9 |
AGENT_MODEL = "Qwen/Qwen2.5-Coder-32B-Instruct"
|
10 |
FINAL_ANSWER_MODEL = "deepseek-ai/DeepSeek-R1" # OpenAIServerModel
|
@@ -18,123 +17,31 @@ ARITHMETIC_MODEL = "Qwen/Qwen2.5-Coder-32B-Instruct"
|
|
18 |
CODE_GENERATION_MODEL = "Qwen/Qwen2.5-Coder-32B-Instruct"
|
19 |
CODE_EXECUTION_MODEL = "Qwen/Qwen2.5-Coder-32B-Instruct"
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
# Tools
|
24 |
-
|
25 |
-
simple_web_search_tool = DuckDuckGoSearchTool()
|
26 |
-
visit_web_page_tool = VisitWebpageTool()
|
27 |
-
|
28 |
-
@tool
|
29 |
-
def web_search_tool(query: str) -> str:
|
30 |
-
"""
|
31 |
-
Given a question, search the web and return a summary answer.
|
32 |
-
|
33 |
-
Args:
|
34 |
-
query (str): The search query to look up.
|
35 |
-
|
36 |
-
Returns:
|
37 |
-
str: A relevant summary or result from DuckDuckGo.
|
38 |
-
"""
|
39 |
-
try:
|
40 |
-
url = "https://api.duckduckgo.com/"
|
41 |
-
params = {"q": query, "format": "json", "no_html": 1}
|
42 |
-
response = requests.get(url, params=params)
|
43 |
-
data = response.json()
|
44 |
-
|
45 |
-
if abstract := data.get("AbstractText"):
|
46 |
-
return abstract
|
47 |
-
elif related := data.get("RelatedTopics"):
|
48 |
-
return related[0]["Text"] if related else "No result found."
|
49 |
-
else:
|
50 |
-
return "No relevant information found via DuckDuckGo."
|
51 |
-
except Exception as e:
|
52 |
-
raise RuntimeError(f"DuckDuckGo search failed: {str(e)}")
|
53 |
-
|
54 |
-
# Promts
|
55 |
-
|
56 |
-
def get_manager_prompt(message, file_path=None):
|
57 |
-
prompt = f"""Your job is to answer the following question.
|
58 |
-
Answer the following question. If needed, delegate to one of your coworkers:\n
|
59 |
-
|
60 |
-
- Web Search Agent: Use when the question requires current information. Web Search Agent requires a question only.\n
|
61 |
-
Format the prompt like:
|
62 |
-
"You are an expert web search assistant. Your task is to search the web and provide accurate answers to the following question: [INSERT QUESTION]"
|
63 |
-
|
64 |
-
...
|
65 |
-
|
66 |
-
In case you cannot answer the question and there is not a good coworker, delegate to the Code Generation Agent.\n.
|
67 |
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
def run_manager_workflow(message, file_path=None):
|
74 |
-
final_prompt = get_manager_prompt(message, file_path)
|
75 |
-
initial_answer = manager_agent.run(message)
|
76 |
-
|
77 |
-
final_answer = get_final_answer(final_answer_agent, message, str(initial_answer))
|
78 |
-
|
79 |
-
print(f"=> Initial question: {message}")
|
80 |
-
print(f"=> Final prompt: {final_prompt}")
|
81 |
-
print(f"=> Initial answer: {initial_answer}")
|
82 |
-
print(f"=> Final answer: {final_answer}")
|
83 |
-
|
84 |
-
return final_answer
|
85 |
-
|
86 |
-
def get_final_answer(agent, question: str, initial_answer: str) -> str:
|
87 |
-
prompt = f"""
|
88 |
-
You are an expert question answering assistant. Given a question and an initial answer, your task is to provide the final answer.
|
89 |
-
Your final answer must be a number and/or string OR as few words as possible OR a comma-separated list of numbers and/or strings.
|
90 |
-
If you are asked for a number, don't use comma to write your number neither use units such as USD, $, percent, or % unless specified otherwise.
|
91 |
-
If you are asked for a string, don't use articles, neither abbreviations (for example cities), and write the digits in plain text unless specified otherwise.
|
92 |
-
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.
|
93 |
-
If the final answer is a number, use a number not a word.
|
94 |
-
If the final answer is a string, start with an uppercase character.
|
95 |
-
If the final answer is a comma-separated list of numbers, use a space character after each comma.
|
96 |
-
If the final answer is a comma-separated list of strings, use a space character after each comma and start with a lowercase character.
|
97 |
-
Do not add any content to the final answer that is not in the initial answer.
|
98 |
-
|
99 |
-
**Question:** """ + question + """
|
100 |
-
|
101 |
-
**Initial answer:** """ + initial_answer + """
|
102 |
-
|
103 |
-
**Example 1:** What is the biggest city in California? Los Angeles
|
104 |
-
**Example 2:** How many 'r's are in strawberry? 3
|
105 |
-
**Example 3:** What is the opposite of black? White
|
106 |
-
**Example 4:** What are the first 5 numbers in the Fibonacci sequence? 0, 1, 1, 2, 3
|
107 |
-
**Example 5:** What is the opposite of bad, worse, worst? good, better, best
|
108 |
-
|
109 |
-
**Final answer:**
|
110 |
-
"""
|
111 |
-
|
112 |
-
return agent.run(prompt)
|
113 |
-
|
114 |
-
# Agents
|
115 |
-
|
116 |
-
web_search_agent = CodeAgent(
|
117 |
-
name="web_search_agent",
|
118 |
-
description="As an expert web search assistant, you search the web to answer the question. Your task is to search the web and provide accurate answers to the question: {message}",
|
119 |
model=InferenceClientModel(WEB_SEARCH_MODEL),
|
120 |
max_steps=2,
|
121 |
-
tools=[
|
122 |
)
|
123 |
|
124 |
-
|
|
|
125 |
name="simple_web_search_agent",
|
126 |
-
description=
|
127 |
-
# system_message="As an expert web search assistant, you search the web to answer the question. Your task is to search the web and provide accurate answers to the question: {message}",
|
128 |
model=InferenceClientModel(WEB_SEARCH_MODEL),
|
129 |
max_steps=2,
|
130 |
-
tools=[simple_web_search_tool, visit_web_page_tool],
|
131 |
)
|
132 |
|
133 |
-
|
134 |
-
|
135 |
name="manager_agent",
|
136 |
model=InferenceClientModel(MANAGER_MODEL, provider="together", max_tokens=8096),
|
137 |
-
description=
|
138 |
tools=[],
|
139 |
planning_interval=4,
|
140 |
verbosity_level=2,
|
@@ -168,15 +75,11 @@ def orchestrate(message, file_path):
|
|
168 |
],
|
169 |
)
|
170 |
|
171 |
-
|
|
|
172 |
name="final_answer_agent",
|
173 |
description="Given a question and an initial answer, return the final refined answer following strict formatting rules.",
|
174 |
model=InferenceClientModel(FINAL_ANSWER_MODEL),
|
175 |
max_steps=1,
|
176 |
tools=[],
|
177 |
)
|
178 |
-
|
179 |
-
final_answer = run_manager_workflow(message)
|
180 |
-
|
181 |
-
# final_answer = manager_agent.run(message)
|
182 |
-
return final_answer
|
|
|
|
|
|
|
|
|
1 |
from smolagents import OpenAIServerModel, CodeAgent, InferenceClientModel, DuckDuckGoSearchTool, VisitWebpageTool
|
|
|
2 |
import markdownify
|
3 |
|
4 |
+
import tools
|
5 |
+
import prompts
|
6 |
+
|
7 |
MANAGER_MODEL = "deepseek-ai/DeepSeek-R1"
|
8 |
AGENT_MODEL = "Qwen/Qwen2.5-Coder-32B-Instruct"
|
9 |
FINAL_ANSWER_MODEL = "deepseek-ai/DeepSeek-R1" # OpenAIServerModel
|
|
|
17 |
CODE_GENERATION_MODEL = "Qwen/Qwen2.5-Coder-32B-Instruct"
|
18 |
CODE_EXECUTION_MODEL = "Qwen/Qwen2.5-Coder-32B-Instruct"
|
19 |
|
20 |
+
# Agents
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
+
def create_custom_web_search_agent(message):
|
23 |
+
return CodeAgent(
|
24 |
+
name="custom_web_search_agent",
|
25 |
+
description=prompts.get_web_search_prompt(message),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
model=InferenceClientModel(WEB_SEARCH_MODEL),
|
27 |
max_steps=2,
|
28 |
+
tools=[tools.simple_web_search_tool, tools.visit_web_page_tool],
|
29 |
)
|
30 |
|
31 |
+
def create_simple_web_search_agent(message):
|
32 |
+
return CodeAgent(
|
33 |
name="simple_web_search_agent",
|
34 |
+
description=prompts.get_web_search_prompt(message),
|
|
|
35 |
model=InferenceClientModel(WEB_SEARCH_MODEL),
|
36 |
max_steps=2,
|
37 |
+
tools=[tools.simple_web_search_tool, tools.visit_web_page_tool],
|
38 |
)
|
39 |
|
40 |
+
def create_manager_agent(message):
|
41 |
+
return CodeAgent(
|
42 |
name="manager_agent",
|
43 |
model=InferenceClientModel(MANAGER_MODEL, provider="together", max_tokens=8096),
|
44 |
+
description=prompts.get_manager_prompt(message),
|
45 |
tools=[],
|
46 |
planning_interval=4,
|
47 |
verbosity_level=2,
|
|
|
75 |
],
|
76 |
)
|
77 |
|
78 |
+
def create_final_answer_agent(message):
|
79 |
+
return CodeAgent(
|
80 |
name="final_answer_agent",
|
81 |
description="Given a question and an initial answer, return the final refined answer following strict formatting rules.",
|
82 |
model=InferenceClientModel(FINAL_ANSWER_MODEL),
|
83 |
max_steps=1,
|
84 |
tools=[],
|
85 |
)
|
|
|
|
|
|
|
|
|
|
app.py
CHANGED
@@ -6,7 +6,7 @@ import pandas as pd
|
|
6 |
from huggingface_hub import login
|
7 |
from dotenv import load_dotenv
|
8 |
|
9 |
-
from
|
10 |
|
11 |
# (Keep Constants as is)
|
12 |
# --- Constants ---
|
@@ -152,8 +152,6 @@ def test_init_agent_for_chat(text_input, history, file_name = ""):
|
|
152 |
|
153 |
submitted_answer = orchestrate(text_input, file_name)
|
154 |
|
155 |
-
print(submitted_answer)
|
156 |
-
|
157 |
return submitted_answer
|
158 |
|
159 |
# --- Build Gradio Interface using Blocks ---
|
@@ -163,15 +161,8 @@ with gr.Blocks() as demo:
|
|
163 |
"""
|
164 |
**Instructions:**
|
165 |
|
166 |
-
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
167 |
-
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
168 |
-
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
169 |
4. who is in the final of champions league this year?
|
170 |
|
171 |
-
---
|
172 |
-
**Disclaimers:**
|
173 |
-
Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions).
|
174 |
-
This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a seperate action or even to answer the questions in async.
|
175 |
"""
|
176 |
)
|
177 |
|
|
|
6 |
from huggingface_hub import login
|
7 |
from dotenv import load_dotenv
|
8 |
|
9 |
+
from multi_agent import orchestrate
|
10 |
|
11 |
# (Keep Constants as is)
|
12 |
# --- Constants ---
|
|
|
152 |
|
153 |
submitted_answer = orchestrate(text_input, file_name)
|
154 |
|
|
|
|
|
155 |
return submitted_answer
|
156 |
|
157 |
# --- Build Gradio Interface using Blocks ---
|
|
|
161 |
"""
|
162 |
**Instructions:**
|
163 |
|
|
|
|
|
|
|
164 |
4. who is in the final of champions league this year?
|
165 |
|
|
|
|
|
|
|
|
|
166 |
"""
|
167 |
)
|
168 |
|
multi_agent.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import pandas as pd
|
3 |
+
import requests
|
4 |
+
from smolagents import OpenAIServerModel, CodeAgent, InferenceClientModel
|
5 |
+
from smolagents.tools import tool
|
6 |
+
import markdownify
|
7 |
+
|
8 |
+
import prompts
|
9 |
+
import agents
|
10 |
+
|
11 |
+
|
12 |
+
def orchestrate(message, file_path):
|
13 |
+
final_prompt = prompts.get_manager_prompt(message, file_path)
|
14 |
+
initial_answer = agents.create_simple_web_search_agent(message).run(message)
|
15 |
+
|
16 |
+
final_answer = agents.create_final_answer_agent(message).run(prompts.get_final_answer_prompt(message, initial_answer))
|
17 |
+
|
18 |
+
return final_answer
|
19 |
+
# def run_manager_workflow(message, file_path=None):
|
20 |
+
# final_prompt = prompts.get_manager_prompt(message, file_path)
|
21 |
+
# initial_answer = agents.create_simple_web_search_agent(message).run(message)
|
22 |
+
|
23 |
+
# final_answer = agents.create_final_answer_agent(message).run(prompts.get_final_answer_prompt(message, initial_answer))
|
24 |
+
|
25 |
+
# return final_answer
|
26 |
+
|
27 |
+
# final_answer = run_manager_workflow(message)
|
28 |
+
|
29 |
+
# return final_answer
|
prompts.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Promts
|
2 |
+
|
3 |
+
def get_web_search_prompt(message, file_path=None):
|
4 |
+
prompt = f"""
|
5 |
+
As an expert web search assistant, you search the web to answer the question. Your task is to search the web and provide accurate answers to the question: {message}
|
6 |
+
"""
|
7 |
+
|
8 |
+
return prompt
|
9 |
+
|
10 |
+
def get_manager_prompt(message, file_path=None):
|
11 |
+
prompt = f"""Your job is to answer the following question.
|
12 |
+
Answer the following question. If needed, delegate to one of your coworkers:\n
|
13 |
+
|
14 |
+
- Web Search Agent: Use when the question requires current information. Web Search Agent requires a question only.\n
|
15 |
+
Format the prompt like:
|
16 |
+
"You are an expert web search assistant. Your task is to search the web and provide accurate answers to the following question: [INSERT QUESTION]"
|
17 |
+
|
18 |
+
...
|
19 |
+
|
20 |
+
In case you cannot answer the question and there is not a good coworker, delegate to the Code Generation Agent.\n.
|
21 |
+
|
22 |
+
Question: {message}
|
23 |
+
"""
|
24 |
+
|
25 |
+
return prompt
|
26 |
+
|
27 |
+
def get_final_answer_prompt(message: str, initial_answer: str):
|
28 |
+
prompt = f"""
|
29 |
+
You are an expert question answering assistant. Given a question and an initial answer, your task is to provide the final answer.
|
30 |
+
Your final answer must be a number and/or string OR as few words as possible OR a comma-separated list of numbers and/or strings.
|
31 |
+
If you are asked for a number, don't use comma to write your number neither use units such as USD, $, percent, or % unless specified otherwise.
|
32 |
+
If you are asked for a string, don't use articles, neither abbreviations (for example cities), and write the digits in plain text unless specified otherwise.
|
33 |
+
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.
|
34 |
+
If the final answer is a number, use a number not a word.
|
35 |
+
If the final answer is a string, start with an uppercase character.
|
36 |
+
If the final answer is a comma-separated list of numbers, use a space character after each comma.
|
37 |
+
If the final answer is a comma-separated list of strings, use a space character after each comma and start with a lowercase character.
|
38 |
+
Do not add any content to the final answer that is not in the initial answer.
|
39 |
+
|
40 |
+
**Question:** """ + message + """
|
41 |
+
|
42 |
+
**Initial answer:** """ + initial_answer + """
|
43 |
+
|
44 |
+
**Example 1:** What is the biggest city in California? Los Angeles
|
45 |
+
**Example 2:** How many 'r's are in strawberry? 3
|
46 |
+
**Example 3:** What is the opposite of black? White
|
47 |
+
**Example 4:** What are the first 5 numbers in the Fibonacci sequence? 0, 1, 1, 2, 3
|
48 |
+
**Example 5:** What is the opposite of bad, worse, worst? good, better, best
|
49 |
+
|
50 |
+
**Final answer:**
|
51 |
+
"""
|
52 |
+
|
53 |
+
return prompt
|
tools.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
from smolagents import DuckDuckGoSearchTool, VisitWebpageTool
|
3 |
+
from smolagents.tools import tool
|
4 |
+
|
5 |
+
# Tools
|
6 |
+
|
7 |
+
simple_web_search_tool = DuckDuckGoSearchTool()
|
8 |
+
visit_web_page_tool = VisitWebpageTool()
|
9 |
+
|
10 |
+
@tool
|
11 |
+
def web_search_tool(query: str) -> str:
|
12 |
+
"""
|
13 |
+
Given a question, search the web and return a summary answer.
|
14 |
+
|
15 |
+
Args:
|
16 |
+
query (str): The search query to look up.
|
17 |
+
|
18 |
+
Returns:
|
19 |
+
str: A relevant summary or result from DuckDuckGo.
|
20 |
+
"""
|
21 |
+
try:
|
22 |
+
url = "https://api.duckduckgo.com/"
|
23 |
+
params = {"q": query, "format": "json", "no_html": 1}
|
24 |
+
response = requests.get(url, params=params)
|
25 |
+
data = response.json()
|
26 |
+
|
27 |
+
if abstract := data.get("AbstractText"):
|
28 |
+
return abstract
|
29 |
+
elif related := data.get("RelatedTopics"):
|
30 |
+
return related[0]["Text"] if related else "No result found."
|
31 |
+
else:
|
32 |
+
return "No relevant information found via DuckDuckGo."
|
33 |
+
except Exception as e:
|
34 |
+
raise RuntimeError(f"DuckDuckGo search failed: {str(e)}")
|