LLM Providers¶
faker-news works with any OpenAI-compatible LLM API. This guide provides detailed setup instructions for popular providers.
OpenAI¶
Setup¶
Recommended Models¶
| Purpose | Model | Speed | Quality | Cost |
|---|---|---|---|---|
| Headlines | gpt-4o-mini |
⚡⚡⚡ | ⭐⭐⭐ | 💰 |
| Headlines | gpt-3.5-turbo |
⚡⚡⚡ | ⭐⭐ | 💰 |
| Articles | gpt-4o-mini |
⚡⚡⚡ | ⭐⭐⭐ | 💰 |
| Articles (Quality) | gpt-4o |
⚡⚡ | ⭐⭐⭐⭐⭐ | 💰💰💰 |
| Articles (Quality) | gpt-4-turbo |
⚡⚡ | ⭐⭐⭐⭐ | 💰💰💰 |
Example Configuration¶
from faker import Faker
from faker_news import NewsProvider, LLMClientConfig
# Balanced setup (recommended)
llm_config = LLMClientConfig(
model_headlines="gpt-4o-mini", # Fast and cheap
model_writing="gpt-4o-mini" # Good quality
)
# Quality-focused setup
llm_config = LLMClientConfig(
model_headlines="gpt-4o-mini", # Headlines don't need premium
model_writing="gpt-4o" # Best quality for articles
)
fake = Faker()
fake.add_provider(NewsProvider(fake, llm_config=llm_config))
Getting an API Key¶
- Sign up at platform.openai.com
- Navigate to API keys
- Create a new secret key
- Copy and store securely
Alibaba DashScope (Qwen)¶
Setup¶
Recommended Models¶
| Purpose | Model | Speed | Quality | Cost |
|---|---|---|---|---|
| Headlines | qwen-flash |
⚡⚡⚡ | ⭐⭐⭐ | 💰 |
| Headlines | qwen-turbo |
⚡⚡ | ⭐⭐⭐⭐ | 💰💰 |
| Articles | qwen-flash |
⚡⚡⚡ | ⭐⭐⭐ | 💰 |
| Articles (Quality) | qwen-plus |
⚡⚡ | ⭐⭐⭐⭐ | 💰💰 |
| Articles (Quality) | qwen-max |
⚡ | ⭐⭐⭐⭐⭐ | 💰💰💰 |
Example Configuration¶
from faker import Faker
from faker_news import NewsProvider, LLMClientConfig
# Balanced setup
llm_config = LLMClientConfig(
model_headlines="qwen-flash", # Fast and cheap
model_writing="qwen-plus" # Better quality
)
fake = Faker()
fake.add_provider(NewsProvider(fake, llm_config=llm_config))
Getting an API Key¶
- Sign up at Alibaba Cloud Model Studio
- Enable DashScope API
- Create an API key
- Use international endpoint for global access
Azure OpenAI¶
Setup¶
from faker_news import LLMClientConfig
llm_config = LLMClientConfig(
api_key="your-azure-api-key",
base_url="https://{your-resource-name}.openai.azure.com/openai/deployments/{deployment-name}",
model_headlines="gpt-4o-mini", # Your deployment name
model_writing="gpt-4" # Your deployment name
)
Example Configuration¶
from faker import Faker
from faker_news import NewsProvider, LLMClientConfig
# Azure OpenAI setup
llm_config = LLMClientConfig(
api_key="abc123...",
base_url="https://my-openai.openai.azure.com/openai/deployments/gpt-4o-mini",
model_headlines="gpt-4o-mini",
model_writing="gpt-4"
)
fake = Faker()
fake.add_provider(NewsProvider(fake, llm_config=llm_config))
Deployment Names
Use your Azure deployment names as the model names. These may differ from OpenAI's model names.
Getting Started with Azure OpenAI¶
- Create an Azure account
- Create an Azure OpenAI resource
- Deploy models (e.g., gpt-4o-mini, gpt-4)
- Get API key and endpoint from Azure portal
Other OpenAI-Compatible APIs¶
faker-news works with any API that implements the OpenAI chat completions interface.
Generic Setup¶
from faker_news import LLMClientConfig
llm_config = LLMClientConfig(
api_key="your-api-key",
base_url="https://your-api-endpoint.com/v1",
model_headlines="your-fast-model",
model_writing="your-quality-model"
)
Compatible Providers¶
These providers have OpenAI-compatible APIs:
- Anthropic (via compatibility layer)
- Groq - Ultra-fast inference
- Together.ai - Multiple open models
- Perplexity - Search-augmented models
- Anyscale - Llama and Mistral models
- DeepSeek - Chinese LLM provider
- Local models (via LM Studio, Ollama, vLLM, etc.)
Example: Local LLM with Ollama¶
from faker_news import LLMClientConfig
# Run Ollama locally (ollama serve)
llm_config = LLMClientConfig(
api_key="not-needed", # Ollama doesn't require API key
base_url="http://localhost:11434/v1",
model_headlines="llama3.1",
model_writing="llama3.1"
)
Example: Groq¶
from faker_news import LLMClientConfig
llm_config = LLMClientConfig(
api_key="gsk_...",
base_url="https://api.groq.com/openai/v1",
model_headlines="llama-3.1-70b-versatile",
model_writing="llama-3.1-70b-versatile"
)
Choosing a Provider¶
By Use Case¶
| Use Case | Recommended Provider | Why |
|---|---|---|
| Production | OpenAI | Best reliability and quality |
| Development | OpenAI (gpt-4o-mini) | Good balance of cost/quality |
| High-volume | Alibaba Qwen | Cost-effective for bulk |
| Local/Offline | Ollama + local models | No API costs, privacy |
| Fast inference | Groq | Ultra-fast responses |
| Budget-conscious | Alibaba Qwen | Lowest cost per token |
By Region¶
| Region | Recommended Provider |
|---|---|
| North America | OpenAI, Azure OpenAI |
| Europe | OpenAI, Azure OpenAI |
| Asia-Pacific | Alibaba DashScope |
| China | Alibaba DashScope |
| Global | OpenAI |
By Requirements¶
Need best quality? → OpenAI GPT-4o
Need lowest cost? → Alibaba Qwen Flash
Need fastest speed? → Groq with Llama
Need privacy? → Local models (Ollama)
Need reliability? → OpenAI or Azure OpenAI
Cost Optimization¶
Model Selection Strategy¶
from faker_news import LLMClientConfig
# Cost-optimized: use cheapest models
llm_config = LLMClientConfig(
model_headlines="gpt-4o-mini", # Cheap for simple headlines
model_writing="gpt-4o-mini" # Still good quality
)
# Quality-optimized: use better model for articles only
llm_config = LLMClientConfig(
model_headlines="gpt-4o-mini", # Cheap for headlines
model_writing="gpt-4o" # Premium for articles
)
Batch Size Tuning¶
Larger batches = fewer API calls = lower cost:
from faker_news import NewsProvider
# Cost-optimized setup
provider = NewsProvider(
fake,
llm_config=llm_config,
headline_batch=200, # Large batches
article_batch=50
)
Caching Strategy¶
Maximize cache reuse to minimize API calls:
from faker import Faker
from faker_news import NewsProvider
fake = Faker()
fake.add_provider(NewsProvider(fake))
# Preload once, use many times
fake.news_preload_headlines(1000)
# Reuse content
headlines = [fake.news_headline() for _ in range(500)]
fake.news_reset("reuse")
headlines = [fake.news_headline() for _ in range(500)] # No new API calls
Troubleshooting¶
API Key Not Working¶
- Verify key format matches provider
- Check environment variables:
echo $OPENAI_API_KEY - Test with curl:
Connection Errors¶
- Verify base URL is correct
- Check network connectivity
- Verify API endpoint is accessible
- Check for firewall/proxy issues
Rate Limiting¶
If you hit rate limits:
- Reduce batch sizes
- Add delays between generations
- Upgrade to higher tier
- Switch to provider with higher limits
Model Not Found¶
If you get "model not found" errors:
- Verify model name is correct
- Check if you have access to that model
- For Azure: use deployment name, not model name
Next Steps¶
- Configuration - Customize batch sizes and settings
- API Reference - Complete API documentation