Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pr_agent/algo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
25 changes: 25 additions & 0 deletions pr_agent/algo/ai_handlers/litellm_ai_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 = []

Expand Down Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions pr_agent/settings/configuration.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
116 changes: 116 additions & 0 deletions tests/unittest/test_nemotron_reasoning.py
Original file line number Diff line number Diff line change
@@ -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")