8000 Add AgentQL query_data tool by desi003 · Pull Request #213 · crewAIInc/crewAI-tools · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add AgentQL query_data tool #213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crewai_tools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .tools import (
AgentQLQueryDataTool,
AIMindTool,
BraveSearchTool,
BrowserbaseLoadTool,
Expand Down
1 change: 1 addition & 0 deletions crewai_tools/tools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .agentql_query_data_tool.agentql_query_data_tool import AgentQLQueryDataTool
from .ai_mind_tool.ai_mind_tool import AIMindTool
from .brave_search_tool.brave_search_tool import BraveSearchTool
from .browserbase_load_tool.browserbase_load_tool import BrowserbaseLoadTool
Expand Down
39 changes: 39 additions & 0 deletions crewai_tools/tools/agentql_query_data_tool/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# AgentQLQueryDataTool

## Description

[AgentQL](https://www.agentql.com/) is a tool that queries from any website using a structured query language.

## Installation

- Get an API key from [agentql.dev](https://dev.agentql.com/api-keys) and set it in environment variables (`AGENTQL_API_KEY`).
- Install the [AgentQL SDK](https://docs.agentql.com/python-sdk/installation) along with `crewai[tools]` package:

```
pip install 'crewai[tools]'
```

## Example

Utilize the AgentQLQueryDataTool as follows to allow your agent to query web 8000 sites:

```python
from crewai_tools import AgentQLQueryDataTool

QUERY = """
{
products[] {
product_name
price
}
}
"""

tool = AgentQLQueryDataTool(url='https://scrapeme.live/?s=fish&post_type=product', query=QUERY)

## Arguments

- `url`: The base URL to extract data from.
- `query`: Optional. The query to use for the AgentQL to extract data from the url.
- `prompt`: Optional. A natural language description of the data you want to scrape.
```
109 changes: 109 additions & 0 deletions crewai_tools/tools/agentql_query_data_tool/agentql_query_data_tool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import os
import httpx

from pydantic import BaseModel, Field
from typing import Optional, Type

from crewai.tools import BaseTool

from dotenv import load_dotenv

load_dotenv()

QUERY_DATA_ENDPOINT = "https://api.agentql.com/v1/query-data"
API_TIMEOUT_SECONDS = 900

API_KEY = os.getenv("AGENTQL_API_KEY")


class AgentQLQueryDataSchema(BaseModel):
url: str = Field(description="Website URL")
query: Optional[str] = Field(
default=None,
description="""
AgentQL query to scrape the url.
Here is a guide on AgentQL query syntax:
Enclose all AgentQL query terms within curly braces `{}`. The following query structure isn't valid because the term "social\_media\_links" is wrongly enclosed within parenthesis `()`.
```
( # Should be {
social_media_links(The icons that lead to Facebook, Snapchat, etc.)[]
) # Should be }
```
The following query is also invalid since its missing the curly braces `{}`
```
# should include {
social_media_links(The icons that lead to Facebook, Snapchat, etc.)[]
# should include }
```
You can't include new lines in your semantic context. The following query structure isn't valid because the semantic context isn't contained within one line.
```
{
social_media_links(The icons that lead
to Facebook, Snapchat, etc.)[]
}
```
""",
)
prompt: Optional[str] = Field(
default=None,
description="Natural language description of the data you want to scrape",
)


class AgentQLQueryDataTool(BaseTool):
name: str = "query_data"
description: str = (
"Scrape a url with a given AgentQL query or a natural language description of the data you want to scrape."
)
args_schema: Type[BaseModel] = AgentQLQueryDataSchema

def __init__(self, **kwargs):
super().__init__(**kwargs)

def _run(
self, url: str, query: Optional[str] = None, prompt: Optional[str] = None
) -> dict:
payload = {
"url": url,
"query": query,
"prompt": prompt,
"metadata": {
"experimental_stealth_mode_enabled": True,
},
}

headers = {
"X-API-Key": f"{API_KEY}",
"Content-Type": "application/json",
"X-TF-Request-Origin": "crewai",
}

try:
response = httpx.post(
QUERY_DATA_ENDPOINT,
headers=headers,
json=payload,
timeout=API_TIMEOUT_SECONDS,
)
response.raise_for_status()

except httpx.HTTPStatusError as e:
response = e.response
if response.status_code in [401, 403]:
raise ValueError(
"Please, provide a valid API Key. You can create one at https://dev.agentql.com."
) from e
else:
try:
error_json = response.json()
msg = (
error_json["error_info"]
if "error_info" in error_json
else str(error_json)
)
except (ValueError, TypeError):
msg = f"HTTP {e}."
raise ValueError(msg) from e
else:
json = response.json()
return json["data"]
22 changes: 22 additions & 0 deletions crewai_tools/tools/agentql_query_data_tool/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from crewai_tools import AgentQLQueryDataTool

# Initialize the tool
query_tool = AgentQLQueryDataTool()

# Example with a simple query
# Note: Make sure you have set your AGENTQL_API_KEY in your environment variables
test_url = "https://scrapeme.live/?s=fish&post_type=product"
test_query = """
{
products[] {
product_name
price
}
}
"""

try:
result = query_tool._run(url=test_url, query=test_query)
print("Result:", result)
except Exception as e:
print("An error occurred:", str(e))
0