diff --git a/examples/smart_scraper_graph/teamorouter/smart_scraper_teamorouter.py b/examples/smart_scraper_graph/teamorouter/smart_scraper_teamorouter.py new file mode 100644 index 000000000..083cc120d --- /dev/null +++ b/examples/smart_scraper_graph/teamorouter/smart_scraper_teamorouter.py @@ -0,0 +1,48 @@ +""" +Basic example of scraping pipeline using SmartScraper with TeamoRouter +""" + +import json +import os + +from dotenv import load_dotenv + +from scrapegraphai.graphs import SmartScraperGraph +from scrapegraphai.utils import prettify_exec_info + +load_dotenv() + +# ************************************************ +# Define the configuration for the graph +# ************************************************ + +graph_config = { + "llm": { + "api_key": os.getenv("TEAMOROUTER_API_KEY"), + # Any chat model id from https://api.teamorouter.com/v1/models, + # prefixed with "teamorouter/". + "model": "teamorouter/deepseek-v4-flash", + }, + "verbose": True, + "headless": True, +} + +# ************************************************ +# Create the SmartScraperGraph instance and run it +# ************************************************ + +smart_scraper_graph = SmartScraperGraph( + prompt="List the first 5 books with their title and price", + source="https://books.toscrape.com/", + 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/scrapegraphai/graphs/abstract_graph.py b/scrapegraphai/graphs/abstract_graph.py index 21f90460c..1104b07a5 100644 --- a/scrapegraphai/graphs/abstract_graph.py +++ b/scrapegraphai/graphs/abstract_graph.py @@ -13,7 +13,7 @@ from pydantic import BaseModel from ..helpers import models_tokens -from ..models import XAI, CLoD, DeepSeek, MiniMax, Nvidia, OneApi +from ..models import XAI, CLoD, DeepSeek, MiniMax, Nvidia, OneApi, TeamoRouter from ..utils.logging import get_logger, set_verbosity_info, set_verbosity_warning logger = get_logger(__name__) @@ -172,6 +172,7 @@ def _create_llm(self, llm_config: dict) -> object: "ernie", "fireworks", "clod", + "teamorouter", "togetherai", "xai", "minimax", @@ -241,6 +242,7 @@ def _create_llm(self, llm_config: dict) -> object: "deepseek", "togetherai", "clod", + "teamorouter", "xai", "minimax", }: @@ -257,6 +259,9 @@ def _create_llm(self, llm_config: dict) -> object: if model_provider == "clod": return CLoD(**llm_params) + if model_provider == "teamorouter": + return TeamoRouter(**llm_params) + if model_provider == "deepseek": return DeepSeek(**llm_params) diff --git a/scrapegraphai/helpers/models_tokens.py b/scrapegraphai/helpers/models_tokens.py index 5a81ecaa7..5cbcc1ed1 100644 --- a/scrapegraphai/helpers/models_tokens.py +++ b/scrapegraphai/helpers/models_tokens.py @@ -352,6 +352,14 @@ "gpt-5.4-mini": 400000, "gpt-5.4": 1000000, }, + "teamorouter": { + "deepseek-v4-flash": 1000000, + "deepseek-v4-pro": 1000000, + "qwen3.8-flash": 1000000, + "qwen3.8-max": 1000000, + "kimi-k3": 1000000, + "glm-5.3": 1000000, + }, "deepseek": { "deepseek-chat": 128000, "deepseek-coder": 128000, diff --git a/scrapegraphai/models/__init__.py b/scrapegraphai/models/__init__.py index db01cd2e4..d7c1e9a3a 100644 --- a/scrapegraphai/models/__init__.py +++ b/scrapegraphai/models/__init__.py @@ -11,6 +11,7 @@ from .oneapi import OneApi from .openai_itt import OpenAIImageToText from .openai_tts import OpenAITextToSpeech +from .teamorouter import TeamoRouter from .xai import XAI -__all__ = ["AtlasCloud", "CheaperInference", "DeepSeek", "MiniMax", "OneApi", "OpenAIImageToText", "OpenAITextToSpeech", "CLoD", "XAI", "Nvidia"] +__all__ = ["AtlasCloud", "CheaperInference", "DeepSeek", "MiniMax", "OneApi", "OpenAIImageToText", "OpenAITextToSpeech", "TeamoRouter", "CLoD", "XAI", "Nvidia"] diff --git a/scrapegraphai/models/teamorouter.py b/scrapegraphai/models/teamorouter.py new file mode 100644 index 000000000..2dbe3f061 --- /dev/null +++ b/scrapegraphai/models/teamorouter.py @@ -0,0 +1,34 @@ +""" +TeamoRouter Module +""" + +from langchain_openai import ChatOpenAI + +TEAMOROUTER_API_BASE = "https://api.teamorouter.com/v1" +TEAMOROUTER_CLIENT_SOURCE = "scrapegraphai-teamorouter" + + +class TeamoRouter(ChatOpenAI): + """ + A wrapper for ChatOpenAI configured for TeamoRouter's OpenAI-compatible + API (https://teamorouter.com), which serves DeepSeek, Qwen, Kimi, GLM and + other models behind one API key. + + Requests carry an ``X-Client-Source`` header so TeamoRouter can attribute + traffic to ScrapeGraphAI; headers passed in ``default_headers`` take + precedence. + + Args: + llm_config (dict): Configuration parameters for the language model. + """ + + def __init__(self, **llm_config): + if "api_key" in llm_config: + llm_config["openai_api_key"] = llm_config.pop("api_key") + llm_config["openai_api_base"] = TEAMOROUTER_API_BASE + llm_config["default_headers"] = { + "X-Client-Source": TEAMOROUTER_CLIENT_SOURCE, + **(llm_config.get("default_headers") or {}), + } + + super().__init__(**llm_config) diff --git a/tests/test_teamorouter_model.py b/tests/test_teamorouter_model.py new file mode 100644 index 000000000..44281e42d --- /dev/null +++ b/tests/test_teamorouter_model.py @@ -0,0 +1,47 @@ +"""Tests for TeamoRouter model configuration.""" + +import importlib.util +import os + + +def _load(name, *parts): + spec = importlib.util.spec_from_file_location( + name, os.path.join(os.path.dirname(__file__), "..", "scrapegraphai", *parts) + ) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +def test_teamorouter_model_sets_openai_compatible_base_url(): + """TeamoRouter should map api_key, set its base URL and tag requests.""" + module = _load("teamorouter", "models", "teamorouter.py") + + model = module.TeamoRouter(api_key="test-key", model="deepseek-v4-flash") + + assert str(model.openai_api_base).rstrip("/") == "https://api.teamorouter.com/v1" + assert model.openai_api_key.get_secret_value() == "test-key" + assert model.default_headers["X-Client-Source"] == "scrapegraphai-teamorouter" + + +def test_teamorouter_model_keeps_user_headers(): + """Headers passed by the caller are kept and can override the default tag.""" + module = _load("teamorouter", "models", "teamorouter.py") + + model = module.TeamoRouter( + api_key="test-key", + model="deepseek-v4-flash", + default_headers={"X-Client-Source": "my-app", "X-Trace": "1"}, + ) + + assert model.default_headers["X-Client-Source"] == "my-app" + assert model.default_headers["X-Trace"] == "1" + + +def test_teamorouter_models_in_token_list(): + """TeamoRouter defaults should be listed with their context lengths.""" + module = _load("models_tokens", "helpers", "models_tokens.py") + + teamorouter_models = module.models_tokens["teamorouter"] + assert teamorouter_models["deepseek-v4-flash"] == 1000000 + assert teamorouter_models["qwen3.8-flash"] == 1000000