Bugfix for local LLMs with no API key supplied
This commit is contained in:
@@ -6,6 +6,8 @@ from typing import Any, Optional, Sequence, Tuple
|
|||||||
from audita.core.config import AuditaConfig, DEFAULT_BASE_URL
|
from audita.core.config import AuditaConfig, DEFAULT_BASE_URL
|
||||||
from audita.core.errors import AuditaLLMError
|
from audita.core.errors import AuditaLLMError
|
||||||
|
|
||||||
|
_NO_KEY_PLACEHOLDER = "audita-no-key-required"
|
||||||
|
|
||||||
|
|
||||||
class OpenAICompatibleStructuredLLMClient:
|
class OpenAICompatibleStructuredLLMClient:
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
@@ -60,7 +62,7 @@ class OpenAICompatibleStructuredLLMClient:
|
|||||||
) from exc
|
) from exc
|
||||||
|
|
||||||
openai_client = OpenAI(
|
openai_client = OpenAI(
|
||||||
api_key=config.api_key,
|
api_key=_client_api_key(config),
|
||||||
base_url=config.base_url,
|
base_url=config.base_url,
|
||||||
timeout=config.llm_timeout_seconds,
|
timeout=config.llm_timeout_seconds,
|
||||||
)
|
)
|
||||||
@@ -92,5 +94,15 @@ def _requires_api_key(config: AuditaConfig) -> bool:
|
|||||||
return _normalized_base_url(config.base_url) == _normalized_base_url(DEFAULT_BASE_URL)
|
return _normalized_base_url(config.base_url) == _normalized_base_url(DEFAULT_BASE_URL)
|
||||||
|
|
||||||
|
|
||||||
|
def _client_api_key(config: AuditaConfig) -> str:
|
||||||
|
if config.api_key:
|
||||||
|
return config.api_key
|
||||||
|
if _requires_api_key(config):
|
||||||
|
raise AuditaLLMError(
|
||||||
|
"OpenRouter credentials are required but missing for the configured endpoint."
|
||||||
|
)
|
||||||
|
return _NO_KEY_PLACEHOLDER
|
||||||
|
|
||||||
|
|
||||||
def _normalized_base_url(base_url: str) -> str:
|
def _normalized_base_url(base_url: str) -> str:
|
||||||
return base_url.rstrip("/")
|
return base_url.rstrip("/")
|
||||||
|
|||||||
@@ -223,7 +223,7 @@ def test_missing_api_key_is_allowed_for_nondefault_endpoint(monkeypatch):
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
assert openai_inits == [{"api_key": None, "base_url": "http://localhost:8000/v1", "timeout": 600}]
|
assert openai_inits == [{"api_key": "audita-no-key-required", "base_url": "http://localhost:8000/v1", "timeout": 600}]
|
||||||
assert create_calls == [
|
assert create_calls == [
|
||||||
{
|
{
|
||||||
"model": "meta-llama/Llama-3.1-8B-Instruct",
|
"model": "meta-llama/Llama-3.1-8B-Instruct",
|
||||||
|
|||||||
Reference in New Issue
Block a user