LLM Interface

SimplerLLM provides a unified interface for interacting with multiple LLM providers through a single, consistent API.

Overview

The LLM interface is the core abstraction layer in SimplerLLM that allows you to work with different AI providers using the same code. Instead of learning separate SDKs for OpenAI, Anthropic, Google, and others, you use one unified interface.

Key Benefits

  • Provider Independence: Switch between providers with a single parameter change
  • Consistent API: Same methods and parameters across all providers
  • Easy Testing: Test your application with different models effortlessly
  • Future-Proof: New providers can be added without changing your code

Supported Providers

SimplerLLM supports 8 major LLM providers:

OpenAI

GPT-4, GPT-4 Turbo, GPT-3.5

LLMProvider.OPENAI

Anthropic

Claude 3.5 Sonnet, Claude 3 Opus, Haiku

LLMProvider.ANTHROPIC

Google Gemini

Gemini 1.5 Pro, Gemini 1.5 Flash

LLMProvider.GEMINI

Cohere

Command R+, Command R

LLMProvider.COHERE

DeepSeek

DeepSeek Chat models

LLMProvider.DEEPSEEK

OpenRouter

Access to 100+ models

LLMProvider.OPENROUTER

Ollama

Local models (Llama, Mistral, etc.)

LLMProvider.OLLAMA

XAI

Grok models

LLMProvider.XAI

Basic Usage

Creating and using an LLM instance is straightforward with the unified interface:

Creating an LLM Instance

from SimplerLLM.language.llm import LLM, LLMProvider

# Create an LLM instance
llm = LLM.create(
    provider=LLMProvider.OPENAI,
    model_name="gpt-4o"
)

# Generate a response
response = llm.generate_response(
    prompt="Explain machine learning in simple terms"
)

print(response)

Switching Providers

To use a different provider, simply change the provider parameter:

# Switch to Anthropic Claude
llm = LLM.create(
    provider=LLMProvider.ANTHROPIC,
    model_name="claude-3-5-sonnet-20241022"
)

# Same method, different provider
response = llm.generate_response(
    prompt="Explain machine learning in simple terms"
)

print(response)

Pro Tip

Your code remains identical when switching providers. This makes it easy to test different models and find the best one for your use case.

Configuration Parameters

The LLM interface supports various configuration parameters to control generation behavior:

Core Parameters

llm = LLM.create(
    provider=LLMProvider.OPENAI,
    model_name="gpt-4o",
    temperature=0.7,        # Controls randomness (0.0 to 2.0)
    top_p=0.9,             # Nucleus sampling parameter
    max_tokens=1000,       # Maximum tokens in response
    api_key="your_key"     # Optional: Override environment variable
)

Parameter Reference

provider (required)

The LLM provider to use (e.g., LLMProvider.OPENAI)

model_name (required)

The specific model to use (e.g., "gpt-4o", "claude-3-5-sonnet-20241022")

temperature (optional, default: 1.0)

Controls randomness: 0.0 = deterministic, 2.0 = very creative

top_p (optional, default: 1.0)

Nucleus sampling: considers tokens with top_p probability mass

max_tokens (optional)

Maximum number of tokens to generate in the response

api_key (optional)

API key for the provider (overrides environment variable)

Generation Methods

The LLM interface provides several methods for generating responses:

Basic Text Generation

response = llm.generate_response(
    prompt="What is quantum computing?",
    max_tokens=500
)
print(response)

With System Message

response = llm.generate_response(
    prompt="Explain neural networks",
    system_message="You are a helpful AI teacher. Explain concepts clearly with examples."
)
print(response)

Chat Conversation

from SimplerLLM.prompts.messages_template import MessagesTemplate

# Create a conversation
messages = MessagesTemplate()
messages.add_system_message("You are a helpful assistant")
messages.add_user_message("What is Python?")
messages.add_assistant_message("Python is a high-level programming language...")
messages.add_user_message("How do I install it?")

# Generate response with conversation context
response = llm.generate_response(messages=messages.messages)
print(response)

Examples by Provider

