-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconvert-locomo.ts
More file actions
371 lines (311 loc) · 10.5 KB
/
convert-locomo.ts
File metadata and controls
371 lines (311 loc) · 10.5 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
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
#!/usr/bin/env bun
/**
* Convert LoCoMo dataset into Basic Memory markdown corpus + queries.
*
* LoCoMo conversations → daily session notes (like an agent's memory)
* LoCoMo QA annotations → benchmark queries with ground truth
*
* Usage:
* bun benchmark/convert-locomo.ts # Convert all 10 conversations
* bun benchmark/convert-locomo.ts --conv=0 # Convert conversation 0 only
* bun benchmark/convert-locomo.ts --conv=0 --conv=1 # Multiple conversations
*/
import { mkdir, readFile, writeFile } from "node:fs/promises"
import { resolve } from "node:path"
// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------
interface LoCoMoTurn {
speaker: string
text: string
dia_id: string
img_url?: string
blip_caption?: string
}
interface LoCoMoQA {
question: string
answer?: string
adversarial_answer?: string
category: number
evidence: string[]
}
interface LoCoMoConversation {
sample_id: string
conversation: Record<string, any>
qa: LoCoMoQA[]
observation?: Record<string, string>
session_summary?: Record<string, string>
event_summary?: Record<string, any>
}
interface BenchmarkQuery {
id: string
query: string
category: string
ground_truth: string[]
expected_content?: string
note?: string
}
// ---------------------------------------------------------------------------
// Config
// ---------------------------------------------------------------------------
const BENCHMARK_DIR = resolve(import.meta.dirname!, ".")
const DATASET_PATH = resolve(BENCHMARK_DIR, "datasets/locomo10.json")
const CATEGORY_MAP: Record<number, string> = {
1: "single_hop",
2: "multi_hop",
3: "temporal",
4: "open_domain",
5: "adversarial",
}
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
function parseDateTime(dateStr: string): { date: string; time: string } | null {
// "8:56 pm on 20 July, 2023" → { date: "2023-07-20", time: "20:56" }
const match = dateStr.match(
/(\d{1,2}):(\d{2})\s*(am|pm)\s+on\s+(\d{1,2})\s+(\w+),?\s+(\d{4})/i,
)
if (!match) return null
const [, hour, min, ampm, day, month, year] = match
let h = Number.parseInt(hour)
if (ampm.toLowerCase() === "pm" && h !== 12) h += 12
if (ampm.toLowerCase() === "am" && h === 12) h = 0
const months: Record<string, string> = {
January: "01",
February: "02",
March: "03",
April: "04",
May: "05",
June: "06",
July: "07",
August: "08",
September: "09",
October: "10",
November: "11",
December: "12",
}
const m = months[month]
if (!m) return null
return {
date: `${year}-${m}-${day.padStart(2, "0")}`,
time: `${String(h).padStart(2, "0")}:${min}`,
}
}
function dialogIdToSessionNum(diaId: string): number | null {
// "D1:3" → session 1, "D15:7" → session 15
const match = diaId.match(/^D(\d+):/)
return match ? Number.parseInt(match[1]) : null
}
// ---------------------------------------------------------------------------
// Conversion
// ---------------------------------------------------------------------------
function convertConversation(
conv: LoCoMoConversation,
convIndex: number,
): { files: Map<string, string>; queries: BenchmarkQuery[] } {
const c = conv.conversation
const speakerA = c.speaker_a || "Speaker A"
const speakerB = c.speaker_b || "Speaker B"
const files = new Map<string, string>()
// Find all sessions
const sessionKeys = Object.keys(c)
.filter((k) => k.match(/^session_\d+$/) && Array.isArray(c[k]))
.sort((a, b) => {
const na = Number.parseInt(a.split("_")[1])
const nb = Number.parseInt(b.split("_")[1])
return na - nb
})
// Create a people note for each speaker
const speakerANote = `---
title: ${speakerA}
type: Person
---
# ${speakerA}
## Observations
- [role] Conversation participant
- [relationship] Regularly chats with ${speakerB}
`
files.set(
`people/${speakerA.toLowerCase().replace(/\s+/g, "-")}.md`,
speakerANote,
)
const speakerBNote = `---
title: ${speakerB}
type: Person
---
# ${speakerB}
## Observations
- [role] Conversation participant
- [relationship] Regularly chats with ${speakerA}
`
files.set(
`people/${speakerB.toLowerCase().replace(/\s+/g, "-")}.md`,
speakerBNote,
)
// Build a MEMORY.md with key facts that accumulate
const memoryLines: string[] = [
"# Long-Term Memory",
"",
"## People",
`- ${speakerA} and ${speakerB} are close friends who chat regularly`,
"",
"## Key Events",
]
// Convert each session to a dated note
for (const sessionKey of sessionKeys) {
const sessionNum = Number.parseInt(sessionKey.split("_")[1])
const turns: LoCoMoTurn[] = c[sessionKey]
const dateTimeStr = c[`${sessionKey}_date_time`]
const parsed = dateTimeStr ? parseDateTime(dateTimeStr) : null
const date =
parsed?.date || `2023-01-${String(sessionNum).padStart(2, "0")}`
const time = parsed?.time || "12:00"
// Get session summary and observations if available
const summary = conv.session_summary?.[`${sessionKey}_summary`] || ""
const rawObs = conv.observation?.[`${sessionKey}_observation`]
let observation = ""
if (rawObs && typeof rawObs === "object") {
// { "Speaker": [["observation text", "D1:3"], ...] }
const lines: string[] = []
for (const [speaker, obs] of Object.entries(rawObs)) {
if (Array.isArray(obs)) {
for (const item of obs) {
const text = Array.isArray(item) ? item[0] : item
if (typeof text === "string")
lines.push(`- [${speaker.toLowerCase()}] ${text}`)
}
}
}
observation = lines.join("\n")
} else if (typeof rawObs === "string") {
observation = rawObs
}
let content = `---
title: ${date} Session ${sessionNum}
type: note
date: ${date}
---
# ${date} — Session ${sessionNum}
*${speakerA} and ${speakerB} — ${time}*
`
// Add observation as a summary if available
if (observation) {
content += `## Summary\n${observation}\n\n`
} else if (summary) {
content += `## Summary\n${summary}\n\n`
}
// Add conversation
content += "## Conversation\n"
for (const turn of turns) {
const text = turn.text.replace(/\n/g, "\n> ")
content += `**${turn.speaker}:** ${text}\n\n`
}
// Add relations
content += "## Relations\n"
content += `- mentions [[${speakerA}]]\n`
content += `- mentions [[${speakerB}]]\n`
// Add to memory summary
if (observation) {
const firstObs =
observation.split("\n")[0]?.replace(/^- \[\w+\] /, "") || ""
if (firstObs) memoryLines.push(`- [${date}] ${firstObs}`)
}
files.set(`conversations/${date}-session-${sessionNum}.md`, content)
}
// Write MEMORY.md
files.set("MEMORY.md", `${memoryLines.join("\n")}\n`)
// Convert QA to benchmark queries
const queries: BenchmarkQuery[] = []
// Map evidence dialog IDs to file paths
for (const [qIdx, qa] of conv.qa.entries()) {
const category = CATEGORY_MAP[qa.category] || `cat_${qa.category}`
const answer = qa.answer || qa.adversarial_answer || ""
// Map evidence to ground truth file paths
const groundTruth = new Set<string>()
for (const ev of qa.evidence || []) {
const sessionNum = dialogIdToSessionNum(ev)
if (sessionNum === null) continue
// Find the session's date
const dateTimeStr = c[`session_${sessionNum}_date_time`]
const parsed = dateTimeStr ? parseDateTime(dateTimeStr) : null
const date =
parsed?.date || `2023-01-${String(sessionNum).padStart(2, "0")}`
groundTruth.add(`conversations/${date}-session-${sessionNum}.md`)
}
// For adversarial questions, ground truth is that the info doesn't exist
// We still include the evidence files (where the premise is contradicted)
const isAdversarial = qa.category === 5
queries.push({
id: `locomo_c${convIndex}_q${qIdx}`,
query: qa.question,
category,
ground_truth: [...groundTruth],
expected_content: isAdversarial
? undefined
: answer.length < 100
? answer
: undefined,
note: isAdversarial
? `Adversarial: correct answer is "${answer}"`
: undefined,
})
}
return { files, queries }
}
// ---------------------------------------------------------------------------
// Main
// ---------------------------------------------------------------------------
async function main() {
const args = process.argv.slice(2)
const convIndices = args
.filter((a) => a.startsWith("--conv="))
.map((a) => Number.parseInt(a.split("=")[1]))
console.log("Loading LoCoMo dataset...")
const raw = await readFile(DATASET_PATH, "utf-8")
const data: LoCoMoConversation[] = JSON.parse(raw)
console.log(` ${data.length} conversations loaded`)
const indices = convIndices.length > 0 ? convIndices : data.map((_, i) => i)
let totalFiles = 0
let totalQueries = 0
for (const idx of indices) {
const conv = data[idx]
if (!conv) {
console.error(` Conversation ${idx} not found, skipping`)
continue
}
const convDir = `corpus-locomo/conv-${idx}`
const outDir = resolve(BENCHMARK_DIR, convDir)
console.log(
`\nConverting conversation ${idx} (${conv.conversation.speaker_a} & ${conv.conversation.speaker_b})...`,
)
const { files, queries } = convertConversation(conv, idx)
// Write files
for (const [path, content] of files) {
const fullPath = resolve(outDir, path)
await mkdir(resolve(fullPath, ".."), { recursive: true })
await writeFile(fullPath, content)
}
// Write queries
const queriesPath = resolve(outDir, "queries.json")
await writeFile(queriesPath, JSON.stringify(queries, null, 2))
console.log(` ${files.size} markdown files, ${queries.length} queries`)
totalFiles += files.size
totalQueries += queries.length
// Category breakdown
const cats: Record<string, number> = {}
for (const q of queries) {
cats[q.category] = (cats[q.category] || 0) + 1
}
for (const [cat, count] of Object.entries(cats).sort()) {
console.log(` ${cat}: ${count}`)
}
}
console.log(
`\n✅ Total: ${totalFiles} files, ${totalQueries} queries across ${indices.length} conversations`,
)
console.log(" Output: benchmark/corpus-locomo/")
}
main().catch((err) => {
console.error("Conversion failed:", err)
process.exit(1)
})