Skip to content

Commit 74a7a2b

Browse files
authored
Merge pull request #1126 from aayushbaluni/fix/1121-expose-model-tokens-fallback
fix(graph): expose when the 8192 token fallback was used
2 parents 27d9d28 + 470da9d commit 74a7a2b

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

scrapegraphai/graphs/abstract_graph.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ def __init__(
6464
self.source = source
6565
self.config = config
6666
self.schema = schema
67+
# Set to True when no token limit is known for the configured model and
68+
# the 8192 fallback is used; see _create_llm.
69+
self.model_tokens_defaulted = False
6770
self.llm_model = self._create_llm(config["llm"])
6871
self.verbose = False if config is None else config.get("verbose", False)
6972
self.headless = True if self.config is None else config.get("headless", True)
@@ -218,6 +221,12 @@ def _create_llm(self, llm_config: dict) -> object:
218221
llm_params["model"],
219222
)
220223
self.model_token = 8192
224+
# A silent 8192 window truncates long pages and changes the
225+
# answer without failing. The log line alone is not reachable
226+
# from the returned object, so callers that batch runs (or
227+
# capture stdout only) have no way to tell a real limit from
228+
# the fallback. Record it so it can be asserted on.
229+
self.model_tokens_defaulted = True
221230
else:
222231
self.model_token = llm_params["model_tokens"]
223232

tests/graphs/abstract_graph_test.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,61 @@ def test_llm_missing_tokens(monkeypatch, capsys):
3232
assert "Max input tokens for model" in captured
3333

3434

35+
def test_llm_missing_tokens_sets_defaulted_flag(monkeypatch):
36+
"""An unknown model must record that the 8192 window is a fallback.
37+
38+
The warning is only a log line, so a caller reading ``model_token`` sees
39+
8192 and cannot tell whether that is the model's real limit or the
40+
default. A silent 8192 window truncates long pages and changes the answer
41+
without failing, which is the failure mode reported in #1121.
42+
"""
43+
from scrapegraphai.graphs import abstract_graph
44+
45+
monkeypatch.setattr(
46+
abstract_graph, "models_tokens", {"openai": {"gpt-3.5-turbo": 4096}}
47+
)
48+
llm_config = {"model": "openai/not-known-model", "openai_api_key": "test"}
49+
with patch.object(TestGraph, "_create_graph", return_value=Mock(nodes=[])):
50+
graph = TestGraph("Test prompt", {"llm": llm_config})
51+
52+
assert graph.model_token == 8192
53+
assert graph.model_tokens_defaulted is True
54+
55+
56+
def test_known_model_does_not_set_defaulted_flag(monkeypatch):
57+
"""A model with a known limit must not be flagged as defaulted."""
58+
from scrapegraphai.graphs import abstract_graph
59+
60+
monkeypatch.setattr(
61+
abstract_graph, "models_tokens", {"openai": {"gpt-3.5-turbo": 4096}}
62+
)
63+
llm_config = {"model": "openai/gpt-3.5-turbo", "openai_api_key": "test"}
64+
with patch.object(TestGraph, "_create_graph", return_value=Mock(nodes=[])):
65+
graph = TestGraph("Test prompt", {"llm": llm_config})
66+
67+
assert graph.model_token == 4096
68+
assert graph.model_tokens_defaulted is False
69+
70+
71+
def test_explicit_model_tokens_does_not_set_defaulted_flag(monkeypatch):
72+
"""An explicit model_tokens is authoritative, not a fallback."""
73+
from scrapegraphai.graphs import abstract_graph
74+
75+
monkeypatch.setattr(
76+
abstract_graph, "models_tokens", {"openai": {"gpt-3.5-turbo": 4096}}
77+
)
78+
llm_config = {
79+
"model": "openai/not-known-model",
80+
"openai_api_key": "test",
81+
"model_tokens": 1000000,
82+
}
83+
with patch.object(TestGraph, "_create_graph", return_value=Mock(nodes=[])):
84+
graph = TestGraph("Test prompt", {"llm": llm_config})
85+
86+
assert graph.model_token == 1000000
87+
assert graph.model_tokens_defaulted is False
88+
89+
3590
def test_burr_kwargs():
3691
"""Test that burr_kwargs configuration correctly sets use_burr and burr_config on the graph."""
3792
dummy_graph = Mock()

0 commit comments

Comments
 (0)