Skip to content

Instantly share code, notes, and snippets.

@drhanlau
Created September 5, 2024 13:25
Show Gist options
  • Save drhanlau/793fcf304ba1aa887bc320a068613f02 to your computer and use it in GitHub Desktop.
Save drhanlau/793fcf304ba1aa887bc320a068613f02 to your computer and use it in GitHub Desktop.
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 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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment