refactor(devgen/providers): shared provider base class

Abstract out generate validation and error handling into BaseProvider, eliminating duplicated logic across Anthropic, OpenRouter, and OpenAI providers.
This commit is contained in:
sankalp committed 2026-06-06 17:10:30 +05:45
1 parent e2ec269f16
commit f70b5208ec
7 files changed
+223 -259

No files matched your search

+30 -33
View File
@@ -1,54 +1,51 @@
from openai import OpenAI
from devgen.providers.base import BaseProvider
from devgen.utils import format_token_limit_error, is_token_limit_error
class OllamaProvider:
class OllamaProvider(BaseProvider):
"""Generates content using a local Ollama server.
Ollama exposes an OpenAI-compatible endpoint at /v1/chat/completions,
so we can reuse the openai client. The host defaults to
http://localhost:11434 but can be overridden via the
`ollama_host` kwarg (or `ollama_host` in ~/.devgen.yaml).
so we reuse the ``openai`` client. The host defaults to
http://localhost:11434 but can be overridden via the ``ollama_host``
kwarg (or ``ollama_host`` in ~/.devgen.yaml).
"""
DISPLAY_NAME = "Ollama"
REQUIRES_API_KEY = False
DEFAULT_HOST = "http://localhost:11434"
DEFAULT_MODEL = "llama3.2"
def generate(
self,
prompt: str,
api_key: str | None = None,
model: str | None = None,
**kwargs,
) -> str:
from openai import OpenAI
def _generate(self, prompt, api_key, model, **kwargs):
host = kwargs.get("ollama_host") or self.DEFAULT_HOST
base_url = f"{host.rstrip('/')}/v1"
chosen_model = model or self.DEFAULT_MODEL
client = OpenAI(base_url=base_url, api_key=api_key or "ollama")
kwargs.pop("debug", None)
kwargs.pop("ollama_host", None)
try:
response = client.chat.completions.create(
model=chosen_model,
messages=[{"role": "user", "content": prompt}],
**kwargs,
)
except Exception as e:
if is_token_limit_error(e):
raise RuntimeError(format_token_limit_error("Ollama", e)) from e
raise RuntimeError(
f"Ollama request failed (host={host}, model={chosen_model}): {e}. "
"Is the Ollama server running? Start it with `ollama serve` and "
"pull a model with `ollama pull <model>`."
) from e
client = OpenAI(base_url=base_url, api_key=api_key or "ollama")
response = client.chat.completions.create(
model=model,
messages=[{"role": "user", "content": prompt}],
**kwargs,
)
if not response.choices:
raise RuntimeError(
f"Ollama returned no choices for model {chosen_model!r}. "
f"Ollama returned no choices for model {model!r}. "
"The model may not be installed locally."
)
return response.choices[0].message.content.strip()
return response.choices[0].message.content
def _handle_error(self, error):
"""Override default wrapping — Ollama has no API key to check."""
if is_token_limit_error(error):
raise RuntimeError(
format_token_limit_error(self.DISPLAY_NAME, error)
) from error
raise RuntimeError(
f"{self.DISPLAY_NAME} request failed: {error}. "
"Is the Ollama server running? Start it with `ollama serve` and "
"pull a model with `ollama pull <model>`."
) from error