-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-vision-api.js
More file actions
142 lines (123 loc) Β· 5.27 KB
/
test-vision-api.js
File metadata and controls
142 lines (123 loc) Β· 5.27 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* OpenAI Vision API Test Script
* Tests the same Vision API endpoint used by the OCR scanner
*
* Run: node test-vision-api.js
*/
require('dotenv').config({ path: '.env.local' });
const OpenAI = require('openai');
// Initialize OpenAI client
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY
});
// Test image: Simple text label (base64 encoded small test image)
// This is a 1x1 transparent PNG - replace with actual dental label image for real testing
const TEST_IMAGE_BASE64 = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==";
async function testVisionAPI() {
console.log('π Testing OpenAI Vision API...\n');
// Check if API key is configured
if (!process.env.OPENAI_API_KEY) {
console.error('β ERROR: OPENAI_API_KEY not found in environment variables');
console.error(' Please ensure .env.local contains OPENAI_API_KEY');
process.exit(1);
}
console.log('β
API Key found:', process.env.OPENAI_API_KEY.substring(0, 20) + '...');
console.log('π‘ Testing connection to OpenAI Vision API (gpt-4o model)...\n');
try {
// Make the exact same API call as the OCR scanner
const response = await openai.chat.completions.create({
model: 'gpt-4o',
messages: [
{
role: 'system',
content: `You are an expert in reading dental material labels and extracting information.
Your task is to extract ALL visible text and data from the material label image.
Common dental material manufacturers: Ivoclar Vivadent, 3M ESPE, Vita, Dentsply Sirona, GC, Kulzer, Shofu, Kuraray Noritake, Straumann, Nobel Biocare.
Material types: CERAMIC, METAL, RESIN, COMPOSITE, PORCELAIN, ZIRCONIA, TITANIUM, ALLOY, ACRYLIC, WAX, OTHER.
Return a JSON object with these fields:
{
"lotNumber": "LOT/Batch number (usually 5-10 alphanumeric characters)",
"expiryDate": "YYYY-MM-DD format",
"quantity": "Number with unit (e.g., '10g', '5ml', '1 unit')",
"manufacturer": "Company name",
"materialName": "Product name",
"materialType": "One of the material types listed above",
"confidence": "high | medium | low",
"reasoning": "Brief explanation of any corrections or assumptions made"
}
IMPORTANT OCR ERROR CORRECTIONS:
- LOT/Batch numbers: Common OCR mistakes are s/S β 5, l/I β 1, O β 0
- Dates: Normalize all dates to YYYY-MM-DD format
- Manufacturer names: Use correct capitalization (e.g., "Ivoclar Vivadent" not "IVOCLAR VIVADENT")
If any field cannot be determined, use null.`
},
{
role: 'user',
content: [
{
type: 'text',
text: 'Extract all information from this dental material label.'
},
{
type: 'image_url',
image_url: {
url: TEST_IMAGE_BASE64,
detail: 'high'
}
}
]
}
],
max_tokens: 1000,
temperature: 0.1,
response_format: { type: 'json_object' }
});
console.log('β
SUCCESS! Vision API responded correctly.\n');
console.log('π Response details:');
console.log(' Model:', response.model);
console.log(' Finish reason:', response.choices[0].finish_reason);
console.log(' Token usage:', {
prompt: response.usage.prompt_tokens,
completion: response.usage.completion_tokens,
total: response.usage.total_tokens
});
console.log('\nπ Extracted data:');
const extractedData = JSON.parse(response.choices[0].message.content);
console.log(JSON.stringify(extractedData, null, 2));
console.log('\nβ
Vision API is working correctly!');
console.log('β
Your local setup is properly configured.');
console.log('\nπ‘ Note: This test used a simple 1x1 test image.');
console.log(' For real testing, replace TEST_IMAGE_BASE64 with an actual dental label image.');
} catch (error) {
console.error('\nβ ERROR: Vision API test failed\n');
if (error.status === 401) {
console.error('π Authentication Error (401)');
console.error(' Your API key is invalid or expired.');
console.error(' Please check your OPENAI_API_KEY in .env.local');
} else if (error.status === 429) {
console.error('π« Rate Limit Error (429)');
console.error(' You have exceeded your API quota or rate limit.');
console.error(' Please check your OpenAI account usage and billing.');
} else if (error.status === 500) {
console.error('π₯ OpenAI Server Error (500)');
console.error(' OpenAI servers are experiencing issues.');
console.error(' Please try again later.');
} else if (error.code === 'ENOTFOUND' || error.code === 'ECONNREFUSED') {
console.error('π Network Error');
console.error(' Cannot connect to OpenAI servers.');
console.error(' Please check your internet connection.');
} else {
console.error('β οΈ Unexpected Error');
console.error(' Error details:', error.message);
if (error.response) {
console.error(' Response status:', error.response.status);
console.error(' Response data:', error.response.data);
}
}
console.error('\nπ Full error object:');
console.error(error);
process.exit(1);
}
}
// Run the test
testVisionAPI();