Skip to content

Instantly share code, notes, and snippets.

@drhanlau
Last active September 6, 2024 12:25
Show Gist options
  • Save drhanlau/4495cc9208d5aef409885e7388a5e0af to your computer and use it in GitHub Desktop.
Save drhanlau/4495cc9208d5aef409885e7388a5e0af to your computer and use it in GitHub Desktop.
Google Sheet Extension
function getHealthAdvice(height, weight) {
const apiKey = "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"; // Replace with your actual API key
const apiUrl = "https://api.openai.com/v1/chat/completions";
const prompt = `Given a person whose height is ${height} cm, weight is ${weight} kg, calculate their BMI and also give some health advice. Please reply in Simplified Chinese.`;
const payload = {
"model": "gpt-4o-mini",
"messages": [{"role": "user", "content": prompt}],
"temperature": 0.7
};
const options = {
"method": "post",
"headers": {
"Authorization": "Bearer " + apiKey,
"Content-Type": "application/json"
},
"payload": JSON.stringify(payload)
};
try {
const response = UrlFetchApp.fetch(apiUrl, options);
const json = JSON.parse(response.getContentText());
result = json.choices[0].message.content.trim();
return result;
} catch (error) {
Logger.log(error);
return "Error: " + error.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment