Skip to content

MoondreamProvider

Base classes

Name Children Inherits
Provider
pydantic_ai.providers
Abstract class for a provider.

â‹” Inheritance diagram

graph TD
  2549841528["moondream_provider.MoondreamProvider"]
  1392801764["providers.Provider"]
  925645236["abc.ABC"]
  2751680898["builtins.object"]
  3481597255["typing.Generic"]
  1392801764 --> 2549841528
  925645236 --> 1392801764
  2751680898 --> 925645236
  3481597255 --> 1392801764
  2751680898 --> 3481597255

🛈 DocStrings

Bases: Provider[AsyncOpenAI]

Provider for Moondream API.

Source code in src/llmling_models/providers/moondream_provider.py
class MoondreamProvider(Provider[AsyncOpenAI]):
    """Provider for Moondream API."""

    @property
    def name(self) -> str:
        """The provider name."""
        return "moondream"

    @property
    def base_url(self) -> str:
        """The base URL for the provider API."""
        return os.environ.get("MOONDREAM_BASE_URL", "https://api.moondream.ai/v1")

    @property
    def client(self) -> AsyncOpenAI:
        """Get a client configured for Moondream."""
        return self._client

    @overload
    def __init__(self) -> None: ...

    @overload
    def __init__(self, *, api_key: str) -> None: ...

    @overload
    def __init__(self, *, api_key: str, http_client: AsyncHTTPClient) -> None: ...

    @overload
    def __init__(self, *, openai_client: AsyncOpenAI | None = None) -> None: ...

    def __init__(
        self,
        *,
        api_key: str | None = None,
        openai_client: AsyncOpenAI | None = None,
        http_client: AsyncHTTPClient | None = None,
    ) -> None:
        """Initialize provider for Moondream.

        Args:
            api_key: The API key to use for authentication.
                     If not provided, the `MOONDREAM_API_KEY`
                     environment variable will be used if available.
            openai_client: An existing AsyncOpenAI client to use. If provided, other parameters must be None.
            http_client: An existing AsyncHTTPClient to use for making HTTP requests.
        """  # noqa: E501
        api_key = api_key or os.environ.get("MOONDREAM_API_KEY")

        if api_key is None and openai_client is None:
            msg = (
                "Set the `MOONDREAM_API_KEY` environment variable or pass it via "
                "`MoondreamProvider(api_key=...)` to use the Moondream provider."
            )
            raise ValueError(msg)

        if openai_client is not None:
            assert http_client is None, "Cannot provide both `openai_client` and `http_client`"
            assert api_key is None, "Cannot provide both `openai_client` and `api_key`"
            self._client = openai_client
        else:
            if http_client is None:
                # Create HTTP client with Moondream headers
                http_client = _create_http_client()

                # Add Moondream-specific headers
                http_client.headers["HTTP-Referer"] = "https://llmling.dev"
                http_client.headers["X-Title"] = "LLMling"

            self._client = AsyncOpenAI(
                base_url=self.base_url,
                api_key=api_key,
                http_client=http_client,
            )

base_url property

base_url: str

The base URL for the provider API.

client property

client: AsyncOpenAI

Get a client configured for Moondream.

name property

name: str

The provider name.

__init__

__init__() -> None
__init__(*, api_key: str) -> None
__init__(*, api_key: str, http_client: AsyncClient) -> None
__init__(*, openai_client: AsyncOpenAI | None = None) -> None
__init__(
    *,
    api_key: str | None = None,
    openai_client: AsyncOpenAI | None = None,
    http_client: AsyncClient | None = None,
) -> None

Initialize provider for Moondream.

Parameters:

Name Type Description Default
api_key str | None

The API key to use for authentication. If not provided, the MOONDREAM_API_KEY environment variable will be used if available.

None
openai_client AsyncOpenAI | None

An existing AsyncOpenAI client to use. If provided, other parameters must be None.

None
http_client AsyncClient | None

An existing AsyncHTTPClient to use for making HTTP requests.

None
Source code in src/llmling_models/providers/moondream_provider.py
def __init__(
    self,
    *,
    api_key: str | None = None,
    openai_client: AsyncOpenAI | None = None,
    http_client: AsyncHTTPClient | None = None,
) -> None:
    """Initialize provider for Moondream.

    Args:
        api_key: The API key to use for authentication.
                 If not provided, the `MOONDREAM_API_KEY`
                 environment variable will be used if available.
        openai_client: An existing AsyncOpenAI client to use. If provided, other parameters must be None.
        http_client: An existing AsyncHTTPClient to use for making HTTP requests.
    """  # noqa: E501
    api_key = api_key or os.environ.get("MOONDREAM_API_KEY")

    if api_key is None and openai_client is None:
        msg = (
            "Set the `MOONDREAM_API_KEY` environment variable or pass it via "
            "`MoondreamProvider(api_key=...)` to use the Moondream provider."
        )
        raise ValueError(msg)

    if openai_client is not None:
        assert http_client is None, "Cannot provide both `openai_client` and `http_client`"
        assert api_key is None, "Cannot provide both `openai_client` and `api_key`"
        self._client = openai_client
    else:
        if http_client is None:
            # Create HTTP client with Moondream headers
            http_client = _create_http_client()

            # Add Moondream-specific headers
            http_client.headers["HTTP-Referer"] = "https://llmling.dev"
            http_client.headers["X-Title"] = "LLMling"

        self._client = AsyncOpenAI(
            base_url=self.base_url,
            api_key=api_key,
            http_client=http_client,
        )

:fa-brands-github: Show source on GitHub