Last active
August 14, 2024 12:58
-
-
Save drhanlau/060f226fcb1fea0177f2017d0e06fb8d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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