svector-sdk
Version:
Official JavaScript and TypeScript SDK for accessing SVECTOR APIs.
153 lines (152 loc) ⢠5.99 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.nodeJSExample = nodeJSExample;
const src_1 = require("../src");
async function nodeJSExample() {
console.log('Node.js SVECTOR SDK Example\n');
const client = new src_1.SVECTOR({
apiKey: process.env.SVECTOR_API_KEY,
});
try {
console.log('š Example 1: Simple Chat');
console.log('ā'.repeat(50));
const response = await client.chat.create({
model: 'spec-3-turbo',
messages: [
{ role: 'user', content: 'Explain Node.js in one paragraph' }
],
temperature: 0.7,
});
console.log('Question: Explain Node.js in one paragraph');
console.log(`Answer: ${response.choices[0].message.content}\n`);
console.log(' Example 2: Express.js Integration Pattern');
console.log('ā'.repeat(50));
async function handleChatRequest(userMessage, conversationHistory = []) {
try {
const messages = [
...conversationHistory,
{ role: 'user', content: userMessage }
];
const response = await client.chat.create({
model: 'spec-3-turbo',
messages,
max_tokens: 500,
});
return {
success: true,
message: response.choices[0].message.content,
requestId: response._request_id,
};
}
catch (error) {
return {
success: false,
error: error instanceof Error ? error.message : 'Unknown error',
};
}
}
const result1 = await handleChatRequest('What is serverless computing?');
console.log('API Response 1:', result1);
const result2 = await handleChatRequest('Give me an example', [
{ role: 'user', content: 'What is serverless computing?' },
{ role: 'assistant', content: result1.message }
]);
console.log('API Response 2:', result2);
console.log('\n Example 3: Batch Processing');
console.log('ā'.repeat(50));
const questions = [
'What is the capital of France?',
'How does photosynthesis work?',
'What is the difference between AI and ML?'
];
const batchResults = await Promise.all(questions.map(async (question, index) => {
try {
const response = await client.chat.create({
model: 'spec-3-turbo',
messages: [{ role: 'user', content: question }],
max_tokens: 100,
});
return {
id: index + 1,
question,
answer: response.choices[0].message.content,
success: true,
};
}
catch (error) {
return {
id: index + 1,
question,
error: error instanceof Error ? error.message : 'Unknown error',
success: false,
};
}
}));
console.log('Batch Results:');
batchResults.forEach(result => {
console.log(`${result.id}. ${result.question}`);
if (result.success) {
console.log(` ${result.answer}`);
}
else {
console.log(` Error: ${result.error}`);
}
});
console.log('\n Example 4: Configuration Patterns');
console.log('ā'.repeat(50));
const devClient = new src_1.SVECTOR({
apiKey: process.env.SVECTOR_API_KEY,
timeout: 30000,
maxRetries: 1,
});
const prodClient = new src_1.SVECTOR({
apiKey: process.env.SVECTOR_API_KEY,
timeout: 120000,
maxRetries: 3,
});
console.log('Development client configured');
console.log('Production client configured');
console.log('\n Example 5: Error Handling Patterns');
console.log('ā'.repeat(50));
async function robustChatCall(message, retries = 3) {
for (let attempt = 1; attempt <= retries; attempt++) {
try {
const response = await client.chat.create({
model: 'spec-3-turbo',
messages: [{ role: 'user', content: message }],
});
return {
success: true,
data: response.choices[0].message.content,
attempt,
};
}
catch (error) {
console.log(`Attempt ${attempt} failed:`, error instanceof Error ? error.message : error);
if (attempt === retries) {
return {
success: false,
error: error instanceof Error ? error.message : 'Unknown error',
attempts: attempt,
};
}
await new Promise(resolve => setTimeout(resolve, 1000 * attempt));
}
}
}
const robustResult = await robustChatCall('Test robust error handling');
console.log('Robust call result:', robustResult);
console.log('\n⨠Node.js examples completed successfully!');
}
catch (error) {
console.error('Example failed:', error);
process.exit(1);
}
}
if (require.main === module) {
if (!process.env.SVECTOR_API_KEY) {
console.error('Please set the SVECTOR_API_KEY environment variable');
process.exit(1);
}
nodeJSExample().catch(console.error);
}