Skip to content

Commit

Permalink
Refactor system handling and update type definitions (#701)
Browse files Browse the repository at this point in the history
* Refactor system array handling and update type definitions for system and tools in templates

* Move `flexTokens` from `ScriptRuntimeOptions` to `ModelOptions` in types file

* Remove extra line in prompt_template.d.ts file

* Refactor system resolution and update functions for improved clarity and extendability

* lint
  • Loading branch information
pelikhan authored Sep 9, 2024
1 parent bbab058 commit 471e9f2
Show file tree
Hide file tree
Showing 20 changed files with 247 additions and 570 deletions.
50 changes: 11 additions & 39 deletions docs/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 11 additions & 39 deletions 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/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,4 @@ export const CONSOLE_COLOR_ERROR = 91

export const PLAYWRIGHT_DEFAULT_BROWSER = "chromium"
export const MAX_TOKENS_ELLIPSE = "..."
export const ESTIMATE_TOKEN_OVERHEAD = 2
export const ESTIMATE_TOKEN_OVERHEAD = 2
50 changes: 11 additions & 39 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.

5 changes: 3 additions & 2 deletions packages/core/src/promptcontext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { resolveModelConnectionInfo } from "./models"
import { resolveLanguageModel } from "./lm"
import { callExpander } from "./expander"
import { Project } from "./ast"
import { resolveSystems } from "./systems"

export async function createPromptContext(
prj: Project,
Expand Down Expand Up @@ -215,7 +216,7 @@ export async function createPromptContext(
},
runPrompt: async (generator, runOptions): Promise<RunPromptResult> => {
try {
const { label, system = [] } = runOptions || {}
const { label } = runOptions || {}
trace.startDetails(`🎁 run prompt ${label || ""}`)
infoCb?.({ text: `run prompt ${label || ""}` })

Expand Down Expand Up @@ -265,7 +266,7 @@ export async function createPromptContext(
role: "system",
content: "",
}
for (const systemId of system) {
for (const systemId of resolveSystems(prj, runOptions)) {
checkCancelled(cancellationToken)

const system = prj.getTemplate(systemId)
Expand Down
8 changes: 7 additions & 1 deletion packages/core/src/promptdom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,13 @@ export function createFunctionNode(
assert(!!description)
assert(parameters !== undefined)
assert(impl !== undefined)
return { type: "function", name, description, parameters, impl }
return {
type: "function",
name,
description: dedent(description),
parameters,
impl,
}
}

export function createFileMerge(fn: FileMergeHandler): PromptFileMergeNode {
Expand Down
17 changes: 10 additions & 7 deletions packages/core/src/systems.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { Project } from "./ast"
import { arrayify, unique } from "./util"

export function resolveSystems(prj: Project, template: PromptScript) {
const { jsSource } = template
const systems = Array.from(template.system ?? []).slice(0)
export function resolveSystems(
prj: Project,
script: PromptSystemOptions & ModelOptions & { jsSource?: string }
) {
const { jsSource } = script
const systems = arrayify(script.system).slice(0)

if (template.system === undefined) {
if (script.system === undefined) {
const useSchema = /\Wdefschema\W/i.test(jsSource)
if (!template.responseType) {
if (!script.responseType) {
systems.push("system")
systems.push("system.explanations")
}
Expand All @@ -26,9 +29,9 @@ export function resolveSystems(prj: Project, template: PromptScript) {
systems.push("system.diagrams")
}

if (template.tools) {
if (script.tools) {
systems.push("system.tools")
arrayify(template.tools).forEach((tool) =>
arrayify(script.tools).forEach((tool) =>
systems.push(...resolveTools(prj, tool))
)
}
Expand Down
Loading

0 comments on commit 471e9f2

Please sign in to comment.