Skip to content

Commit

Permalink
feat: 🎉 add XML parsing and CLI command update
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Oct 19, 2024
1 parent eb1cfea commit 2245a93
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 8 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@
"genai:docify": "node packages/cli/built/genaiscript.cjs run docify",
"gcm": "node packages/cli/built/genaiscript.cjs run gcm",
"genai": "node packages/cli/built/genaiscript.cjs run",
"upgrade:deps": "zx scripts/upgrade-deps.mjs"
"upgrade:deps": "zx scripts/upgrade-deps.mjs",
"cli": "node packages/cli/built/genaiscript.cjs"
},
"release-it": {
"github": {
Expand Down
12 changes: 11 additions & 1 deletion packages/cli/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
PROMPTY_REGEX,
TOML_REGEX,
XLSX_REGEX,
XML_REGEX,
YAML_REGEX,
} from "../../core/src/constants"
import { promptyParse, promptyToGenAIScript } from "../../core/src/prompty"
Expand All @@ -35,6 +36,7 @@ import { XLSXParse } from "../../core/src/xlsx"
import { jinjaRender } from "../../core/src/jinja"
import { splitMarkdown } from "../../core/src/frontmatter"
import { parseOptionsVars } from "./vars"
import { XMLParse } from "../../core/src/xml"

/**
* This module provides various parsing utilities for different file types such
Expand Down Expand Up @@ -98,7 +100,14 @@ export async function parseJinja2(
if (PROMPTY_REGEX.test(file)) src = promptyParse(src).content
else if (MD_REGEX.test(file)) src = splitMarkdown(src).content

const vars = parseOptionsVars(options.vars, process.env)
const vars: Record<string, any> = parseOptionsVars(
options.vars,
process.env
)
for (const k in vars) {
const i = parseFloat(vars[k])
if (!isNaN(i)) vars[k] = i
}
const res = jinjaRender(src, vars)
console.log(res)
}
Expand All @@ -116,6 +125,7 @@ export async function parseAnyToJSON(
else if (TOML_REGEX.test(file)) data = TOMLParse(src)
else if (JSON5_REGEX.test(file)) data = JSON5parse(src)
else if (YAML_REGEX.test(file)) data = YAMLParse(src)
else if (XML_REGEX.test(file)) data = XMLParse(src)
else throw new Error("Unsupported file format")
}

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const YAML_REGEX = /\.yaml$/i
export const INI_REGEX = /\.ini$/i
export const TOML_REGEX = /\.toml$/i
export const XLSX_REGEX = /\.xlsx$/i
export const XML_REGEX = /\.xml$/i
export const DOCX_REGEX = /\.docx$/i
export const PDF_REGEX = /\.pdf$/i
export const MD_REGEX = /\.md$/i
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/jinja.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Import the Template class from the @huggingface/jinja package
import { Template } from "@huggingface/jinja"
import { ChatCompletionMessageParam } from "./chattypes"
import { collapseEmptyLines } from "./util"

/**
* Renders a string template using the Jinja templating engine.
Expand All @@ -24,7 +25,7 @@ export function jinjaRender(
const res = t.render(values)

// Return the rendered string
return res
return collapseEmptyLines(res)
}

export function jinjaRenderChatMessage(
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/prompty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ function promptyFrontmatterToMeta(frontmatter: PromptyFrontmatter): PromptArgs {
if (parameters && sample && typeof sample === "object")
for (const p in sample) {
const s = sample[p]
if (s !== undefined && parameters[p].type !== "object")
parameters[p].default = s
const pp = parameters[p]
if (s !== undefined && pp && pp?.type !== "object") pp.default = s
}

let modelName: string = undefined
Expand Down Expand Up @@ -123,7 +123,7 @@ function promptyFrontmatterToMeta(frontmatter: PromptyFrontmatter): PromptArgs {
export function promptyParse(text: string): PromptyDocument {
const { frontmatter = "", content = "" } = splitMarkdown(text)
const fm = YAMLTryParse(frontmatter) ?? {}
const meta = promptyFrontmatterToMeta(fm)
const meta = fm ? promptyFrontmatterToMeta(fm) : {}
// todo: validate frontmatter?
const messages: ChatCompletionMessageParam[] = []

Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ export function deleteUndefinedValues<T extends Record<string, any>>(o: T): T {
return o
}

export function collapseEmptyLines(text: string) {
return text?.replace(/(\r?\n){2,}/g, "\n\n")
}

export function assert(
cond: boolean,
msg = "Assertion failed",
Expand Down
12 changes: 10 additions & 2 deletions packages/sample/src/basic.prompty
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ inputs:
question:
type: string
sample:
"question": Who is the most famous person in the world?
question: Who is the most famous person in the world?
n: 5
tests:
- vars:
question: what is the capital of france?
Expand All @@ -24,7 +25,14 @@ system:
You are an AI assistant who helps people find information.
As the assistant, you answer questions briefly, succinctly.

{% if n > 0 %}
Give multiple hints.
{% else %}
Give only 1 hint.
{% endif %}


user:
{{question}}

{{hint}}
{{hint}}

0 comments on commit 2245a93

Please sign in to comment.