Files
pfm-ocr/backend/pfm-web-app/test_guided.js
T
fhanyuh caf8e98378 chore: normalize line endings (CRLF -> LF)
No content changes: git diff --ignore-all-space over these files is empty.
The churn came from editing on Windows against a repo checked out with LF.
2026-08-27 10:40:49 +07:00

114 lines
3.4 KiB
JavaScript

const fs = require('fs');
const path = require('path');
const PROXY_URL = 'http://localhost:8000/api/vllm-proxy/v1/chat/completions';
const IMAGE_PATH = path.join(__dirname, '..', 'sources', 'test-images', 'do-001.jpg');
async function testGuidedDecoding() {
console.log('Reading test image from:', IMAGE_PATH);
if (!fs.existsSync(IMAGE_PATH)) {
console.error('Test image not found!');
process.exit(1);
}
const imageBuffer = fs.readFileSync(IMAGE_PATH);
const base64Image = imageBuffer.toString('base64');
const imageUrl = `data:image/jpeg;base64,${base64Image}`;
// JSON Schema for DO metadata
const jsonSchema = {
type: "object",
properties: {
tanggal: { type: "string" },
noPo: { type: "string" },
noSo: { type: "string" },
noDo: { type: "string" },
kepadaYth: { type: "string" },
orderUntuk: { type: "string" },
alamat: { type: "string" },
platTruk: { type: "string" },
namaDriver: { type: "string" },
namaPenerima: { type: "string" },
items: {
type: "array",
items: {
type: "object",
properties: {
nomor_sku: { type: "string" },
nama_barang: { type: "string" },
banyak: { type: "string" },
jumlah: { type: "string" }
},
required: ["nomor_sku", "nama_barang", "banyak", "jumlah"]
}
}
},
required: ["tanggal", "noPo", "noSo", "noDo", "kepadaYth", "items"]
};
const payload = {
model: "PaddleOCR-VL-1.6-0.9B",
messages: [
{
role: "user",
content: [
{
type: "image_url",
image_url: {
url: imageUrl
}
},
{
type: "text",
text: "Extract all structural details from this Delivery Order. Match the requested JSON Schema exactly."
}
]
}
],
temperature: 0.1,
max_tokens: 1024,
guided_json: JSON.stringify(jsonSchema) // standard vLLM guided JSON schema format
};
console.log('Sending request to vLLM proxy with JSON Schema...');
try {
const startTime = Date.now();
const response = await fetch(PROXY_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
});
console.log(`Response status: ${response.status} (${response.statusText})`);
const duration = ((Date.now() - startTime) / 1000).toFixed(2);
console.log(`Request completed in ${duration}s`);
const result = await response.json();
if (response.ok) {
console.log('=== SUCCESS RESPONSE ===');
console.log(JSON.stringify(result, null, 2));
const content = result.choices?.[0]?.message?.content;
console.log('\n=== EXTRACTED CONTENT ===');
console.log(content);
try {
const parsed = JSON.parse(content);
console.log('\nValid JSON parsed successfully! ✅');
console.log(parsed);
} catch (err) {
console.error('\nFailed to parse content as JSON! ❌', err.message);
}
} else {
console.error('=== ERROR RESPONSE ===');
console.error(result);
}
} catch (error) {
console.error('Request failed:', error);
}
}
testGuidedDecoding();