Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/any_llm/providers/ollama/ollama.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import json
import os
from collections.abc import AsyncIterator, Iterator
from collections.abc import AsyncIterator, Iterator, Sequence
from typing import Any

try:
from ollama import AsyncClient, Client
from ollama import ChatResponse as OllamaChatResponse

from any_llm.providers.ollama.utils import (
_convert_models_list,
_create_chat_completion_from_ollama_response,
_create_openai_chunk_from_ollama_chunk,
_create_openai_embedding_response_from_ollama,
Expand All @@ -21,6 +22,7 @@

from any_llm.provider import ApiConfig, Provider
from any_llm.types.completion import ChatCompletion, ChatCompletionChunk, CompletionParams, CreateEmbeddingResponse
from any_llm.types.model import Model


class OllamaProvider(Provider):
Expand All @@ -40,7 +42,7 @@ class OllamaProvider(Provider):
SUPPORTS_RESPONSES = False
SUPPORTS_COMPLETION_REASONING = True
SUPPORTS_EMBEDDING = True
SUPPORTS_LIST_MODELS = False
SUPPORTS_LIST_MODELS = True

PACKAGES_INSTALLED = PACKAGES_INSTALLED

Expand Down Expand Up @@ -248,3 +250,11 @@ def embedding(
**kwargs,
)
return _create_openai_embedding_response_from_ollama(response)

def list_models(self, **kwargs: Any) -> Sequence[Model]:
"""
Fetch available models from the /v1/models endpoint.
"""
client = Client(host=self.url, timeout=kwargs.pop("timeout", None))
models_list = client.list(**kwargs)
return _convert_models_list(models_list)
15 changes: 15 additions & 0 deletions src/any_llm/providers/ollama/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from ollama import ChatResponse as OllamaChatResponse
from ollama import EmbedResponse
from ollama import ListResponse as OllamaListResponse
from ollama import Message as OllamaMessage

from any_llm.types.completion import (
Expand All @@ -25,6 +26,7 @@
Reasoning,
Usage,
)
from any_llm.types.model import Model


def _create_openai_embedding_response_from_ollama(
Expand Down Expand Up @@ -203,3 +205,16 @@ def _create_chat_completion_from_ollama_response(response: OllamaChatResponse) -
choices=[choice],
usage=usage,
)


def _convert_models_list(models_list: OllamaListResponse) -> list[Model]:
models = models_list.models
return [
Model(
id=model.model or "Unknown",
object="model",
created=int(model.modified_at.timestamp()) if model.modified_at else 0,
owned_by="ollama",
)
for model in models
]