Skip to content

Commit

Permalink
parsers.validateJSON api (#455)
Browse files Browse the repository at this point in the history
* updated docs

* adding a few tests
  • Loading branch information
pelikhan authored May 16, 2024
1 parent 5ddf3aa commit 766837c
Show file tree
Hide file tree
Showing 20 changed files with 188 additions and 1 deletion.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"gptoolsjs",
"llmrequest",
"localai",
"mardownify",
"millis",
"ollama",
"openai",
Expand Down
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.

16 changes: 16 additions & 0 deletions docs/src/content/docs/reference/scripts/parsers.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,19 @@ for the current model. This is useful for estimating the number of prompts that
```js
const count = parsers.tokens("...")
```

## math

The `parsers.math` function uses [mathjs](https://mathjs.org/) to parse a math expression.

```js
const res = parsers.math("1 + 1")
```

## validateJSON

The `parsers.validateJSON` function validates a JSON string against a schema.

```js
const validation = parsers.validateJSON(schema, json)
```
8 changes: 8 additions & 0 deletions docs/src/content/docs/reference/scripts/schemas.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,11 @@ and can confuse the LLM.

GenAIScript will automatically try to repair the data by issues additional messages
back to the LLM with the parsing output.

## Runtime Validation

Use `parsers.validateJSON` to validate JSON when running the script.

```js
const validation = parsers.validateJSON(schema, json)
```
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.

60 changes: 60 additions & 0 deletions packages/core/src/parsers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { describe, beforeEach, test } from "node:test"
import assert from "node:assert/strict"
import { createParsers } from "./parsers"
import { MarkdownTrace } from "./trace"

describe("parsers", () => {
let trace: MarkdownTrace
let model: string
let parsers: ReturnType<typeof createParsers>

beforeEach(() => {
trace = new MarkdownTrace()
model = "test model"
parsers = createParsers({ trace, model })
})

test("JSON5", () => {
const result = parsers.JSON5('{"key": "value"}')
assert.deepStrictEqual(result, { key: "value" })
})

test("YAML", () => {
const result = parsers.YAML("key: value")
assert.deepStrictEqual(result, { key: "value" })
})

test("XML parser", () => {
const result = parsers.XML("<key>value</key>")
assert.deepStrictEqual(result, { key: "value" })
})

test("TOML", () => {
const result = parsers.TOML('key = "value"')
assert.equal(result.key, "value")
})

test("frontmatter", () => {
const result = parsers.frontmatter("---\nkey: value\n---\n")
assert.deepStrictEqual(result, { key: "value" })
})

test("math", () => {
const res = parsers.math("1 + 3")
assert.strictEqual(res, 4)
})

test("validateJSON", () => {
const res = parsers.validateJSON(
{
type: "object",
properties: {
key: { type: "string" },
},
required: ["key"],
},
{ key: "value" }
)
assert.strictEqual(res.valid, true)
})
})
3 changes: 3 additions & 0 deletions packages/core/src/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { treeSitterQuery } from "./treesitter"
import { parsePdf } from "./pdf"
import { HTMLToText } from "./html"
import { MathTryEvaluate } from "./math"
import { validateJSONWithSchema } from "./schema"

export function createParsers(options: {
trace: MarkdownTrace
Expand Down Expand Up @@ -82,5 +83,7 @@ export function createParsers(options: {
code: async (file, query) =>
await treeSitterQuery(file, query, { trace }),
math: (expression) => MathTryEvaluate(expression, { trace }),
validateJSON: (schema, content) =>
validateJSONWithSchema(content, schema, { trace }),
})
}
3 changes: 2 additions & 1 deletion packages/core/src/toml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { parse } from "toml"

export function TOMLTryParse(text: string, options?: { defaultValue?: any }) {
try {
return parse(text)
const res = parse(text)
return res
} catch (e) {
return options?.defaultValue
}
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 @@ -857,6 +857,13 @@ interface Parsers {
* @param expression math expression compatible with mathjs
*/
math(expression: string): string | number | undefined

/**
* Using the JSON schema, validates the content
* @param schema JSON schema instance
* @param content object to validate
*/
validateJSON(schema: JSONSchema, content: any): JSONSchemaValidation
}

interface AICIGenOptions {
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 766837c

Please sign in to comment.