Skip to content

Instantly share code, notes, and snippets.

@J2TEAM
Last active June 20, 2024 16:25
Show Gist options
  • Save J2TEAM/91bc15ab65941d8db9cfb30de2b849a3 to your computer and use it in GitHub Desktop.
Save J2TEAM/91bc15ab65941d8db9cfb30de2b849a3 to your computer and use it in GitHub Desktop.
Hàm để dùng ChatGPT trong Google Sheets. Xem cách sử dụng: https://www.tiktok.com/@juno_okyo/video/7378880956209401094?_r=1&_t=8n5NIEqPjqE
// Author: JUNO_OKYO - J2TEAM
const API_KEY = 'EDIT_ME'; // CHÚ Ý: sửa key của bạn trước khi sử dụng!!!
const URL = 'https://api.openai.com/v1/chat/completions';
function askGPT(prompt) {
const payload = {
model: "gpt-4o",
messages: [
{ role: "system", content: "You are a helpful assistant. Your name is J2TEAM GPT." },
{ role: "user", content: prompt }
]
};
const options = {
method: 'post',
contentType: 'application/json',
headers: {
Authorization: `Bearer ${API_KEY}`
},
payload: JSON.stringify(payload)
};
try {
const response = UrlFetchApp.fetch(URL, options);
const json = JSON.parse(response.getContentText());
return json.choices[0].message.content.trim();
} catch (e) {
return `Error: ${e.message}`;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment