-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
133 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { promptyParse } from "./prompty" | ||
import { describe, test, beforeEach } from "node:test" | ||
import assert from "node:assert/strict" | ||
|
||
describe("promptyParse", () => { | ||
test("correctly parses an empty markdown string", () => { | ||
const result = promptyParse("") | ||
assert.deepStrictEqual(result, { | ||
frontmatter: {}, | ||
content: "", | ||
messages: [], | ||
}) | ||
}) | ||
|
||
test("correctly parses a markdown string without frontmatter", () => { | ||
const content = "This is a sample content without frontmatter." | ||
const result = promptyParse(content) | ||
assert.deepStrictEqual(result, { | ||
frontmatter: {}, | ||
content: content, | ||
messages: [{ role: "system", content: content }], | ||
}) | ||
}) | ||
|
||
test("correctly parses a markdown string with valid frontmatter", () => { | ||
const markdownString = `--- | ||
name: Test | ||
description: A test description | ||
version: 1.0.0 | ||
authors: | ||
- Author1 | ||
- Author2 | ||
tags: | ||
- tag1 | ||
- tag2 | ||
sample: | ||
key: value | ||
--- | ||
# Heading | ||
Content below heading.` | ||
const result = promptyParse(markdownString) | ||
assert.deepStrictEqual(result.frontmatter, { | ||
name: "Test", | ||
description: "A test description", | ||
version: "1.0.0", | ||
authors: ["Author1", "Author2"], | ||
tags: ["tag1", "tag2"], | ||
sample: { key: "value" }, | ||
}) | ||
assert.strictEqual(result.content, "# Heading\nContent below heading.") | ||
}) | ||
|
||
test("correctly parses a markdown string with content split into roles", () => { | ||
const markdownContent = `user: | ||
User's message | ||
assistant: | ||
Assistant's reply | ||
user: | ||
Another message from the user` | ||
const result = promptyParse(markdownContent) | ||
assert.deepStrictEqual(result.messages, [ | ||
{ role: "user", content: "User's message" }, | ||
{ role: "assistant", content: "Assistant's reply" }, | ||
{ role: "user", content: "Another message from the user" }, | ||
]) | ||
}) | ||
|
||
test("correctly handles a markdown string with content but without roles", () => { | ||
const markdownContent = `Just some content without specifying roles.` | ||
const result = promptyParse(markdownContent) | ||
assert.deepStrictEqual(result.messages, [ | ||
{ role: "system", content: markdownContent }, | ||
]) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { ChatCompletionMessageParam } from "./chattypes" | ||
import { splitMarkdown } from "./frontmatter" | ||
import { YAMLTryParse } from "./yaml" | ||
|
||
export interface PromptyFrontmatter { | ||
name?: string | ||
description?: string | ||
version?: string | ||
authors?: string[] | ||
tags?: string[] | ||
sample?: Record<string, any> | ||
} | ||
|
||
export function promptyParse(text: string): { | ||
frontmatter: PromptyFrontmatter | ||
content: string | ||
messages: ChatCompletionMessageParam[] | ||
} { | ||
const { frontmatter = "", content = "" } = splitMarkdown(text) | ||
const fm = YAMLTryParse(frontmatter) ?? {} | ||
// todo: validate frontmatter? | ||
const messages: ChatCompletionMessageParam[] = [] | ||
|
||
// split | ||
const rx = /^\s*(system|user|assistant)\s*:\s*$/gim | ||
const lines = content.split(/\r?\n/g) | ||
let role: "system" | "user" | "assistant" | undefined = "system" | ||
let chunk: string[] = [] | ||
|
||
const pushMessage = () => { | ||
if (role && chunk.length && chunk.some((l) => !!l)) { | ||
messages.push({ | ||
role, | ||
content: chunk.join("\n"), | ||
}) | ||
} | ||
} | ||
|
||
for (const line of lines) { | ||
const m = rx.exec(line) | ||
if (m) { | ||
// next role starts | ||
pushMessage() | ||
role = m[1] as "system" | "user" | "assistant" | ||
chunk = [] | ||
} else { | ||
chunk.push(line) | ||
} | ||
} | ||
pushMessage() | ||
return { frontmatter: fm, content, messages } | ||
} |