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/groq/groq.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from collections.abc import AsyncIterator, Iterator
from collections.abc import AsyncIterator, Iterator, Sequence
from typing import TYPE_CHECKING, Any, cast

from openai import AsyncOpenAI, AsyncStream, OpenAI, Stream
Expand All @@ -20,10 +20,12 @@

from any_llm.provider import Provider
from any_llm.providers.groq.utils import (
_convert_models_list,
_create_openai_chunk_from_groq_chunk,
to_chat_completion,
)
from any_llm.types.completion import ChatCompletion, ChatCompletionChunk, CompletionParams
from any_llm.types.model import Model
from any_llm.utils.instructor import _convert_instructor_response

if TYPE_CHECKING:
Expand All @@ -43,7 +45,7 @@ class GroqProvider(Provider):
SUPPORTS_RESPONSES = True
SUPPORTS_COMPLETION_REASONING = True
SUPPORTS_EMBEDDING = False
SUPPORTS_LIST_MODELS = False
SUPPORTS_LIST_MODELS = True

PACKAGES_INSTALLED = PACKAGES_INSTALLED

Expand Down Expand Up @@ -212,3 +214,11 @@ def responses(self, model: str, input_data: Any, **kwargs: Any) -> Response | It
msg = f"Responses API returned an unexpected type: {type(response)}"
raise ValueError(msg)
return response

def list_models(self, **kwargs: Any) -> Sequence[Model]:
"""
Fetch available models from the /v1/models endpoint.
"""
client = groq.Groq(api_key=self.config.api_key)
models_list = client.models.list(**kwargs)
return _convert_models_list(models_list)
6 changes: 6 additions & 0 deletions src/any_llm/providers/groq/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Literal, cast

from groq.types import ModelListResponse as GroqModelListResponse
from groq.types.chat import ChatCompletion as GroqChatCompletion
from groq.types.chat import ChatCompletionChunk as GroqChatCompletionChunk

Expand All @@ -18,6 +19,7 @@
Function,
Reasoning,
)
from any_llm.types.model import Model


def to_chat_completion(response: GroqChatCompletion) -> ChatCompletion:
Expand Down Expand Up @@ -139,3 +141,7 @@ def _create_openai_chunk_from_groq_chunk(groq_chunk: GroqChatCompletionChunk) ->
object="chat.completion.chunk",
usage=usage,
)


def _convert_models_list(models_list: GroqModelListResponse) -> list[Model]:
return [Model(id=model.id, object="model", created=model.created, owned_by="groq") for model in models_list.data]