diff --git a/graphify/llm.py b/graphify/llm.py index ddac2e1..b4237c2 100644 --- a/graphify/llm.py +++ b/graphify/llm.py @@ -279,6 +279,21 @@ def _call_openai_compat( # Kimi-k2.6 is a reasoning model — disable thinking so content isn't empty if "moonshot" in base_url: kwargs["extra_body"] = {"thinking": {"type": "disabled"}} + # Ollama defaults num_ctx to 2048 and silently truncates prompts larger + # than that — the symptom is hollow 200 OK responses after the first few + # chunks (#798). We send num_ctx large enough to fit our default 60k-token + # chunk budget plus system prompt and JSON output headroom. Ollama caps + # gracefully at the model's built-in limit if it's lower, so a large + # default is safe. keep_alive pins the model in VRAM across chunks so it + # isn't unloaded/reloaded mid-run under concurrency pressure. + if backend == "ollama": + num_ctx_raw = os.environ.get("GRAPHIFY_OLLAMA_NUM_CTX", "").strip() + try: + num_ctx = int(num_ctx_raw) if num_ctx_raw else 131072 + except ValueError: + num_ctx = 131072 + keep_alive = os.environ.get("GRAPHIFY_OLLAMA_KEEP_ALIVE", "30m") + kwargs["extra_body"] = {"options": {"num_ctx": num_ctx}, "keep_alive": keep_alive} resp = client.chat.completions.create(**kwargs) raw_content = resp.choices[0].message.content result = _parse_llm_json(raw_content or "{}") @@ -742,6 +757,11 @@ def extract_corpus_parallel( except Exception as exc: # noqa: BLE001 — caller-facing surface, log + continue return idx, None, exc + # Ollama serves one request at a time per loaded model on a single GPU. + # Four concurrent 60k-token requests cause VRAM pressure and hollow + # responses after 3-4 chunks (#798). Force serial unless the user opts in. + if backend == "ollama" and os.environ.get("GRAPHIFY_OLLAMA_PARALLEL", "").strip() != "1": + max_concurrency = 1 workers = max(1, min(max_concurrency, total)) if workers == 1: # Avoid thread pool overhead for single-worker runs (and keep diff --git a/tests/test_llm_backends.py b/tests/test_llm_backends.py index cd83853..ec7f7a8 100644 --- a/tests/test_llm_backends.py +++ b/tests/test_llm_backends.py @@ -325,6 +325,131 @@ def test_call_openai_compat_preserves_real_finish_reason(monkeypatch): assert result["nodes"] == [{"id": "a"}] +# --------------------------------------------------------------------------- +# Ollama context-window fix (#798): num_ctx + keep_alive in extra_body, +# serial execution by default. +# --------------------------------------------------------------------------- + + +def _install_capturing_openai(monkeypatch): + """Like _install_fake_openai but records kwargs passed to create().""" + import sys + import types + + captured = {} + + class _FakeOpenAI: + def __init__(self, *_, **__): + self.chat = self + self.completions = self + + def create(self, **kwargs): + captured.update(kwargs) + return _fake_openai_response( + '{"nodes":[{"id":"x"}],"edges":[],"hyperedges":[]}', + finish_reason="stop", + completion_tokens=100, + ) + + fake_module = types.ModuleType("openai") + fake_module.OpenAI = _FakeOpenAI + monkeypatch.setitem(sys.modules, "openai", fake_module) + return captured + + +def test_ollama_extra_body_sets_num_ctx_and_keep_alive(monkeypatch): + captured = _install_capturing_openai(monkeypatch) + monkeypatch.delenv("GRAPHIFY_OLLAMA_NUM_CTX", raising=False) + monkeypatch.delenv("GRAPHIFY_OLLAMA_KEEP_ALIVE", raising=False) + + llm._call_openai_compat( + "http://localhost:11434/v1", "ollama", "qwen2.5-coder:7b", + "user msg", temperature=0, max_completion_tokens=8192, backend="ollama", + ) + + assert "extra_body" in captured, "extra_body must be sent to Ollama" + eb = captured["extra_body"] + assert eb.get("options", {}).get("num_ctx") == 131072, "default num_ctx must be 131072" + assert eb.get("keep_alive") == "30m", "default keep_alive must be 30m" + + +def test_ollama_num_ctx_env_override(monkeypatch): + captured = _install_capturing_openai(monkeypatch) + monkeypatch.setenv("GRAPHIFY_OLLAMA_NUM_CTX", "65536") + monkeypatch.delenv("GRAPHIFY_OLLAMA_KEEP_ALIVE", raising=False) + + llm._call_openai_compat( + "http://localhost:11434/v1", "ollama", "qwen2.5-coder:7b", + "u", temperature=0, max_completion_tokens=8192, backend="ollama", + ) + + assert captured["extra_body"]["options"]["num_ctx"] == 65536 + + +def test_non_ollama_backend_gets_no_num_ctx_extra_body(monkeypatch): + captured = _install_capturing_openai(monkeypatch) + + llm._call_openai_compat( + "https://api.openai.com/v1", "sk-test", "gpt-4.1-mini", + "u", temperature=0, max_completion_tokens=8192, backend="openai", + ) + + eb = captured.get("extra_body") + assert eb is None or "options" not in eb, "non-ollama backends must not get num_ctx injection" + + +def test_extract_corpus_parallel_ollama_runs_serially(tmp_path, monkeypatch): + # With 3 chunks and backend=ollama, ThreadPoolExecutor must NOT be used + # (workers=1 takes the sequential path). We verify by ensuring all chunks + # are processed and no pool is spun up. + files = [tmp_path / f"f{i}.md" for i in range(6)] + for f in files: + f.write_text("hello") + + call_order = [] + + def fake_extract(chunk, *_, **__): + call_order.append(len(chunk)) + return _ok(nodes=[{"id": f.stem} for f in chunk]) + + monkeypatch.delenv("GRAPHIFY_OLLAMA_PARALLEL", raising=False) + + with patch("graphify.llm.extract_files_direct", side_effect=fake_extract): + with patch("graphify.llm.ThreadPoolExecutor") as mock_pool: + result = llm.extract_corpus_parallel( + files, backend="ollama", api_key="ollama", model="qwen2.5-coder:7b", + root=tmp_path, token_budget=None, chunk_size=2, max_concurrency=4, + ) + + mock_pool.assert_not_called() + assert len(result["nodes"]) == 6 + + +def test_extract_corpus_parallel_ollama_parallel_env_restores_concurrency(tmp_path, monkeypatch): + files = [tmp_path / f"f{i}.md" for i in range(4)] + for f in files: + f.write_text("hello") + + monkeypatch.setenv("GRAPHIFY_OLLAMA_PARALLEL", "1") + + with patch("graphify.llm.extract_files_direct", return_value=_ok()): + with patch("graphify.llm.ThreadPoolExecutor") as mock_pool: + mock_pool.return_value.__enter__ = lambda s: s + mock_pool.return_value.__exit__ = lambda s, *a: False + mock_pool.return_value.submit = lambda fn, *a, **kw: type( + "F", (), {"result": lambda self: fn(*a, **kw)} + )() + try: + llm.extract_corpus_parallel( + files, backend="ollama", api_key="ollama", model="m", + root=tmp_path, token_budget=None, chunk_size=2, max_concurrency=4, + ) + except Exception: + pass # mock scaffolding may not be complete; we only care about the call + + mock_pool.assert_called() + + def test_adaptive_retry_bisects_on_hollow_ollama_response(tmp_path): # End-to-end: an overwhelmed Ollama returns hollow on the full 4-file # chunk; halves succeed. The bug being fixed is that pre-fix this