Initial commit with agent code and project structure
Browse files
multi_tool_agent/__pycache__/agent.cpython-311.pyc
CHANGED
|
Binary files a/multi_tool_agent/__pycache__/agent.cpython-311.pyc and b/multi_tool_agent/__pycache__/agent.cpython-311.pyc differ
|
|
|
multi_tool_agent/agent.py
CHANGED
|
@@ -1,69 +1,136 @@
|
|
| 1 |
-
from google.adk.agents import LlmAgent
|
| 2 |
-
from google.adk.tools import google_search
|
| 3 |
from firecrawl import FirecrawlApp
|
| 4 |
-
import google.genai as genai
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
-
import
|
| 7 |
-
import
|
| 8 |
-
import time
|
| 9 |
|
| 10 |
-
# Load
|
| 11 |
load_dotenv()
|
| 12 |
FIRECRAWL_API_KEY = os.getenv("FIRECRAWL_API_KEY")
|
| 13 |
-
|
| 14 |
-
# Setup Firecrawl
|
| 15 |
firecrawl_app = FirecrawlApp(api_key=FIRECRAWL_API_KEY)
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
for url in urls:
|
| 25 |
try:
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
)
|
| 32 |
-
if hasattr(response, 'markdown') and response.markdown:
|
| 33 |
-
combined_content += f"\n\n---\n\n# {url}\n\n" + response.markdown
|
| 34 |
-
time.sleep(2) # to respect rate limits
|
| 35 |
except Exception as e:
|
| 36 |
-
print(f"Failed to scrape {url}
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
#
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
)
|
| 67 |
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
|
|
|
| 1 |
+
from google.adk.agents import Agent, LlmAgent
|
|
|
|
| 2 |
from firecrawl import FirecrawlApp
|
|
|
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
+
from pydantic import BaseModel
|
| 5 |
+
from typing import List
|
| 6 |
+
import os, time, re
|
| 7 |
|
| 8 |
+
# 🔹 Load API key from .env
|
| 9 |
load_dotenv()
|
| 10 |
FIRECRAWL_API_KEY = os.getenv("FIRECRAWL_API_KEY")
|
|
|
|
|
|
|
| 11 |
firecrawl_app = FirecrawlApp(api_key=FIRECRAWL_API_KEY)
|
| 12 |
|
| 13 |
+
# 🔹 Search Result Schema
|
| 14 |
+
class SearchResult(BaseModel):
|
| 15 |
+
urls: List[str]
|
| 16 |
+
|
| 17 |
+
# 🔹 Search Agent
|
| 18 |
+
search_agent = LlmAgent(
|
| 19 |
+
name="SearchAgent",
|
| 20 |
+
model="gemini-2.0-flash",
|
| 21 |
+
instruction="""
|
| 22 |
+
You are a helpful web assistant. Given a travel prompt, return exactly 5 valid URLs relevant to that query.
|
| 23 |
|
| 24 |
+
Only return this format:
|
| 25 |
+
{"urls": ["https://site1.com", "https://site2.com", ...]}
|
| 26 |
+
|
| 27 |
+
Do not include any description or explanation.
|
| 28 |
+
""",
|
| 29 |
+
output_schema=SearchResult,
|
| 30 |
+
output_key="search_urls"
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
# 🔹 Scrape and Summarize Logic
|
| 34 |
+
def scrape_and_summarize(user_prompt: str, urls: List[str]) -> str:
|
| 35 |
+
if not user_prompt or not urls:
|
| 36 |
+
return "Missing user_prompt or urls."
|
| 37 |
+
|
| 38 |
+
combined = ""
|
| 39 |
for url in urls:
|
| 40 |
try:
|
| 41 |
+
res = firecrawl_app.scrape_url(url=url, formats=["markdown"], only_main_content=True)
|
| 42 |
+
if hasattr(res, "markdown") and res.markdown:
|
| 43 |
+
combined += f"\n\n---\n# {url}\n\n{res.markdown}"
|
| 44 |
+
else:
|
| 45 |
+
print(f"No markdown content found at: {url}")
|
| 46 |
+
time.sleep(1.5)
|
|
|
|
|
|
|
|
|
|
| 47 |
except Exception as e:
|
| 48 |
+
print(f"Failed to scrape {url} - {e}")
|
| 49 |
+
|
| 50 |
+
if not combined.strip():
|
| 51 |
+
return "Scraping failed or returned no usable data."
|
| 52 |
+
|
| 53 |
+
# Remove links to simplify text for LLM
|
| 54 |
+
trimmed = re.sub(r'\[.*?\]\(.*?\)', '', combined)[:6000]
|
| 55 |
+
|
| 56 |
+
return f"User Prompt: {user_prompt}\n\nScraped Content:\n{trimmed}"
|
| 57 |
+
|
| 58 |
+
# 🔹 Scraper Agent
|
| 59 |
+
scraper_agent = Agent(
|
| 60 |
+
name="ScraperAgent",
|
| 61 |
+
model="gemini-2.0-flash",
|
| 62 |
+
instruction="""
|
| 63 |
+
You are a strict itinerary planner. You must generate a structured 3-day travel itinerary using ONLY the scraped content provided.
|
| 64 |
+
|
| 65 |
+
❌ DO NOT:
|
| 66 |
+
- Write introductions or summaries like "Let's plan your trip..."
|
| 67 |
+
- Mention assumptions like "I’ll assume you’re interested in..."
|
| 68 |
+
- Ask the user for more information
|
| 69 |
+
- Add personal opinions or suggestions
|
| 70 |
+
|
| 71 |
+
✅ DO:
|
| 72 |
+
- Only use scraped content
|
| 73 |
+
- Follow this exact format:
|
| 74 |
+
|
| 75 |
+
Title: "3-Day Trip to <Destination> under ₹<Budget>"
|
| 76 |
+
|
| 77 |
+
Day 1: <Title>
|
| 78 |
+
Morning:
|
| 79 |
+
- <Activity>
|
| 80 |
+
Afternoon:
|
| 81 |
+
- <Activity>
|
| 82 |
+
Evening:
|
| 83 |
+
- <Activity>
|
| 84 |
+
Night:
|
| 85 |
+
- <Activity>
|
| 86 |
+
Estimated Day 1 Spend: ₹<Amount>
|
| 87 |
+
|
| 88 |
+
Day 2: ...
|
| 89 |
+
Day 3: ...
|
| 90 |
+
|
| 91 |
+
Budget Breakdown:
|
| 92 |
+
- Transport: ₹<amount>
|
| 93 |
+
- Food: ₹<amount>
|
| 94 |
+
- Accommodation: ₹<amount>
|
| 95 |
+
- Sightseeing: ₹<amount>
|
| 96 |
+
- Total: ₹<amount>
|
| 97 |
+
|
| 98 |
+
Only generate this format. If content is missing, say: "Not enough data to generate a valid itinerary."
|
| 99 |
+
"""
|
| 100 |
)
|
| 101 |
|
| 102 |
+
|
| 103 |
+
# 🔹 Root Agent
|
| 104 |
+
class TravelRootAgent(Agent):
|
| 105 |
+
def run(self, user_prompt: str):
|
| 106 |
+
|
| 107 |
+
# STEP 1: Get 5 travel URLs
|
| 108 |
+
search_result = search_agent.run(user_prompt)
|
| 109 |
+
urls = search_result.get("urls", [])
|
| 110 |
+
|
| 111 |
+
for u in urls: print(" -", u)
|
| 112 |
+
|
| 113 |
+
if not urls:
|
| 114 |
+
return "Search failed — no URLs returned."
|
| 115 |
+
|
| 116 |
+
# STEP 2: Scrape and summarize
|
| 117 |
+
scraped_content = scrape_and_summarize(user_prompt, urls)
|
| 118 |
+
print("\n Scraped Content Preview:\n", scraped_content[:1000])
|
| 119 |
+
|
| 120 |
+
# STEP 3: Send to ScraperAgent
|
| 121 |
+
payload = f"""
|
| 122 |
+
user_prompt: {user_prompt}
|
| 123 |
+
|
| 124 |
+
scraped_content:
|
| 125 |
+
{scraped_content}
|
| 126 |
+
"""
|
| 127 |
+
|
| 128 |
+
return scraper_agent.run(payload)
|
| 129 |
+
|
| 130 |
+
# ✅ Instantiate root agent
|
| 131 |
+
root_agent = TravelRootAgent(
|
| 132 |
+
name="TravelRootAgent",
|
| 133 |
+
model="gemini-2.0-flash",
|
| 134 |
+
instruction="Plan a 3-day travel itinerary using external scraped travel content."
|
| 135 |
+
)
|
| 136 |
|