Skip to content

armgit / kong

Public Imported from GitHub
0

The world's first agentic reverse engineer.

fix: probe respects base_url, use dummy api_key for local servers

Amruth41081fdce3f2
diff --git a/kong/__main__.py b/kong/__main__.py
index 06fdf81..54ddb3b 100644
--- a/kong/__main__.py
+++ b/kong/__main__.py
@@ -51,6 +51,7 @@ _PROVIDER_LABELS: dict[LLMProvider, str] = {
LLMProvider.CUSTOM: "Custom (OpenAI-compatible)",
}
+_NOT_NEEDED_STR = "not-needed"
def create_llm_client(config: LLMConfig) -> LLMClient:
"""Instantiate the appropriate LLM client based on provider config."""
@@ -58,7 +59,10 @@ def create_llm_client(config: LLMConfig) -> LLMClient:
model = config.model or _DEFAULT_MODELS.get(config.provider, "gpt-4o")
if config.provider is LLMProvider.CUSTOM:
- api_key = config.api_key if config.api_key else ""
+ # Local servers don't need auth, but the OpenAI SDK rejects None/empty
+ # api_key by falling back to OPENAI_API_KEY env var or raising an error.
+ # A dummy value satisfies the SDK while local servers ignore it.
+ api_key = config.api_key if config.api_key else _NOT_NEEDED_STR
register_custom_model(model)
return OpenAIClient(
model=model,
diff --git a/kong/llm/probe.py b/kong/llm/probe.py
index c008ee5..1bb5e96 100644
--- a/kong/llm/probe.py
+++ b/kong/llm/probe.py
@@ -43,7 +43,7 @@ def _probe_custom(config: LLMConfig) -> bool:
def _probe_openai(config: LLMConfig) -> bool:
try:
- client = openai.OpenAI(api_key=config.api_key)
+ client = openai.OpenAI(api_key=config.api_key, base_url=config.base_url)
client.models.list()
return True
except openai.AuthenticationError:
diff --git a/tests/test_cli.py b/tests/test_cli.py
index 15bf094..8ec0787 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -127,7 +127,7 @@ class TestCreateLLMClient:
client = create_llm_client(config)
assert isinstance(client, OpenAIClient)
mock_openai_cls.assert_called_once_with(
- api_key="",
+ api_key="not-needed",
base_url="http://localhost:11434/v1",
max_retries=5,
)
diff --git a/tests/test_probe.py b/tests/test_probe.py
index 01799ec..7a40bad 100644
--- a/tests/test_probe.py
+++ b/tests/test_probe.py
@@ -63,6 +63,23 @@ class TestProbeOpenAI:
mock_client.models.list.return_value = MagicMock()
config = LLMConfig(provider=LLMProvider.OPENAI, api_key="sk-test")
assert probe_endpoint(config) is True
+ mock_openai_cls.assert_called_once_with(api_key="sk-test", base_url=None)
+
+ @patch("kong.llm.probe.openai.OpenAI")
+ def test_openai_probe_passes_base_url(self, mock_openai_cls):
+ mock_client = MagicMock()
+ mock_openai_cls.return_value = mock_client
+ mock_client.models.list.return_value = MagicMock()
+ config = LLMConfig(
+ provider=LLMProvider.OPENAI,
+ api_key="sk-test",
+ base_url="https://my-proxy.example.com/v1",
+ )
+ assert probe_endpoint(config) is True
+ mock_openai_cls.assert_called_once_with(
+ api_key="sk-test",
+ base_url="https://my-proxy.example.com/v1",
+ )
@patch("kong.llm.probe.openai.OpenAI")
def test_openai_probe_auth_failure(self, mock_openai_cls):