Skip to content

Commit

Permalink
Refactor system resolution and update functions for improved clarity …
Browse files Browse the repository at this point in the history
…and extendability
  • Loading branch information
pelikhan committed Sep 9, 2024
1 parent c8fcc99 commit e06d16d
Show file tree
Hide file tree
Showing 19 changed files with 143 additions and 197 deletions.
17 changes: 5 additions & 12 deletions docs/genaisrc/genaiscript.d.ts

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

17 changes: 5 additions & 12 deletions genaisrc/genaiscript.d.ts

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

17 changes: 5 additions & 12 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)) {

Check failure on line 269 in packages/core/src/promptcontext.ts

View workflow job for this annotation

GitHub Actions / build

The function `resolveSystems` is missing the second argument `runOptions`. This could lead to unexpected behavior or runtime errors. Please ensure that the necessary arguments are provided. 🧐
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),

Check failure on line 258 in packages/core/src/promptdom.ts

View workflow job for this annotation

GitHub Actions / build

The function `dedent` is not defined in this scope. Please ensure that it is imported or defined before use. 🕵️‍♀️
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 = arrayify(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
17 changes: 5 additions & 12 deletions packages/core/src/types/prompt_template.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ interface EmbeddingsModelConnectionOptions {

interface EmbeddingsModelOptions extends EmbeddingsModelConnectionOptions {}

interface ScriptRuntimeOptions {
interface PromptSystemOptions {
/**
* List of system script ids used by the prompt.
*/
Expand All @@ -218,7 +218,9 @@ interface ScriptRuntimeOptions {
* List of tools used by the prompt.
*/
tools?: SystemToolId | SystemToolId[]
}

interface ScriptRuntimeOptions {
/**
* Secrets required by the prompt
*/
Expand Down Expand Up @@ -339,6 +341,7 @@ interface PromptTest {
interface PromptScript
extends PromptLike,
ModelOptions,
PromptSystemOptions,
EmbeddingsModelOptions,
ScriptRuntimeOptions {
/**
Expand Down Expand Up @@ -1445,21 +1448,11 @@ interface WriteTextOptions extends ContextExpansionOptions {

type PromptGenerator = (ctx: ChatGenerationContext) => Awaitable<unknown>

interface PromptGeneratorOptions extends ModelOptions {
interface PromptGeneratorOptions extends ModelOptions, PromptSystemOptions {
/**
* Label for trace
*/
label?: string

/**
* List of system prompts if any
*/
system?: SystemPromptId | SystemPromptId[]

/**
* List of tools used by the prompt.
*/
tools?: SystemToolId | SystemToolId[]
}

interface FileOutputOptions {
Expand Down
17 changes: 5 additions & 12 deletions packages/sample/genaisrc/genaiscript.d.ts

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

55 changes: 48 additions & 7 deletions packages/sample/genaisrc/nested-agents.genai.mts
Original file line number Diff line number Diff line change
@@ -1,20 +1,61 @@
script({ model: "openai:gpt-4o" })

/**
* This agent loads the file system prompts.
*/
defTool(
"agent-fs",
"Invokes gpt-4o to execute a LLM request",
"agent_file_system",
`An agent that uses gpt-4o to execute an LLM requests with tools that can search and read the file system.
`,
{
prompt: {
type: "string",
description: "the prompt to be executed by the LLM",
},
},
async ({ prompt }) => {
const res = await env.generator.runPrompt(_ => {
_.writeText(prompt)
}, {
const res = await env.generator.runPrompt(
(_) => {
_.$`You are an AI assistant that can help with file system tasks.
Answer the user question in the most concise way possible. Use wildcards and regex if needed.
If the question is ambiguous, ask for clarification.
Use tools to search and read the file system.
QUESTION:`
_.writeText(prompt)
},
{
model: "openai:gpt-4o",
label: `llm-4o agent_fs ${prompt}`,
tools: "fs",
}
)
return res.text
}
)

/**
* This agent loads the file system prompts.
*/
defTool(
"agent_code_interpreter",
"An LLM agent that execute python code in a sandboxed container.",
{
code: {
type: "string",
description: "the python to be executed by the LLM",
},
},
async ({ code }) => {
const res = await env.generator.runPrompt(code, {
model: "openai:gpt-4o",
label: "llm-4o with fs",
tools: "fs"
label: "llm-4o agent_fs",
tools: "python",
})
return res.text
}
)

// now as a question about the file system
$`Do a statistical analyzis of all data (*.csv) in the project.`
Loading

0 comments on commit e06d16d

Please sign in to comment.