Skip to content

Instantly share code, notes, and snippets.

@drhanlau
Created August 14, 2024 13:17
Show Gist options
  • Save drhanlau/3d90022500cd0fbb11bf636a7a0395b1 to your computer and use it in GitHub Desktop.
Save drhanlau/3d90022500cd0fbb11bf636a7a0395b1 to your computer and use it in GitHub Desktop.
Sentiment
function hello() {
Logger.log(2+3)
}
function getHealthAdvice(height, weight, gender) {
const apiKey="put your api key here"
const apiUrl = "https://api.openai.com/v1/chat/completions";
const prompt = `Given a person of gender ${gender} 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. Don't show the BMI calculation, just return the health advice only.`;
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 getSentiment(review) {
const apiKey="put your api key here"
const apiUrl = "https://api.openai.com/v1/chat/completions";
const prompt = `Calculate the sentiment score of this review "${review}" using Vader. Return the results in a single score only. Do not include other information.`;
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);
}
function testSentiment() {
result =getSentiment("Hello today is a beautiful day.");
Logger.log(result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment