@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.
112 lines (96 loc) • 3.43 kB
JavaScript
/**
* RelayPlane SDK - Quick Start Example
*
* Copy this file and run it to get started with RelayPlane in under 2 minutes!
*
* Prerequisites:
* 1. npm install @relayplane/sdk
* 2. Set at least one API key:
* export OPENAI_API_KEY="sk-..."
* export ANTHROPIC_API_KEY="sk-ant-..."
* export GOOGLE_API_KEY="..."
* OR export RELAY_API_KEY="rp_..."
*
* Usage: node quick-start.js
*/
const RelayPlane = require('@relayplane/sdk');
async function quickStart() {
console.log('🚀 RelayPlane SDK Quick Start\n');
// Example 1: Zero-Config Magic ✨
console.log('--- Example 1: Zero-Config Magic ---');
try {
const result = await RelayPlane.ask("What is the capital of France?");
console.log('✅ Answer:', result.response.body);
console.log('🧠 Model used:', result.reasoning.selectedModel);
console.log('💡 Why:', result.reasoning.rationale);
console.log();
} catch (error) {
console.log('❌ Error:', error.message);
console.log('💡 Tip: Set an API key environment variable\n');
}
// Example 2: Built-in Examples 📚
console.log('--- Example 2: Built-in Examples ---');
try {
// Summarize text
const summary = await RelayPlane.examples.summarize(
"Artificial intelligence is transforming industries worldwide. From healthcare to finance, AI systems are becoming more sophisticated and capable of handling complex tasks that previously required human intelligence.",
{ length: 'brief' }
);
console.log('📝 Summary:', summary);
// Translate text
const translation = await RelayPlane.examples.translate(
"Hello, how are you today?",
"Spanish"
);
console.log('🌍 Translation:', translation);
console.log();
} catch (error) {
console.log('❌ Examples error:', error.message, '\n');
}
// Example 3: Manual Control 🎛️
console.log('--- Example 3: Manual Control ---');
try {
const response = await RelayPlane.relay({
to: 'claude-3-5-haiku-20241022',
payload: {
messages: [
{ role: 'user', content: 'Write a haiku about coding' }
],
max_tokens: 100,
temperature: 0.8
}
});
console.log('🎨 Haiku:', response.body.content[0].text);
console.log('⚡ Response time:', response.latency_ms + 'ms');
console.log();
} catch (error) {
console.log('❌ Manual control error:', error.message, '\n');
}
// Example 4: Error Handling 🛡️
console.log('--- Example 4: Smart Error Handling ---');
try {
// This will demonstrate error handling
await RelayPlane.relay({
to: 'invalid-model',
payload: { messages: [{ role: 'user', content: 'test' }] }
});
} catch (error) {
if (error.getHelpfulMessage) {
console.log('🔧 Helpful error message:');
console.log(error.getHelpfulMessage());
} else {
console.log('⚠️ Standard error:', error.message);
}
console.log();
}
console.log('🎉 Quick start complete!');
console.log('📖 Next steps:');
console.log(' • Read the full docs: https://relayplane.com/docs/sdk');
console.log(' • Try the interactive demo: node examples/interactive-demo.js');
console.log(' • Explore more examples in the examples/ directory');
}
// Run the quick start
if (require.main === module) {
quickStart().catch(console.error);
}
module.exports = { quickStart };