Files
markitect-main/markitect/llm/factory.py
tegwick 880c1d1374 feat(llm): add Gemini adapter and process book-1-chapter-05
Add GeminiAdapter calling Google's Generative Language REST API
(default model: gemini-2.5-flash). Register "gemini" as third
provider in the factory and CLI. Add rate-limit retry with
exponential backoff to the pipeline's _call_llm helper. Increase
default max_tokens from 2000 to 4096.

Process book-1-chapter-05 via Gemini free tier — 1 new entity
extracted (necessaries-conveniencies-and-amusements-of-life),
41 existing entities correctly skipped by dedup. Canonical set
now at 42 unique entities.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-11 22:54:37 +01:00

60 lines
2.0 KiB
Python

"""
Factory for creating LLM adapters by provider name.
"""
from typing import Optional, Dict, Any
from markitect.prompts.execution.llm_adapter import LLMAdapter
from markitect.llm.exceptions import LLMConfigurationError
# Lazy imports to avoid pulling in every adapter at module load time.
_PROVIDERS: Dict[str, str] = {
"openrouter": "markitect.llm.openrouter.OpenRouterAdapter",
"claude-code": "markitect.llm.claude_code.ClaudeCodeAdapter",
"gemini": "markitect.llm.gemini.GeminiAdapter",
}
def create_adapter(
provider: str = "openrouter",
model: Optional[str] = None,
api_key: Optional[str] = None,
system_prompt: Optional[str] = None,
**kwargs: Any,
) -> LLMAdapter:
"""Instantiate an :class:`LLMAdapter` for the given *provider*.
Args:
provider: ``"openrouter"``, ``"claude-code"``, or ``"gemini"``.
model: Model name (passed to the adapter constructor).
api_key: Explicit API key (OpenRouter / Gemini).
system_prompt: Optional system prompt (OpenRouter / Gemini).
**kwargs: Extra keyword arguments forwarded to the adapter.
Returns:
A ready-to-use :class:`LLMAdapter` instance.
Raises:
LLMConfigurationError: If *provider* is not recognised.
"""
if provider not in _PROVIDERS:
known = ", ".join(sorted(_PROVIDERS))
raise LLMConfigurationError(
f"Unknown LLM provider {provider!r}. Choose from: {known}",
context={"provider": provider},
)
# Lazy import
fqn = _PROVIDERS[provider]
module_path, class_name = fqn.rsplit(".", 1)
import importlib
mod = importlib.import_module(module_path)
cls = getattr(mod, class_name)
if provider in ("openrouter", "gemini"):
return cls(model=model, api_key=api_key, system_prompt=system_prompt, **kwargs)
elif provider == "claude-code":
return cls(model=model, **kwargs)
else:
return cls(**kwargs) # pragma: no cover