Some checks failed
Test Suite / unit-tests (3.11) (push) Has been cancelled
Test Suite / unit-tests (3.12) (push) Has been cancelled
Test Suite / code-quality (push) Has been cancelled
Test Suite / security-scan (push) Has been cancelled
Test Suite / integration-tests (push) Has been cancelled
Test Suite / e2e-tests (push) Has been cancelled
Test Suite / performance-tests (push) Has been cancelled
Test Suite / test-summary (push) Has been cancelled
Stage 1 — Decouple:
- Move RunConfig + LLMResponse to markitect/llm/models.py (canonical)
- Move LLMAdapter + Mock/ErrorLLMAdapter to markitect/llm/adapter.py
- markitect/prompts/execution/models.py and llm_adapter.py become re-export shims
- All 4 adapters + factory.py updated to import from markitect.llm.*
- Parameterize app_name in toml_config.py (resolve_llm, get_default_layers,
get_preference_layers): paths and env var now derived from app_name arg
- Add tests/test_llm_isolation.py: 7 isolation + backward-compat tests
Stage 2 — Extract:
- Standalone llm-connect package created at ~/llm-connect/
- All 18 llm files copied; markitect.* imports replaced with llm_connect.*
- LLMError base inlined in llm_connect/exceptions.py (no markitect dep)
- llm-connect installed into markitect-venv; declared in pyproject.toml
Smoke test: markitect llm-check succeeds (live Gemini API call).
Backward compat: markitect.prompts.execution.{models,llm_adapter} still work.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
140 lines
5.2 KiB
Python
140 lines
5.2 KiB
Python
"""
|
|
OpenRouter adapter — calls the OpenAI-compatible chat completions API.
|
|
"""
|
|
|
|
import time
|
|
from typing import Optional, Dict, Any
|
|
|
|
from markitect.llm.adapter import LLMAdapter
|
|
from markitect.llm.models import RunConfig, LLMResponse
|
|
from markitect.llm.config import LLMConfig, resolve_api_key, find_project_root
|
|
from markitect.llm._http import post_json
|
|
from markitect.llm.exceptions import (
|
|
LLMConfigurationError,
|
|
LLMAPIError,
|
|
LLMRateLimitError,
|
|
)
|
|
|
|
_DEFAULT_MODEL = "anthropic/claude-sonnet-4"
|
|
|
|
|
|
class OpenRouterAdapter(LLMAdapter):
|
|
"""LLM adapter that calls the OpenRouter chat completions endpoint.
|
|
|
|
Constructor args override values from *config*; *config* overrides
|
|
global defaults. The model used for a given call is resolved as:
|
|
``constructor model > RunConfig.model_name > default``.
|
|
"""
|
|
|
|
def __init__(
|
|
self,
|
|
model: Optional[str] = None,
|
|
api_key: Optional[str] = None,
|
|
api_base: Optional[str] = None,
|
|
config: Optional[LLMConfig] = None,
|
|
system_prompt: Optional[str] = None,
|
|
extra_headers: Optional[Dict[str, str]] = None,
|
|
max_retries: Optional[int] = None,
|
|
):
|
|
self._config = config or LLMConfig()
|
|
self._model = model or self._config.model or _DEFAULT_MODEL
|
|
self._api_base = (api_base or self._config.api_base).rstrip("/")
|
|
self._system_prompt = system_prompt
|
|
self._extra_headers = extra_headers or {}
|
|
self._max_retries = max_retries if max_retries is not None else self._config.max_retries
|
|
|
|
# Resolve API key
|
|
root = find_project_root()
|
|
key_file_paths = [root / "apikey-openrouter.txt"] if root else []
|
|
self._api_key = resolve_api_key(
|
|
explicit=api_key or self._config.api_key,
|
|
env_var="OPENROUTER_API_KEY",
|
|
key_file_paths=key_file_paths,
|
|
)
|
|
|
|
# ── LLMAdapter interface ────────────────────────────────────────
|
|
|
|
def execute_prompt(self, prompt: str, config: RunConfig) -> LLMResponse:
|
|
model = self._model if self._model != _DEFAULT_MODEL else (config.model_name or self._model)
|
|
|
|
messages: list[Dict[str, str]] = []
|
|
if self._system_prompt:
|
|
messages.append({"role": "system", "content": self._system_prompt})
|
|
messages.append({"role": "user", "content": prompt})
|
|
|
|
payload: Dict[str, Any] = {
|
|
"model": model,
|
|
"messages": messages,
|
|
"temperature": config.temperature,
|
|
"max_tokens": config.max_tokens,
|
|
}
|
|
# Merge extra model_params from RunConfig
|
|
if config.model_params:
|
|
payload.update(config.model_params)
|
|
|
|
headers = {
|
|
"Authorization": f"Bearer {self._api_key}",
|
|
**self._extra_headers,
|
|
}
|
|
url = f"{self._api_base}/chat/completions"
|
|
|
|
start = time.time()
|
|
data = self._post_with_retries(url, payload, headers, config.timeout_seconds)
|
|
latency = time.time() - start
|
|
|
|
# Parse response
|
|
choice = data.get("choices", [{}])[0]
|
|
content = choice.get("message", {}).get("content", "")
|
|
finish_reason = choice.get("finish_reason", "stop")
|
|
usage = data.get("usage", {})
|
|
|
|
return LLMResponse(
|
|
content=content,
|
|
model=data.get("model", model),
|
|
usage={
|
|
"prompt_tokens": usage.get("prompt_tokens", 0),
|
|
"completion_tokens": usage.get("completion_tokens", 0),
|
|
"total_tokens": usage.get("total_tokens", 0),
|
|
},
|
|
finish_reason=finish_reason,
|
|
metadata={
|
|
"provider": "openrouter",
|
|
"latency_seconds": round(latency, 3),
|
|
"response_id": data.get("id", ""),
|
|
},
|
|
)
|
|
|
|
def validate_config(self, config: RunConfig) -> bool:
|
|
if not self._api_key:
|
|
return False
|
|
if not (self._model or config.model_name):
|
|
return False
|
|
if not (0.0 <= config.temperature <= 2.0):
|
|
return False
|
|
return True
|
|
|
|
# ── Internals ───────────────────────────────────────────────────
|
|
|
|
def _post_with_retries(
|
|
self,
|
|
url: str,
|
|
payload: Dict[str, Any],
|
|
headers: Dict[str, str],
|
|
timeout: int,
|
|
) -> Dict[str, Any]:
|
|
last_exc: Optional[Exception] = None
|
|
for attempt in range(self._max_retries + 1):
|
|
try:
|
|
return post_json(url, payload, headers, timeout=timeout)
|
|
except LLMRateLimitError as exc:
|
|
last_exc = exc
|
|
if attempt < self._max_retries:
|
|
time.sleep(2 ** attempt)
|
|
except LLMAPIError as exc:
|
|
if exc.status_code >= 500 and attempt < self._max_retries:
|
|
last_exc = exc
|
|
time.sleep(2 ** attempt)
|
|
else:
|
|
raise
|
|
raise last_exc # type: ignore[misc]
|