-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathseedDatabase.ts
393 lines (334 loc) · 11.1 KB
/
seedDatabase.ts
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import dotenv from 'dotenv'
dotenv.config({ path: '.env.local' })
import { getPayload } from 'payload'
import config from '../payload.config'
// Function to fetch Schema.org JSONLD
export async function fetchSchemaOrgData() {
try {
console.log('Fetching Schema.org JSONLD data...')
const response = await fetch('https://schema.org/version/latest/schemaorg-current-https.jsonld')
const data = await response.json()
console.log('Successfully fetched Schema.org data')
return data
} catch (error) {
console.error('Error fetching Schema.org data:', error)
throw error
}
}
export async function fetchOpenRouterModels() {
try {
console.log('Fetching OpenRouter models...')
const response = await fetch('https://openrouter.ai/api/frontend/models/find')
const data = await response.json()
console.log('Successfully fetched OpenRouter models')
return data
} catch (error) {
console.error('Error fetching OpenRouter models:', error)
throw error
}
}
export function extractNounsAndVerbs(data: any) {
const nouns: Set<string> = new Set()
const things: Set<string> = new Set()
const verbs: { action: string; type: string }[] = []
// Extract @graph from the JSONLD
const graph = data['@graph']
if (!graph) {
console.error('No @graph found in Schema.org data')
return { nouns: [], things: [], verbs }
}
// Process each item in the graph
graph.forEach((item: any) => {
if (item['@type'] === 'rdfs:Class') {
const typeName = item['@id'].replace('schema:', '')
// If it ends with "Action", it's a verb
if (typeName.endsWith('Action')) {
// Extract the verb action from the type name (remove 'Action' suffix)
const action = typeName.replace('Action', '')
// Only add if we have a valid action name
if (action && action.length > 0) {
verbs.push({ action, type: typeName })
}
} else if (typeName && typeName.length > 0) {
nouns.add(typeName)
things.add(typeName)
}
}
})
return { nouns: Array.from(nouns), things: Array.from(things), verbs }
}
// Function to seed the database
export async function seedDatabase() {
try {
console.log('Starting database seed process...')
// Initialize Payload
const payload = await getPayload({ config })
// Fetch Schema.org data
const schemaOrgData = await fetchSchemaOrgData()
const { nouns, things, verbs } = extractNounsAndVerbs(schemaOrgData)
console.log(`Found ${nouns.length} nouns and ${verbs.length} verbs`)
// Seed Nouns
console.log('Seeding Nouns...')
let nounsCreated = 0
let nounsSkipped = 0
for (const noun of nouns) {
try {
// Check if noun already exists
const exists = await payload.find({
collection: 'nouns',
where: { name: { equals: noun } },
})
if (exists.docs.length > 0) {
console.log(`Noun already exists: ${noun}`)
nounsSkipped++
continue
}
// Create if it doesn't exist
await payload.create({
collection: 'nouns',
data: {
name: noun,
},
})
console.log(`Created noun: ${noun}`)
nounsCreated++
} catch (error) {
console.error(`Error processing noun ${noun}:`, error)
}
}
console.log(`Nouns: ${nounsCreated} created, ${nounsSkipped} skipped`)
console.log('Seeding Things...')
let thingsCreated = 0
let thingsSkipped = 0
for (const thing of things) {
try {
const exists = await payload.find({
collection: 'things',
where: { name: { equals: thing } },
})
if (exists.docs.length > 0) {
console.log(`Thing already exists: ${thing}`)
thingsSkipped++
continue
}
// Create if it doesn't exist
await payload.create({
collection: 'things',
data: {
name: thing,
},
})
console.log(`Created thing: ${thing}`)
thingsCreated++
} catch (error) {
console.error(`Error processing thing ${thing}:`, error)
}
}
console.log(`Things: ${thingsCreated} created, ${thingsSkipped} skipped`)
// Seed Verbs
console.log('Seeding Verbs...')
let verbsCreated = 0
let verbsSkipped = 0
for (const verb of verbs) {
try {
// Skip verbs with empty action
if (!verb.action || verb.action.trim() === '') {
console.log(`Skipping verb with empty action: ${verb.type}`)
verbsSkipped++
continue
}
// Check if verb already exists
const exists = await payload.find({
collection: 'verbs',
where: { action: { equals: verb.action } },
})
if (exists.docs.length > 0) {
console.log(`Verb already exists: ${verb.action}`)
verbsSkipped++
continue
}
// Basic verb form
const action = verb.action
// Generate other verb forms (simplified)
const act = `${action}s`
const activity = `${action}ing`
const event = `${action}ed`
const subject = `${action}er`
const object = `${action}ion`
await payload.create({
collection: 'verbs',
data: {
action,
act,
activity,
event,
subject,
object,
},
})
console.log(`Created verb: ${action}`)
verbsCreated++
} catch (error) {
console.error(`Error creating verb ${verb.action}:`, error)
}
}
console.log(`Verbs: ${verbsCreated} created, ${verbsSkipped} skipped`)
console.log('Fetching OpenRouter models...')
const openRouterData = await fetchOpenRouterModels()
if (!openRouterData || !openRouterData.data) {
console.error('No data returned from OpenRouter API')
return
}
console.log(`Found ${openRouterData.data.length} models from OpenRouter`)
const providers = new Map<string, any>()
const labs = new Map<string, any>()
openRouterData.data.forEach((model: any) => {
if (model.provider && !providers.has(model.provider.id)) {
providers.set(model.provider.id, model.provider)
}
if (model.lab && !labs.has(model.lab.id)) {
labs.set(model.lab.id, model.lab)
}
})
console.log('Seeding Providers...')
let providersCreated = 0
let providersSkipped = 0
for (const [id, provider] of providers.entries()) {
try {
const exists = await payload.find({
collection: 'providers' as any,
where: { id: { equals: id } },
})
if (exists.docs.length > 0) {
console.log(`Provider already exists: ${provider.name}`)
providersSkipped++
continue
}
// Create if it doesn't exist
await payload.create({
collection: 'providers' as any,
data: {
id,
name: provider.name,
description: provider.description || '',
logoUrl: provider.logoUrl || '',
},
})
console.log(`Created provider: ${provider.name}`)
providersCreated++
} catch (error) {
console.error(`Error creating provider ${provider.name}:`, error)
}
}
console.log(`Providers: ${providersCreated} created, ${providersSkipped} skipped`)
console.log('Seeding Labs...')
let labsCreated = 0
let labsSkipped = 0
for (const [id, lab] of labs.entries()) {
try {
const exists = await payload.find({
collection: 'labs' as any,
where: { id: { equals: id } },
})
if (exists.docs.length > 0) {
console.log(`Lab already exists: ${lab.name}`)
labsSkipped++
continue
}
// Create if it doesn't exist
await payload.create({
collection: 'labs' as any,
data: {
id,
name: lab.name,
description: lab.description || '',
logoUrl: lab.logoUrl || '',
},
})
console.log(`Created lab: ${lab.name}`)
labsCreated++
} catch (error) {
console.error(`Error creating lab ${lab.name}:`, error)
}
}
console.log(`Labs: ${labsCreated} created, ${labsSkipped} skipped`)
console.log('Seeding Models...')
let modelsCreated = 0
let modelsSkipped = 0
for (const model of openRouterData.data) {
try {
const exists = await payload.find({
collection: 'models',
where: { id: { equals: model.id } },
})
if (exists.docs.length > 0) {
console.log(`Model already exists: ${model.name}`)
modelsSkipped++
continue
}
let providerId = null
if (model.provider) {
const providerDoc = await payload.find({
collection: 'providers' as any,
where: { id: { equals: model.provider.id } },
})
if (providerDoc.docs.length > 0) {
providerId = providerDoc.docs[0].id
}
}
let labId = null
if (model.lab) {
const labDoc = await payload.find({
collection: 'labs' as any,
where: { id: { equals: model.lab.id } },
})
if (labDoc.docs.length > 0) {
labId = labDoc.docs[0].id
}
}
await payload.create({
collection: 'models',
data: {
id: model.id,
name: model.name,
provider: providerId as any,
lab: labId as any,
description: model.description || '',
context_length: model.context_length || 0,
pricing: {
prompt: model.pricing?.prompt || 0,
completion: model.pricing?.completion || 0,
},
capabilities: model.capabilities?.map((capability: any) => ({ capability })) || [],
modelUrl: model.modelUrl || '',
imageUrl: model.imageUrl || '',
} as any,
})
console.log(`Created model: ${model.name}`)
modelsCreated++
} catch (error) {
console.error(`Error creating model ${model.name}:`, error)
}
}
console.log(`Models: ${modelsCreated} created, ${modelsSkipped} skipped`)
console.log('Database seeding completed successfully!')
return {
nouns: { created: nounsCreated, skipped: nounsSkipped },
things: { created: thingsCreated, skipped: thingsSkipped },
verbs: { created: verbsCreated, skipped: verbsSkipped },
providers: { created: providersCreated, skipped: providersSkipped },
labs: { created: labsCreated, skipped: labsSkipped },
models: { created: modelsCreated, skipped: modelsSkipped },
}
} catch (error) {
console.error('Error seeding database:', error)
throw error
}
}
// // Run the seed function if this script is executed directly
// import { fileURLToPath } from 'url'
// const currentFile = fileURLToPath(import.meta.url)
// const isMainModule = process.argv[1] === currentFile
// if (isMainModule) {
// seedDatabase()
// }