Skip to content

Instantly share code, notes, and snippets.

@NimaiMalle
Last active July 18, 2024 07:17
Show Gist options
  • Save NimaiMalle/d9a060498c88a1c29013c5b3e949bd5d to your computer and use it in GitHub Desktop.
Save NimaiMalle/d9a060498c88a1c29013c5b3e949bd5d to your computer and use it in GitHub Desktop.
LLM Prompt Creation Scripts
// Connect to the database
db = db.getSiblingDB("cdtserver");
var ignore = ["collection-metadata", "Roulette", "fs.", "test"];
var collectionNames = db.getCollectionNames();
var descriptions = {};
if (collectionNames.includes("collection-metadata")) {
db.getCollection("collection-metadata")
.find()
.forEach(function (doc) {
descriptions[doc["collectionName"]] = doc["description"];
});
}
function stringify(obj) {
function isPrimitiveArray(value) {
if (!Array.isArray(value)) return false;
for (var i = 0; i < value.length; i++) {
if (typeof value[i] === "object") return false;
}
return true;
}
function toHexString(bytes) {
return Array.from(Object.values(bytes), (byte) =>
("0" + (Number(byte) & 0xff).toString(16)).slice(-2)
).join("");
}
function isBinary(obj) {
return obj && typeof obj === "object" && typeof obj.buffer == "object";
}
function replacer(key, value) {
if (isBinary(value)) {
return `Binary('${toHexString(value.buffer)}',${value.sub_type})`;
} else if (isPrimitiveArray(value)) {
return "[" + value.join(", ") + "]";
}
return value;
}
return JSON.stringify(obj, replacer, 2);
}
function limitArrayEntries(obj, maxEntries = 3) {
function recurse(value) {
if (Array.isArray(value)) {
if (value.length > maxEntries) {
value.length = maxEntries;
}
return value.map(recurse);
} else if (value && typeof value === "object") {
return Object.keys(value).reduce((acc, key) => {
acc[key] = recurse(value[key]);
return acc;
}, {});
}
return value;
}
return recurse(obj);
}
function formatDocument(doc, indent = 2, maxDepth = 10) {
var jsonString = stringify(doc, null, indent);
var lines = jsonString.split("\n");
var finalLines = [];
var skipDepth = null;
var maxIndent = maxDepth * indent;
var indentRx = new RegExp("^ {" + maxIndent + ",}");
for (let i = 0; i < lines.length; i++) {
let line = lines[i];
let nextLine = lines[i + 1] || "";
let currentDepth = line.match(/^ */)[0].length;
if (indentRx.test(line) && skipDepth === null) {
skipDepth = currentDepth;
}
if (
skipDepth !== null &&
(!indentRx.test(nextLine) || currentDepth < skipDepth)
) {
if (indentRx.test(line)) {
let previousLine = finalLines.pop();
previousLine = previousLine.replace(/: {/, ": {...},");
finalLines.push(previousLine);
}
skipDepth = null;
} else if (skipDepth === null) {
finalLines.push(line);
}
}
return finalLines.join("\n");
}
// Get all collection names in the database
collectionNames.forEach(function (collectionName) {
// if (collectionName !== "CDT.Models.RPR.Pool") return; // TEST
if (ignore.find((kw) => collectionName.includes(kw)))
// Skip any we know we don't need
return;
// Print collection name
print("----------");
print('Collection: "' + collectionName + '"\n');
print(`Description: ${descriptions[collectionName] || "n/a"}\n`);
// Fetch the example document using $natural sort to get the most recently inserted document
var exampleDocument = db[collectionName]
.find()
.sort({ $natural: -1 })
.limit(1)
.toArray();
if (exampleDocument && exampleDocument.length > 0) {
print("Example Document:");
print(formatDocument(limitArrayEntries(exampleDocument[0], 10), 2, 12));
} else {
print("No documents found in this collection.");
}
// Fetch and format indexes for the collection
var indexes = db[collectionName].getIndexes();
indexes.forEach(function (index) {
if (index.name === "_id_") return;
var keyFields = Object.keys(index.key).join(", ");
var unique = index.unique ? "Unique" : "";
print(`${unique}Index: "${index.name}" on ${keyFields}`);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment