@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.
192 lines (163 loc) ⢠5.74 kB
JavaScript
/**
* RelayPlane SDK - GPT Examples (v0.2)
*
* This example demonstrates how to use RelayPlane v0.2 with OpenAI GPT models,
* showcasing both the new Zero-Config Wins and advanced features.
*/
const RelayPlane = require('@relayplane/sdk');
async function runGPTExample() {
console.log('š§ RelayPlane SDK v0.2 - GPT Examples\n');
// Example 1: Zero-Config Win
console.log('--- Example 1: Zero-Config Win ---');
console.log('Using RelayPlane.ask() with intelligent model selection\n');
try {
const result = await RelayPlane.ask(
"What are the key differences between neural networks and traditional algorithms?",
{
budget: 'moderate',
priority: 'quality',
taskType: 'analysis'
}
);
console.log('ā
Magic response received:');
console.log('Selected Model:', result.reasoning.selectedModel);
console.log('Reasoning:', result.reasoning.rationale);
console.log('Response:', result.response.body.substring(0, 200) + '...\n');
} catch (error) {
console.error('ā Magic mode failed:', error.message);
if (error.getHelpfulMessage) {
console.log(error.getHelpfulMessage());
}
console.log('Tip: Set OPENAI_API_KEY, ANTHROPIC_API_KEY, or RELAYPLANE_API_KEY\n');
}
// Example 2: Built-in Examples
console.log('--- Example 2: Built-in Examples ---');
console.log('Using ready-to-use patterns\n');
try {
// Creative writing example
const story = await RelayPlane.examples.creativeWriting(
"A robot learning to paint",
{
style: "short story",
length: "exactly 100 words"
}
);
console.log('ā
Creative story generated:');
console.log(story.substring(0, 200) + '...\n');
// Code explanation example
const explanation = await RelayPlane.examples.explain(
"How do neural networks work?",
{
level: "beginner",
format: "simple"
}
);
console.log('ā
Explanation generated:');
console.log(explanation.substring(0, 200) + '...\n');
} catch (error) {
console.error('ā Examples failed:', error.message);
console.log('Tip: These examples work with any available API keys\n');
}
// Example 3: Advanced Relay Usage
console.log('--- Example 3: Advanced Relay Control ---');
console.log('Using RelayPlane.relay() for full control\n');
try {
const response = await RelayPlane.relay({
to: 'gpt-4',
payload: {
max_tokens: 300,
temperature: 0.9,
messages: [
{
role: 'system',
content: 'You are a helpful AI assistant that explains complex topics clearly.'
},
{
role: 'user',
content: 'Explain quantum computing in simple terms'
}
]
},
metadata: {
example: 'gpt-advanced',
timestamp: new Date().toISOString()
}
});
console.log('ā
Advanced 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.choices[0].message.content.substring(0, 200) + '...\n');
} catch (error) {
console.error('ā Advanced mode failed:', error.message);
console.log('This requires valid API credentials.\n');
}
// Example 4: Hosted Mode with Configuration
console.log('--- Example 4: Hosted Mode Configuration ---');
console.log('Using RelayPlane hosted service for optimization\n');
// Configure for hosted mode
RelayPlane.configure({
apiKey: process.env.RELAYPLANE_API_KEY,
debug: true,
defaultOptimization: {
enabled: true,
strategy: 'balanced'
}
});
try {
const response = await RelayPlane.relay({
to: 'gpt-3.5-turbo',
payload: {
max_tokens: 150,
temperature: 0.7,
messages: [
{
role: 'user',
content: 'Write a haiku about artificial intelligence'
}
]
},
optimization: {
enabled: true,
fallback: ['gpt-4', 'claude-3-sonnet-20240229']
}
});
console.log('ā
Hosted response received:');
console.log('Relay ID:', response.relay_id);
console.log('Optimized:', response.optimized || false);
console.log('Haiku:', response.body.choices[0].message.content + '\n');
} catch (error) {
console.error('ā Hosted mode failed:', error.message);
console.log('Get a free API key at https://relayplane.com\n');
}
// Example 5: Temperature Comparison
console.log('--- Example 5: Temperature Comparison ---');
console.log('Showing how temperature affects creativity\n');
const prompt = 'Write a one-sentence description of a magical forest.';
for (const temp of [0.2, 0.7, 1.0]) {
try {
const result = await RelayPlane.ask(prompt, {
budget: 'minimal',
priority: 'speed',
config: {
temperature: temp,
max_tokens: 50
}
});
console.log(`Temperature ${temp}:`, result.response.body.trim());
} catch (error) {
console.log(`Temperature ${temp}: Failed (${error.message})`);
}
}
console.log('\nš GPT examples completed!');
console.log('š” Next steps:');
console.log(' ⢠Try RelayPlane.ask() for zero-config magic');
console.log(' ⢠Explore RelayPlane.examples.* for common patterns');
console.log(' ⢠Use RelayPlane.relay() for full control');
console.log(' ⢠Get hosted features at https://relayplane.com');
}
// Run the example
if (require.main === module) {
runGPTExample().catch(console.error);
}
module.exports = { runGPTExample };