|
| 1 | +import json |
| 2 | + |
| 3 | +from pydantic import BaseModel, Field |
| 4 | +from scrapegraph_py.logger import sgai_logger |
| 5 | + |
| 6 | +from langchain_scrapegraph.tools import AgenticScraperTool |
| 7 | + |
| 8 | +sgai_logger.set_logging(level="INFO") |
| 9 | + |
| 10 | + |
| 11 | +# Define output schemas for different use cases |
| 12 | +class UserProfileInfo(BaseModel): |
| 13 | + username: str = Field(description="The user's username") |
| 14 | + email: str = Field(description="The user's email address") |
| 15 | + dashboard_sections: list[str] = Field(description="Available dashboard sections") |
| 16 | + available_settings: list[str] = Field(description="Available user settings") |
| 17 | + |
| 18 | + |
| 19 | +class ProductInfo(BaseModel): |
| 20 | + product_name: str = Field(description="The name of the product") |
| 21 | + price: str = Field(description="The price of the product") |
| 22 | + description: str = Field(description="Product description") |
| 23 | + availability: str = Field(description="Product availability status") |
| 24 | + rating: float = Field(description="Product rating out of 5") |
| 25 | + |
| 26 | + |
| 27 | +class LoginResult(BaseModel): |
| 28 | + success: bool = Field(description="Whether login was successful") |
| 29 | + error_message: str = Field(description="Error message if login failed", default="") |
| 30 | + redirect_url: str = Field(description="URL to redirect to after login", default="") |
| 31 | + |
| 32 | + |
| 33 | +# Initialize the tool with different schemas for different use cases |
| 34 | +print("=== Example 1: User Profile Extraction with Schema ===") |
| 35 | +tool_with_profile_schema = AgenticScraperTool(llm_output_schema=UserProfileInfo) |
| 36 | + |
| 37 | +dashboard_url = "https://dashboard.example.com" |
| 38 | +dashboard_steps = [ |
| 39 | + "Navigate to user profile section", |
| 40 | + "Click on settings tab", |
| 41 | + "Wait for page to load", |
| 42 | +] |
| 43 | + |
| 44 | +try: |
| 45 | + result = tool_with_profile_schema.invoke( |
| 46 | + { |
| 47 | + "url": dashboard_url, |
| 48 | + "steps": dashboard_steps, |
| 49 | + "ai_extraction": True, |
| 50 | + "user_prompt": "Extract user profile information and available dashboard sections and settings", |
| 51 | + "use_session": True, |
| 52 | + } |
| 53 | + ) |
| 54 | + print("User Profile Result:") |
| 55 | + print(json.dumps(result, indent=2)) |
| 56 | +except Exception as e: |
| 57 | + print(f"Error: {e}") |
| 58 | + |
| 59 | +print("\n" + "=" * 50 + "\n") |
| 60 | + |
| 61 | +print("=== Example 2: Product Information Extraction with Schema ===") |
| 62 | +tool_with_product_schema = AgenticScraperTool(llm_output_schema=ProductInfo) |
| 63 | + |
| 64 | +ecommerce_url = "https://shop.example.com" |
| 65 | +search_steps = [ |
| 66 | + "Type 'laptop' in search input box", |
| 67 | + "Click on search button", |
| 68 | + "Wait for results to load", |
| 69 | + "Click on first product", |
| 70 | +] |
| 71 | + |
| 72 | +try: |
| 73 | + result = tool_with_product_schema.invoke( |
| 74 | + { |
| 75 | + "url": ecommerce_url, |
| 76 | + "steps": search_steps, |
| 77 | + "ai_extraction": True, |
| 78 | + "user_prompt": "Extract product information including name, price, description, availability, and rating", |
| 79 | + "use_session": True, |
| 80 | + } |
| 81 | + ) |
| 82 | + print("Product Info Result:") |
| 83 | + print(json.dumps(result, indent=2)) |
| 84 | +except Exception as e: |
| 85 | + print(f"Error: {e}") |
| 86 | + |
| 87 | +print("\n" + "=" * 50 + "\n") |
| 88 | + |
| 89 | +print("=== Example 3: Login Process with Schema ===") |
| 90 | +tool_with_login_schema = AgenticScraperTool(llm_output_schema=LoginResult) |
| 91 | + |
| 92 | +login_url = "https://example.com/login" |
| 93 | +login_steps = [ |
| 94 | + "Type '[email protected]' in email input box", |
| 95 | + "Type 'password123' in password input box", |
| 96 | + "Click on login button", |
| 97 | + "Wait for response", |
| 98 | +] |
| 99 | + |
| 100 | +try: |
| 101 | + result = tool_with_login_schema.invoke( |
| 102 | + { |
| 103 | + "url": login_url, |
| 104 | + "steps": login_steps, |
| 105 | + "ai_extraction": True, |
| 106 | + "user_prompt": "Determine if login was successful and extract any error messages or redirect URLs", |
| 107 | + "use_session": True, |
| 108 | + } |
| 109 | + ) |
| 110 | + print("Login Result:") |
| 111 | + print(json.dumps(result, indent=2)) |
| 112 | +except Exception as e: |
| 113 | + print(f"Error: {e}") |
| 114 | + |
| 115 | +print("\n" + "=" * 50 + "\n") |
| 116 | + |
| 117 | +# Example 4: Using dictionary schema instead of Pydantic model |
| 118 | +print("=== Example 4: Dictionary Schema ===") |
| 119 | +tool_with_dict_schema = AgenticScraperTool() |
| 120 | + |
| 121 | +# Define schema as a dictionary |
| 122 | +news_schema = { |
| 123 | + "news_article": { |
| 124 | + "type": "object", |
| 125 | + "properties": { |
| 126 | + "headline": {"type": "string"}, |
| 127 | + "author": {"type": "string"}, |
| 128 | + "publish_date": {"type": "string"}, |
| 129 | + "content_summary": {"type": "string"}, |
| 130 | + "tags": {"type": "array", "items": {"type": "string"}}, |
| 131 | + }, |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +news_url = "https://news.example.com" |
| 136 | +news_steps = [ |
| 137 | + "Navigate to latest news section", |
| 138 | + "Click on first article", |
| 139 | + "Wait for page to load", |
| 140 | +] |
| 141 | + |
| 142 | +try: |
| 143 | + result = tool_with_dict_schema.invoke( |
| 144 | + { |
| 145 | + "url": news_url, |
| 146 | + "steps": news_steps, |
| 147 | + "ai_extraction": True, |
| 148 | + "user_prompt": "Extract article headline, author, publish date, content summary, and tags", |
| 149 | + "output_schema": news_schema, |
| 150 | + "use_session": True, |
| 151 | + } |
| 152 | + ) |
| 153 | + print("News Article Result:") |
| 154 | + print(json.dumps(result, indent=2)) |
| 155 | +except Exception as e: |
| 156 | + print(f"Error: {e}") |
0 commit comments