File size: 3,356 Bytes
c120a1c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
import express from 'express';
import fetch from 'node-fetch';
export const router = express.Router();
const API_OPENROUTER = 'https://openrouter.ai/api/v1';
router.post('/models/providers', async (req, res) => {
try {
const { model } = req.body;
const response = await fetch(`${API_OPENROUTER}/models/${model}/endpoints`, {
method: 'GET',
headers: {
'Accept': 'application/json',
},
});
if (!response.ok) {
return res.json([]);
}
/** @type {any} */
const data = await response.json();
const endpoints = data?.data?.endpoints || [];
const providerNames = endpoints.map(e => e.provider_name);
return res.json(providerNames);
} catch (error) {
console.error(error);
return res.sendStatus(500);
}
});
router.post('/models/multimodal', async (_req, res) => {
try {
// The endpoint is available without authentication
const response = await fetch(`${API_OPENROUTER}/models`, {
method: 'GET',
headers: {
'Accept': 'application/json',
},
});
if (!response.ok) {
return res.json([]);
}
/** @type {any} */
const data = await response.json();
if (!Array.isArray(data?.data)) {
console.warn('OpenRouter API response was not an array');
return res.json([]);
}
const multimodalModels = data.data
.filter(m => Array.isArray(m?.architecture?.input_modalities))
.filter(m => m.architecture.input_modalities.includes('image'))
.filter(m => Array.isArray(m?.architecture?.output_modalities))
.filter(m => m.architecture.output_modalities.includes('text'))
.sort((a, b) => a?.id && b?.id && a.id.localeCompare(b.id))
.map(m => m.id);
return res.json(multimodalModels);
} catch (error) {
console.error(error);
return res.sendStatus(500);
}
});
router.post('/models/embedding', async (_req, res) => {
try {
// The endpoint is available without authentication
const response = await fetch(`${API_OPENROUTER}/embeddings/models`, {
method: 'GET',
headers: {
'Accept': 'application/json',
},
});
if (!response.ok) {
console.warn('OpenRouter API request failed', response.statusText);
return res.json([]);
}
/** @type {any} */
const data = await response.json();
if (!Array.isArray(data?.data)) {
console.warn('OpenRouter API response was not an array');
return res.json([]);
}
const embeddingModels = data.data
.filter(m => Array.isArray(m?.architecture?.input_modalities))
.filter(m => m.architecture.input_modalities.includes('text'))
.filter(m => Array.isArray(m?.architecture?.output_modalities))
.filter(m => m.architecture.output_modalities.includes('embeddings'))
.sort((a, b) => a?.id && b?.id && a.id.localeCompare(b.id));
return res.json(embeddingModels);
} catch (error) {
console.error(error);
return res.sendStatus(500);
}
});
|