Setting Up Providers
Setting Up Providers
Section titled “Setting Up Providers”This guide will help you set up and configure AI providers in UniCraft to start routing your AI requests.
Prerequisites
Section titled “Prerequisites”Before setting up providers, you’ll need:
- API keys for the providers you want to use
- UniCraft account
- Basic understanding of the providers you’re integrating
Supported Providers
Section titled “Supported Providers”UniCraft supports the following AI providers:
OpenAI
Section titled “OpenAI”- Models: GPT-4, GPT-3.5 Turbo, GPT-4 Turbo, Embeddings
- API: OpenAI API
- Documentation: OpenAI API Docs
Anthropic
Section titled “Anthropic”- Models: Claude 3 Haiku, Claude 3 Sonnet, Claude 3 Opus
- API: Anthropic API
- Documentation: Anthropic API Docs
- Models: Gemini Pro, Gemini Pro Vision, PaLM 2
- API: Google AI Studio API
- Documentation: Google AI Docs
Other Providers
Section titled “Other Providers”- Cohere
- Hugging Face
- Custom API endpoints
Step 1: Obtain API Keys
Section titled “Step 1: Obtain API Keys”OpenAI
Section titled “OpenAI”- Visit OpenAI Platform
- Sign up or log in to your account
- Navigate to API Keys section
- Click Create new secret key
- Copy and securely store your API key
Anthropic
Section titled “Anthropic”- Visit Anthropic Console
- Sign up or log in to your account
- Navigate to API Keys section
- Click Create Key
- Copy and securely store your API key
- Visit Google AI Studio
- Sign in with your Google account
- Navigate to Get API Key
- Create a new API key
- Copy and securely store your API key
Step 2: Add Providers via Dashboard
Section titled “Step 2: Add Providers via Dashboard”Using the Web Interface
Section titled “Using the Web Interface”-
Log into UniCraft Dashboard
- Navigate to your UniCraft dashboard
- Log in with your credentials
-
Access Provider Settings
- Click on Settings in the sidebar
- Select Providers from the settings menu
-
Add New Provider
- Click Add Provider button
- Select the provider type from the dropdown
-
Configure Provider
- Enter a descriptive name for the provider
- Paste your API key
- Select which models to enable
- Set provider priority (1 = highest priority)
- Configure rate limits if needed
-
Test Connection
- Click Test Connection to verify the setup
- Review the test results
-
Save Configuration
- Click Save to add the provider
Example Configuration
Section titled “Example Configuration”{ "name": "OpenAI Production", "type": "openai", "api_key": "sk-...", "models": ["gpt-4", "gpt-4-turbo", "gpt-3.5-turbo"], "priority": 1, "enabled": true, "rate_limits": { "requests_per_minute": 60, "tokens_per_minute": 100000 }}Step 3: Add Providers via API
Section titled “Step 3: Add Providers via API”Using the UniCraft API
Section titled “Using the UniCraft API”// Add OpenAI providerconst openaiProvider = await unicraft.providers.create({ name: "OpenAI Production", type: "openai", api_key: process.env.OPENAI_API_KEY, models: ["gpt-4", "gpt-3.5-turbo"], priority: 1, enabled: true, rate_limits: { requests_per_minute: 60, tokens_per_minute: 100000, },});
// Add Anthropic providerconst anthropicProvider = await unicraft.providers.create({ name: "Anthropic Production", type: "anthropic", api_key: process.env.ANTHROPIC_API_KEY, models: ["claude-3-sonnet", "claude-3-haiku"], priority: 2, enabled: true, rate_limits: { requests_per_minute: 50, tokens_per_minute: 80000, },});
// Add Google providerconst googleProvider = await unicraft.providers.create({ name: "Google Production", type: "google", api_key: process.env.GOOGLE_API_KEY, models: ["gemini-pro"], priority: 3, enabled: true, rate_limits: { requests_per_minute: 100, tokens_per_minute: 120000, },});Step 4: Configure Environment Variables
Section titled “Step 4: Configure Environment Variables”For security, store your API keys as environment variables:
.env File
Section titled “.env File”# OpenAIOPENAI_API_KEY=sk-your-openai-key-here
# AnthropicANTHROPIC_API_KEY=sk-ant-your-anthropic-key-here
# GoogleGOOGLE_API_KEY=your-google-api-key-here
# UniCraftUNICRAFT_API_KEY=your-unicraft-api-key-hereUsing Environment Variables in Code
Section titled “Using Environment Variables in Code”const providers = [ { name: "OpenAI", type: "openai", api_key: process.env.OPENAI_API_KEY, models: ["gpt-4", "gpt-3.5-turbo"], }, { name: "Anthropic", type: "anthropic", api_key: process.env.ANTHROPIC_API_KEY, models: ["claude-3-sonnet", "claude-3-haiku"], },];Step 5: Test Provider Configuration
Section titled “Step 5: Test Provider Configuration”Test Individual Providers
Section titled “Test Individual Providers”// Test OpenAI providerconst openaiTest = await unicraft.providers.test({ provider_id: "openai-production", test_request: { messages: [{ role: "user", content: "Hello, world!" }], model: "gpt-3.5-turbo", max_tokens: 10, },});
console.log("OpenAI Test Result:", openaiTest);Test All Providers
Section titled “Test All Providers”// Test all enabled providersconst allProviders = await unicraft.providers.list();const testResults = [];
for (const provider of allProviders) { if (provider.enabled) { const result = await unicraft.providers.test({ provider_id: provider.id, test_request: { messages: [{ role: "user", content: "Test message" }], model: provider.models[0], max_tokens: 10, }, }); testResults.push({ provider: provider.name, result }); }}
console.log("All Provider Tests:", testResults);Step 6: Configure Provider Settings
Section titled “Step 6: Configure Provider Settings”Rate Limiting
Section titled “Rate Limiting”Set appropriate rate limits for each provider:
const rateLimits = { openai: { requests_per_minute: 60, requests_per_hour: 1000, tokens_per_minute: 100000, tokens_per_hour: 1000000, }, anthropic: { requests_per_minute: 50, requests_per_hour: 800, tokens_per_minute: 80000, tokens_per_hour: 800000, },};Model Configuration
Section titled “Model Configuration”Configure which models to use from each provider:
const modelConfig = { openai: { default_model: "gpt-3.5-turbo", available_models: ["gpt-4", "gpt-4-turbo", "gpt-3.5-turbo"], model_preferences: { content_generation: "gpt-4", simple_qa: "gpt-3.5-turbo", code_generation: "gpt-4", }, },};Failover Configuration
Section titled “Failover Configuration”Set up automatic failover between providers:
const failoverConfig = { enabled: true, primary_provider: "openai", fallback_providers: ["anthropic", "google"], retry_attempts: 3, retry_delay: 1000, circuit_breaker: { enabled: true, failure_threshold: 5, recovery_timeout: 30000, },};Step 7: Monitor Provider Health
Section titled “Step 7: Monitor Provider Health”Health Check Configuration
Section titled “Health Check Configuration”const healthCheck = { enabled: true, interval: 60000, // Check every minute timeout: 5000, // 5 second timeout endpoints: { openai: "https://api.openai.com/v1/models", anthropic: "https://api.anthropic.com/v1/messages", google: "https://generativelanguage.googleapis.com/v1/models", },};Monitoring Dashboard
Section titled “Monitoring Dashboard”Monitor provider health in the UniCraft dashboard:
- Provider availability status
- Response time metrics
- Error rate tracking
- Usage statistics
Troubleshooting
Section titled “Troubleshooting”Common Issues
Section titled “Common Issues”-
Invalid API Key
- Verify the API key is correct
- Check if the key has expired
- Ensure the key has proper permissions
-
Rate Limit Exceeded
- Check your rate limit configuration
- Monitor usage patterns
- Consider upgrading your provider plan
-
Model Not Available
- Verify the model is available in your provider account
- Check if you have access to the model
- Update your model configuration
-
Network Issues
- Check your internet connection
- Verify firewall settings
- Test provider endpoints directly
Debug Mode
Section titled “Debug Mode”Enable debug mode to troubleshoot provider issues:
const response = await unicraft.chat.completions.create({ messages: [{ role: "user", content: "Hello" }], debug: true,});
console.log("Provider used:", response.debug.provider);console.log("Model used:", response.debug.model);console.log("Response time:", response.debug.response_time);console.log("Provider status:", response.debug.provider_status);Best Practices
Section titled “Best Practices”- Use Environment Variables: Never hardcode API keys in your code
- Set Appropriate Rate Limits: Configure rate limits based on your usage patterns
- Monitor Provider Health: Regularly check provider status and performance
- Implement Failover: Always have backup providers configured
- Test Thoroughly: Test all providers before deploying to production
- Regular Reviews: Periodically review and update provider configurations
- Security: Use encrypted storage for API keys and implement proper access controls
Next Steps
Section titled “Next Steps”After setting up your providers:
- Configure smart routing to optimize model selection
- Set up monitoring and alerts
- Test your configuration with various request types
- Implement failover and load balancing
- Monitor usage and optimize based on patterns