-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseed-db.ts
144 lines (122 loc) · 4.42 KB
/
seed-db.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
import { ChatOpenAI, OpenAIEmbeddings } from "@langchain/openai";
import { StructuredOutputParser } from "@langchain/core/output_parsers";
import { MongoClient } from "mongodb";
import { MongoDBAtlasVectorSearch } from "@langchain/mongodb";
import { z } from "zod";
import "dotenv/config";
const client = new MongoClient(process.env.MONGO_DB_URL as string);
const llm = new ChatOpenAI({
modelName: "gpt-4o-mini",
temperature: 0.7,
});
const EmployeeSchema = z.object({
employee_id: z.string(),
first_name: z.string(),
last_name: z.string(),
date_of_birth: z.string(),
address: z.object({
street: z.string(),
city: z.string(),
state: z.string(),
postal_code: z.string(),
country: z.string(),
}),
contact_details: z.object({
email: z.string().email(),
phone_number: z.string(),
}),
job_details: z.object({
job_title: z.string(),
department: z.string(),
hire_date: z.string(),
employment_type: z.string(),
salary: z.number(),
currency: z.string(),
}),
work_location: z.object({
nearest_office: z.string(),
is_remote: z.boolean(),
}),
reporting_manager: z.string().nullable(),
skills: z.array(z.string()),
performance_reviews: z.array(
z.object({
review_date: z.string(),
rating: z.number(),
comments: z.string(),
})
),
benefits: z.object({
health_insurance: z.string(),
retirement_plan: z.string(),
paid_time_off: z.number(),
}),
emergency_contact: z.object({
name: z.string(),
relationship: z.string(),
phone_number: z.string(),
}),
notes: z.string(),
});
type Employee = z.infer<typeof EmployeeSchema>;
const parser = StructuredOutputParser.fromZodSchema(z.array(EmployeeSchema));
async function generateSyntheticData(): Promise<Employee[]> {
const prompt = `You are a helpful assistant that generates employee data. Generate 10 fictional employee records. Each record should include the following fields: employee_id, first_name, last_name, date_of_birth, address, contact_details, job_details, work_location, reporting_manager, skills, performance_reviews, benefits, emergency_contact, notes. Ensure variety in the data and realistic values.
${parser.getFormatInstructions()}`;
console.log("Generating synthetic data...");
const response = await llm.invoke(prompt);
return parser.parse(response.content as string);
}
async function createEmployeeSummary(employee: Employee): Promise<string> {
return new Promise((resolve) => {
const jobDetails = `${employee.job_details.job_title} in ${employee.job_details.department}`;
const skills = employee.skills.join(", ");
const performanceReviews = employee.performance_reviews
.map(
(review) =>
`Rated ${review.rating} on ${review.review_date}: ${review.comments}`
)
.join(" ");
const basicInfo = `${employee.first_name} ${employee.last_name}, born on ${employee.date_of_birth}`;
const workLocation = `Works at ${employee.work_location.nearest_office}, Remote: ${employee.work_location.is_remote}`;
const notes = employee.notes;
const summary = `${basicInfo}. Job: ${jobDetails}. Skills: ${skills}. Reviews: ${performanceReviews}. Location: ${workLocation}. Notes: ${notes}`;
resolve(summary);
});
}
async function seedDatabase(): Promise<void> {
try {
await client.connect();
await client.db("admin").command({ ping: 1 });
console.log("Pinged your deployment. You successfully connected to MongoDB!");
const db = client.db("hr_database");
const collection = db.collection("employees");
await collection.deleteMany({});
const syntheticData = await generateSyntheticData();
const recordsWithSummaries = await Promise.all(
syntheticData.map(async (record) => ({
pageContent: await createEmployeeSummary(record),
metadata: {...record},
}))
);
for (const record of recordsWithSummaries) {
await MongoDBAtlasVectorSearch.fromDocuments(
[record],
new OpenAIEmbeddings(),
{
collection,
indexName: "vector_index",
textKey: "embedding_text",
embeddingKey: "embedding",
}
);
console.log("Successfully processed & saved record:", record.metadata.employee_id);
}
console.log("Database seeding completed");
} catch (error) {
console.error("Error seeding database:", error);
} finally {
await client.close();
}
}
seedDatabase().catch(console.error);