@relayplane/sdk
Version:
RelayPlane SDK with zero-config AI access, intelligent model selection, built-in examples, and contextual error handling. The easiest way to add AI to your app with automatic optimization and fallback.
136 lines (113 loc) ⢠4.6 kB
JavaScript
/**
* Claude Integration Example
*
* This example demonstrates how to use the RelayPlane SDK with Claude models
* using your own Anthropic API key (BYOK - Bring Your Own Keys).
*/
const { relay } = require('@relayplane/sdk');
const RelayPlane = require('@relayplane/sdk');
async function runClaudeExample() {
console.log('š¤ RelayPlane SDK - Claude Example\n');
// Example 1: Basic Claude Usage
console.log('--- Example 1: Basic Claude Usage ---');
console.log('Using Claude with your Anthropic API key (requires ANTHROPIC_API_KEY)\n');
try {
const response = await relay({
to: 'claude-3-sonnet',
payload: {
model: 'claude-3-sonnet-20240229',
max_tokens: 1000,
messages: [
{
role: 'user',
content: 'Explain quantum computing in simple terms.'
}
]
},
metadata: {
example: 'claude-basic',
timestamp: new Date().toISOString()
}
});
console.log('ā
Response received:');
console.log('Relay ID:', response.relay_id);
console.log('Status Code:', response.status_code);
console.log('Latency:', response.latency_ms + 'ms');
console.log('Response:', response.body.content[0].text.substring(0, 200) + '...\n');
} catch (error) {
console.error('ā Request failed:', error.message);
console.log('Make sure ANTHROPIC_API_KEY is set in your environment.\n');
}
// Example 2: Zero-Config Smart Usage
console.log('--- Example 2: Zero-Config Smart Usage ---');
console.log('Using RelayPlane\'s intelligent features with Claude\n');
try {
const result = await RelayPlane.ask('Write a haiku about artificial intelligence', {
taskType: 'creative',
priority: 'quality'
});
console.log('ā
Smart response received:');
console.log('Selected Model:', result.reasoning.selectedModel);
console.log('Reasoning:', result.reasoning.rationale);
console.log('Response:', result.response.body);
console.log('Latency:', result.latency_ms + 'ms\n');
} catch (error) {
console.error('ā Smart usage failed:', error.message);
console.log('Make sure ANTHROPIC_API_KEY is set in your environment.\n');
}
// Example 3: Built-in Examples
console.log('--- Example 3: Built-in Examples ---');
console.log('Using RelayPlane\'s ready-to-use patterns\n');
try {
// Summarization example
const longText = "Artificial intelligence (AI) is intelligence demonstrated by machines, in contrast to the natural intelligence displayed by humans and animals. Leading AI textbooks define the field as the study of 'intelligent agents': any device that perceives its environment and takes actions that maximize its chance of successfully achieving its goals.";
const summary = await RelayPlane.examples.summarize(longText, {
length: 'brief'
});
console.log('ā
Summarization example:');
console.log('Summary:', summary.response.body);
// Translation example
const translation = await RelayPlane.examples.translate('Hello, how are you?', 'French');
console.log('ā
Translation example:');
console.log('Translation:', translation.response.body);
} catch (error) {
console.error('ā Built-in examples failed:', error.message);
console.log('Make sure ANTHROPIC_API_KEY is set in your environment.\n');
}
// Example 4: Error Handling
console.log('--- Example 4: Error Handling ---');
console.log('Demonstrating proper error handling with RelayPlane SDK\n');
try {
const response = await relay({
to: 'claude-3-opus',
payload: {
model: 'claude-3-opus-20240229',
max_tokens: 2000,
messages: [
{
role: 'user',
content: 'This is a test of error handling and timeout behavior.'
}
]
}
}, {
timeout: 5000 // 5 second timeout for demo
});
console.log('ā
Error handling test completed successfully');
} catch (error) {
if (error.name === 'RelayTimeoutError') {
console.log('ā±ļø Request timed out as expected');
} else if (error.name === 'RelayAuthError') {
console.log('š Authentication failed - check your API keys');
} else {
console.log('š Other error:', error.message);
}
}
console.log('\nš Claude example completed!');
console.log('š” Set ANTHROPIC_API_KEY to unlock all Claude models and features.');
}
// Run the example
if (require.main === module) {
runClaudeExample().catch(console.error);
}
module.exports = { runClaudeExample };