From 1fe4df1eb13f611cb896a6cff2ef651f756d0079 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 22 Nov 2026 18:50:05 +0000 Subject: [PATCH 1/2] docs(examples): add local Ollama SmartScraperGraph example Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01B4sQj3sEyWi9Ke2aciLBFd --- local/ollama_smart_scraper.py | 37 +++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 local/ollama_smart_scraper.py diff --git a/local/ollama_smart_scraper.py b/local/ollama_smart_scraper.py new file mode 100644 index 00000000..54d76a1e --- /dev/null +++ b/local/ollama_smart_scraper.py @@ -0,0 +1,37 @@ +"""Free, local ScrapeGraphAI example: SmartScraperGraph + Ollama (no API keys).""" +import json + +from pydantic import BaseModel + +from scrapegraphai.graphs import SmartScraperGraph + +class Book(BaseModel): + title: str + price: str + + +class Books(BaseModel): + books: list[Book] + + +graph_config = { + "llm": { + "model": "ollama/llama3.2:3b", + "base_url": "http://127.0.0.1:11434", + "temperature": 0, + "format": "json", + "model_tokens": 8192, + "num_ctx": 8192, + }, + "headless": True, + "verbose": False, +} + +graph = SmartScraperGraph( + prompt="List every book on the page with its title and price.", + source="https://books.toscrape.com/", + config=graph_config, + schema=Books, +) + +print(json.dumps(graph.run(), indent=2)) From ec8f0e386443d83efbd64c296fd60c0b82371f80 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 22 Nov 2026 19:02:48 +0000 Subject: [PATCH 2/2] docs(examples): move Ollama example into examples/ with num_ctx Ollama's default context window truncates whole web pages, which can cut off the model's JSON answer. The example now lives with the other Ollama SmartScraper examples, sets num_ctx alongside model_tokens and uses a Pydantic schema, runnable fully locally. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01B4sQj3sEyWi9Ke2aciLBFd --- .../smart_scraper_schema_ollama_num_ctx.py | 69 +++++++++++++++++++ local/ollama_smart_scraper.py | 37 ---------- 2 files changed, 69 insertions(+), 37 deletions(-) create mode 100644 examples/smart_scraper_graph/ollama/smart_scraper_schema_ollama_num_ctx.py delete mode 100644 local/ollama_smart_scraper.py diff --git a/examples/smart_scraper_graph/ollama/smart_scraper_schema_ollama_num_ctx.py b/examples/smart_scraper_graph/ollama/smart_scraper_schema_ollama_num_ctx.py new file mode 100644 index 00000000..dc4ff362 --- /dev/null +++ b/examples/smart_scraper_graph/ollama/smart_scraper_schema_ollama_num_ctx.py @@ -0,0 +1,69 @@ +""" +SmartScraper with a schema on a local Ollama model, with a larger context window. + +Ollama runs models with a small context window by default, so a whole web page +gets truncated and the model's JSON answer can come back cut off. Setting +``num_ctx`` (and keeping ``model_tokens`` in line with it) fixes this. +Runs fully locally with no API keys: + + ollama pull llama3.2:3b +""" + +import json + +from pydantic import BaseModel, Field + +from scrapegraphai.graphs import SmartScraperGraph +from scrapegraphai.utils import prettify_exec_info + + +# ************************************************ +# Define the output schema +# ************************************************ +class Book(BaseModel): + title: str = Field(description="The title of the book") + price: str = Field(description="The price of the book, including currency") + + +class Books(BaseModel): + books: list[Book] + + +# ************************************************ +# Define the configuration for the graph +# ************************************************ + +graph_config = { + "llm": { + "model": "ollama/llama3.2:3b", + "temperature": 0, + "format": "json", + "base_url": "http://localhost:11434", + "model_tokens": 8192, + # Ollama's context window; without it the page is truncated + "num_ctx": 8192, + }, + "verbose": True, + "headless": True, +} + +# ************************************************ +# Create the SmartScraperGraph instance and run it +# ************************************************ + +smart_scraper_graph = SmartScraperGraph( + prompt="List every book on the page with its title and price.", + source="https://books.toscrape.com/", + schema=Books, + config=graph_config, +) + +result = smart_scraper_graph.run() +print(json.dumps(result, indent=4)) + +# ************************************************ +# Get graph execution info +# ************************************************ + +graph_exec_info = smart_scraper_graph.get_execution_info() +print(prettify_exec_info(graph_exec_info)) diff --git a/local/ollama_smart_scraper.py b/local/ollama_smart_scraper.py deleted file mode 100644 index 54d76a1e..00000000 --- a/local/ollama_smart_scraper.py +++ /dev/null @@ -1,37 +0,0 @@ -"""Free, local ScrapeGraphAI example: SmartScraperGraph + Ollama (no API keys).""" -import json - -from pydantic import BaseModel - -from scrapegraphai.graphs import SmartScraperGraph - -class Book(BaseModel): - title: str - price: str - - -class Books(BaseModel): - books: list[Book] - - -graph_config = { - "llm": { - "model": "ollama/llama3.2:3b", - "base_url": "http://127.0.0.1:11434", - "temperature": 0, - "format": "json", - "model_tokens": 8192, - "num_ctx": 8192, - }, - "headless": True, - "verbose": False, -} - -graph = SmartScraperGraph( - prompt="List every book on the page with its title and price.", - source="https://books.toscrape.com/", - config=graph_config, - schema=Books, -) - -print(json.dumps(graph.run(), indent=2))