Created
September 26, 2024 13:28
-
-
Save avimar/e8f39f59f345157482f73efaee529086 to your computer and use it in GitHub Desktop.
Fetch OpenRouter vision models
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const https = require('https'); | |
function fetchJSON(url) { | |
return new Promise((resolve, reject) => { | |
https.get(url, (response) => { | |
if (response.statusCode !== 200) { | |
reject(new Error(`HTTP error! status: ${response.statusCode}`)); | |
return; | |
} | |
let data = ''; | |
response.on('data', (chunk) => { | |
data += chunk; | |
}); | |
response.on('end', () => { | |
try { | |
const jsonResult = JSON.parse(data); | |
resolve(jsonResult); | |
} catch (error) { | |
reject(new Error('Error parsing JSON: ' + error.message)); | |
} | |
}); | |
}).on('error', (error) => { | |
reject(new Error('Error fetching JSON: ' + error.message)); | |
}); | |
}); | |
} | |
// Usage example: | |
// fetchJSON('https://api.example.com/data') | |
// .then(result => console.log(result)) | |
// .catch(error => console.error(error)); | |
async function listOpenRouterModels() { | |
const response = await fetchJSON('https://openrouter.ai/api/v1/models'); | |
var allModels = response.data; | |
const imageInput = allModels.filter(function(model){ | |
if(model.architecture.modality === 'text+image->text') return true; //current happy path, but to future proof: | |
const input = model.architecture.modality.split('->')[0]; | |
if(input.includes('image')) return true; | |
if(parseFloat(model.pricing.image)>0) return true; | |
return false; | |
}); | |
return imageInput.map(model => model.id) | |
} | |
async function run(){ | |
try{ | |
const results = await listOpenRouterModels(); | |
console.log('Models with image modality input or price for images:'); | |
console.log(results); | |
} | |
catch (error) { | |
console.error(error); | |
} | |
} | |
if(module === require.main) { | |
run(); | |
} | |
module.exports = listOpenRouterModels; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment