Spaces:
Sleeping
Sleeping
Developed the basis
Browse files(cherry picked from commit 2a41ac40514a56388d6366704838754986f53aa2)
- app.py +2 -3
- requirements.txt +14 -4
- src/agent.py +84 -0
- src/party_planner/utils.py +40 -0
- {tools → src/tools}/final_answer.py +0 -0
- {tools → src/tools}/visit_webpage.py +1 -0
- {tools → src/tools}/web_search.py +0 -0
app.py
CHANGED
|
@@ -1,9 +1,8 @@
|
|
| 1 |
-
from smolagents import CodeAgent,
|
| 2 |
import datetime
|
| 3 |
-
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
-
from tools.final_answer import FinalAnswerTool
|
| 7 |
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, HfApiModel,load_tool,tool
|
| 2 |
import datetime
|
|
|
|
| 3 |
import pytz
|
| 4 |
import yaml
|
| 5 |
+
from src.tools.final_answer import FinalAnswerTool
|
| 6 |
|
| 7 |
from Gradio_UI import GradioUI
|
| 8 |
|
requirements.txt
CHANGED
|
@@ -1,5 +1,15 @@
|
|
| 1 |
-
markdownify
|
| 2 |
-
smolagents
|
| 3 |
-
requests
|
| 4 |
-
duckduckgo_search
|
| 5 |
pandas
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
markdownify~=1.1.0
|
| 2 |
+
smolagents~=1.10.0
|
| 3 |
+
requests~=2.32.3
|
| 4 |
+
duckduckgo_search~=7.5.1
|
| 5 |
pandas
|
| 6 |
+
smolagents[litellm]
|
| 7 |
+
matplotlib
|
| 8 |
+
geopandas
|
| 9 |
+
shapely
|
| 10 |
+
kaleido
|
| 11 |
+
pytz~=2025.1
|
| 12 |
+
PyYAML~=6.0.2
|
| 13 |
+
langchain~=0.3.20
|
| 14 |
+
langchain-community~=0.3.19
|
| 15 |
+
python-dotenv~=1.0.1
|
src/agent.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, GoogleSearchTool, VisitWebpageTool, HfApiModel
|
| 2 |
+
|
| 3 |
+
from src.party_planner.tools.travel_time import calculate_cargo_travel_time
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def create_agent(
|
| 7 |
+
agent_type: str,
|
| 8 |
+
name: str,
|
| 9 |
+
model: HfApiModel,
|
| 10 |
+
tools: list,
|
| 11 |
+
max_steps: int = 10,
|
| 12 |
+
additional_imports: list[str] = None,
|
| 13 |
+
description: str = "",
|
| 14 |
+
interval: int = 0,
|
| 15 |
+
verbosity: int = 0,
|
| 16 |
+
**kwargs
|
| 17 |
+
):
|
| 18 |
+
"""
|
| 19 |
+
**kwargs can be: managed_agents, final_answer_checks, ...
|
| 20 |
+
"""
|
| 21 |
+
if agent_type == "code_agent":
|
| 22 |
+
return CodeAgent(
|
| 23 |
+
name=name,
|
| 24 |
+
model=model,
|
| 25 |
+
tools=tools,
|
| 26 |
+
additional_authorized_imports=additional_imports,
|
| 27 |
+
max_steps=max_steps,
|
| 28 |
+
description=description,
|
| 29 |
+
planning_interval=interval,
|
| 30 |
+
verbosity_level=verbosity,
|
| 31 |
+
**kwargs
|
| 32 |
+
)
|
| 33 |
+
return None
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
if __name__ == "__main__":
|
| 37 |
+
import os
|
| 38 |
+
from dotenv import load_dotenv
|
| 39 |
+
from src.model import get_model
|
| 40 |
+
|
| 41 |
+
load_dotenv()
|
| 42 |
+
# os.environ["SERPER_API_KEY"] = os.getenv("SERPER_API_KEY")
|
| 43 |
+
os.environ["SERPAPI_API_KEY"] = os.getenv("SERPAPI_API_KEY")
|
| 44 |
+
|
| 45 |
+
AgentType = "code_agent"
|
| 46 |
+
Name = "web_agent"
|
| 47 |
+
Model = get_model(
|
| 48 |
+
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 49 |
+
provider="hf-inference" # "together"
|
| 50 |
+
)
|
| 51 |
+
ToolNames = [
|
| 52 |
+
GoogleSearchTool(provider="serpapi"),
|
| 53 |
+
VisitWebpageTool(),
|
| 54 |
+
calculate_cargo_travel_time
|
| 55 |
+
]
|
| 56 |
+
AdditionalImports = ["pandas"]
|
| 57 |
+
MaxSteps = 3
|
| 58 |
+
Description = "Browses the web to find information"
|
| 59 |
+
Interval = 4
|
| 60 |
+
|
| 61 |
+
# Simple agent served as a baseline for the multi-agent system
|
| 62 |
+
Agent = create_agent(
|
| 63 |
+
agent_type=AgentType,
|
| 64 |
+
name=Name,
|
| 65 |
+
model=Model,
|
| 66 |
+
tools=ToolNames,
|
| 67 |
+
additional_imports=AdditionalImports,
|
| 68 |
+
max_steps=MaxSteps,
|
| 69 |
+
description=Description,
|
| 70 |
+
interval=Interval
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
Task = """Find all Batman filming locations in the world, calculate the time to transfer via cargo plane to
|
| 74 |
+
here (we're in Gotham, 40.7128° N, 74.0060° W), and return them to me as a pandas dataframe. Also give me some
|
| 75 |
+
supercar factories with the same cargo plane transfer time."""
|
| 76 |
+
Prompt = f"""
|
| 77 |
+
You're an expert analyst. You make comprehensive reports after visiting many websites.
|
| 78 |
+
Don't hesitate to search for many queries at once in a for loop.
|
| 79 |
+
For each data point that you find, visit the source url to confirm numbers.
|
| 80 |
+
|
| 81 |
+
{Task}
|
| 82 |
+
"""
|
| 83 |
+
result = Agent.run(Prompt)
|
| 84 |
+
print('\n' * 2, result)
|
src/party_planner/utils.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents.utils import encode_image_base64, make_image_url
|
| 2 |
+
from smolagents import OpenAIServerModel
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def check_reasoning_and_plot(
|
| 7 |
+
final_answer,
|
| 8 |
+
agent_memory,
|
| 9 |
+
map_image_path: str = "src/party_planner/saved_map.png"
|
| 10 |
+
) -> bool:
|
| 11 |
+
final_answer
|
| 12 |
+
multimodal_model = OpenAIServerModel("gpt-4o", max_tokens=2048)
|
| 13 |
+
image = Image.open(map_image_path)
|
| 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
|
{tools → src/tools}/final_answer.py
RENAMED
|
File without changes
|
{tools → src/tools}/visit_webpage.py
RENAMED
|
@@ -3,6 +3,7 @@ from smolagents.tools import Tool
|
|
| 3 |
import requests
|
| 4 |
import markdownify
|
| 5 |
import smolagents
|
|
|
|
| 6 |
|
| 7 |
class VisitWebpageTool(Tool):
|
| 8 |
name = "visit_webpage"
|
|
|
|
| 3 |
import requests
|
| 4 |
import markdownify
|
| 5 |
import smolagents
|
| 6 |
+
import re
|
| 7 |
|
| 8 |
class VisitWebpageTool(Tool):
|
| 9 |
name = "visit_webpage"
|
{tools → src/tools}/web_search.py
RENAMED
|
File without changes
|