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>
This commit is contained in:
2026-02-11 22:54:37 +01:00
parent 2d1282a61e
commit 880c1d1374
22 changed files with 12008 additions and 57 deletions

View File

@@ -15,6 +15,7 @@ Quick start::
from markitect.llm.factory import create_adapter
from markitect.llm.openrouter import OpenRouterAdapter
from markitect.llm.claude_code import ClaudeCodeAdapter
from markitect.llm.gemini import GeminiAdapter
from markitect.llm.config import LLMConfig, load_config
from markitect.llm.exceptions import (
LLMError,
@@ -29,6 +30,7 @@ __all__ = [
"create_adapter",
"OpenRouterAdapter",
"ClaudeCodeAdapter",
"GeminiAdapter",
"LLMConfig",
"load_config",
"LLMError",

View File

@@ -11,6 +11,7 @@ from markitect.llm.exceptions import LLMConfigurationError
_PROVIDERS: Dict[str, str] = {
"openrouter": "markitect.llm.openrouter.OpenRouterAdapter",
"claude-code": "markitect.llm.claude_code.ClaudeCodeAdapter",
"gemini": "markitect.llm.gemini.GeminiAdapter",
}
@@ -24,10 +25,10 @@ def create_adapter(
"""Instantiate an :class:`LLMAdapter` for the given *provider*.
Args:
provider: ``"openrouter"`` or ``"claude-code"``.
provider: ``"openrouter"``, ``"claude-code"``, or ``"gemini"``.
model: Model name (passed to the adapter constructor).
api_key: Explicit API key (OpenRouter only).
system_prompt: Optional system prompt (OpenRouter only).
api_key: Explicit API key (OpenRouter / Gemini).
system_prompt: Optional system prompt (OpenRouter / Gemini).
**kwargs: Extra keyword arguments forwarded to the adapter.
Returns:
@@ -50,7 +51,7 @@ def create_adapter(
mod = importlib.import_module(module_path)
cls = getattr(mod, class_name)
if provider == "openrouter":
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)

115
markitect/llm/gemini.py Normal file
View File

@@ -0,0 +1,115 @@
"""
Google Gemini adapter — calls the Generative Language REST API directly.
"""
import time
from typing import Optional, Dict, Any
from markitect.prompts.execution.llm_adapter import LLMAdapter
from markitect.prompts.execution.models import RunConfig, LLMResponse
from markitect.llm.config import resolve_api_key, find_project_root
from markitect.llm._http import post_json
from markitect.llm.exceptions import LLMConfigurationError
_DEFAULT_MODEL = "gemini-2.5-flash"
_API_BASE = "https://generativelanguage.googleapis.com/v1beta"
class GeminiAdapter(LLMAdapter):
"""LLM adapter that calls the Google Generative Language API.
Supports the free tier of Gemini models via a Google AI Studio API key.
"""
def __init__(
self,
model: Optional[str] = None,
api_key: Optional[str] = None,
system_prompt: Optional[str] = None,
**_kwargs: Any,
):
self._model = model or _DEFAULT_MODEL
self._system_prompt = system_prompt
root = find_project_root()
key_file_paths = [root / "apikey-geminifree.txt"] if root else []
self._api_key = resolve_api_key(
explicit=api_key,
env_var="GEMINI_API_KEY",
key_file_paths=key_file_paths,
)
if not self._api_key:
raise LLMConfigurationError(
"No Gemini API key found. Set GEMINI_API_KEY or create "
"apikey-geminifree.txt in the project root.",
context={"provider": "gemini"},
)
# ── LLMAdapter interface ────────────────────────────────────────
def execute_prompt(self, prompt: str, config: RunConfig) -> LLMResponse:
model = self._model
# Build Gemini request
contents: list[Dict[str, Any]] = []
if self._system_prompt:
contents.append({
"role": "user",
"parts": [{"text": self._system_prompt}],
})
contents.append({
"role": "model",
"parts": [{"text": "Understood."}],
})
contents.append({
"role": "user",
"parts": [{"text": prompt}],
})
payload: Dict[str, Any] = {
"contents": contents,
"generationConfig": {
"temperature": config.temperature,
"maxOutputTokens": config.max_tokens,
},
}
url = f"{_API_BASE}/models/{model}:generateContent?key={self._api_key}"
start = time.time()
data = post_json(url, payload, timeout=config.timeout_seconds)
latency = time.time() - start
# Parse Gemini response
candidates = data.get("candidates", [])
if not candidates:
content = ""
finish_reason = "error"
else:
parts = candidates[0].get("content", {}).get("parts", [])
content = "".join(p.get("text", "") for p in parts)
finish_reason = candidates[0].get("finishReason", "STOP").lower()
usage_meta = data.get("usageMetadata", {})
return LLMResponse(
content=content,
model=model,
usage={
"prompt_tokens": usage_meta.get("promptTokenCount", 0),
"completion_tokens": usage_meta.get("candidatesTokenCount", 0),
"total_tokens": usage_meta.get("totalTokenCount", 0),
},
finish_reason=finish_reason,
metadata={
"provider": "gemini",
"latency_seconds": round(latency, 3),
},
)
def validate_config(self, config: RunConfig) -> bool:
if not self._api_key:
return False
if not (0.0 <= config.temperature <= 2.0):
return False
return True