-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-ai.ts
More file actions
60 lines (51 loc) · 1.93 KB
/
Copy pathtest-ai.ts
File metadata and controls
60 lines (51 loc) · 1.93 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
import fs from 'fs';
import path from 'path';
import { extractIngredients } from './src/ai/flows/extract-ingredients';
import { assessHealthSafety } from './src/ai/flows/assess-health-safety';
import dotenv from 'dotenv';
dotenv.config({ path: '.env.local' });
async function run() {
try {
const imagePath = '/home/amitdevx/Downloads/71q038rjYWL.jpg';
console.log(`Loading image from ${imagePath}...`);
const imageBuffer = fs.readFileSync(imagePath);
const base64Image = imageBuffer.toString('base64');
const dataUri = `data:image/jpeg;base64,${base64Image}`;
console.log("Extracting ingredients using NVIDIA NIM 11b...");
const { nimClient, parseNIMResponse } = await import('./src/ai/nim');
const systemPrompt = `You are a strict JSON data extraction API. Return NOTHING BUT A RAW JSON OBJECT. No markdown, no conversational text.
{
"ingredients": ["item1"],
"nutrition": {
"rawText": "...",
"servingSizeLabel": "...",
"nutrients": [{"nutrient": "Fat", "perServing": "1g", "per100mL": "2g"}]
},
"status": "success"
}
Extract the ingredients list and nutritional facts. IF YOU OUTPUT ANY TEXT OUTSIDE THE JSON OBJECT, THE SYSTEM WILL CRASH.`;
const response = await nimClient!.chat.completions.create({
model: 'meta/llama-3.2-11b-vision-instruct',
messages: [
{
role: 'user',
content: [
{ type: 'text', text: systemPrompt },
{ type: 'image_url', image_url: { url: dataUri } },
],
},
],
max_tokens: 2000,
temperature: 0.1,
top_p: 0.9,
});
const rawContent = response.choices?.[0]?.message?.content;
console.log("\n--- Extraction Result ---");
console.log("RAW:", rawContent);
const extractResult = parseNIMResponse(rawContent);
console.log("PARSED:", JSON.stringify(extractResult, null, 2));
} catch (err) {
console.error("Error running test:", err);
}
}
run();