A unified interface for multiple AI language model providers. This wrapper provides a consistent API to interact with various AI providers while handling provider-specific configurations and fallback mechanisms.
- Unified Interface: Single API to interact with multiple AI providers
-
Provider Support:
- OpenAI (GPT-4, GPT-3.5)
- Anthropic (Claude)
- Error Handling: Robust error handling with Turbot error types
- Logging: Structured logging with Turbot logging utilities
- Type Safety: Full TypeScript support
- Proxy Support: Built-in support for HTTP/HTTPS proxies
npm install guardrails-lib-ai
const AI = require('guardrails-lib-ai');
// Initialize with OpenAI using direct API key and proxy
const ai = new AI({
provider: 'openai',
modelName: 'gpt-4',
apiKey: 'your-openai-api-key', // Direct API key
system: 'You are a helpful AI assistant.',
proxyUrl: 'http://your-proxy-server:port' // Optional proxy configuration
});
// Query the model
try {
const response = await ai.generate('Explain quantum computing in simple terms');
console.log(response);
} catch (error) {
// Errors are instances of Turbot error types
if (error.name === 'ProviderError') {
console.error('Provider error:', error.message);
} else if (error.name === 'BadConfigurationError') {
console.error('Configuration error:', error.message);
console.error('Missing parameters:', error.missingParams);
} else {
console.error('Unexpected error:', error.message);
}
}
// Initialize with Anthropic
const claude = new AI({
provider: 'anthropic',
modelName: 'claude-3-opus-20240229',
apiKey: 'your-anthropic-api-key', // Direct API key
system: 'You are a helpful AI assistant.'
});
// Query with streaming
try {
const response = await claude.generate('Write a short poem about AI');
console.log(response);
} catch (error) {
console.error('Error:', error);
}
The library supports proxy configuration in two ways:
-
Direct Configuration:
const ai = new AI({ provider: 'openai', modelName: 'gpt-4', proxyUrl: 'http://your-proxy-server:port' });
-
No Proxy:
const ai = new AI({ provider: 'openai', modelName: 'gpt-4' // No proxy configuration });
The proxy configuration is supported for both OpenAI and Anthropic providers. When using a proxy:
- All API requests will be routed through the specified proxy server
- The proxy configuration is applied consistently across all providers
- Both HTTP and HTTPS proxies are supported
The wrapper uses Turbot's error handling system (@turbot/errors
) to provide structured error types:
-
BadConfigurationError
: Thrown when required configuration is missingtry { const ai = new AI({}); // Missing required config } catch (error) { if (error.name === 'BadConfigurationError') { console.error('Missing parameters:', error.missingParams); } }
-
ProviderError
: Thrown for provider-specific issuestry { await ai.generate('prompt'); } catch (error) { if (error.name === 'ProviderError') { console.error('Provider error:', error.message); console.error('Provider:', error.provider); } }
-
ModelError
: Thrown for model-specific issuestry { await ai.generate('prompt'); } catch (error) { if (error.name === 'ModelError') { console.error('Model error:', error.message); console.error('Model:', error.modelName); } }
The AI
constructor accepts the following configuration options:
{
provider: string; // The AI provider to use ('openai' or 'anthropic')
modelName: string; // The specific model to use (e.g., 'gpt-4', 'claude-3-opus-20240229')
apiKey: string; // API key (required)
system?: string; // Optional system prompt
proxyUrl?: string; // Optional proxy URL
max_tokens?: number; // Optional max tokens
temperature?: number; // Optional temperature
}
You must provide API keys directly in the configuration:
const ai = new AI({
provider: 'openai',
modelName: 'gpt-4',
apiKey: 'your-api-key-here' // Direct API key (required)
});
- OpenAI
- Anthropic
Requires Node.js >= 18.0.0