Spaces:
Sleeping
Sleeping
use google search tool
Browse files- .env.example +3 -1
- app.py +7 -5
- app_agents/manager_agent copy.py +58 -0
- app_agents/manager_agent.py +37 -44
- app_agents/web_agent.py +32 -6
- requirements.txt +5 -1
.env.example
CHANGED
|
@@ -1,2 +1,4 @@
|
|
| 1 |
SPACE_ID=
|
| 2 |
-
HF_TOKEN=
|
|
|
|
|
|
|
|
|
| 1 |
SPACE_ID=
|
| 2 |
+
HF_TOKEN=
|
| 3 |
+
OPENAI_API_KEY=
|
| 4 |
+
SERPAPI_API_KEY=
|
app.py
CHANGED
|
@@ -7,8 +7,8 @@ from huggingface_hub import login
|
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
|
| 9 |
import agent
|
| 10 |
-
from app_agents.manager_agent import manager_agent
|
| 11 |
-
|
| 12 |
|
| 13 |
# (Keep Constants as is)
|
| 14 |
# --- Constants ---
|
|
@@ -143,12 +143,14 @@ def test_init_agent_for_chat(text_input, history):
|
|
| 143 |
# print(f"Error instantiating agent: {e}")
|
| 144 |
# return f"Error initializing agent: {e}", None
|
| 145 |
|
| 146 |
-
manager_agent.visualize()
|
| 147 |
|
| 148 |
-
manager_agent.run(text_input)
|
|
|
|
|
|
|
| 149 |
# submitted_answer = basicAgent(text_input)
|
| 150 |
|
| 151 |
-
|
| 152 |
|
| 153 |
# --- Build Gradio Interface using Blocks ---
|
| 154 |
with gr.Blocks() as demo:
|
|
|
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
|
| 9 |
import agent
|
| 10 |
+
# from app_agents.manager_agent import manager_agent
|
| 11 |
+
import app_agents.manager_agent as manager_agent
|
| 12 |
|
| 13 |
# (Keep Constants as is)
|
| 14 |
# --- Constants ---
|
|
|
|
| 143 |
# print(f"Error instantiating agent: {e}")
|
| 144 |
# return f"Error initializing agent: {e}", None
|
| 145 |
|
| 146 |
+
# manager_agent.visualize()
|
| 147 |
|
| 148 |
+
submitted_answer = manager_agent.manager_agent.run(text_input)
|
| 149 |
+
# print(type(submitted_answer))
|
| 150 |
+
# print(submitted_answer)
|
| 151 |
# submitted_answer = basicAgent(text_input)
|
| 152 |
|
| 153 |
+
return submitted_answer
|
| 154 |
|
| 155 |
# --- Build Gradio Interface using Blocks ---
|
| 156 |
with gr.Blocks() as demo:
|
app_agents/manager_agent copy.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from smolagents.utils import encode_image_base64, make_image_url
|
| 3 |
+
from smolagents import OpenAIServerModel, CodeAgent, InferenceClientModel
|
| 4 |
+
|
| 5 |
+
import app_agents.web_agent as web_agent
|
| 6 |
+
from app_agents.web_agent import web_agent
|
| 7 |
+
import app_tools.tools as agent_tools
|
| 8 |
+
|
| 9 |
+
def check_reasoning_and_plot(final_answer, agent_memory):
|
| 10 |
+
multimodal_model = OpenAIServerModel("gpt-4o", max_tokens=8096)
|
| 11 |
+
filepath = "saved_map.png"
|
| 12 |
+
assert os.path.exists(filepath), "Make sure to save the plot under saved_map.png!"
|
| 13 |
+
image = Image.open(filepath)
|
| 14 |
+
prompt = (
|
| 15 |
+
f"Here is a user-given task and the agent steps: {agent_memory.get_succinct_steps()}. Now here is the plot that was made."
|
| 16 |
+
"Please check that the reasoning process and plot are correct: do they correctly answer the given task?"
|
| 17 |
+
"First list reasons why yes/no, then write your final decision: PASS in caps lock if it is satisfactory, FAIL if it is not."
|
| 18 |
+
"Don't be harsh: if the plot mostly solves the task, it should pass."
|
| 19 |
+
"To pass, a plot should be made using px.scatter_map and not any other method (scatter_map looks nicer)."
|
| 20 |
+
)
|
| 21 |
+
messages = [
|
| 22 |
+
{
|
| 23 |
+
"role": "user",
|
| 24 |
+
"content": [
|
| 25 |
+
{
|
| 26 |
+
"type": "text",
|
| 27 |
+
"text": prompt,
|
| 28 |
+
},
|
| 29 |
+
{
|
| 30 |
+
"type": "image_url",
|
| 31 |
+
"image_url": {"url": make_image_url(encode_image_base64(image))},
|
| 32 |
+
},
|
| 33 |
+
],
|
| 34 |
+
}
|
| 35 |
+
]
|
| 36 |
+
output = multimodal_model(messages).content
|
| 37 |
+
print("Feedback: ", output)
|
| 38 |
+
if "FAIL" in output:
|
| 39 |
+
raise Exception(output)
|
| 40 |
+
return True
|
| 41 |
+
|
| 42 |
+
manager_agent = CodeAgent(
|
| 43 |
+
model=InferenceClientModel("deepseek-ai/DeepSeek-R1", provider="together", max_tokens=8096),
|
| 44 |
+
tools=[agent_tools.calculate_cargo_travel_time],
|
| 45 |
+
managed_agents=[web_agent],
|
| 46 |
+
additional_authorized_imports=[
|
| 47 |
+
"geopandas",
|
| 48 |
+
"plotly",
|
| 49 |
+
"shapely",
|
| 50 |
+
"json",
|
| 51 |
+
"pandas",
|
| 52 |
+
"numpy",
|
| 53 |
+
],
|
| 54 |
+
planning_interval=5,
|
| 55 |
+
verbosity_level=2,
|
| 56 |
+
final_answer_checks=[check_reasoning_and_plot],
|
| 57 |
+
max_steps=5,
|
| 58 |
+
)
|
app_agents/manager_agent.py
CHANGED
|
@@ -2,57 +2,50 @@ import os
|
|
| 2 |
from smolagents.utils import encode_image_base64, make_image_url
|
| 3 |
from smolagents import OpenAIServerModel, CodeAgent, InferenceClientModel
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
import app_agents.web_agent as web_agent
|
| 6 |
-
|
| 7 |
-
import app_tools.tools as agent_tools
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
assert os.path.exists(filepath), "Make sure to save the plot under saved_map.png!"
|
| 13 |
-
image = Image.open(filepath)
|
| 14 |
-
prompt = (
|
| 15 |
-
f"Here is a user-given task and the agent steps: {agent_memory.get_succinct_steps()}. Now here is the plot that was made."
|
| 16 |
-
"Please check that the reasoning process and plot are correct: do they correctly answer the given task?"
|
| 17 |
-
"First list reasons why yes/no, then write your final decision: PASS in caps lock if it is satisfactory, FAIL if it is not."
|
| 18 |
-
"Don't be harsh: if the plot mostly solves the task, it should pass."
|
| 19 |
-
"To pass, a plot should be made using px.scatter_map and not any other method (scatter_map looks nicer)."
|
| 20 |
-
)
|
| 21 |
-
messages = [
|
| 22 |
-
{
|
| 23 |
-
"role": "user",
|
| 24 |
-
"content": [
|
| 25 |
-
{
|
| 26 |
-
"type": "text",
|
| 27 |
-
"text": prompt,
|
| 28 |
-
},
|
| 29 |
-
{
|
| 30 |
-
"type": "image_url",
|
| 31 |
-
"image_url": {"url": make_image_url(encode_image_base64(image))},
|
| 32 |
-
},
|
| 33 |
-
],
|
| 34 |
-
}
|
| 35 |
-
]
|
| 36 |
-
output = multimodal_model(messages).content
|
| 37 |
-
print("Feedback: ", output)
|
| 38 |
-
if "FAIL" in output:
|
| 39 |
-
raise Exception(output)
|
| 40 |
-
return True
|
| 41 |
|
| 42 |
manager_agent = CodeAgent(
|
| 43 |
model=InferenceClientModel("deepseek-ai/DeepSeek-R1", provider="together", max_tokens=8096),
|
| 44 |
-
tools=[
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
additional_authorized_imports=[
|
| 47 |
-
"
|
| 48 |
-
"
|
| 49 |
-
"
|
| 50 |
-
"json",
|
| 51 |
"pandas",
|
| 52 |
"numpy",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
],
|
| 54 |
-
planning_interval=5,
|
| 55 |
-
verbosity_level=2,
|
| 56 |
-
final_answer_checks=[check_reasoning_and_plot],
|
| 57 |
-
max_steps=15,
|
| 58 |
)
|
|
|
|
| 2 |
from smolagents.utils import encode_image_base64, make_image_url
|
| 3 |
from smolagents import OpenAIServerModel, CodeAgent, InferenceClientModel
|
| 4 |
|
| 5 |
+
# from gradio_tools import (StableDiffusionTool, ImageCaptioningTool, StableDiffusionPromptGeneratorTool,
|
| 6 |
+
# TextToVideoTool)
|
| 7 |
+
# from langchain.agents import initialize_agent
|
| 8 |
+
# from langchain.memory import ConversationBufferMemory
|
| 9 |
+
|
| 10 |
import app_agents.web_agent as web_agent
|
| 11 |
+
# import app_tools.tools as agent_tools
|
|
|
|
| 12 |
|
| 13 |
+
# tools = [StableDiffusionTool().langchain, ImageCaptioningTool().langchain,
|
| 14 |
+
# StableDiffusionPromptGeneratorTool().langchain, TextToVideoTool().langchain]
|
| 15 |
+
# memory = ConversationBufferMemory(memory_key="chat_history")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
manager_agent = CodeAgent(
|
| 18 |
model=InferenceClientModel("deepseek-ai/DeepSeek-R1", provider="together", max_tokens=8096),
|
| 19 |
+
tools=[],
|
| 20 |
+
planning_interval=4,
|
| 21 |
+
verbosity_level=2,
|
| 22 |
+
managed_agents=[web_agent.web_agent],
|
| 23 |
+
max_steps=10,
|
| 24 |
+
# grammar=DEFAULT_CODEAGENT_REGEX_GRAMMAR,
|
| 25 |
additional_authorized_imports=[
|
| 26 |
+
"requests",
|
| 27 |
+
"zipfile",
|
| 28 |
+
"os",
|
|
|
|
| 29 |
"pandas",
|
| 30 |
"numpy",
|
| 31 |
+
"sympy",
|
| 32 |
+
"json",
|
| 33 |
+
"bs4",
|
| 34 |
+
"pubchempy",
|
| 35 |
+
"xml",
|
| 36 |
+
"yahoo_finance",
|
| 37 |
+
"Bio",
|
| 38 |
+
"sklearn",
|
| 39 |
+
"scipy",
|
| 40 |
+
"pydub",
|
| 41 |
+
"io",
|
| 42 |
+
"PIL",
|
| 43 |
+
"chess",
|
| 44 |
+
"PyPDF2",
|
| 45 |
+
"pptx",
|
| 46 |
+
"torch",
|
| 47 |
+
"datetime",
|
| 48 |
+
"csv",
|
| 49 |
+
"fractions",
|
| 50 |
],
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
)
|
app_agents/web_agent.py
CHANGED
|
@@ -1,13 +1,39 @@
|
|
| 1 |
-
from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel, VisitWebpageTool
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
model = InferenceClientModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct", provider="together")
|
| 4 |
|
| 5 |
web_agent = CodeAgent(
|
| 6 |
model=model,
|
| 7 |
-
tools=[
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
| 9 |
name="web_agent",
|
| 10 |
-
description="
|
| 11 |
verbosity_level=0,
|
| 12 |
-
max_steps=
|
| 13 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel, VisitWebpageTool, ManagedAgent
|
| 2 |
|
| 3 |
+
# model = InferenceClientModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct", provider="together")
|
| 4 |
+
|
| 5 |
+
# web_agent = CodeAgent(
|
| 6 |
+
# model=model,
|
| 7 |
+
# tools=[DuckDuckGoSearchTool(), VisitWebpageTool()],
|
| 8 |
+
# additional_authorized_imports=["pandas"],
|
| 9 |
+
# name="web_agent",
|
| 10 |
+
# description="Browses the web to find information",
|
| 11 |
+
# verbosity_level=0,
|
| 12 |
+
# max_steps=20,
|
| 13 |
+
# )
|
| 14 |
+
|
| 15 |
+
from dotenv import load_dotenv
|
| 16 |
+
from smolagents import InferenceClientModel, CodeAgent, DuckDuckGoSearchTool, GoogleSearchTool, VisitWebpageTool
|
| 17 |
+
|
| 18 |
+
load_dotenv()
|
| 19 |
model = InferenceClientModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct", provider="together")
|
| 20 |
|
| 21 |
web_agent = CodeAgent(
|
| 22 |
model=model,
|
| 23 |
+
tools=[
|
| 24 |
+
# DuckDuckGoSearchTool(),
|
| 25 |
+
GoogleSearchTool("serper"),
|
| 26 |
+
VisitWebpageTool()
|
| 27 |
+
],
|
| 28 |
name="web_agent",
|
| 29 |
+
description="Runs web searches for you. Give it your query as an argument.",
|
| 30 |
verbosity_level=0,
|
| 31 |
+
max_steps=10,
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
# managed_web_agent = CodeAgent(
|
| 35 |
+
# model=model,
|
| 36 |
+
# agent=web_agent,
|
| 37 |
+
# name="web_search",
|
| 38 |
+
# description="Runs web searches for you. Give it your query as an argument."
|
| 39 |
+
# )
|
requirements.txt
CHANGED
|
@@ -8,4 +8,8 @@ kaleido
|
|
| 8 |
smolagents
|
| 9 |
typing
|
| 10 |
duckduckgo-search
|
| 11 |
-
huggingface_hub
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
smolagents
|
| 9 |
typing
|
| 10 |
duckduckgo-search
|
| 11 |
+
huggingface_hub
|
| 12 |
+
markdownify
|
| 13 |
+
transformers
|
| 14 |
+
gradio_tools
|
| 15 |
+
langchain
|