Skip to content

Instantly share code, notes, and snippets.

@opsJson
Created June 14, 2024 21:23
Show Gist options
  • Save opsJson/c9f27e87305f566d55091a9df4c5b1ae to your computer and use it in GitHub Desktop.
Save opsJson/c9f27e87305f566d55091a9df4c5b1ae to your computer and use it in GitHub Desktop.
Colador de Prova usando GPT-4o Vision e NodeJs
const TelegramBot = require("node-telegram-bot-api");
const OPENAI_TOKEN = "openai_token";
const TELEGRAM_TOKEN = "telegram_token";
const bot = new TelegramBot(TELEGRAM_TOKEN, { polling: true });
bot.on("message", async msg => {
bot.sendMessage(msg.chat.id, "Pensando...");
if (msg.photo) {
const link = await bot.getFileLink(msg.photo[msg.photo.length-1].file_id);
bot.sendMessage(msg.chat.id, await chatgpt(link));
}
else {
bot.sendMessage(msg.chat.id, await chatgpt(msg.text));
}
});
async function chatgpt(prompt) {
if (typeof(prompt) != "string") return null;
let isString;
try {
new URL(prompt);
isString = false;
}
catch (e) {
isString = true;
}
let r = await fetch("https://api.openai.com/v1/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${OPENAI_TOKEN}`
},
body: JSON.stringify({
model: "gpt-4o",
temperature: 0,
messages: [
{ role: "system", content: "Responda brevemente a seguinte questão:"},
{ role: "user", content: [
{
type: isString ? "text" : "image_url",
[isString ? "text" : "image_url"]: isString ? prompt : ({ "url" : prompt })
}
]}
]
})
});
if (r.status != 200) {
r = await r.json();
return r.error.message;
}
r = await r.json();
return r.choices[0].message.content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment