-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_openai_format.js
More file actions
71 lines (59 loc) · 2.61 KB
/
Copy pathtest_openai_format.js
File metadata and controls
71 lines (59 loc) · 2.61 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
const url = 'http://localhost:3000/v1/chat/completions';
const payload = {
model: 'auto',
messages: [
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: 'What is 2+2? Reply with just the number.' }
]
};
async function testAPI() {
console.log(`\n🚀 Sending POST request to ${url}...`);
console.log('Payload:', JSON.stringify(payload, null, 2));
const startTime = Date.now();
try {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer test-key' // App Router ignores this currently, but it's standard OpenAI format
},
body: JSON.stringify(payload)
});
const data = await response.json();
const timeTaken = Date.now() - startTime;
console.log(`\n✅ Received Response in ${timeTaken}ms:`);
console.log(JSON.stringify(data, null, 2));
// Verify OpenAI format
console.log('\n🔍 Verifying OpenAI Format Compliance:');
const checks = [
{ name: 'Has "id" string', passed: typeof data.id === 'string' },
{ name: 'Has "object" === "chat.completion"', passed: data.object === 'chat.completion' },
{ name: 'Has "created" integer timestamp', passed: typeof data.created === 'number' },
{ name: 'Has "model" string', passed: typeof data.model === 'string' },
{ name: 'Has "choices" array', passed: Array.isArray(data.choices) && data.choices.length > 0 },
{ name: 'Choice has "message" object with "role" and "content"', passed: !!(data.choices[0] && data.choices[0].message && data.choices[0].message.role && data.choices[0].message.content) },
{ name: 'Choice has "finish_reason"', passed: typeof data.choices[0].finish_reason === 'string' },
{ name: 'Has "usage" object', passed: typeof data.usage === 'object' },
{ name: 'Usage has "prompt_tokens"', passed: typeof data.usage.prompt_tokens === 'number' },
{ name: 'Usage has "completion_tokens"', passed: typeof data.usage.completion_tokens === 'number' },
{ name: 'Usage has "total_tokens"', passed: typeof data.usage.total_tokens === 'number' }
];
let allPassed = true;
checks.forEach(c => {
if (c.passed) {
console.log(` [PASS] ${c.name}`);
} else {
console.log(` [FAIL] ${c.name}`);
allPassed = false;
}
});
if (allPassed) {
console.log('\n🎉 RESULT: 100% STRICT OPENAI COMPLIANT!');
} else {
console.log('\n⚠️ RESULT: Some format checks failed.');
}
} catch (error) {
console.error('❌ Error testing API:', error);
}
}
testAPI();