Buckets:

download
raw
6.45 kB
"""Ollama Provider Adapter Implementation.
This provider uses LiteLLM as the backend for unified API access,
while preserving Ollama-specific fallback URL logic for development workflows.
"""
from typing import Any
from osprey.utils.logger import get_logger
from .base import BaseProvider
from .litellm_adapter import execute_litellm_completion
logger = get_logger("ollama")
class OllamaProviderAdapter(BaseProvider):
"""Ollama local model provider implementation using LiteLLM."""
# Metadata (single source of truth)
name = "ollama"
description = "Ollama (local models)"
requires_api_key = False
requires_base_url = True
requires_model_id = True
supports_proxy = False
default_base_url = "http://localhost:11434"
# A local Ollama on the default port is the normal case, so a config that
# omits base_url means "the default" rather than "unconfigured". Without the
# fallback the requires_base_url check in get_chat_completion rejects such a
# config, and this adapter's own URL probing is handed None.
apply_default_base_url_fallback = True
default_model_id = "mistral:7b" # Mistral 7B as recommended default
health_check_model_id = "mistral:7b" # Same for health check (local, no cost)
available_models = ["mistral:7b", "gpt-oss:20b", "gpt-oss:120b"]
# API key acquisition information
api_key_url = None
api_key_instructions = []
api_key_note = "Ollama runs locally and does not require an API key"
# LiteLLM integration
litellm_prefix = "ollama"
@staticmethod
def _get_fallback_urls(base_url: str) -> list[str]:
"""Generate fallback URLs for Ollama based on the current base URL."""
fallback_urls = []
if "host.containers.internal" in base_url:
# Running in container but Ollama might be on localhost
fallback_urls = [
base_url.replace("host.containers.internal", "localhost"),
"http://localhost:11434",
]
elif "localhost" in base_url:
# Running locally but Ollama might be in container context
fallback_urls = [
base_url.replace("localhost", "host.containers.internal"),
"http://host.containers.internal:11434",
]
else:
# Generic fallbacks for other scenarios
fallback_urls = ["http://localhost:11434", "http://host.containers.internal:11434"]
return fallback_urls
@staticmethod
def _test_connection(base_url: str) -> bool:
"""Test if Ollama is accessible at the given URL."""
try:
import requests
test_url = base_url.rstrip("/") + "/v1/models"
response = requests.get(test_url, timeout=2)
return response.status_code == 200
except Exception:
return False
def _resolve_base_url(self, base_url: str | None) -> str:
"""Resolve a reachable base URL, probing container/localhost variants.
The configured value is resolved through
:meth:`~osprey.models.providers.base.BaseProvider.require_effective_base_url`
first, so this method never has to interpret ``None`` — the connectivity
fallbacks below all do substring matching, which a ``None`` breaks with an
unhelpful ``TypeError``.
"""
base_url = self.require_effective_base_url(base_url)
# Test primary URL first
if self._test_connection(base_url):
logger.debug(f"Successfully connected to Ollama at {base_url}")
return base_url
logger.debug(f"Failed to connect to Ollama at {base_url}")
# Try fallback URLs
fallback_urls = self._get_fallback_urls(base_url)
for fallback_url in fallback_urls:
logger.debug(f"Attempting fallback connection to Ollama at {fallback_url}")
if self._test_connection(fallback_url):
logger.warning(
f"Ollama connection fallback: configured URL '{base_url}' failed, "
f"using fallback '{fallback_url}'. Consider updating your configuration."
)
return fallback_url
# All connection attempts failed
raise ValueError(
f"Failed to connect to Ollama at configured URL '{base_url}' "
f"and all fallback URLs {fallback_urls}. Please ensure Ollama is running "
f"and accessible, or update your configuration."
)
def execute_completion(
self,
message: str,
model_id: str,
api_key: str | None,
base_url: str | None,
max_tokens: int = 1024,
temperature: float = 0.0,
thinking: dict | None = None,
system_prompt: str | None = None,
output_format: Any | None = None,
**kwargs,
) -> str | Any:
"""Execute Ollama chat completion via LiteLLM with fallback support."""
# Resolve working base URL with fallbacks
effective_base_url = self._resolve_base_url(base_url)
return execute_litellm_completion(
provider=self.name,
message=message,
model_id=model_id,
api_key=api_key or "ollama", # Ollama doesn't need real key
base_url=effective_base_url,
max_tokens=max_tokens,
temperature=temperature,
output_format=output_format,
**kwargs,
)
def check_health(
self,
api_key: str | None,
base_url: str | None,
timeout: float = 5.0,
model_id: str | None = None,
) -> tuple[bool, str]:
"""Check Ollama connectivity (no API key needed).
Probes the same endpoint ``execute_completion`` would use, so a missing
base_url is reported against the declared default rather than as
"unconfigured" — the health check must not disagree with what a request
will actually reach.
"""
try:
probe_url = self.require_effective_base_url(base_url)
except ValueError as e:
return False, str(e)
try:
if self._test_connection(probe_url):
return True, f"Accessible at {probe_url}"
else:
return False, f"Not accessible at {probe_url}"
except Exception as e:
return False, f"Connection test failed: {str(e)[:50]}"

Xet Storage Details

Size:
6.45 kB
·
Xet hash:
f7ef61ffd3f7e519aecd23a7e965ef4b603a989811991af0b3c2210329d61c0c

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.