Last active
December 29, 2021 00:35
-
-
Save jaypozo/ca8110a7ad81046c3548ee677f2aaa80 to your computer and use it in GitHub Desktop.
This file contains 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
import type {Request, Response} from "express"; | |
import * as functions from "firebase-functions"; | |
import * as express from "express"; | |
import * as admin from "firebase-admin"; | |
import {QueryDocumentSnapshot} | |
from "firebase-functions/lib/providers/firestore"; | |
import axios from "axios"; | |
const app = express(); | |
admin.initializeApp(); | |
let bucket: any; | |
/** | |
* @param {string} message - is the message to display | |
*/ | |
function notify(message: string) { | |
axios.post("https://discord.com/api/webhooks/yourDiscordWebhook", | |
{ | |
content: message, | |
}).catch((e) => { | |
console.log(e); | |
}); | |
} | |
/** | |
* Init the bucket async | |
*/ | |
async function initialize() { | |
const bucketName = "someBucket"; | |
bucket = await admin.storage().bucket(bucketName); | |
notify(`Initialized bucket ${bucket.name}`); | |
} | |
initialize().catch((e) => console.log(e)); | |
app.get("/", async (req: Request, res: Response) => { | |
res.status(200).send(); | |
notify(`Generating data on ${bucket.name}...`); | |
const dateString = Date.now(); | |
const fileName = `${dateString}.jsonl`; | |
const blob = await bucket.file(fileName); | |
const writeStream = await blob.createWriteStream({ | |
metadata: { | |
contentType: "text", | |
}, | |
}); | |
// get all the "liked" headlines from firebase | |
const query = await admin.firestore().collection("collectionName") | |
.where("liked", "==", true); | |
await query.stream() | |
.on("data", (data: QueryDocumentSnapshot) => { | |
const jsonData = data.data(); | |
// transform data into JSONL expected by OpenAI | |
const newLine = | |
`{"prompt": "${jsonData.input}", "completion": "${jsonData.result}"}\n`; | |
writeStream.write(newLine); | |
}) | |
.on("error", (err: Error) => { | |
console.log(err); | |
}) | |
.on("finish", () => { | |
writeStream.end(); | |
console.log(`Finished writing ${fileName}.`); | |
notify(`Wrote https://storage.cloud.google.com/${bucket.name}/${fileName}`); | |
}); | |
}); | |
exports.export = functions.https.onRequest(app); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment