#!/usr/bin/env node /** * Automated OpenAI Compatible Embedder API test for local endpoints * * Tests three local embedding endpoints: * - Ollama (port 11434) * - LM Studio (port 1234) * - Llama-server (port 8080) * * Usage (run from the src/ directory): * node test-openai-embeddings.js */ const OpenAI = require('openai'); // Endpoint configurations const ENDPOINTS = [ { name: 'Ollama', baseURL: 'http://localhost:11434/v1', apiKey: 'ollama', model: 'Definity/snowflake-arctic-embed-l-v2.0-q8_0', expectedDimensions: 1024 }, { name: 'LM Studio', baseURL: 'http://localhost:1234/v1', apiKey: 'lm-studio', model: 'text-embedding-snowflake-arctic-embed-l-v2.0@q6_k', expectedDimensions: 1024 } ]; // Test cases const TEST_CASES = [ { name: 'Single text embedding', input: ['Hello, world!'], }, { name: 'Multiple text embeddings', input: ['Hello, world!', 'This is a test', 'OpenAI embeddings'], }, { name: 'Code snippet embedding', input: ['function add(a, b) { return a + b; }'], } ]; async function testEndpoint(endpoint) { console.log(`\n๐Ÿš€ Testing ${endpoint.name}`); console.log(`๐Ÿ“ Base URL: ${endpoint.baseURL}`); console.log(`๐Ÿ”‘ API Key: ${endpoint.apiKey}`); console.log(`๐Ÿค– Model: ${endpoint.model}`); console.log(`๐Ÿ“ Expected Dimensions: ${endpoint.expectedDimensions}`); console.log(''); // Initialize OpenAI client const client = new OpenAI({ baseURL: endpoint.baseURL, apiKey: endpoint.apiKey, }); let successCount = 0; const totalTests = TEST_CASES.length; const results = []; for (const testCase of TEST_CASES) { try { console.log(`๐Ÿ”„ Testing: ${testCase.name}`); const startTime = Date.now(); // This is the exact API call used in _embedBatchWithRetries const response = await client.embeddings.create({ input: testCase.input, model: endpoint.model, }); const endTime = Date.now(); const duration = endTime - startTime; // Validate response if (!response.data || response.data.length !== testCase.input.length) { throw new Error(`Invalid response: expected ${testCase.input.length} embeddings, got ${response.data?.length || 0}`); } const actualDimensions = response.data[0].embedding.length; // Validate dimensions if (actualDimensions !== endpoint.expectedDimensions) { throw new Error(`Dimension mismatch: expected ${endpoint.expectedDimensions}, got ${actualDimensions}`); } console.log(`โœ… Success! (${duration}ms)`); console.log(` ๐Ÿ“Š Embeddings: ${response.data.length}`); console.log(` ๐Ÿ“ Dimensions: ${actualDimensions} โœ“`); if (response.usage) { console.log(` ๐ŸŽฏ Tokens: ${response.usage.prompt_tokens} prompt, ${response.usage.total_tokens} total`); } console.log(''); successCount++; results.push({ testCase: testCase.name, success: true, duration, dimensions: actualDimensions }); } catch (error) { console.log(`โŒ Failed: ${error.message}`); if (error.status) { console.log(` ๐Ÿ“ก HTTP Status: ${error.status}`); } if (error.code) { console.log(` ๐Ÿ”ง Error Code: ${error.code}`); } console.log(''); results.push({ testCase: testCase.name, success: false, error: error.message }); } } return { endpoint: endpoint.name, successCount, totalTests, results, allPassed: successCount === totalTests }; } async function main() { console.log('๐Ÿงช Automated OpenAI Compatible Embeddings API Test'); console.log('Testing Ollama and LM Studio endpoints with snowflake-arctic models'); console.log('Expected dimensions: 1024 for all endpoints'); console.log('='.repeat(60)); const endpointResults = []; let totalSuccessfulEndpoints = 0; for (const endpoint of ENDPOINTS) { try { const result = await testEndpoint(endpoint); endpointResults.push(result); if (result.allPassed) { totalSuccessfulEndpoints++; } } catch (error) { console.log(`๐Ÿ’ฅ Failed to test ${endpoint.name}: ${error.message}`); endpointResults.push({ endpoint: endpoint.name, successCount: 0, totalTests: TEST_CASES.length, results: [], allPassed: false, error: error.message }); } } // Overall Summary console.log('\n' + '=' * 60); console.log('๐Ÿ“‹ OVERALL TEST SUMMARY'); console.log('=' * 60); for (const result of endpointResults) { const status = result.allPassed ? 'โœ…' : 'โŒ'; console.log(`${status} ${result.endpoint}: ${result.successCount}/${result.totalTests} tests passed`); if (result.error) { console.log(` ๐Ÿ’ฅ Endpoint Error: ${result.error}`); } } console.log(''); console.log(`๐ŸŽฏ Endpoints fully working: ${totalSuccessfulEndpoints}/${ENDPOINTS.length}`); console.log(`๐Ÿ“Š Total test cases: ${TEST_CASES.length} per endpoint`); if (totalSuccessfulEndpoints === ENDPOINTS.length) { console.log('๐ŸŽ‰ All endpoints passed all tests!'); console.log('โœ… All embeddings.create() API calls are working correctly with 1024 dimensions.'); process.exit(0); } else { console.log('๐Ÿ’ฅ Some endpoints failed tests!'); console.log('๐Ÿ”ง Check that the servers are running and models are available.'); process.exit(1); } } main().catch(console.error);