Skip to content

Instantly share code, notes, and snippets.

@drhanlau
Last active August 14, 2024 12:58
Show Gist options
  • Save drhanlau/060f226fcb1fea0177f2017d0e06fb8d to your computer and use it in GitHub Desktop.
Save drhanlau/060f226fcb1fea0177f2017d0e06fb8d to your computer and use it in GitHub Desktop.
function hello() {
Logger.log(2+3)
}
function getHealthAdvice(height, weight) {
const apiKey="PUT YOUR API KEY HERE"
const apiUrl = "https://api.openai.com/v1/chat/completions";
const prompt = `Given a person with a height of ${height} cm and a weight of ${weight} kg, calculate their BMI, and provide a brief health advice focusing on maintaining a healthy weight and lifestyle, based on the BMI. Return results with plain text, no formatting required.`;
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();
}
}
function testHealthAdvice() {
result = getHealthAdvice(170, 90);
Logger.log(result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment