Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion bench/src/benchmarks/rag-benchmarks.test.mts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import assert from 'node:assert/strict'
import { mkdtemp, rm, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { test } from 'node:test'
import { resolveAdapter } from '../adapters'
import { createCragAdapter } from './crag'
import { createNoMiraclAdapter } from './nomiracl'
import { createOpenRagBenchAdapter } from './open-rag-bench'
import { createRagBenchAdapter } from './ragbench'
import { FINAL_ANSWER_SENTINEL } from './rag-shared'
import { FINAL_ANSWER_SENTINEL, readJsonRows } from './rag-shared'
import { createT2RagBenchAdapter } from './t2-ragbench'

async function withEnv<T>(patch: Record<string, string | undefined>, fn: () => Promise<T>): Promise<T> {
Expand All @@ -31,6 +34,20 @@ test('RAG benchmark adapters are registered', () => {
}
})

test('RAG JSON reader accepts object-starting JSONL exports', async () => {
const dir = await mkdtemp(join(tmpdir(), 'rag-jsonl-'))
try {
const file = join(dir, 'rows.jsonl')
await writeFile(file, '{"id":"a","question":"q1"}\n{"id":"b","question":"q2"}\n')
assert.deepEqual(await readJsonRows(file), [
{ id: 'a', question: 'q1' },
{ id: 'b', question: 'q2' },
])
} finally {
await rm(dir, { recursive: true, force: true })
}
})

test('RAGBench fixture mode loads rows and scores final answers', async () => {
await withEnv({ RAGBENCH_FIXTURES: '1', RAGBENCH_DATA_FILE: undefined }, async () => {
const adapter = createRagBenchAdapter()
Expand Down
21 changes: 15 additions & 6 deletions bench/src/benchmarks/rag-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,16 +178,25 @@ export function answerScoreToBenchScore(
export async function readJsonRows(path: string): Promise<unknown[]> {
const raw = (await readFile(path, 'utf8')).trim()
if (raw.length === 0) return []
if (raw.startsWith('[') || raw.startsWith('{')) {
if (raw.startsWith('[')) {
const parsed = JSON.parse(raw) as unknown
if (Array.isArray(parsed)) return parsed
if (isObject(parsed)) {
for (const key of ['rows', 'data', 'examples', 'items']) {
const value = parsed[key]
if (Array.isArray(value)) return value
throw new Error(`${path} must contain a JSON array when it starts with [`)
}
if (raw.startsWith('{')) {
try {
const parsed = JSON.parse(raw) as unknown
if (isObject(parsed)) {
for (const key of ['rows', 'data', 'examples', 'items']) {
const value = parsed[key]
if (Array.isArray(value)) return value
}
return [parsed]
}
throw new Error(`${path} must contain a JSON object, JSON array, JSONL rows, or an object with rows/data/examples/items`)
} catch (err) {
if (!raw.includes('\n')) throw err
}
throw new Error(`${path} must contain a JSON array, JSONL rows, or an object with rows/data/examples/items`)
}
return raw
.split(/\r?\n/)
Expand Down
Loading