Skip to content

Commit 51c1c36

Browse files
authored
feat: add new scrapegraph endpoint (#17709)
1 parent f938a1b commit 51c1c36

File tree

4 files changed

+71
-8
lines changed

4 files changed

+71
-8
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""Example of using scrapegraph search with LlamaIndex."""
2+
3+
from llama_index.core.agent import ReActAgent
4+
from llama_index.tools.scrapegraph import ScrapegraphToolSpec
5+
from llama_index.core.llms import OpenAI
6+
7+
# Initialize the tool spec with your API key
8+
SCRAPEGRAPH_API_KEY = "your-api-key-here" # Replace with your actual API key
9+
tool_spec = ScrapegraphToolSpec()
10+
11+
# Create a list of tools from the tool spec
12+
tools = tool_spec.to_tool_list()
13+
14+
# Initialize the LLM
15+
llm = OpenAI(temperature=0)
16+
17+
# Create an agent with the tools
18+
agent = ReActAgent.from_tools(tools, llm=llm, verbose=True)
19+
20+
21+
# Example queries to demonstrate usage
22+
def run_search_example():
23+
"""Run an example search query using the scrapegraph tool."""
24+
# Example 1: Basic search
25+
query = "What are the latest developments in artificial intelligence?"
26+
response = agent.chat(
27+
f"Use the scrapegraph search tool to find information about: {query}"
28+
)
29+
print("\nSearch Results:")
30+
print(f"Query: {query}")
31+
print(f"Response: {response}")
32+
33+
# Example 2: More specific search
34+
query = "What are the key features of Python 3.12?"
35+
response = agent.chat(
36+
f"Use the scrapegraph search tool to find detailed information about: {query}"
37+
)
38+
print("\nSearch Results:")
39+
print(f"Query: {query}")
40+
print(f"Response: {response}")
41+
42+
43+
if __name__ == "__main__":
44+
# Make sure to set your API keys in environment variables
45+
# os.environ["OPENAI_API_KEY"] = "your-openai-api-key"
46+
run_search_example()

llama-index-integrations/tools/llama-index-tools-scrapegraph/llama_index/tools/scrapegraph/base.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class ScrapegraphToolSpec(BaseToolSpec):
1414
spec_functions = [
1515
"scrapegraph_smartscraper",
1616
"scrapegraph_markdownify",
17-
"scrapegraph_local_scrape",
17+
"scrapegraph_search",
1818
]
1919

2020
def scrapegraph_smartscraper(
@@ -56,16 +56,15 @@ def scrapegraph_markdownify(self, url: str, api_key: str) -> str:
5656

5757
return client.markdownify(website_url=url)
5858

59-
def scrapegraph_local_scrape(self, text: str, api_key: str) -> str:
60-
"""Extract structured data from raw text using scrapegraph.
59+
def scrapegraph_search(self, query: str, api_key: str) -> str:
60+
"""Perform a search query using scrapegraph.
6161
6262
Args:
63-
text (str): Raw text to process and extract data from
63+
query (str): Search query to execute
6464
api_key (str): scrapegraph API key
6565
6666
Returns:
67-
str: Structured data extracted from the input text
67+
str: Search results from scrapegraph
6868
"""
6969
client = Client(api_key=api_key)
70-
71-
return client.local_scrape(text=text)
70+
return client.search(query=query)

llama-index-integrations/tools/llama-index-tools-scrapegraph/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ license = "MIT"
2929
maintainers = ["Vincigit00"]
3030
name = "llama-index-tools-scrapegraphai"
3131
readme = "README.md"
32-
version = "0.1.0"
32+
version = "0.1.1"
3333

3434
[tool.poetry.dependencies]
3535
python = ">=3.10,<4.0"

llama-index-integrations/tools/llama-index-tools-scrapegraph/tests/test_tools_scrapegraph.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,21 @@ def test_sync_scraping(tool_spec: ScrapegraphToolSpec, mock_sync_client: Mock):
5252
website_url=url, user_prompt=prompt, output_schema=schema
5353
)
5454
assert response == expected_response
55+
56+
57+
def test_search(tool_spec: ScrapegraphToolSpec, mock_sync_client: Mock):
58+
"""Test search functionality."""
59+
# Test data
60+
query = "test search query"
61+
api_key = "sgai-0000-0000-0000-0000-0000-0000-0000-0000"
62+
expected_response = "Search results"
63+
64+
# Configure mock
65+
mock_sync_client.search.return_value = expected_response
66+
67+
# Execute test
68+
response = tool_spec.scrapegraph_search(query=query, api_key=api_key)
69+
70+
# Verify
71+
mock_sync_client.search.assert_called_once_with(query=query)
72+
assert response == expected_response

0 commit comments

Comments
 (0)