Skip to content

Commit

Permalink
feat(agents): add retry for parser errors (i-am-bee#204)
Browse files Browse the repository at this point in the history
Signed-off-by: Tomas Dvorak <[email protected]>
  • Loading branch information
Tomas2D authored and tonxxd committed Dec 3, 2024
1 parent c176d05 commit f9de559
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/agents/bee/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,9 @@ export const BeeToolNotFoundPrompt = new PromptTemplate({
Use one of the following functions: {{#trim}}{{#tools}}{{name}},{{/tools}}{{/trim}}
{{/tools.length}}`,
});

export const BeeSchemaErrorPrompt = new PromptTemplate({
schema: z.object({}).passthrough(),
template: `Error: The generated response does not adhere to the communication structure mentioned in the system prompt.
You communicate only in instruction lines. Valid instruction lines are 'Thought' followed by either 'Function Name' + 'Function Input' + 'Function Output' or 'Final Answer'.`,
});
8 changes: 7 additions & 1 deletion src/agents/bee/runners/granite/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { PromptTemplate } from "@/template.js";
import { z } from "zod";
import { BeeAssistantPrompt } from "@/agents/bee/prompts.js";
import { BeeAssistantPrompt, BeeSchemaErrorPrompt } from "@/agents/bee/prompts.js";

export const GraniteBeeAssistantPrompt = BeeAssistantPrompt.fork((config) => ({
...config,
Expand Down Expand Up @@ -78,3 +78,9 @@ The current date and time is contained in the most recent user message.
{{instructions}}
`,
});

export const GraniteBeeSchemaErrorPrompt = BeeSchemaErrorPrompt.fork((config) => ({
...config,
template: `Error: The generated response does not adhere to the communication structure mentioned in the system prompt.
You communicate only in instruction lines. Valid instruction lines are 'Thought' followed by either 'Tool Name' + 'Tool Input' or 'Final Answer'.`,
}));
2 changes: 2 additions & 0 deletions src/agents/bee/runners/granite/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { BeeAgent, BeeInput } from "@/agents/bee/agent.js";
import type { GetRunContext } from "@/context.js";
import {
GraniteBeeAssistantPrompt,
GraniteBeeSchemaErrorPrompt,
GraniteBeeSystemPrompt,
} from "@/agents/bee/runners/granite/prompts.js";

Expand All @@ -39,6 +40,7 @@ export class GraniteRunner extends DefaultRunner {
...input.templates,
system: input.templates?.system ?? GraniteBeeSystemPrompt,
assistant: input.templates?.assistant ?? GraniteBeeAssistantPrompt,
schemaError: input.templates?.schemaError ?? GraniteBeeSchemaErrorPrompt,
},
},
options,
Expand Down
2 changes: 2 additions & 0 deletions src/agents/bee/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { Callback } from "@/emitter/types.js";
import { AnyTool, BaseToolRunOptions, ToolError, ToolOutput } from "@/tools/base.js";
import {
BeeAssistantPrompt,
BeeSchemaErrorPrompt,
BeeSystemPrompt,
BeeToolErrorPrompt,
BeeToolInputErrorPrompt,
Expand Down Expand Up @@ -128,6 +129,7 @@ export interface BeeAgentTemplates {
toolInputError: typeof BeeToolInputErrorPrompt;
toolNoResultError: typeof BeeToolNoResultsPrompt;
toolNotFoundError: typeof BeeToolNotFoundPrompt;
schemaError: typeof BeeSchemaErrorPrompt;
}

export type BeeParserInput = LinePrefixParser.define<{
Expand Down

0 comments on commit f9de559

Please sign in to comment.