Here are complete examples for each supported provider:

OpenAI

from SimplerLLM.language.llm import LLM, LLMProvider

llm = LLM.create(
    provider=LLMProvider.OPENAI,
    model_name="gpt-4o",
    temperature=0.7
)

response = llm.generate_response(
    prompt="Write a haiku about programming"
)
print(response)

Anthropic Claude

llm = LLM.create(
    provider=LLMProvider.ANTHROPIC,
    model_name="claude-3-5-sonnet-20241022",
    temperature=0.7,
    max_tokens=1000
)

response = llm.generate_response(
    prompt="Explain the concept of recursion with an example"
)
print(response)

Google Gemini

llm = LLM.create(
    provider=LLMProvider.GEMINI,
    model_name="gemini-1.5-pro"
)

response = llm.generate_response(
    prompt="Summarize the history of artificial intelligence"
)
print(response)

Ollama (Local Models)

# Make sure Ollama is running locally
llm = LLM.create(
    provider=LLMProvider.OLLAMA,
    model_name="llama2"
)

response = llm.generate_response(
    prompt="Explain Docker containers"
)
print(response)

OpenRouter (Access 100+ Models)

llm = LLM.create(
    provider=LLMProvider.OPENROUTER,
    model_name="openai/gpt-4o"  # Or any other supported model
)

response = llm.generate_response(
    prompt="What are the benefits of microservices?"
)
print(response)

Error Handling

Always implement proper error handling when working with LLM APIs:

from SimplerLLM.language.llm import LLM, LLMProvider

try:
    llm = LLM.create(
        provider=LLMProvider.OPENAI,
        model_name="gpt-4o"
    )

    response = llm.generate_response(
        prompt="Your prompt here",
        max_tokens=500
    )

    print(f"Response: {response}")

except ValueError as e:
    # Handle configuration errors
    print(f"Configuration error: {e}")

except Exception as e:
    # Handle API errors
    print(f"API error: {e}")
    # Could implement retry logic here

Common Error Scenarios

  • Invalid API Key: Ensure your API key is set correctly in environment variables
  • Rate Limiting: Implement exponential backoff for rate-limited requests
  • Timeout: Consider increasing timeout or breaking requests into smaller chunks
  • Invalid Model: Verify the model name is correct for the provider

Best Practices

1. Use Environment Variables for API Keys

Store API keys in a .env file instead of hardcoding them

# .env file
OPENAI_API_KEY=your_key_here
ANTHROPIC_API_KEY=your_key_here

2. Start with Lower Temperature for Factual Tasks

Use temperature 0.0-0.3 for factual responses, 0.7-1.0 for creative tasks

3. Set Reasonable Token Limits

Specify max_tokens to control costs and response length

4. Test with Multiple Providers

Different providers excel at different tasks - test to find the best fit

5. Implement Fallback Mechanisms

Use ReliableLLM for automatic failover between providers

Complete Example

Here's a complete example showing the flexibility of the unified interface:

from SimplerLLM.language.llm import LLM, LLMProvider

def generate_with_provider(provider, model_name, prompt):
    """Generate response with any provider"""
    try:
        llm = LLM.create(
            provider=provider,
            model_name=model_name,
            temperature=0.7
        )

        response = llm.generate_response(prompt=prompt)
        return response

    except Exception as e:
        return f"Error: {str(e)}"

# Test with different providers
prompt = "Explain the importance of code documentation"

# OpenAI
print("=== OpenAI ===")
print(generate_with_provider(
    LLMProvider.OPENAI,
    "gpt-4o",
    prompt
))

# Anthropic
print("\n=== Anthropic ===")
print(generate_with_provider(
    LLMProvider.ANTHROPIC,
    "claude-3-5-sonnet-20241022",
    prompt
))

# Google Gemini
print("\n=== Google Gemini ===")
print(generate_with_provider(
    LLMProvider.GEMINI,
    "gemini-1.5-pro",
    prompt
))

What's Next?

Need More Help?

Check out our full documentation, join the Discord community, or browse example code on GitHub.