Skip to content

Commit

Permalink
feat: add jobUrl param, optimize memory queries 🛠️
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Oct 16, 2024
1 parent 25a44c6 commit 40fc22d
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 10 deletions.
1 change: 1 addition & 0 deletions docs/src/content/docs/reference/scripts/system.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ defAgent(
`Your are a helpfull LLM agent that can query GitHub to accomplish tasks. Answer the question in QUERY.
- Prefer diffing job logs rather downloading entire logs which can be very large.
- Always return sha, head_sha information for runs
- do NOT return full job logs, they are too large and will fill the response buffer.
`,
{
model,
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,7 @@ export async function executeChatSession(
}
: undefined,
}
if (model === "gpt-4-32k") delete req.max_completion_tokens
try {
trace.startDetails(`📤 llm request`)
resp = await completer(
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/genaisrc/system.agent_github.genai.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defAgent(
`Your are a helpfull LLM agent that can query GitHub to accomplish tasks. Answer the question in QUERY.
- Prefer diffing job logs rather downloading entire logs which can be very large.
- Always return sha, head_sha information for runs
- do NOT return full job logs, they are too large and will fill the response buffer.
`,
{
model,
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/genaisrc/system.files.genai.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ $`- Make sure to use precisely \`\`\` to guard file code sections.
- Always sure to use precisely \`\`\`\`\` to guard file markdown sections.
- Use full path of filename in code section header.
- do NOT use ## headers with filename
- do NOT emit "preferred_output_format=FILE" when responding to an LLM.
`
if (folder !== ".")
$`When generating new files, place files in folder "${folder}".`
Expand Down
8 changes: 6 additions & 2 deletions packages/core/src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { resolveTokenEncoder } from "./encoders"
export class GitClient implements Git {
readonly git = "git" // Git command identifier
private _defaultBranch: string // Stores the default branch name
private _branch: string // Stores the current branch name
private async resolveExcludedPaths(options?: {
excludedPaths?: ElementOrArray<string>
}): Promise<string[]> {
Expand Down Expand Up @@ -55,8 +56,11 @@ export class GitClient implements Git {
* @returns
*/
async branch(): Promise<string> {
const res = await this.exec(["branch", "--show-current"])
return res.trim()
if (!this._branch) {
const res = await this.exec(["branch", "--show-current"])
this._branch = res.trim()
}
return this._branch
}

async listBranches(): Promise<string[]> {
Expand Down
4 changes: 1 addition & 3 deletions packages/core/src/promptdom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,7 @@ function renderDefNode(def: PromptDefNode): string {
const diffFormat =
body.length > 500
? " preferred_output_format=CHANGELOG "
: body.length < 200
? " preferred_output_format=FILE "
: ""
: ""
const res =
(name ? name + ":\n" : "") +
dfence +
Expand Down
27 changes: 23 additions & 4 deletions packages/core/src/runpromptcontext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ import {
} from "./promptdom"
import { MarkdownTrace } from "./trace"
import { GenerationOptions } from "./generation"
import { promptParametersSchemaToJSONSchema } from "./parameters"
import {
parametersToVars,
promptParametersSchemaToJSONSchema,
} from "./parameters"
import { consoleLogFormat } from "./logging"
import { isGlobMatch } from "./glob"
import { arrayify, logError, logVerbose } from "./util"
Expand Down Expand Up @@ -62,6 +65,7 @@ import { dedent } from "./indent"
import { runtimeHost } from "./host"
import { writeFileEdits } from "./fileedits"
import { agentAddMemory, agentQueryMemory, defMemory } from "./agent"
import { shellQuote } from "./shell"

export function createChatTurnGenerationContext(
options: GenerationOptions,
Expand Down Expand Up @@ -353,12 +357,22 @@ export function createChatGenerationContext(
required: ["query"],
},
async (args) => {
const { context, query } = args
infoCb?.({ text: `${agentLabel}: ${query}` })
// the LLM automatically adds extract arguments to the context
const { context, ...rest } = args
const { query, ...argsNoQuery } = rest
infoCb?.({
text: `${agentLabel}: ${query} ${parametersToVars(argsNoQuery)}`,
})

const hasExtraArgs = Object.keys(argsNoQuery).length > 0
let memoryAnswer: string
if (memory && query && !disableMemoryQuery)
memoryAnswer = await agentQueryMemory(ctx, query)
memoryAnswer = await agentQueryMemory(
ctx,
query + hasExtraArgs
? `\n${YAML.stringify(argsNoQuery)}`
: ""
)

const res = await ctx.runPrompt(
async (_) => {
Expand All @@ -374,6 +388,11 @@ export function createChatGenerationContext(
if (memoryAnswer)
_.$`- The QUERY applied to the agent memory is in MEMORY.`
_.def("QUERY", query)
if (Object.keys(argsNoQuery).length)
_.defData("QUERY_CONTEXT", argsNoQuery, {
format: "yaml",
})

if (memoryAnswer) _.def("MEMORY", memoryAnswer)
if (memory)
_.defOutputProcessor(async ({ text }) => {
Expand Down
7 changes: 6 additions & 1 deletion packages/sample/genaisrc/github-agent.genai.mts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ script({
"agent_docs",
],
parameters: {
jobUrl: { type: "string" }, // URL of the job
workflow: { type: "string" }, // Workflow name
failure_run_id: { type: "number" }, // ID of the failed run
branch: { type: "string" }, // Branch name
Expand All @@ -17,9 +18,13 @@ const {
workflow = "build.yml",
failure_run_id,
branch = await git.branch(),
jobUrl,
} = env.vars

if (failure_run_id) {
if (jobUrl) {
$`1. Extract the run id and job id from the ${jobUrl}`
$`2. Find the last successful run before the failed run for the same workflow and branch`
} else if (failure_run_id) {
$`1. Find the failed run ${failure_run_id} of ${workflow} for branch ${branch}
2. Find the last successful run before the failed run for the same workflow and branch`
} else {
Expand Down

0 comments on commit 40fc22d

Please sign in to comment.