Codemini-LLM 1.0.0
Production-ready LLM wrappers with robust error handling for Google Gemini, OpenAI, Anthropic Claude, Groq, and Ollama
Requires Python: >=3.8
Keywords: llm, ai, openai, anthropic, google, gemini, groq, ollama, wrapper

# codemni_LLM

**Production-ready LLM wrappers with robust error handling, retries, and clean error messages.**

A lightweight Python package providing unified, production-ready wrappers for multiple LLM providers:
- **Google Gemini**
- **OpenAI**
- **Anthropic Claude**
- **Groq**
- **Ollama**

## Features

✨ **Simple & Consistent API** - Same interface across all providers  
🛡️ **Robust Error Handling** - Custom exceptions for better error management  
🔄 **Built-in Retries** - Automatic retry logic with exponential backoff  
⚡ **Production Ready** - Input validation, timeouts, and defensive coding  
🪶 **Lightweight** - Minimal dependencies, install only what you need  
🔕 **No Logging** - Clean execution without verbose logging

## Installation

Install the base package:

```bash
pip install codemni_LLM
```

Install with specific provider support:

```bash
# For Google Gemini
pip install codemni_LLM[google]

# For OpenAI
pip install codemni_LLM[openai]

# For Anthropic Claude
pip install codemni_LLM[anthropic]

# For Groq
pip install codemni_LLM[groq]

# For Ollama
pip install codemni_LLM[ollama]

# Install all providers
pip install codemni_LLM[all]
```

## Quick Start

### Google Gemini

```python
from codemni_LLM import google_llm, GoogleLLMError

try:
    response = google_llm(
        prompt="Explain Python in one sentence",
        model="gemini-pro",
        api_key="your-api-key"  # or set GOOGLE_API_KEY env var
    )
    print(response)
except GoogleLLMError as e:
    print(f"Error: {e}")
```

### OpenAI

```python
from codemni_LLM import openai_llm, OpenAILLMError

try:
    response = openai_llm(
        prompt="What is machine learning?",
        model="gpt-4",
        api_key="your-api-key"  # or set OPENAI_API_KEY env var
    )
    print(response)
except OpenAILLMError as e:
    print(f"Error: {e}")
```

### Anthropic Claude

```python
from codemni_LLM import anthropic_llm, AnthropicLLMError

try:
    response = anthropic_llm(
        prompt="Describe AI in simple terms",
        model="claude-3-opus-20240229",
        api_key="your-api-key"  # or set ANTHROPIC_API_KEY env var
    )
    print(response)
except AnthropicLLMError as e:
    print(f"Error: {e}")
```

### Groq

```python
from codemni_LLM import groq_llm, GroqLLMError

try:
    response = groq_llm(
        prompt="What is quantum computing?",
        model="mixtral-8x7b-32768",
        api_key="your-api-key"  # or set GROQ_API_KEY env var
    )
    print(response)
except GroqLLMError as e:
    print(f"Error: {e}")
```

### Ollama

```python
from codemni_LLM import ollama_llm, OllamaLLMError

try:
    response = ollama_llm(
        prompt="Explain neural networks",
        model="llama2",
        host="http://localhost:11434"  # optional, defaults to localhost
    )
    print(response)
except OllamaLLMError as e:
    print(f"Error: {e}")
```

## API Reference

All wrapper functions follow a consistent signature:

```python
def llm_function(
    prompt: str,
    model: str,
    api_key: Optional[str] = None,  # Not needed for Ollama
    max_retries: int = 3,
    retry_delay: float = 1.0,
    timeout: float = 30.0,
    **kwargs
) -> str:
    """
    Args:
        prompt: The input text prompt
        model: Model identifier (e.g., "gpt-4", "gemini-pro")
        api_key: API key (falls back to environment variable)
        max_retries: Number of retry attempts on failure
        retry_delay: Initial delay between retries (exponential backoff)
        timeout: Request timeout in seconds
        **kwargs: Additional provider-specific parameters
        
    Returns:
        str: The generated text response
        
    Raises:
        LLMError: Base exception for all errors
        LLMAPIError: API-related errors
        LLMImportError: Missing dependency errors
        LLMResponseError: Invalid or empty response errors
    """
```

## Exception Hierarchy

Each provider has a consistent exception hierarchy:

```
LLMError (base)
├── LLMImportError (missing dependencies)
├── LLMAPIError (API failures)
└── LLMResponseError (invalid responses)
```

Example:
- `GoogleLLMError`, `GoogleLLMImportError`, `GoogleLLMAPIError`, `GoogleLLMResponseError`
- `OpenAILLMError`, `OpenAILLMImportError`, `OpenAILLMAPIError`, `OpenAILLMResponseError`
- And so on for each provider...

## Environment Variables

Set these environment variables to avoid passing API keys in code:

- `GOOGLE_API_KEY` - For Google Gemini
- `OPENAI_API_KEY` - For OpenAI
- `ANTHROPIC_API_KEY` - For Anthropic Claude
- `GROQ_API_KEY` - For Groq

## Advanced Usage

### Custom Retry Logic

```python
response = google_llm(
    prompt="Your prompt here",
    model="gemini-pro",
    max_retries=5,
    retry_delay=2.0,  # Start with 2 second delay
    timeout=60.0
)
```

### Provider-Specific Parameters

Pass additional parameters using `**kwargs`:

```python
# OpenAI with temperature and top_p
response = openai_llm(
    prompt="Be creative!",
    model="gpt-4",
    temperature=0.9,
    top_p=0.95,
    max_tokens=500
)

# Anthropic with max tokens
response = anthropic_llm(
    prompt="Write a story",
    model="claude-3-opus-20240229",
    max_tokens=1024
)
```

## Development

To contribute or run tests:

```bash
# Clone the repository
git clone https://github.com/Codexjitin/Codemni-ToolCallAgent.git
cd Codemni-ToolCallAgent

# Install with dev dependencies
pip install -e .[dev]

# Run tests
pytest
```

## License

MIT License - see LICENSE file for details.

## Author

**codexJitin**

## Support

- GitHub Issues: [Report bugs or request features](https://github.com/Codexjitin/Codemni-ToolCallAgent/issues)
- Source Code: [View on GitHub](https://github.com/Codexjitin/Codemni-ToolCallAgent)

## Changelog

### Version 1.0.0
- Initial release
- Support for Google Gemini, OpenAI, Anthropic Claude, Groq, and Ollama
- Robust error handling with custom exceptions
- Built-in retry logic with exponential backoff
- Production-ready with input validation and timeouts