Happy Friday to all.
I am using OpenAI to do some analysis on a photo taken by a RPi camera. With the help provided in this post, I got that all figured out by using a function node.
Now I wish to send several pieces of data into MSSQL via a stored procedure, and one such piece is a small image. My understanding is that this image passed from Node-red to SQL should be in the form of a “byte array”. The MSSQL stored procedure specifies it as varbinary(max)
.
Here's where I need help. As shown by these 3 debug nodes, the msg.readingimage
is being dropped in the flow after the function 1
node. How can I keep msg.readingimage
intact and have it be an input into the MSSQL-Plus node? Perhaps just modifying function 1 below? Or maybe function 3
is completely the wrong way to create a byte array?
function 3 (this is supposed to convert it to byte array)
msg.readingimage = [...msg.readingimage];
return msg;
function 1 (this sends the image & question to OpenAI):
const OAI = new openai.OpenAI({ apiKey: 'sk-myAPIkey' });
// Function to encode the image
function encodeImage(imagePath) {
const image = fs.readFileSync(imagePath);
const base64Image = Buffer.from(image).toString('base64');
return `data:image/jpeg;base64,${base64Image}`;
}
// Path to the image
const imagePath = '/home/rpi0e/test1.png';
// Get the data URL
const dataUrl = encodeImage(imagePath);
async function main() {
const response = await OAI.chat.completions.create({
model: "gpt-4-vision-preview",
messages: [
{
role: "user",
content: [
{ type: "text", text: "Which five digits are in this image? Return the message.content as only the five digits with no additional text."
},
{
type: "image_url",
image_url: {
url:dataUrl
},
},
],
},
],
});
node.send({ payload: response.choices[0] })
}
main();
Attached is a sample image in case it helps.