Skip to content

Instantly share code, notes, and snippets.

@tronghieu60s
Forked from J2TEAM/gpt-for-google-sheet.js
Last active June 17, 2024 07:41
Show Gist options
  • Save tronghieu60s/591650f690dc5dd1bd941cd510d40b4a to your computer and use it in GitHub Desktop.
Save tronghieu60s/591650f690dc5dd1bd941cd510d40b4a to your computer and use it in GitHub Desktop.
Hàm để dùng Gemini 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';
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