diff --git a/.vscode/settings.json b/.vscode/settings.json index dfa312dcd2..78a1c974a0 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -14,6 +14,7 @@ "gptoolsjs", "llmrequest", "localai", + "mardownify", "millis", "ollama", "openai", diff --git a/docs/genaisrc/genaiscript.d.ts b/docs/genaisrc/genaiscript.d.ts index 6f855d1cdc..a64d52cbb4 100644 --- a/docs/genaisrc/genaiscript.d.ts +++ b/docs/genaisrc/genaiscript.d.ts @@ -892,6 +892,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 { diff --git a/docs/src/content/docs/reference/scripts/parsers.md b/docs/src/content/docs/reference/scripts/parsers.md index d3999ea3bb..a878fd8f1d 100644 --- a/docs/src/content/docs/reference/scripts/parsers.md +++ b/docs/src/content/docs/reference/scripts/parsers.md @@ -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) +``` diff --git a/docs/src/content/docs/reference/scripts/schemas.mdx b/docs/src/content/docs/reference/scripts/schemas.mdx index 7f188574e6..ce985f3b14 100644 --- a/docs/src/content/docs/reference/scripts/schemas.mdx +++ b/docs/src/content/docs/reference/scripts/schemas.mdx @@ -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) +``` \ No newline at end of file diff --git a/genaisrc/genaiscript.d.ts b/genaisrc/genaiscript.d.ts index 6f855d1cdc..a64d52cbb4 100644 --- a/genaisrc/genaiscript.d.ts +++ b/genaisrc/genaiscript.d.ts @@ -892,6 +892,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 { diff --git a/packages/core/src/genaisrc/genaiscript.d.ts b/packages/core/src/genaisrc/genaiscript.d.ts index 6f855d1cdc..a64d52cbb4 100644 --- a/packages/core/src/genaisrc/genaiscript.d.ts +++ b/packages/core/src/genaisrc/genaiscript.d.ts @@ -892,6 +892,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 { diff --git a/packages/core/src/parsers.test.ts b/packages/core/src/parsers.test.ts new file mode 100644 index 0000000000..b54fe557c8 --- /dev/null +++ b/packages/core/src/parsers.test.ts @@ -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 + + 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("value") + 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) + }) +}) diff --git a/packages/core/src/parsers.ts b/packages/core/src/parsers.ts index f77712dcf5..d3d3210af7 100644 --- a/packages/core/src/parsers.ts +++ b/packages/core/src/parsers.ts @@ -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 @@ -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 }), }) } diff --git a/packages/core/src/toml.ts b/packages/core/src/toml.ts index 47ca9e3de9..30884a10d5 100644 --- a/packages/core/src/toml.ts +++ b/packages/core/src/toml.ts @@ -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 } diff --git a/packages/core/src/types/prompt_template.d.ts b/packages/core/src/types/prompt_template.d.ts index fb19f01a50..54bfb2310b 100644 --- a/packages/core/src/types/prompt_template.d.ts +++ b/packages/core/src/types/prompt_template.d.ts @@ -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 { diff --git a/packages/sample/genaisrc/genaiscript.d.ts b/packages/sample/genaisrc/genaiscript.d.ts index 6f855d1cdc..a64d52cbb4 100644 --- a/packages/sample/genaisrc/genaiscript.d.ts +++ b/packages/sample/genaisrc/genaiscript.d.ts @@ -892,6 +892,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 { diff --git a/packages/sample/genaisrc/node/genaiscript.d.ts b/packages/sample/genaisrc/node/genaiscript.d.ts index 6f855d1cdc..a64d52cbb4 100644 --- a/packages/sample/genaisrc/node/genaiscript.d.ts +++ b/packages/sample/genaisrc/node/genaiscript.d.ts @@ -892,6 +892,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 { diff --git a/packages/sample/genaisrc/python/genaiscript.d.ts b/packages/sample/genaisrc/python/genaiscript.d.ts index 6f855d1cdc..a64d52cbb4 100644 --- a/packages/sample/genaisrc/python/genaiscript.d.ts +++ b/packages/sample/genaisrc/python/genaiscript.d.ts @@ -892,6 +892,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 { diff --git a/packages/sample/genaisrc/style/genaiscript.d.ts b/packages/sample/genaisrc/style/genaiscript.d.ts index 6f855d1cdc..a64d52cbb4 100644 --- a/packages/sample/genaisrc/style/genaiscript.d.ts +++ b/packages/sample/genaisrc/style/genaiscript.d.ts @@ -892,6 +892,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 { diff --git a/packages/sample/src/aici/genaiscript.d.ts b/packages/sample/src/aici/genaiscript.d.ts index 6f855d1cdc..a64d52cbb4 100644 --- a/packages/sample/src/aici/genaiscript.d.ts +++ b/packages/sample/src/aici/genaiscript.d.ts @@ -892,6 +892,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 { diff --git a/packages/sample/src/errors/genaiscript.d.ts b/packages/sample/src/errors/genaiscript.d.ts index 6f855d1cdc..a64d52cbb4 100644 --- a/packages/sample/src/errors/genaiscript.d.ts +++ b/packages/sample/src/errors/genaiscript.d.ts @@ -892,6 +892,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 { diff --git a/packages/sample/src/makecode/genaiscript.d.ts b/packages/sample/src/makecode/genaiscript.d.ts index 6f855d1cdc..a64d52cbb4 100644 --- a/packages/sample/src/makecode/genaiscript.d.ts +++ b/packages/sample/src/makecode/genaiscript.d.ts @@ -892,6 +892,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 { diff --git a/packages/sample/src/tla/genaiscript.d.ts b/packages/sample/src/tla/genaiscript.d.ts index 6f855d1cdc..a64d52cbb4 100644 --- a/packages/sample/src/tla/genaiscript.d.ts +++ b/packages/sample/src/tla/genaiscript.d.ts @@ -892,6 +892,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 { diff --git a/packages/sample/src/vision/genaiscript.d.ts b/packages/sample/src/vision/genaiscript.d.ts index 6f855d1cdc..a64d52cbb4 100644 --- a/packages/sample/src/vision/genaiscript.d.ts +++ b/packages/sample/src/vision/genaiscript.d.ts @@ -892,6 +892,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 { diff --git a/slides/genaisrc/genaiscript.d.ts b/slides/genaisrc/genaiscript.d.ts index 6f855d1cdc..a64d52cbb4 100644 --- a/slides/genaisrc/genaiscript.d.ts +++ b/slides/genaisrc/genaiscript.d.ts @@ -892,6 +892,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 {