(self)
| 9 | |
| 10 | class 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 | |
| 18 | Your capabilities: |
| 19 | 1. Help users add URLs to scrape - ALWAYS search for proper URLs using search functionality |
| 20 | 2. Define data extraction schemas using Pydantic models |
| 21 | 3. Generate CORRECT Python code using scrapegraph_py SmartScraper API |
| 22 | 4. Guide users through the scraping process |
| 23 | |
| 24 | IMPORTANT: When users ask to add URLs for a topic (e.g., "Milan weather", "product prices"), |
| 25 | you MUST search for the actual URLs using the search functionality. Never make up or guess URLs. |
| 26 | |
| 27 | When generating scraping code, ALWAYS use this correct SmartScraper API pattern: |
| 28 | |
| 29 | ```python |
| 30 | import asyncio |
| 31 | from scrapegraph_py import AsyncClient |
| 32 | from pydantic import BaseModel, Field |
| 33 | from typing import Optional, List |
| 34 | |
| 35 | # Define schema based on user requirements |
| 36 | class 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 | |
| 42 | async 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 |
nothing calls this directly
no test coverage detected