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#
Client for generating text using LLM providers. |
Functions#
Context manager to suppress litellm’s noisy stdout/stderr output. |
|
Detect LLM provider from environment variables or model. |
|
Resolve provider by checking for known API key environment variables. |
|
Resolve model name, using provider auto-detection if needed. |
|
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() Generator[None, None, 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(*, model: str | None = None, credential_lookup_timeout: float | None = None) Optional[str]#
Detect LLM provider from environment variables or model.
Checks for common API key environment variables in priority order:
OPENAI_API_KEY -> “openai”
ANTHROPIC_API_KEY -> “anthropic”
COHERE_API_KEY -> “cohere”
AZURE_API_KEY -> “azure”
GEMINI_API_KEY or GOOGLE_API_KEY -> “gemini”
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_provider_from_key_env_vars() Optional[str]#
Resolve provider by checking for known API key environment variables.
Returns: Provider name if found, None otherwise
- gmuse.llm.resolve_model(provider: str, model: Optional[str] = None) str#
Resolve model name, using provider auto-detection if needed.
Resolution priority:
Explicit model parameter
GMUSE_MODEL environment variable
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, credential_lookup_timeout: float | None = None)#
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