forked from evalscience/deepgov-ndi-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist-responses.ts
More file actions
64 lines (56 loc) · 1.58 KB
/
list-responses.ts
File metadata and controls
64 lines (56 loc) · 1.58 KB
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
import * as schemas from "./src/db/schema";
import { db } from "./src/db/client";
import { openai } from "./src/openai";
import { writeFileSync } from "fs";
// Get all responses from database
const responses = await db.select().from(schemas.responses);
// Fetch response items for each response
const allItems = await Promise.all(
responses.map(async (r) => {
const response = await openai.responses.inputItems.list(r.responseId, {
limit: 100,
});
return response.data
.filter((item: any) => item.role === "user")
.map((msg: any) => ({
id: msg.id,
comment: msg.content[0]?.text,
interview: r.userId,
}));
})
);
// Flatten and deduplicate items
const uniqueItems = allItems
.flat()
.filter(
(item, index, self) =>
index ===
self.findIndex(
(t) => t.comment === item.comment && t.interview === item.interview
)
);
// Export to CSV
function saveAsCSV(data: any[], filename: string) {
if (data.length === 0) {
console.log("No data to export");
return;
}
const headers = Object.keys(data[0]);
const csvContent = [
headers.join(","),
...data.map((item) =>
headers
.map((header) => {
const value = String(item[header] || "");
const escaped = value.replace(/"/g, '""');
return escaped.includes(",") || escaped.includes("\n")
? `"${escaped}"`
: escaped;
})
.join(",")
),
].join("\n");
writeFileSync(filename, csvContent);
console.log(`CSV saved to ${filename}`);
}
saveAsCSV(uniqueItems, "responses.csv");