|
| 1 | +from os import getenv |
| 2 | +from typing import Optional |
| 3 | + |
| 4 | +from agno.tools import Toolkit |
| 5 | +from agno.utils.log import logger |
| 6 | + |
| 7 | +try: |
| 8 | + import agentql |
| 9 | + from playwright.sync_api import sync_playwright |
| 10 | +except ImportError: |
| 11 | + raise ImportError("`agentql` not installed. Please install using `pip install agentql`") |
| 12 | + |
| 13 | + |
| 14 | +class AgentQLTools(Toolkit): |
| 15 | + def __init__( |
| 16 | + self, api_key: Optional[str] = None, scrape: bool = True, agentql_query: str = "" |
| 17 | + ): |
| 18 | + super().__init__(name="agentql_tools") |
| 19 | + |
| 20 | + self.api_key = api_key or getenv("AGENTQL_API_KEY") |
| 21 | + if not self.api_key: |
| 22 | + raise ValueError("AGENTQL_API_KEY not set. Please set the AGENTQL_API_KEY environment variable.") |
| 23 | + |
| 24 | + self.agentql_query = agentql_query |
| 25 | + |
| 26 | + if scrape: |
| 27 | + self.register(self.scrape_website) |
| 28 | + |
| 29 | + if agentql_query: |
| 30 | + logger.info("Custom AgentQL query provided. Registering custom scrape function.") |
| 31 | + self.register(self.custom_scrape_website) |
| 32 | + |
| 33 | + def scrape_website(self, url: str) -> str: |
| 34 | + """ |
| 35 | + Scrape all text content from a website using AgentQL. |
| 36 | +
|
| 37 | + Args: |
| 38 | + url (str): The URL of the website to scrape |
| 39 | +
|
| 40 | + Returns: |
| 41 | + str: Extracted text content or error message |
| 42 | + """ |
| 43 | + if not url: |
| 44 | + return "No URL provided" |
| 45 | + |
| 46 | + TEXT_SEARCH_QUERY = """ |
| 47 | + { |
| 48 | + text_content[] |
| 49 | + } |
| 50 | + """ |
| 51 | + |
| 52 | + try: |
| 53 | + with sync_playwright() as playwright, playwright.chromium.launch(headless=False) as browser: |
| 54 | + page = agentql.wrap(browser.new_page()) |
| 55 | + page.goto(url) |
| 56 | + |
| 57 | + try: |
| 58 | + # Get response from AgentQL query |
| 59 | + response = page.query_data(TEXT_SEARCH_QUERY) |
| 60 | + |
| 61 | + # Extract text based on response format |
| 62 | + if isinstance(response, dict) and "text_content" in response: |
| 63 | + text_items = [item for item in response["text_content"] if item and item.strip()] |
| 64 | + |
| 65 | + deduplicated = list(set(text_items)) |
| 66 | + return " ".join(deduplicated) |
| 67 | + |
| 68 | + except Exception as e: |
| 69 | + return f"Error extracting text: {e}" |
| 70 | + except Exception as e: |
| 71 | + return f"Error launching browser: {e}" |
| 72 | + |
| 73 | + return "No text content found" |
| 74 | + |
| 75 | + def custom_scrape_website(self, url: str) -> str: |
| 76 | + """ |
| 77 | + Scrape a website using a custom AgentQL query. |
| 78 | +
|
| 79 | + Args: |
| 80 | + url (str): The URL of the website to scrape |
| 81 | +
|
| 82 | + Returns: |
| 83 | + str: Extracted text content or error message |
| 84 | + """ |
| 85 | + if not url: |
| 86 | + return "No URL provided" |
| 87 | + |
| 88 | + if self.agentql_query == "": |
| 89 | + return "Custom AgentQL query not provided. Please provide a custom AgentQL query." |
| 90 | + |
| 91 | + try: |
| 92 | + with sync_playwright() as playwright, playwright.chromium.launch(headless=False) as browser: |
| 93 | + page = agentql.wrap(browser.new_page()) |
| 94 | + page.goto(url) |
| 95 | + |
| 96 | + try: |
| 97 | + # Get response from AgentQL query |
| 98 | + response = page.query_data(self.agentql_query) |
| 99 | + |
| 100 | + # Extract text based on response format |
| 101 | + if isinstance(response, dict): |
| 102 | + items = [item for item in response] |
| 103 | + text_items = [text_item for text_item in items if text_item] |
| 104 | + |
| 105 | + deduplicated = list(set(text_items)) |
| 106 | + return " ".join(deduplicated) |
| 107 | + |
| 108 | + except Exception as e: |
| 109 | + return f"Error extracting text: {e}" |
| 110 | + except Exception as e: |
| 111 | + return f"Error launching browser: {e}" |
| 112 | + |
| 113 | + return "No text content found" |
0 commit comments