gmuse.llm#

LLM client for interacting with various LLM providers.

This module provides a unified interface for calling LLM APIs using LiteLLM, which supports 100+ providers including OpenAI, Anthropic, Cohere, and more.

Public API:
  • detect_provider: Auto-detect LLM provider from environment

  • resolve_model: Resolve model name with auto-detection

  • is_llm_available: Check if LLM is configured

  • LLMClient: Client for generating text

Note:

All providers are supported out of the box via LiteLLM.

Module Contents#

Classes#

LLMClient

Client for generating text using LLM providers.

Functions#

_suppress_litellm_output

Context manager to suppress litellm’s noisy stdout/stderr output.

detect_provider

Detect LLM provider from environment variables.

resolve_model

Resolve model name, using provider auto-detection if needed.

_convert_to_llm_error

Convert various exceptions to appropriate LLMError messages.

Data#

API#

gmuse.llm.logger = 'get_logger(...)'#
gmuse.llm._DEFAULT_MODELS: Final[dict[str, str]] = None#
gmuse.llm._suppress_litellm_output() Iterator[None]#

Context manager to suppress litellm’s noisy stdout/stderr output.

LiteLLM prints debug info like “Provider List: …” that clutters output. This context manager captures and discards that output unless debug mode is enabled.

Yields:

None

gmuse.llm.detect_provider() Optional[str]#

Detect LLM provider from environment variables.

Checks for common API key environment variables in priority order:

  1. OPENAI_API_KEY -> “openai”

  2. ANTHROPIC_API_KEY -> “anthropic”

  3. COHERE_API_KEY -> “cohere”

  4. AZURE_API_KEY -> “azure”

  5. GEMINI_API_KEY or GOOGLE_API_KEY -> “gemini”

  6. GMUSE_MODEL containing “gemini” -> “gemini”

Returns:

Provider name if API key found, None otherwise

Example:
>>> os.environ["OPENAI_API_KEY"] = "sk-..."
>>> detect_provider()
'openai'
gmuse.llm.resolve_model(provider: str, model: Optional[str] = None) str#

Resolve model name, using provider auto-detection if needed.

Resolution priority:

  1. Explicit model parameter

  2. GMUSE_MODEL environment variable

  3. Auto-detect from provider API keys

Args:

model: Explicit model name (e.g., “gpt-4”, “claude-3-opus”) provider: Explicit provider override

Returns:

Resolved model name

Raises:

LLMError: If no model can be resolved

Example:
>>> resolve_model("gpt-4")
'gpt-4'
>>> os.environ["OPENAI_API_KEY"] = "sk-..."
>>> resolve_model()  # Auto-detects
'gpt-4o-mini'
class gmuse.llm.LLMClient(model: Optional[str] = None, timeout: int = 30)#

Client for generating text using LLM providers.

This class wraps LiteLLM to provide a simple interface for generating commit messages using various LLM providers.

Attributes:

model: LLM model identifier timeout: Request timeout in seconds

Example:
>>> client = LLMClient(model="gpt-4", timeout=30)
>>> response = client.generate(
...     system_prompt="You are a commit message generator.",
...     user_prompt="Generate a commit message for: Added tests"
... )
>>> print(response)
'Add unit tests for authentication module'

Initialization

Initialize LLM client.

Args:

model: LLM model identifier (auto-detects if None) timeout: Request timeout in seconds (default: 30)

Raises:

LLMError: If no provider is configured

generate(system_prompt: str, user_prompt: str, temperature: float = 0.7, max_tokens: int = 500) str#

Generate text using the LLM.

Args:

system_prompt: System message defining role and constraints user_prompt: User message with context and task temperature: Sampling temperature (0.0-1.0, default: 0.7) max_tokens: Maximum tokens in response (default: 500)

Returns:

Generated text from LLM

Raises:

LLMError: If API call fails

Example:
>>> client = LLMClient(model="gpt-4")
>>> message = client.generate(
...     system_prompt="You are a helpful assistant.",
...     user_prompt="Say hello"
... )
>>> print(message)
'Hello! How can I help you today?'
gmuse.llm._convert_to_llm_error(error: Exception, timeout: int) gmuse.exceptions.LLMError#

Convert various exceptions to appropriate LLMError messages.

Args:

error: The original exception timeout: Request timeout value for error messages

Returns:

LLMError with user-friendly message