Help loading Byte Array for SQL insert

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.
test1

As far as I remember, you should simply be able to send the buffer (unmodified)

Your image does show where MSSQL-PLUS node is but i assume after function 1?

Since you already have (had) it in msg.reading simply set the parameter in the MSSQL nodes UI to use msg.reading as the parameter value!

Thank you @Steve-Mcl, but passing msg.readingimage into the input of the stored procedure does not work.

Debug with complete msg object after the MSSQL-Plus node indicates everything was fine except for the ReadingImage.

It seems that function 1 is not allowing the byte array to be passed as msg.readingimage

This send a new object. Instead, set msg.payload and send the original msg

Thanks @Steve-Mcl

I ended up using a join node and combined the two payloads into an array and as you remembered, I simply sent the buffer (unmodified) into MSSQL-PLUS and it worked.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.