|
| 1 | + |
| 2 | +const path = require("path"); |
| 3 | +const fs = require("fs"); |
| 4 | +const { OpenAI } = require("openai"); |
| 5 | +require("dotenv").config(); |
| 6 | +let recipe = "Recipe:" |
| 7 | +let url |
| 8 | + |
| 9 | +console.log(` server.js file value of recipe is: ${recipe}`) |
| 10 | + |
| 11 | + |
| 12 | +const openai = new OpenAI({ |
| 13 | + apiKey: process.env.openaiAPI, |
| 14 | + }); |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | +// async function processEmail(req, res) { |
| 19 | +// var transporter = nodemailer.createTransport({ |
| 20 | +// service: process.env.service, |
| 21 | +// auth: { |
| 22 | +// user: process.env.from, |
| 23 | +// pass: process.env.third_party_app_password, |
| 24 | +// }, |
| 25 | +// }); |
| 26 | + |
| 27 | +// const emailDocument = ` |
| 28 | +// <html> |
| 29 | +// <head> |
| 30 | +// <style> |
| 31 | +// .preserve-line-breaks { |
| 32 | +// white-space: pre-line |
| 33 | +// } |
| 34 | +// .user-img { |
| 35 | +// width: 200px; |
| 36 | +// height: 200px; |
| 37 | +// } |
| 38 | +// </style> |
| 39 | +// </head> |
| 40 | +// <body class="preserve-line-breaks" > |
| 41 | +// ${recipe} |
| 42 | +// <br /> |
| 43 | +// Embedded image: |
| 44 | +// <br /> |
| 45 | +// <img class="user-img" src="${url}"/> |
| 46 | +// </body> |
| 47 | +// </html> |
| 48 | +// `; |
| 49 | + |
| 50 | +// if (recipe !== "") { |
| 51 | +// var mailOptions = { |
| 52 | +// from: process.env.from, |
| 53 | +// to: req.query.user_email_address, |
| 54 | +// subject: "Your recipe from recipe-for-success dynamic app", |
| 55 | +// text: recipe, |
| 56 | +// html: emailDocument, |
| 57 | +// attachments: [ |
| 58 | +// { |
| 59 | +// filename: "url_folder.txt", |
| 60 | +// path: path.join(__dirname, "../public/url_folder/url_folder.txt"), |
| 61 | +// cid: "url", |
| 62 | +// }, |
| 63 | +// ], |
| 64 | +// }; |
| 65 | +// } else { |
| 66 | +// console.log("doubleResponse is not defined yet."); |
| 67 | +// } |
| 68 | + |
| 69 | +// transporter.sendMail(mailOptions, function (error, info) { |
| 70 | +// if (error) { |
| 71 | +// console.log(error); |
| 72 | +// } else { |
| 73 | +// console.log("Email sent: " + info.response); |
| 74 | +// } |
| 75 | +// }); |
| 76 | +// } |
| 77 | + |
| 78 | + async function processUpload(req, res) { |
| 79 | + const picture = req.body.image; |
| 80 | + console.log({ josue_upload: picture }); |
| 81 | + // res.status(200).json({ message: `variable ${JSON.stringify(picture)} received` }); |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | + const response = await openai.chat.completions.create({ |
| 86 | + model: "gpt-4o", |
| 87 | + messages: [ |
| 88 | + { |
| 89 | + role: "user", |
| 90 | + content: [ |
| 91 | + { type: "text", text: "What can I cook with these ingredients?" }, |
| 92 | + { |
| 93 | + type: "image_url", |
| 94 | + image_url: { |
| 95 | + url: picture, |
| 96 | + }, |
| 97 | + }, |
| 98 | + ], |
| 99 | + }, |
| 100 | + ], |
| 101 | + }); |
| 102 | + |
| 103 | + recipe = response.choices[0].message.content |
| 104 | + |
| 105 | + console.log(`your recipe is ${recipe}`); |
| 106 | + res.send(response.choices[0]); |
| 107 | + |
| 108 | + recipe = "Recipe:" |
| 109 | + |
| 110 | + |
| 111 | + } |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | +module.exports = { |
| 117 | + processUpload, |
| 118 | +} |
0 commit comments