Skip to content

Instantly share code, notes, and snippets.

@evinjaff
Created November 28, 2023 17:28
Show Gist options
  • Save evinjaff/c5637dfd96204c73167df6c09e5b3724 to your computer and use it in GitHub Desktop.
Save evinjaff/c5637dfd96204c73167df6c09e5b3724 to your computer and use it in GitHub Desktop.
Secret Santa gs code
DRY_RUN = true
OPENAI_API_KEY = "YOUR_API_KEY_HERE"
BASE_URL = "https://api.openai.com/v1/chat/completions";
function myFunction() {
/* Random Number Generation Setup */
// Create cyrb128 state:
var seed = cyrb128("Your Seed Phrase");
// Four 32-bit component hashes provide the seed for sfc32.
var rand = sfc32(seed[0], seed[1], seed[2], seed[3]);
// test some random numbers
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].forEach((i) => {
console.log( (rand() * 80) % 1 )
})
var sheet = SpreadsheetApp.getActiveSheet()
var data = sheet.getDataRange().getValues();
console.log(data)
//slice data to exclude descriptor
data = data.slice(1, undefined)
nrows = data.length
console.log("# of Rows", nrows)
//Turn each row into an object
arr_of_recipients = []
data.forEach( (person) => {
console.log(person)
type_raw = person[3]
type_proc = []
console.log(typeof(type_raw))
if (type_raw.includes("Physical")) {
type_proc.push("physical")
}
if (type_raw.includes("Digital")) {
type_proc.push("digital")
}
if (type_raw.includes("Steam")) {
type_proc.push("steam")
}
arr_of_recipients.push({
"Name": person[1],
"Email": person[2],
"Types": type_proc,
"Address": person[4],
"Steam": person[5],
"Notes": person[6]
})
})
console.log(arr_of_recipients)
//randomly sort arr
arr_of_recipients.sort(function(a, b) {
return 0.5 - rand()});
console.log(arr_of_recipients)
arr_of_sender = JSON.parse(JSON.stringify(arr_of_recipients));
//shift sender by one
arr_of_sender = [].concat(arr_of_sender.slice(-1), arr_of_sender.slice(0, -1))
// Verify that the arrays have the same length
console.log(arr_of_sender.length, arr_of_recipients.length)
if (arr_of_sender.length != arr_of_recipients.length) {
throw Error("Length of arrays don't match")
}
for (i=0;i<arr_of_sender.length;i++){
send = arr_of_sender[i]
receive = arr_of_recipients[i]
console.log("sender", send, "receiver", receive)
gpt_response = callgpt(receive["Notes"], receive["Types"])
steam_bool = "steam" in receive["Types"] ? `Steam: ${receive["Types"]}` : ``
send_email(send["Email"], send["Name"], receive["Name"], receive["Email"], receive["Types"], receive["Address"], steam_bool, receive["Notes"], gpt_response)
}
if (!DRY_RUN){
}
}
function send_email(sender_email, sender_name, recipient_name, recipient_email, types, recipient_addr, steam, notes, gpt_response) {
email_body = `Hello ${sender_name},\nYou have been assigned to ${recipient_name} here are some details. \n\nEmail: ${recipient_email}\nTypes of Gifts Allowed: ${types}\nReceivable Address: ${recipient_addr}\n${steam}\nOther Notes: ${notes} \nBased on what was included in the description, here is a response from GPT-4 to help you get started:\n${gpt_response}\nHappy hunting!`
// Send an email with two attachments: a file from Google Drive (as a PDF) and an HTML file.
// var file = DriveApp.getFileById('1234567890abcdefghijklmnopqrstuvwxyz');
var blob = Utilities.newBlob('Insert any HTML content here', 'text/html', 'my_document.html');
MailApp.sendEmail(sender_email, 'Catboy Baseball Team White Elephant - You have been assigned someone', email_body, {
name: 'Catboy Baseball Team White Elephant - Automated Email Script',
attachments: []
});
}
function callgpt(description, allowed_types){
formatted_prompt = `Description ${description}, types ${JSON.stringify(allowed_types)}`
console.log(formatted_prompt)
return fetchGPT("You are a secret santa AI that recommends secret santa gift ideas. I will give you three enumerated type of gifts allowed (physical, digital, steam) in addition to optional further details. I will quickly describe each of these types\n-physical: physical gifts that can be wrapped in a box (I.e. plushies )\n-digital: digital gifts include gifts that can be digitally delivered to an email (commission digital art from an artist, code, etc.)\n-steam: Steam games on Valve\’s steam store. While your training data might not include a list of Steam's catalog, you MUST name a specific steam game if you include a gift in this category and mention that it can be found on Steam.\n Please recommend at least 5 gift ideas from any of these categories if I specify they are allowed", formatted_prompt)
}
function fetchGPT(systemContent, userContent) {
try {
const headers = {
"Content-Type": "application/json",
"Authorization": `Bearer ${OPENAI_API_KEY}`
};
const options = {
headers,
method: "GET",
muteHttpExceptions: true,
payload: JSON.stringify({
"model": "gpt-4",
"messages": [{
"role": "system",
"content": systemContent,
},
{
"role": "user",
"content": userContent
},
],
"temperature": 0.7
})
};
const response = JSON.parse(UrlFetchApp.fetch(BASE_URL, options));
console.log(response);
//console.log(response.choices[0].message.content)
return response.choices[0].message.content;
} catch (e) {
console.log(e)
return "Some Error Occured Please check your formula or try again later.\n" + e.toString();
}
}
function sfc32(a, b, c, d) {
return function() {
a >>>= 0; b >>>= 0; c >>>= 0; d >>>= 0;
var t = (a + b) | 0;
a = b ^ b >>> 9;
b = c + (c << 3) | 0;
c = (c << 21 | c >>> 11);
d = d + 1 | 0;
t = t + d | 0;
c = c + t | 0;
return (t >>> 0) / 4294967296;
}
}
function cyrb128(str) {
let h1 = 1779033703, h2 = 3144134277,
h3 = 1013904242, h4 = 2773480762;
for (let i = 0, k; i < str.length; i++) {
k = str.charCodeAt(i);
h1 = h2 ^ Math.imul(h1 ^ k, 597399067);
h2 = h3 ^ Math.imul(h2 ^ k, 2869860233);
h3 = h4 ^ Math.imul(h3 ^ k, 951274213);
h4 = h1 ^ Math.imul(h4 ^ k, 2716044179);
}
h1 = Math.imul(h3 ^ (h1 >>> 18), 597399067);
h2 = Math.imul(h4 ^ (h2 >>> 22), 2869860233);
h3 = Math.imul(h1 ^ (h3 >>> 17), 951274213);
h4 = Math.imul(h2 ^ (h4 >>> 19), 2716044179);
h1 ^= (h2 ^ h3 ^ h4), h2 ^= h1, h3 ^= h1, h4 ^= h1;
return [h1>>>0, h2>>>0, h3>>>0, h4>>>0];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment