import OpenAI from 'openai';
import fs from 'node:fs';
import { Laminar, observe } from '@lmnr-ai/lmnr';
Laminar.initialize({
projectApiKey: process.env.LMNR_PROJECT_API_KEY,
instrumentModules: { OpenAI },
});
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
const analyzeImage = (imagePath: string, userQuestion: string) =>
observe({ name: 'analyzeImage' }, async () => {
const base64 = fs.readFileSync(imagePath).toString('base64');
const response = await openai.chat.completions.create({
model: 'gpt-4o',
messages: [
{
role: 'user',
content: [
{ type: 'text', text: userQuestion },
{
type: 'image_url',
image_url: { url: `data:image/jpeg;base64,${base64}` },
},
],
},
],
max_tokens: 500,
});
return response.choices[0].message.content;
});