|
| 1 | +import fs from 'fs'; |
| 2 | +import { SVECTOR } from '../src/index'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Advanced SVECTOR Vision API Examples |
| 6 | + * Demonstrates advanced vision capabilities and API patterns |
| 7 | + */ |
| 8 | + |
| 9 | +const client = new SVECTOR({ |
| 10 | + apiKey: process.env.SVECTOR_API_KEY, |
| 11 | +}); |
| 12 | + |
| 13 | +/** |
| 14 | + * Example 1: SVECTOR-style responses.create with image URL |
| 15 | + */ |
| 16 | +async function svectorStyleURL() { |
| 17 | + console.log('🔗 SVECTOR-style URL analysis...'); |
| 18 | + |
| 19 | + try { |
| 20 | + const response = await client.vision.createResponse({ |
| 21 | + model: "spec-3-turbo", |
| 22 | + input: [{ |
| 23 | + role: "user", |
| 24 | + content: [ |
| 25 | + { type: "input_text", text: "what's in this image?" }, |
| 26 | + { |
| 27 | + type: "input_image", |
| 28 | + image_url: "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg", |
| 29 | + }, |
| 30 | + ], |
| 31 | + }], |
| 32 | + }); |
| 33 | + |
| 34 | + console.log('Analysis:', response.output_text); |
| 35 | + } catch (error) { |
| 36 | + console.error('Error:', error); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Example 2: SVECTOR-style with base64 image |
| 42 | + */ |
| 43 | +async function svectorStyleBase64() { |
| 44 | + console.log('📁 SVECTOR-style base64 analysis...'); |
| 45 | + |
| 46 | + try { |
| 47 | + const imagePath = "./sample-image.jpg"; |
| 48 | + const base64Image = fs.readFileSync(imagePath, "base64"); |
| 49 | + |
| 50 | + const response = await client.vision.createResponse({ |
| 51 | + model: "spec-3-turbo", |
| 52 | + input: [ |
| 53 | + { |
| 54 | + role: "user", |
| 55 | + content: [ |
| 56 | + { type: "input_text", text: "what's in this image?" }, |
| 57 | + { |
| 58 | + type: "input_image", |
| 59 | + image_url: `data:image/jpeg;base64,${base64Image}`, |
| 60 | + }, |
| 61 | + ], |
| 62 | + }, |
| 63 | + ], |
| 64 | + }); |
| 65 | + |
| 66 | + console.log('Analysis:', response.output_text); |
| 67 | + } catch (error) { |
| 68 | + console.error('Error:', error); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * Example 3: SVECTOR-style with file ID |
| 74 | + */ |
| 75 | +async function svectorStyleFileID() { |
| 76 | + console.log('📤 SVECTOR-style file ID analysis...'); |
| 77 | + |
| 78 | + try { |
| 79 | + // Upload file first |
| 80 | + const fileContent = fs.createReadStream("./sample-image.jpg"); |
| 81 | + const fileResult = await client.files.create(fileContent, "default"); |
| 82 | + const fileId = fileResult.file_id; |
| 83 | + |
| 84 | + const response = await client.vision.createResponse({ |
| 85 | + model: "spec-3-turbo", |
| 86 | + input: [ |
| 87 | + { |
| 88 | + role: "user", |
| 89 | + content: [ |
| 90 | + { type: "input_text", text: "what's in this image?" }, |
| 91 | + { |
| 92 | + type: "input_image", |
| 93 | + file_id: fileId, |
| 94 | + }, |
| 95 | + ], |
| 96 | + }, |
| 97 | + ], |
| 98 | + }); |
| 99 | + |
| 100 | + console.log('File ID:', fileId); |
| 101 | + console.log('Analysis:', response.output_text); |
| 102 | + } catch (error) { |
| 103 | + console.error('Error:', error); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * Example 4: Batch processing with enhanced features |
| 109 | + */ |
| 110 | +async function batchImageProcessing() { |
| 111 | + console.log('📊 Batch image processing...'); |
| 112 | + |
| 113 | + try { |
| 114 | + const images = [ |
| 115 | + { |
| 116 | + image_url: 'https://example.com/product1.jpg', |
| 117 | + prompt: 'Describe this product and its key features' |
| 118 | + }, |
| 119 | + { |
| 120 | + image_url: 'https://example.com/product2.jpg', |
| 121 | + prompt: 'What is the main selling point of this product?' |
| 122 | + }, |
| 123 | + { |
| 124 | + image_url: 'https://example.com/product3.jpg', |
| 125 | + prompt: 'Who is the target audience for this product?' |
| 126 | + } |
| 127 | + ]; |
| 128 | + |
| 129 | + const results = await client.vision.batchAnalyze(images, { |
| 130 | + model: 'spec-3-turbo', |
| 131 | + max_tokens: 300, |
| 132 | + delay: 1500 // 1.5 second delay between requests |
| 133 | + }); |
| 134 | + |
| 135 | + results.forEach((result: { analysis: string; usage?: any; error?: string }, index: number) => { |
| 136 | + if (result.error) { |
| 137 | + console.log(`Image ${index + 1}: Error - ${result.error}`); |
| 138 | + } else { |
| 139 | + console.log(`Image ${index + 1}: ${result.analysis}`); |
| 140 | + console.log(`Tokens used: ${result.usage?.total_tokens || 'N/A'}`); |
| 141 | + } |
| 142 | + console.log('---'); |
| 143 | + }); |
| 144 | + } catch (error) { |
| 145 | + console.error('Error in batch processing:', error); |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +/** |
| 150 | + * Example 5: Confidence scoring |
| 151 | + */ |
| 152 | +async function confidenceScoring() { |
| 153 | + console.log('🎯 Image analysis with confidence scoring...'); |
| 154 | + |
| 155 | + try { |
| 156 | + const result = await client.vision.analyzeWithConfidence({ |
| 157 | + image_url: 'https://example.com/medical-scan.jpg', |
| 158 | + prompt: 'Analyze this medical image and identify any abnormalities', |
| 159 | + model: 'spec-3-turbo' |
| 160 | + }); |
| 161 | + |
| 162 | + console.log('Analysis:', result.analysis); |
| 163 | + console.log('Confidence:', result.confidence ? `${result.confidence}%` : 'Not provided'); |
| 164 | + } catch (error) { |
| 165 | + console.error('Error:', error); |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +/** |
| 170 | + * Example 6: Social media caption generation |
| 171 | + */ |
| 172 | +async function socialMediaCaptions() { |
| 173 | + console.log('📱 Social media caption generation...'); |
| 174 | + |
| 175 | + try { |
| 176 | + const imageUrl = 'https://example.com/vacation-photo.jpg'; |
| 177 | + |
| 178 | + // Professional caption |
| 179 | + const professional = await client.vision.generateCaption({ |
| 180 | + image_url: imageUrl |
| 181 | + }, 'professional'); |
| 182 | + |
| 183 | + // Casual caption |
| 184 | + const casual = await client.vision.generateCaption({ |
| 185 | + image_url: imageUrl |
| 186 | + }, 'casual'); |
| 187 | + |
| 188 | + // Funny caption |
| 189 | + const funny = await client.vision.generateCaption({ |
| 190 | + image_url: imageUrl |
| 191 | + }, 'funny'); |
| 192 | + |
| 193 | + console.log('Professional:', professional.analysis); |
| 194 | + console.log('Casual:', casual.analysis); |
| 195 | + console.log('Funny:', funny.analysis); |
| 196 | + } catch (error) { |
| 197 | + console.error('Error:', error); |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +/** |
| 202 | + * Example 7: Advanced vision with streaming |
| 203 | + */ |
| 204 | +async function streamingVisionAnalysis() { |
| 205 | + console.log('🌊 Streaming vision analysis...'); |
| 206 | + |
| 207 | + try { |
| 208 | + const stream = await client.conversations.createStream({ |
| 209 | + model: 'spec-3-turbo', |
| 210 | + instructions: 'You are an expert art critic and historian. Provide detailed analysis.', |
| 211 | + input: 'Analyze this artwork in detail, including style, composition, historical context, and artistic techniques.', |
| 212 | + // Note: For full image + text streaming, use chat.createStream with proper message format |
| 213 | + stream: true, |
| 214 | + max_tokens: 1500 |
| 215 | + }); |
| 216 | + |
| 217 | + console.log('Art analysis: '); |
| 218 | + for await (const chunk of stream) { |
| 219 | + if (!chunk.done) { |
| 220 | + process.stdout.write(chunk.content); |
| 221 | + } |
| 222 | + } |
| 223 | + console.log('\n✅ Analysis complete'); |
| 224 | + } catch (error) { |
| 225 | + console.error('Error in streaming analysis:', error); |
| 226 | + } |
| 227 | +} |
| 228 | + |
| 229 | +/** |
| 230 | + * Example 8: Technical image analysis |
| 231 | + */ |
| 232 | +async function technicalAnalysis() { |
| 233 | + console.log('🔬 Technical image analysis...'); |
| 234 | + |
| 235 | + try { |
| 236 | + const result = await client.vision.analyzeFromUrl( |
| 237 | + 'https://example.com/circuit-board.jpg', |
| 238 | + 'Provide a technical analysis of this electronic circuit board. Identify components, trace paths, and assess the overall design.', |
| 239 | + { |
| 240 | + model: 'spec-3-turbo', |
| 241 | + max_tokens: 1200, |
| 242 | + temperature: 0.1, // Low temperature for technical accuracy |
| 243 | + detail: 'high' |
| 244 | + } |
| 245 | + ); |
| 246 | + |
| 247 | + console.log('Technical analysis:', result.analysis); |
| 248 | + console.log('Token usage:', result.usage); |
| 249 | + } catch (error) { |
| 250 | + console.error('Error:', error); |
| 251 | + } |
| 252 | +} |
| 253 | + |
| 254 | +/** |
| 255 | + * Example 9: Multi-modal conversation |
| 256 | + */ |
| 257 | +async function multiModalConversation() { |
| 258 | + console.log('💬 Multi-modal conversation...'); |
| 259 | + |
| 260 | + try { |
| 261 | + // Start with image analysis |
| 262 | + const initialAnalysis = await client.conversations.create({ |
| 263 | + model: 'spec-3-turbo', |
| 264 | + instructions: 'You are a helpful assistant that can analyze images and maintain conversation context.', |
| 265 | + input: 'What do you see in this image?', |
| 266 | + // Note: For full multi-modal support with conversations, consider using chat.create instead |
| 267 | + }); |
| 268 | + |
| 269 | + console.log('Initial analysis:', initialAnalysis.output); |
| 270 | + |
| 271 | + // Follow up with a text-only question |
| 272 | + const followUp = await client.conversations.create({ |
| 273 | + model: 'spec-3-turbo', |
| 274 | + instructions: 'You are a helpful assistant. Remember the previous image analysis.', |
| 275 | + input: 'Based on what you saw in that image, what time of day do you think it was taken?', |
| 276 | + context: [ |
| 277 | + 'What do you see in this image?', |
| 278 | + initialAnalysis.output |
| 279 | + ] |
| 280 | + }); |
| 281 | + |
| 282 | + console.log('Follow-up response:', followUp.output); |
| 283 | + } catch (error) { |
| 284 | + console.error('Error:', error); |
| 285 | + } |
| 286 | +} |
| 287 | + |
| 288 | +// Run all examples |
| 289 | +async function runAllExamples() { |
| 290 | + console.log('🚀 SVECTOR Advanced Vision API Examples'); |
| 291 | + console.log('========================================\n'); |
| 292 | + |
| 293 | + await svectorStyleURL(); |
| 294 | + console.log('\n---\n'); |
| 295 | + |
| 296 | + await svectorStyleBase64(); |
| 297 | + console.log('\n---\n'); |
| 298 | + |
| 299 | + await svectorStyleFileID(); |
| 300 | + console.log('\n---\n'); |
| 301 | + |
| 302 | + await batchImageProcessing(); |
| 303 | + console.log('\n---\n'); |
| 304 | + |
| 305 | + await confidenceScoring(); |
| 306 | + console.log('\n---\n'); |
| 307 | + |
| 308 | + await socialMediaCaptions(); |
| 309 | + console.log('\n---\n'); |
| 310 | + |
| 311 | + await streamingVisionAnalysis(); |
| 312 | + console.log('\n---\n'); |
| 313 | + |
| 314 | + await technicalAnalysis(); |
| 315 | + console.log('\n---\n'); |
| 316 | + |
| 317 | + await multiModalConversation(); |
| 318 | +} |
| 319 | + |
| 320 | +// Export functions for individual use |
| 321 | +export { |
| 322 | + batchImageProcessing, |
| 323 | + confidenceScoring, multiModalConversation, socialMediaCaptions, |
| 324 | + streamingVisionAnalysis, svectorStyleBase64, |
| 325 | + svectorStyleFileID, svectorStyleURL, technicalAnalysis |
| 326 | +}; |
| 327 | + |
| 328 | +// Run if called directly |
| 329 | +if (require.main === module) { |
| 330 | + runAllExamples().catch(console.error); |
| 331 | +} |
0 commit comments