Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PromptTemplate -> PromptScript #384

Merged
merged 2 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/cli/src/sarif.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function isSARIFFilename(f: string) {

// use with MS-SarifVSCode.sarif-viewer
export function convertDiagnosticsToSARIF(
template: PromptTemplate,
template: PromptScript,
issues: Diagnostic[]
) {
const sarifRunBuilder = new SarifRunBuilder().initSimple({
Expand Down
5 changes: 4 additions & 1 deletion packages/core/bundleprompts.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ const promptDefs = {
null,
4
),
"genaiscript.d.ts": ["./src/prompt_template.d.ts", "./prompt_type.d.ts"]
"genaiscript.d.ts": [
"./src/types/prompt_template.d.ts",
"./src/types/prompt_type.d.ts",
]
.map((fn) => readFileSync(fn, { encoding: "utf-8" }))
.map((src) =>
src.replace(/^\/\/\/\s+<reference\s+path="[^"]+"\s*\/>\s*$/gm, "")
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/ast.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/// <reference path="./prompt_template.d.ts" />
/// <reference path="./types/prompt_template.d.ts" />

import { host } from "./host"

type PromptTemplate = globalThis.PromptTemplate
export type { PromptTemplate }
type PromptScript = globalThis.PromptScript
export type { PromptScript }

export interface FileReference {
name: string
Expand Down Expand Up @@ -77,7 +77,7 @@ export class Fragment {
}
}

export function templateGroup(template: PromptTemplate) {
export function templateGroup(template: PromptScript) {
return (
template.group ||
(/^system/i.test(template.id) ? "system" : "") ||
Expand All @@ -94,7 +94,7 @@ export class Project {
readonly fragmentById: Record<string, Fragment[]> = {}
readonly fragmentByFullId: Record<string, Fragment> = {}
readonly allFragments: Fragment[] = []
readonly templates: PromptTemplate[] = []
readonly templates: PromptScript[] = []
readonly diagnostics: Diagnostic[] = []

_finalizers: (() => void)[] = []
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/clihelp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { NPM_CLI_PACKAGE } from "./constants"
import { MarkdownTrace } from "./trace"

export function generateCliArguments(
template: PromptTemplate,
template: PromptScript,
fragment: Fragment,
options: RunTemplateOptions,
command: "run" | "batch"
Expand All @@ -30,7 +30,7 @@ export function generateCliArguments(

export function traceCliArgs(
trace: MarkdownTrace,
template: PromptTemplate,
template: PromptScript,
fragment: Fragment,
options: RunTemplateOptions
) {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/copy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PromptTemplate } from "./ast"
import { PromptScript } from "./ast"
import { GENAI_JS_EXT, GENAI_SRC } from "./constants"
import { host } from "./host"
import { fileExists, writeText } from "./fs"
Expand All @@ -9,7 +9,7 @@ function promptPath(id: string) {
return host.resolvePath(prompts, id + GENAI_JS_EXT)
}
export async function copyPrompt(
t: PromptTemplate,
t: PromptScript,
options: { fork: boolean; name?: string }
) {
await host.createDirectory(promptPath(null))
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/evalprompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { consoleLogFormat } from "./logging"

export async function evalPrompt(
ctx0: PromptContext,
r: PromptTemplate,
r: PromptScript,
options?: {
sourceMaps?: boolean
logCb?: (msg: string) => void
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/expander.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Fragment, PromptTemplate } from "./ast"
import { Fragment, PromptScript } from "./ast"
import { assert, normalizeFloat, normalizeInt, normalizeString } from "./util"
import { MarkdownTrace } from "./trace"
import { isCancelError } from "./error"
Expand Down Expand Up @@ -75,7 +75,7 @@ export interface FragmentTransformResponse extends PromptGenerationOutput {
}

async function callExpander(
r: PromptTemplate,
r: PromptScript,
vars: ExpansionVariables,
trace: MarkdownTrace,
options: RunTemplateOptions
Expand Down Expand Up @@ -198,7 +198,7 @@ function traceEnv(
trace.endDetails()
}

function resolveSystems(template: PromptTemplate) {
function resolveSystems(template: PromptScript) {
const { jsSource } = template
const systems = (template.system ?? []).slice(0)
if (template.system === undefined) {
Expand All @@ -219,7 +219,7 @@ function resolveSystems(template: PromptTemplate) {
}

export async function expandTemplate(
template: PromptTemplate,
template: PromptScript,
fragment: Fragment,
options: RunTemplateOptions,
env: ExpansionVariables,
Expand Down
26 changes: 17 additions & 9 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.

2 changes: 1 addition & 1 deletion packages/core/src/importprompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function resolveGlobal(): any {

export async function importPrompt(
ctx0: PromptContext,
r: PromptTemplate,
r: PromptScript,
options?: {
logCb?: (msg: string) => void
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/parser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { lookupMime } from "./mime"
import { sha256string, strcmp } from "./util"
import { Project, TextFile, PromptTemplate, Fragment } from "./ast"
import { Project, TextFile, PromptScript, Fragment } from "./ast"
import { defaultPrompts } from "./default_prompts"
import { parsePromptTemplate } from "./template"
import { fileExists, readText } from "./fs"
Expand Down Expand Up @@ -116,7 +116,7 @@ export async function parseProject(options: {
}
runFinalizers()

function templKey(t: PromptTemplate) {
function templKey(t: PromptScript) {
const pref = t.unlisted ? "Z" : t.filename ? "A" : "B"
return pref + t.title + t.id
}
Expand Down
36 changes: 18 additions & 18 deletions packages/core/src/promptdom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@ export interface PromptNode extends ContextExpansionOptions {

export interface PromptTextNode extends PromptNode {
type: "text"
value: string | Promise<string>
value: Awaitable<string>
resolved?: string
}

export interface PromptDefNode extends PromptNode, DefOptions {
type: "def"
name: string
value: LinkedFile | Promise<LinkedFile>
value: Awaitable<LinkedFile>
resolved?: LinkedFile
}

export interface PromptAssistantNode extends PromptNode {
type: "assistant"
value: string | Promise<string>
value: Awaitable<string>
resolve?: string
}

Expand All @@ -61,7 +61,7 @@ export interface PromptImage {

export interface PromptImageNode extends PromptNode {
type: "image"
value: PromptImage | Promise<PromptImage>
value: Awaitable<PromptImage>
resolved?: PromptImage
}

Expand Down Expand Up @@ -91,7 +91,7 @@ export interface PromptOutputProcessorNode extends PromptNode {
}

export function createTextNode(
value: string | Promise<string>,
value: Awaitable<string>,
options?: ContextExpansionOptions
): PromptTextNode {
assert(value !== undefined)
Expand Down Expand Up @@ -157,7 +157,7 @@ function renderDefNode(def: PromptDefNode): string {
}

export function createAssistantNode(
value: string | Promise<string>,
value: Awaitable<string>,
options?: ContextExpansionOptions
): PromptAssistantNode {
assert(value !== undefined)
Expand All @@ -174,7 +174,7 @@ export function createStringTemplateNode(
}

export function createImageNode(
value: PromptImage | Promise<PromptImage>,
value: Awaitable<PromptImage>,
options?: ContextExpansionOptions
): PromptImageNode {
assert(value !== undefined)
Expand Down Expand Up @@ -255,17 +255,17 @@ export function appendChild(parent: PromptNode, child: PromptNode): void {
}

export interface PromptNodeVisitor {
node?: (node: PromptNode) => void | Promise<void>
afterNode?: (node: PromptNode) => void | Promise<void>
text?: (node: PromptTextNode) => void | Promise<void>
def?: (node: PromptDefNode) => void | Promise<void>
image?: (node: PromptImageNode) => void | Promise<void>
schema?: (node: PromptSchemaNode) => void | Promise<void>
function?: (node: PromptFunctionNode) => void | Promise<void>
fileMerge?: (node: PromptFileMergeNode) => void | Promise<void>
stringTemplate?: (node: PromptStringTemplateNode) => void | Promise<void>
outputProcessor?: (node: PromptOutputProcessorNode) => void | Promise<void>
assistant?: (node: PromptAssistantNode) => void | Promise<void>
node?: (node: PromptNode) => Awaitable<void>
afterNode?: (node: PromptNode) => Awaitable<void>
text?: (node: PromptTextNode) => Awaitable<void>
def?: (node: PromptDefNode) => Awaitable<void>
image?: (node: PromptImageNode) => Awaitable<void>
schema?: (node: PromptSchemaNode) => Awaitable<void>
function?: (node: PromptFunctionNode) => Awaitable<void>
fileMerge?: (node: PromptFileMergeNode) => Awaitable<void>
stringTemplate?: (node: PromptStringTemplateNode) => Awaitable<void>
outputProcessor?: (node: PromptOutputProcessorNode) => Awaitable<void>
assistant?: (node: PromptAssistantNode) => Awaitable<void>
}

export async function visitNode(node: PromptNode, visitor: PromptNodeVisitor) {
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/promptrunner.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ChatCompletionResponse, ChatCompletionTool } from "./chat"
import { Fragment, PromptTemplate } from "./ast"
import { Fragment, PromptScript } from "./ast"
import { commentAttributes, stringToPos } from "./parser"
import { assert, logVerbose, relativePath } from "./util"
import {
Expand Down Expand Up @@ -31,7 +31,7 @@ import { createFetch } from "./fetch"

async function fragmentVars(
trace: MarkdownTrace,
template: PromptTemplate,
template: PromptScript,
frag: Fragment
) {
const { file } = frag
Expand Down Expand Up @@ -133,7 +133,7 @@ async function fragmentVars(
}

export async function runTemplate(
template: PromptTemplate,
template: PromptScript,
fragment: Fragment,
options: RunTemplateOptions
): Promise<FragmentTransformResponse> {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { logVerbose } from "./util";

export function createScript(
name: string,
options?: { template: PromptTemplate; title?: string }
options?: { template: PromptScript; title?: string }
) {
const { template, title } = options || {}
const t = structuredClone(
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/template.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Project, Fragment, PromptTemplate } from "./ast"
import { Project, Fragment, PromptScript } from "./ast"
import { BUILTIN_PREFIX } from "./constants"
import { JSON5TryParse } from "./json5"
function templateIdFromFileName(filename: string) {
Expand Down Expand Up @@ -178,7 +178,7 @@ class Checker<T extends PromptLike> {
}
}

async function parseMeta(r: PromptTemplate) {
async function parseMeta(r: PromptScript) {
// shortcut
const m = /\b(?<kind>system|script)\(\s*(?<meta>\{.*?\})\s*\)/s.exec(
r.jsSource
Expand Down Expand Up @@ -384,18 +384,18 @@ async function parsePromptTemplateCore(
filename: string,
content: string,
prj: Project,
finalizer: (checker: Checker<PromptTemplate>) => void
finalizer: (checker: Checker<PromptScript>) => void
) {
const r = {
id: templateIdFromFileName(filename),
text: "<nothing yet>",
jsSource: content,
} as PromptTemplate
} as PromptScript
if (!filename.startsWith(BUILTIN_PREFIX)) r.filename = filename

try {
const meta = await parseMeta(r)
const checker = new Checker<PromptTemplate>(
const checker = new Checker<PromptScript>(
r,
filename,
prj.diagnostics,
Expand Down
Loading
Loading