Skip to content

Instantly share code, notes, and snippets.

@jaypozo
Last active December 29, 2021 00:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaypozo/ca8110a7ad81046c3548ee677f2aaa80 to your computer and use it in GitHub Desktop.
Save jaypozo/ca8110a7ad81046c3548ee677f2aaa80 to your computer and use it in GitHub Desktop.
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