Parsers
The parsers
object provides various parsers for common data formats.
JSON5
The parsers.json5
function parses the JSON5 format.
JSON5 is an extension to the popular JSON file format that aims to be easier to write and maintain by hand (e.g. for config files).
In general, parsing a JSON file as JSON5 does not cause harm, but it might be more forgiving to syntactic errors. In addition to JSON5, JSON repair is applied if the initial parse fails.
- JSON5 example
To parse, use parsers.JSON5
. It supports both a text content or a file as input.
YAML
The parsers.YAML
function parses the YAML format.
YAML is more friendly to the LLM tokenizer than JSON and is commonly used in configuration
files.
To parse, use parsers.YAML
. It supports both a text content or a file as input.
TOML
The parsers.TOML
function parses the TOML format.
TOML is more friendly to the LLM tokenizer than JSON and is commonly used in configuration
files.
To parse, use parsers.TOML
. It supports both a text content or a file as input.
JSONL
JSONL 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).
You can use parsers.JSONL
to parse the JSONL files into an array of object (any[]
).
XML
The parsers.XML
function parses for the XML format.
Attribute names are prepended with ”@_“.
front matter
Front matter is a metadata section at the head of a file, typically formatted as YAML.
You can use the parsers.frontmatter
or MD to parse out the metadata into an object
CSV
The parsers.CSV
function parses for the CSV format. If successful, the function returns an array of object where each object represents a row in the CSV file.
The parsers will auto-detect the header names if present; otherwise you should pass an array of header names in the options.
The parsers.PDF
function reads a PDF file and attempts to cleanly convert it into a text format. Read the /genaiscript/reference/scripts/pdf for more information.
DOCX
The parsers.DOCX
function reads a .docx file as raw text.
INI
The parsers.INI
parses .ini files, typically
used for configuration files. This format is similar to the key=value
format.
XLSX
The parsers.XLSX
function reads a .xlsx file and returns an array of objects where each object represents a row in the spreadsheet.
The first row is used as headers.
The function uses the xlsx library.
By default, it reads the first sheet and the first row as headers. You can pass a worksheet name and/or a range to process as options.
Unzip
Unpacks the contents of a zip file and returns an array of files.
HTML to Text
The parsers.HTMLToText
converts HTML to plain text using html-to-text.
Code (JavaScript, Python, C, C++, Java, …)
The parsers.code
function parses source code using the Tree Sitter
library. It returns an AST (Abstract Syntax Tree) that can be used to analyze the code.
The tags
query is a built-in alias for the tree-sitter tags
query that is made available in most tree-sitter libraries.
.env
The parsers.dotEnv
parses .env files, typically
using for configuration files. This format is similar to the key=value
format.
fences
Parse output of LLM similar to output of genaiscript def() function. Expect text to look something like this:
Also supported. …
Returns a list of parsed code sections.
annotations
Parses error, warning annotations in various formats into a list of objects.
tokens
The parsers.tokens
estimates the number of tokens in a string
for the current model. This is useful for estimating the number of prompts that can be generated from a string.
validateJSON
The parsers.validateJSON
function validates a JSON string against a schema.
mustache
Runs the mustache template engine in the string and arguments.
jinja
Runs the jinja template (using @huggingface/jinja).
tidyData
A set of data manipulation options that is internally
used with defData
.
hash
Utility to hash an object, array into a string that is appropriate for hashing purposes.
By default, uses sha-1
, but sha-256
can also be used. The hash packing logic may change between versions of genaiscript.
Command line
Use the parse command from the CLI to try out various parsers.