Skip to content

Commit 76fe6a2

Browse files
committed
chore(llmobs): use csv parser for dataset import
1 parent cc2ea89 commit 76fe6a2

4 files changed

Lines changed: 13 additions & 138 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@
169169
"version.js"
170170
],
171171
"dependencies": {
172+
"csv-parse": "^7.0.1",
172173
"dc-polyfill": "^0.1.11",
173174
"import-in-the-middle": "^3.3.1",
174175
"opentracing": ">=0.14.7"

packages/dd-trace/src/llmobs/experiments/DATASET_PARITY_TODO.md

Lines changed: 0 additions & 94 deletions
This file was deleted.

packages/dd-trace/src/llmobs/experiments/index.js

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
const fs = require('node:fs')
44

5+
const { parse: parseCsv } = require('csv-parse/sync')
6+
57
const log = require('../../log')
68
const { ExperimentsClient, API_BASE_PATH } = require('./client')
79
const { Dataset, DatasetRecord } = require('./dataset')
@@ -24,49 +26,6 @@ async function retryWithBackoff (attempt, { maxTotalMs = 30_000, baseDelayMs = 2
2426
}
2527
}
2628

27-
function parseCsv (content, delimiter) {
28-
const rows = []
29-
let field = ''
30-
let row = []
31-
let inQuotes = false
32-
33-
for (let i = 0; i < content.length; i++) {
34-
const char = content[i]
35-
if (inQuotes) {
36-
if (char === '"') {
37-
if (content[i + 1] === '"') {
38-
field += '"'
39-
i++
40-
} else {
41-
inQuotes = false
42-
}
43-
} else {
44-
field += char
45-
}
46-
} else if (char === '"') {
47-
inQuotes = true
48-
} else if (char === delimiter) {
49-
row.push(field)
50-
field = ''
51-
} else if (char === '\n' || char === '\r') {
52-
row.push(field)
53-
field = ''
54-
rows.push(row)
55-
row = []
56-
if (char === '\r' && content[i + 1] === '\n') i++
57-
} else {
58-
field += char
59-
}
60-
}
61-
62-
if (field !== '' || row.length > 0) {
63-
row.push(field)
64-
rows.push(row)
65-
}
66-
67-
return rows
68-
}
69-
7029
function assertColumns (name, columns, allowEmpty = true) {
7130
if (!Array.isArray(columns) || (!allowEmpty && columns.length === 0)) {
7231
throw new Error(`${name} must be a ${allowEmpty ? '' : 'non-empty '}array of column names`)
@@ -106,7 +65,11 @@ function recordsFromCsv (csvPath, options) {
10665
const content = fs.readFileSync(csvPath, 'utf8')
10766
if (content.trim().length === 0) throw new Error('CSV file appears to be empty or header is missing')
10867

109-
const rows = parseCsv(content, csvDelimiter)
68+
const rows = parseCsv(content, {
69+
delimiter: csvDelimiter,
70+
bom: true,
71+
relaxColumnCount: true,
72+
})
11073
const header = rows.shift()
11174
if (!header || header.every(column => column === '')) {
11275
throw new Error('CSV file appears to be empty or header is missing')

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1747,6 +1747,11 @@ crypt@0.0.2:
17471747
resolved "https://registry.yarnpkg.com/crypt/-/crypt-0.0.2.tgz#88d7ff7ec0dfb86f713dc87bbb42d044d3e6c41b"
17481748
integrity sha512-mCxBlsHFYh9C+HVpiEacem8FEBnMXgU9gy4zmNC+SXAZNB/1idgp/aulFJ4FgCi7GPEVbfyng092GqL2k2rmow==
17491749

1750+
csv-parse@^7.0.1:
1751+
version "7.0.1"
1752+
resolved "https://registry.yarnpkg.com/csv-parse/-/csv-parse-7.0.1.tgz#77d0f216dc25af5e8006f3b7899526081dd81b07"
1753+
integrity sha512-+2z7Ar0APQ7Uu6fX4cn+pitRmxjZ1WPBcGmZFKmA74FCyi7Et/XZx8cjNQ5CjbZ4HCOxXCOpRBYvYH08Qa003A==
1754+
17501755
data-view-buffer@^1.0.2:
17511756
version "1.0.2"
17521757
resolved "https://registry.yarnpkg.com/data-view-buffer/-/data-view-buffer-1.0.2.tgz#211a03ba95ecaf7798a8c7198d79536211f88570"

0 commit comments

Comments
 (0)