File size: 8,105 Bytes
6e801f2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
import readline from "readline";
import minimist from "minimist";
// @ts-expect-error: vite-node makes the var available but the typescript compiler doesn't see them
import { MONGODB_URL } from "$env/static/private";
import { faker } from "@faker-js/faker";
import { ObjectId } from "mongodb";
import { collections } from "../src/lib/server/database.ts";
import { models } from "../src/lib/server/models.ts";
import type { User } from "../src/lib/types/User";
import type { Assistant } from "../src/lib/types/Assistant";
import type { Conversation } from "../src/lib/types/Conversation";
import type { Settings } from "../src/lib/types/Settings";
import { defaultEmbeddingModel } from "../src/lib/server/embeddingModels.ts";
import { Message } from "../src/lib/types/Message.ts";
import { addChildren } from "../src/lib/utils/tree/addChildren.ts";
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.on("close", function () {
process.exit(0);
});
const possibleFlags = ["reset", "all", "users", "settings", "assistants", "conversations"];
const argv = minimist(process.argv.slice(2));
const flags = argv["_"].filter((flag) => possibleFlags.includes(flag));
async function generateMessages(preprompt?: string): Promise<Message[]> {
const isLinear = faker.datatype.boolean(0.5);
const isInterrupted = faker.datatype.boolean(0.05);
const messages: Message[] = [];
messages.push({
id: crypto.randomUUID(),
from: "system",
content: preprompt ?? "",
createdAt: faker.date.recent({ days: 30 }),
updatedAt: faker.date.recent({ days: 30 }),
});
let isUser = true;
let lastId = messages[0].id;
if (isLinear) {
const convLength = faker.number.int({ min: 1, max: 25 }) * 2; // must always be even
for (let i = 0; i < convLength; i++) {
lastId = addChildren(
{
messages,
rootMessageId: messages[0].id,
},
{
from: isUser ? "user" : "assistant",
content: faker.lorem.sentence({
min: 10,
max: isUser ? 50 : 200,
}),
createdAt: faker.date.recent({ days: 30 }),
updatedAt: faker.date.recent({ days: 30 }),
interrupted: i === convLength - 1 && isInterrupted,
},
lastId
);
isUser = !isUser;
}
} else {
const convLength = faker.number.int({ min: 2, max: 200 });
for (let i = 0; i < convLength; i++) {
addChildren(
{
messages,
rootMessageId: messages[0].id,
},
{
from: isUser ? "user" : "assistant",
content: faker.lorem.sentence({
min: 10,
max: isUser ? 50 : 200,
}),
createdAt: faker.date.recent({ days: 30 }),
updatedAt: faker.date.recent({ days: 30 }),
interrupted: i === convLength - 1 && isInterrupted,
},
faker.helpers.arrayElement([
messages[0].id,
...messages.filter((m) => m.from === (isUser ? "assistant" : "user")).map((m) => m.id),
])
);
isUser = !isUser;
}
}
return messages;
}
async function seed() {
console.log("Seeding...");
const modelIds = models.map((model) => model.id);
if (flags.includes("reset")) {
console.log("Starting reset of DB");
await collections.users.deleteMany({});
await collections.settings.deleteMany({});
await collections.assistants.deleteMany({});
await collections.conversations.deleteMany({});
console.log("Reset done");
}
if (flags.includes("users") || flags.includes("all")) {
console.log("Creating 100 new users");
const newUsers: User[] = Array.from({ length: 100 }, () => ({
_id: new ObjectId(),
createdAt: faker.date.recent({ days: 30 }),
updatedAt: faker.date.recent({ days: 30 }),
username: faker.internet.userName(),
name: faker.person.fullName(),
hfUserId: faker.string.alphanumeric(24),
avatarUrl: faker.image.avatar(),
}));
await collections.users.insertMany(newUsers);
console.log("Done creating users.");
}
const users = await collections.users.find().toArray();
if (flags.includes("settings") || flags.includes("all")) {
console.log("Updating settings for all users");
users.forEach(async (user) => {
const settings: Settings = {
userId: user._id,
shareConversationsWithModelAuthors: faker.datatype.boolean(0.25),
hideEmojiOnSidebar: faker.datatype.boolean(0.25),
ethicsModalAcceptedAt: faker.date.recent({ days: 30 }),
activeModel: faker.helpers.arrayElement(modelIds),
createdAt: faker.date.recent({ days: 30 }),
updatedAt: faker.date.recent({ days: 30 }),
customPrompts: {},
assistants: [],
};
await collections.settings.updateOne(
{ userId: user._id },
{ $set: { ...settings } },
{ upsert: true }
);
});
console.log("Done updating settings.");
}
if (flags.includes("assistants") || flags.includes("all")) {
console.log("Creating assistants for all users");
await Promise.all(
users.map(async (user) => {
const assistants = faker.helpers.multiple<Assistant>(
() => ({
_id: new ObjectId(),
name: faker.animal.insect(),
createdById: user._id,
createdByName: user.username,
createdAt: faker.date.recent({ days: 30 }),
updatedAt: faker.date.recent({ days: 30 }),
userCount: faker.number.int({ min: 1, max: 100000 }),
featured: faker.datatype.boolean(0.25),
modelId: faker.helpers.arrayElement(modelIds),
description: faker.lorem.sentence(),
preprompt: faker.hacker.phrase(),
exampleInputs: faker.helpers.multiple(() => faker.lorem.sentence(), {
count: faker.number.int({ min: 0, max: 4 }),
}),
}),
{ count: faker.number.int({ min: 3, max: 10 }) }
);
await collections.assistants.insertMany(assistants);
await collections.settings.updateOne(
{ userId: user._id },
{ $set: { assistants: assistants.map((a) => a._id.toString()) } },
{ upsert: true }
);
})
);
console.log("Done creating assistants.");
}
if (flags.includes("conversations") || flags.includes("all")) {
console.log("Creating conversations for all users");
await Promise.all(
users.map(async (user) => {
const conversations = faker.helpers.multiple(
async () => {
const settings = await collections.settings.findOne<Settings>({ userId: user._id });
const assistantId =
settings?.assistants && settings.assistants.length > 0 && faker.datatype.boolean(0.1)
? faker.helpers.arrayElement<ObjectId>(settings.assistants)
: undefined;
const preprompt =
(assistantId
? await collections.assistants
.findOne({ _id: assistantId })
.then((assistant: Assistant) => assistant?.preprompt ?? "")
: faker.helpers.maybe(() => faker.hacker.phrase(), { probability: 0.5 })) ?? "";
const messages = await generateMessages(preprompt);
const conv = {
_id: new ObjectId(),
userId: user._id,
assistantId,
preprompt,
createdAt: faker.date.recent({ days: 145 }),
updatedAt: faker.date.recent({ days: 145 }),
model: faker.helpers.arrayElement(modelIds),
title: faker.internet.emoji() + " " + faker.hacker.phrase(),
embeddingModel: defaultEmbeddingModel.id,
messages,
rootMessageId: messages[0].id,
} satisfies Conversation;
return conv;
},
{ count: faker.number.int({ min: 10, max: 200 }) }
);
await collections.conversations.insertMany(await Promise.all(conversations));
})
);
console.log("Done creating conversations.");
}
}
// run seed
(async () => {
try {
rl.question(
"You're about to run a seeding script on the following MONGODB_URL: \x1b[31m" +
MONGODB_URL +
"\x1b[0m\n\n With the following flags: \x1b[31m" +
flags.join("\x1b[0m , \x1b[31m") +
"\x1b[0m\n \n\n Are you sure you want to continue? (yes/no): ",
async (confirm) => {
if (confirm !== "yes") {
console.log("Not 'yes', exiting.");
rl.close();
process.exit(0);
}
console.log("Starting seeding...");
await seed();
console.log("Seeding done.");
rl.close();
}
);
} catch (e) {
console.error(e);
process.exit(1);
}
})();
|