The world's first agentic reverse engineer.
feat: update create_llm_client factory for CUSTOM provider
Amruth22d51b7beddf
diff --git a/kong/__main__.py b/kong/__main__.pyindex fe70bde..568863d 100644--- a/kong/__main__.py+++ b/kong/__main__.py@@ -48,12 +48,23 @@ _DEFAULT_MODELS: dict[LLMProvider, str] = {_PROVIDER_LABELS: dict[LLMProvider, str] = {LLMProvider.ANTHROPIC: "Anthropic (Claude)",LLMProvider.OPENAI: "OpenAI (GPT-4o)",+ LLMProvider.CUSTOM: "Custom (OpenAI-compatible)",}def create_llm_client(config: LLMConfig) -> LLMClient:"""Instantiate the appropriate LLM client based on provider config."""- model = config.model or _DEFAULT_MODELS[config.provider]+ from kong.llm.usage import register_custom_model++ 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 ""+ register_custom_model(model)+ return OpenAIClient(+ model=model,+ base_url=config.base_url,+ api_key=api_key,+ )if config.provider is LLMProvider.OPENAI:return OpenAIClient(model=model, api_key=config.api_key)return AnthropicClient(model=model, api_key=config.api_key)diff --git a/tests/test_cli.py b/tests/test_cli.pyindex 34ca221..9fcb2c2 100644--- a/tests/test_cli.py+++ b/tests/test_cli.py@@ -2,12 +2,12 @@from __future__ import annotations-from unittest.mock import patch+from unittest.mock import MagicMock, patchfrom click.testing import CliRunner-from kong.__main__ import cli-from kong.config import LLMProvider+from kong.__main__ import cli, create_llm_client+from kong.config import LLMConfig, LLMProviderfrom kong.db import save_setup@@ -79,3 +79,61 @@ def test_eval_with_test_data(tmp_path):assert result.exit_code == 0assert "hash_string" in result.outputassert "Symbol Accuracy" in result.output+++class TestCreateLLMClient:+ @patch("kong.llm.openai_client.openai.OpenAI")+ def test_custom_returns_openai_client_with_base_url(self, mock_openai_cls):+ from kong.llm.openai_client import OpenAIClient++ config = LLMConfig(+ provider=LLMProvider.CUSTOM,+ model="llama3:8b",+ base_url="http://localhost:11434/v1",+ api_key="test-key",+ )+ client = create_llm_client(config)+ assert isinstance(client, OpenAIClient)+ mock_openai_cls.assert_called_once_with(+ api_key="test-key",+ base_url="http://localhost:11434/v1",+ max_retries=5,+ )++ @patch("kong.llm.openai_client.openai.OpenAI")+ def test_custom_no_auth_passes_empty_string(self, mock_openai_cls):+ from kong.llm.openai_client import OpenAIClient++ config = LLMConfig(+ provider=LLMProvider.CUSTOM,+ model="llama3:8b",+ base_url="http://localhost:11434/v1",+ )+ client = create_llm_client(config)+ assert isinstance(client, OpenAIClient)+ mock_openai_cls.assert_called_once_with(+ api_key="",+ base_url="http://localhost:11434/v1",+ max_retries=5,+ )++ @patch("kong.llm.openai_client.openai.OpenAI")+ def test_openai_returns_openai_client_no_base_url(self, mock_openai_cls):+ from kong.llm.openai_client import OpenAIClient++ config = LLMConfig(provider=LLMProvider.OPENAI, model="gpt-4o")+ client = create_llm_client(config)+ assert isinstance(client, OpenAIClient)+ mock_openai_cls.assert_called_once_with(+ api_key=None,+ base_url=None,+ max_retries=5,+ )++ @patch("kong.llm.client.anthropic.Anthropic")+ def test_anthropic_returns_anthropic_client(self, mock_anthropic_cls):+ from kong.llm.client import AnthropicClient++ config = LLMConfig(provider=LLMProvider.ANTHROPIC, model="claude-opus-4-6")+ client = create_llm_client(config)+ assert isinstance(client, AnthropicClient)