Skip to content

Commit

Permalink
added JSONL parser
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Jun 10, 2024
1 parent 524ca79 commit 19ea829
Show file tree
Hide file tree
Showing 19 changed files with 123 additions and 2 deletions.
7 changes: 7 additions & 0 deletions docs/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/src/content/docs/reference/cli/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ Options:
-td, --test-delay <string> delay between tests in seconds
--no-cache disable LLM result cache
-v, --verbose verbose output
-pv, --promptfoo-version [version] promptfoo version, default is ^0.62.1
-pv, --promptfoo-version [version] promptfoo version, default is ^0.63.0
-os, --out-summary <file> append output summary in file
-h, --help display help for command
```
Expand Down
15 changes: 15 additions & 0 deletions docs/src/content/docs/reference/scripts/parsers.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@ To parse, use `parsers.TOML`. It supports both a text content or a file as input
const res = parsers.TOML("...")
```

## JSONL

JSON**L** is a format that stores JSON objects in a line-by-line format. Each line is a valid JSON(5) object (we use the JSON5 parser to be more error resilient).

```jsonl title="data.jsonl"
{"name": "Alice"}
{"name": "Bob"}
```

You can use `parsers.JSONL` to parse the JSONL files into an array of object (`any[]`).

```js
const res = parsers.JSONL(file)
```

## XML

The `parsers.XML` function parses for the [XML format](https://en.wikipedia.org/wiki/XML).
Expand Down
7 changes: 7 additions & 0 deletions genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions packages/core/src/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/core/src/jsonl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export async function readJSONL(fn: string) {
return res
}

export function JSONLParse(
export function JSONLTryParse(
text: string,
options?: {
repair?: boolean
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/parsers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ describe("parsers", () => {
assert.deepStrictEqual(result, { key: "value" })
})

test("JSONL", () => {
const result = parsers.JSONL('{"key": "value"}\n{"key2": "value2"}')
assert.deepStrictEqual(result[0], { key: "value" })
assert.deepStrictEqual(result[1], { key2: "value2" })
})

test("YAML", () => {
const result = parsers.YAML("key: value")
assert.deepStrictEqual(result, { key: "value" })
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { validateJSONWithSchema } from "./schema"
import { XLSXTryParse } from "./xlsx"
import { host } from "./host"
import { unzip } from "./zip"
import { JSONLTryParse } from "./jsonl"

export function createParsers(options: {
trace: MarkdownTrace
Expand All @@ -29,6 +30,7 @@ export function createParsers(options: {
return Object.freeze<Parsers>({
JSON5: (text, options) =>
JSON5TryParse(filenameOrFileToContent(text), options?.defaultValue),
JSONL: (text) => JSONLTryParse(filenameOrFileToContent(text)),
YAML: (text, options) =>
YAMLTryParse(filenameOrFileToContent(text), options?.defaultValue),
XML: (text, options) => {
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/types/prompt_template.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,13 @@ interface Parsers {
content: string | WorkspaceFile,
options?: { defaultValue?: any }
): any | undefined

/**
* Parses text or file as a JSONL payload. Empty lines are ignore, and JSON5 is used for parsing.
* @param content
*/
JSONL(content: string | WorkspaceFile): any[] | undefined

/**
* Parses text as a YAML paylaod
*/
Expand Down
7 changes: 7 additions & 0 deletions packages/sample/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions packages/sample/genaisrc/node/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions packages/sample/genaisrc/python/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions packages/sample/genaisrc/style/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions packages/sample/src/aici/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions packages/sample/src/errors/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions packages/sample/src/makecode/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions packages/sample/src/tla/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions packages/sample/src/vision/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions slides/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 19ea829

Please sign in to comment.