From 119ad84e2dc02f6818755a67b6e9474dec14ff48 Mon Sep 17 00:00:00 2001 From: Ilya Kislenko Date: Tue, 14 Jul 2026 01:29:17 +0000 Subject: [PATCH] feat: add Nemotron reasoning model support with configurable trigger - Add nemotron_reasoning_models and nemotron_reasoning_trigger config options - Add NEMOTRON_REASONING_MODELS constant in pr_agent/algo/__init__.py - Add _apply_nemotron_trigger() helper in LiteLLMAIHandler - Apply trigger on normal path (before user_message_only/custom_reasoning_model) - Add real unit tests in tests/unittest/test_nemotron_reasoning.py --- pr_agent/algo/__init__.py | 8 ++ .../algo/ai_handlers/litellm_ai_handler.py | 25 ++++ pr_agent/settings/configuration.toml | 5 + tests/unittest/test_nemotron_reasoning.py | 116 ++++++++++++++++++ 4 files changed, 154 insertions(+) create mode 100644 tests/unittest/test_nemotron_reasoning.py diff --git a/pr_agent/algo/__init__.py b/pr_agent/algo/__init__.py index c1bbb28fa0..eecf16e491 100644 --- a/pr_agent/algo/__init__.py +++ b/pr_agent/algo/__init__.py @@ -428,3 +428,11 @@ STREAMING_REQUIRED_MODELS = [ "openai/qwq-plus" ] + +# Nemotron reasoning models that require a system prompt trigger to enable reasoning. +# These models (e.g., Nemotron 3 Ultra via LiteLLM) only activate reasoning when the +# system message begins with a specific trigger phrase. +NEMOTRON_REASONING_MODELS = [ + "openai/large", + "openai/large-reviewer", +] diff --git a/pr_agent/algo/ai_handlers/litellm_ai_handler.py b/pr_agent/algo/ai_handlers/litellm_ai_handler.py index 6a306bc90a..b881ecf985 100644 --- a/pr_agent/algo/ai_handlers/litellm_ai_handler.py +++ b/pr_agent/algo/ai_handlers/litellm_ai_handler.py @@ -11,6 +11,7 @@ retry_if_not_exception_type, stop_after_attempt) from pr_agent.algo import (CLAUDE_EXTENDED_THINKING_MODELS, + NEMOTRON_REASONING_MODELS, NO_SUPPORT_TEMPERATURE_MODELS, STREAMING_REQUIRED_MODELS, SUPPORT_REASONING_EFFORT_MODELS, @@ -357,6 +358,28 @@ def _configure_claude_extended_thinking(self, model: str, kwargs: dict) -> dict: return kwargs + def _apply_nemotron_trigger(self, model: str, system: str) -> str: + """ + Apply Nemotron reasoning trigger to system prompt if model is a Nemotron reasoning model. + + Nemotron models (e.g., Nemotron 3 Ultra via LiteLLM names "openai/large", + "openai/large-reviewer") only enable reasoning when the system message + starts with a specific trigger phrase. + + Args: + model (str): The AI model being used + system (str): The system prompt + + Returns: + str: System prompt with trigger prepended if applicable + """ + trigger = get_settings().config.nemotron_reasoning_trigger + if system is None: + system = "" + if model in get_settings().config.nemotron_reasoning_models and not system.startswith(trigger): + return f"{trigger}\n\n{system}" + return system + def add_litellm_callbacks(self, kwargs) -> dict: captured_extra = [] @@ -506,6 +529,8 @@ async def chat_completion(self, model: str, system: str, user: str, temperature: provider_prefix = 'openai/' model = provider_prefix + model_base.replace('_thinking', '') # remove _thinking suffix + # Apply Nemotron reasoning trigger on the normal path (not user_message_only/custom_reasoning_model) + system = self._apply_nemotron_trigger(model, system) # Currently, some models do not support a separate system and user prompts if model in self.user_message_only_models or get_settings().config.custom_reasoning_model: diff --git a/pr_agent/settings/configuration.toml b/pr_agent/settings/configuration.toml index 1309e27b34..c696499814 100644 --- a/pr_agent/settings/configuration.toml +++ b/pr_agent/settings/configuration.toml @@ -72,6 +72,11 @@ extended_thinking_max_output_tokens = 4096 # When non-empty, this list fully replaces the built-in defaults (see CLAUDE_EXTENDED_THINKING_MODELS # in pr_agent/algo/__init__.py). Leave empty to use the defaults. claude_extended_thinking_models_override = [] + +# Nemotron reasoning models (LiteLLM model names) that need a system prompt trigger +# to enable reasoning mode. The trigger text is prepended to the system prompt. +nemotron_reasoning_models = ["openai/large", "openai/large-reviewer"] +nemotron_reasoning_trigger = "detailed thinking on" # Extract issue number from PR source branch name (e.g. feature/1-auth-google -> issue #1). When true, branch-derived # issue URLs are merged with tickets from the PR description for compliance. Set to false to restore description-only behaviour. # Note: Branch-name extraction is GitHub-only for now; other providers planned for later. diff --git a/tests/unittest/test_nemotron_reasoning.py b/tests/unittest/test_nemotron_reasoning.py new file mode 100644 index 0000000000..9383d3dad6 --- /dev/null +++ b/tests/unittest/test_nemotron_reasoning.py @@ -0,0 +1,116 @@ +from types import SimpleNamespace +from unittest.mock import MagicMock, patch + +import pytest + +import pr_agent.algo.ai_handlers.litellm_ai_handler as litellm_handler +from pr_agent.algo import NEMOTRON_REASONING_MODELS + + +def create_mock_settings(models=None, trigger="detailed thinking on"): + """Create a fake settings object with configurable Nemotron settings.""" + if models is None: + models = ["openai/large", "openai/large-reviewer"] + return type('', (), { + 'config': type('', (), { + 'nemotron_reasoning_models': models, + 'nemotron_reasoning_trigger': trigger, + 'verbosity_level': 0, + 'get': lambda self, key, default=None: default + })(), + 'litellm': type('', (), { + 'get': lambda self, key, default=None: default + })(), + 'get': lambda self, key, default=None: default + })() + + +@pytest.fixture +def mock_logger(): + """Mock logger to capture info and warning calls.""" + with patch("pr_agent.algo.ai_handlers.litellm_ai_handler.get_logger") as mock_log: + mock_log_instance = MagicMock() + mock_log.return_value = mock_log_instance + yield mock_log_instance + + +def test_nemotron_trigger_applies_to_configured_model(mock_logger): + """Test that trigger is prepended for configured Nemotron models.""" + fake_settings = create_mock_settings() + with patch("pr_agent.algo.ai_handlers.litellm_ai_handler.get_settings", lambda: fake_settings): + handler = litellm_handler.LiteLLMAIHandler() + result = handler._apply_nemotron_trigger("openai/large", "ORIGINAL") + + assert result.startswith("detailed thinking on") + assert "ORIGINAL" in result + assert result.count("detailed thinking on") == 1 + + +def test_nemotron_trigger_does_not_apply_to_unconfigured_model(mock_logger): + """Test that trigger is NOT applied for models not in the configured list.""" + fake_settings = create_mock_settings() + with patch("pr_agent.algo.ai_handlers.litellm_ai_handler.get_settings", lambda: fake_settings): + handler = litellm_handler.LiteLLMAIHandler() + result = handler._apply_nemotron_trigger("openai/coder", "ORIGINAL") + + assert result == "ORIGINAL" + + +def test_nemotron_trigger_idempotent(mock_logger): + """Test that applying trigger twice doesn't duplicate it.""" + fake_settings = create_mock_settings() + with patch("pr_agent.algo.ai_handlers.litellm_ai_handler.get_settings", lambda: fake_settings): + handler = litellm_handler.LiteLLMAIHandler() + # Apply once + result1 = handler._apply_nemotron_trigger("openai/large", "ORIGINAL") + # Apply again + result2 = handler._apply_nemotron_trigger("openai/large", result1) + + assert result1 == result2 + assert result2.count("detailed thinking on") == 1 + + +def test_nemotron_trigger_respects_custom_trigger_text(mock_logger): + """Test that custom trigger text from config is used.""" + fake_settings = create_mock_settings(trigger="custom trigger") + with patch("pr_agent.algo.ai_handlers.litellm_ai_handler.get_settings", lambda: fake_settings): + handler = litellm_handler.LiteLLMAIHandler() + result = handler._apply_nemotron_trigger("openai/large", "ORIGINAL") + + assert result.startswith("custom trigger") + assert "ORIGINAL" in result + + +def test_nemotron_trigger_respects_custom_model_list(mock_logger): + """Test that custom model list from config is used.""" + custom_models = ["custom/nemotron-model"] + fake_settings = create_mock_settings(models=custom_models) + with patch("pr_agent.algo.ai_handlers.litellm_ai_handler.get_settings", lambda: fake_settings): + handler = litellm_handler.LiteLLMAIHandler() + # Model in custom list should get trigger + result = handler._apply_nemotron_trigger("custom/nemotron-model", "ORIGINAL") + assert result.startswith("detailed thinking on") + # Default model should NOT get trigger with custom config + result2 = handler._apply_nemotron_trigger("openai/large", "ORIGINAL") + assert result2 == "ORIGINAL" + + +def test_nemotron_trigger_handles_empty_system_prompt(mock_logger): + """Test that trigger works with empty system prompt.""" + fake_settings = create_mock_settings() + with patch("pr_agent.algo.ai_handlers.litellm_ai_handler.get_settings", lambda: fake_settings): + handler = litellm_handler.LiteLLMAIHandler() + result = handler._apply_nemotron_trigger("openai/large", "") + + assert result == "detailed thinking on\n\n" + + +def test_nemotron_trigger_handles_none_system_prompt(mock_logger): + """Test that trigger handles None system prompt gracefully.""" + fake_settings = create_mock_settings() + with patch("pr_agent.algo.ai_handlers.litellm_ai_handler.get_settings", lambda: fake_settings): + handler = litellm_handler.LiteLLMAIHandler() + # None should be treated as empty string + result = handler._apply_nemotron_trigger("openai/large", None) + + assert result.startswith("detailed thinking on") \ No newline at end of file