-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-providers.mjs
More file actions
86 lines (77 loc) · 2.94 KB
/
test-providers.mjs
File metadata and controls
86 lines (77 loc) · 2.94 KB
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
import fs from 'fs';
import path from 'path';
// Minimal .env loader
const envContent = fs.readFileSync('.env.local', 'utf8');
const env = Object.fromEntries(
envContent.split('\n')
.filter(line => line.includes('='))
.map(line => {
const [key, ...val] = line.split('=');
return [key.trim(), val.join('=').trim()];
})
);
const providers = [
{
name: 'Gemini',
url: `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key=${env.GEMINI_API_KEY}`,
body: (p) => ({
contents: [{ parts: [{ text: p }] }]
}),
headers: { 'x-goog-api-key': env.GEMINI_API_KEY },
parser: (data) => data.candidates?.[0]?.content?.parts?.[0]?.text
},
{
name: 'Groq',
url: 'https://api.groq.com/openai/v1/chat/completions',
apiKey: env.GROQ_API_KEY,
body: (p) => ({
model: 'llama-3.3-70b-versatile',
messages: [{ role: 'user', content: p }]
}),
parser: (data) => data.choices?.[0]?.message?.content
},
{
name: 'NVIDIA',
url: 'https://integrate.api.nvidia.com/v1/chat/completions',
apiKey: env.NVIDIA_API_KEY,
body: (p) => ({
model: 'nvidia/nemotron-3-nano-30b-a3b',
messages: [{ role: 'user', content: p }]
}),
parser: (data) => data.choices?.[0]?.message?.content
}
];
async function testProviders() {
const prompt = "Say 'SUCCESS' if you can read this.";
console.log(`Testing with prompt: "${prompt}"\n`);
for (const provider of providers) {
console.log(`[Testing ${provider.name}]...`);
try {
const key = provider.apiKey || (provider.headers ? provider.headers['x-goog-api-key'] : null);
if (!key || key === 'undefined') {
console.error(`❌ ${provider.name} Failed: API Key not found in env`);
continue;
}
// console.log(` Key snippet: ${key.slice(0, 5)}...`);
const headers = { 'Content-Type': 'application/json' };
if (provider.apiKey) headers['Authorization'] = `Bearer ${provider.apiKey}`;
if (provider.headers) Object.assign(headers, provider.headers);
const res = await fetch(provider.url, {
method: 'POST',
headers,
body: JSON.stringify(provider.body(prompt))
});
if (!res.ok) {
const errText = await res.text();
console.error(`❌ ${provider.name} Failed: ${res.status} - ${errText.slice(0, 100)}...`);
continue;
}
const data = await res.json();
const output = provider.parser(data);
console.log(`✅ ${provider.name} Response: ${output.trim()}\n`);
} catch (err) {
console.error(`❌ ${provider.name} Error:`, err.message);
}
}
}
testProviders();