Run models locally with Ollama, LM Studio, or directly with transformers.
# For Ollama
curl -fsSL https://ollama.com/install.sh | sh
ollama pull mistral
# For Python transformers
pip install torch transformers sentencepiece
local_model = {
"type": "ollama", // or "transformers"
"base_url": "http://localhost:11434",
"model": "mistral",
"temperature": 0.7
}
Connect to OpenAI, Anthropic, Groq, or any API-compatible service.
openai_config = {
"type": "openai",
"api_key": "sk-your-key-here",
"model": "gpt-4-turbo",
"temperature": 0.7
}
anthropic_config = {
"type": "anthropic",
"api_key": "sk-your-key-here",
"model": "claude-3-opus-20240229"
}
Use free Inference API or Pro endpoints for private models.
hf_config = {
"type": "huggingface",
"api_key": "hf_your_key_here",
"model": "mistralai/Mistral-7B-Instruct-v0.1",
"endpoint": "https://api-inference.huggingface.co/models/",
"pro_endpoint": false // Set true for Pro
}
Connect to Stable Diffusion, DALL-E, or other image generation models.
image_config = {
"type": "dalle", // or "stable_diffusion"
"api_key": "sk-your-key-here", // For DALL-E
"model": "dall-e-3", // or "stabilityai/stable-diffusion-xl-base-1.0"
"size": "1024x1024"
}
Copy this starter code to begin using any provider:
# Install required packages
pip install requests python-dotenv
# In your .env file:
OPENAI_API_KEY=your_key_here
ANTHROPIC_API_KEY=your_key_here
HF_API_KEY=your_key_here
# Basic usage example
from llm_connector import LLMConnector
connector = LLMConnector(provider="openai")
response = connector.generate("Explain quantum computing")
print(response)
Response will appear here...
import LLMConnector
# Initialize with your preferred provider
llm = LLMConnector(provider="openai", api_key="your_key")
# Simple generation
response = llm.generate(
prompt="Explain quantum computing",
model="gpt-4-turbo",
temperature=0.7,
max_tokens=1000
)
print(response)
import LLMConnector
# Initialize image generator
image_gen = LLMConnector(provider="dalle", api_key="your_key")
# Generate image
image_url = image_gen.generate_image(
prompt="A futuristic cityscape at sunset",
model="dall-e-3",
size="1024x1024",
quality="hd"
)
print(f"Image generated at: {image_url}")
import LLMConnector
# Initialize chat
chat = LLMConnector(provider="anthropic", api_key="your_key")
# Start conversation
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What's the weather today?"}
]
# Get response
response = chat.chat_complete(
messages=messages,
model="claude-3-opus-20240229",
temperature=0.5
)
print(response)
import LLMConnector
# Initialize local Ollama model
local_llm = LLMConnector(
provider="ollama",
base_url="http://localhost:11434",
model="mistral"
)
# Generate text
response = local_llm.generate(
prompt="Explain the theory of relativity",
temperature=0.7
)
print(response)
Switch between different LLM providers with a single line of code. No need to rewrite your application when changing models.
Consistent API for text generation, chat completion, and image generation across all providers.
Environment variable support for API keys and sensitive configuration. Never hardcode credentials.
Track response times, token usage, and costs across different providers and models.
Automatic fallback to alternative models when primary model is unavailable or rate-limited.
Easy to add new providers or customize existing ones with plugin architecture.