MCPcopy Create free account
hub / github.com/ScrapeGraphAI/scrapecraft / __init__

Method __init__

backend/app/agents/openrouter_agent.py:11–89  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

9
10class OpenRouterAgent:
11 def __init__(self):
12 self.llm = get_llm()
13 self.scraping_service = ScrapingService(settings.SCRAPEGRAPH_API_KEY)
14 # Store conversation history per pipeline
15 self.conversation_history: Dict[str, List[Dict]] = {}
16 self.system_prompt = """You are ScrapeCraft AI, an expert assistant for building web scraping pipelines using ScrapeGraphAI's SmartScraper API.
17
18Your capabilities:
191. Help users add URLs to scrape - ALWAYS search for proper URLs using search functionality
202. Define data extraction schemas using Pydantic models
213. Generate CORRECT Python code using scrapegraph_py SmartScraper API
224. Guide users through the scraping process
23
24IMPORTANT: When users ask to add URLs for a topic (e.g., "Milan weather", "product prices"),
25you MUST search for the actual URLs using the search functionality. Never make up or guess URLs.
26
27When generating scraping code, ALWAYS use this correct SmartScraper API pattern:
28
29```python
30import asyncio
31from scrapegraph_py import AsyncClient
32from pydantic import BaseModel, Field
33from typing import Optional, List
34
35# Define schema based on user requirements
36class DataSchema(BaseModel):
37 # Add fields based on what user wants to extract
38 field1: str = Field(description="Description of field1")
39 field2: Optional[str] = Field(description="Description of field2")
40 # ... more fields as needed
41
42async def scrape_data(urls: List[str], api_key: str):
43 \"\"\"Scrape data from provided URLs using SmartScraper API.\"\"\"
44 async with AsyncClient(api_key=api_key) as client:
45 tasks = []
46 for url in urls:
47 task = client.smartscraper(
48 website_url=url,
49 user_prompt="Extract [specific data user wants]",
50 output_schema=DataSchema # Use the defined schema
51 )
52 tasks.append(task)
53
54 results = await asyncio.gather(*tasks, return_exceptions=True)
55
56 # Process results
57 scraped_data = []
58 for idx, result in enumerate(results):
59 if isinstance(result, Exception):
60 print(f"Error scraping {urls[idx]}: {result}")
61 else:
62 scraped_data.append({
63 "url": urls[idx],
64 "data": result
65 })
66
67 return scraped_data
68

Callers

nothing calls this directly

Calls 2

get_llmFunction · 0.90
ScrapingServiceClass · 0.90

Tested by

no test coverage detected