Skip to content

Commit

Permalink
feat(agent): allow to run with an empty prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomas2D committed Sep 5, 2024
1 parent ad4bb21 commit 6f9ddc8
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 15 deletions.
17 changes: 10 additions & 7 deletions src/agents/bee/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,16 @@ export class BeeAgent extends BaseAgent<BeeRunInput, BeeRunOutput, BeeRunOptions
iterations.push(iteration);
}

await this.input.memory.addMany([
BaseMessage.of({
role: Role.USER,
text: input.prompt,
}),
finalMessage,
]);
if (input.prompt !== null) {
await this.input.memory.add(
BaseMessage.of({
role: Role.USER,
text: input.prompt,
}),
);
}
await this.input.memory.add(finalMessage);

return { result: finalMessage, iterations, memory: runner.memory };
}

Expand Down
5 changes: 5 additions & 0 deletions src/agents/bee/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ export const BeeUserPrompt = new PromptTemplate({
template: `Question: {{input}}`,
});

export const BeeUserEmptyPrompt = new PromptTemplate({
variables: [],
template: `Question: Empty message.`,
});

export const BeeToolErrorPrompt = new PromptTemplate({
variables: ["reason"],
template: `The tool has failed; the error log is shown below. If the tool cannot accomplish what you want, use a different tool or explain why you can't use it.
Expand Down
23 changes: 16 additions & 7 deletions src/agents/bee/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
BeeToolInputErrorPrompt,
BeeToolNoResultsPrompt,
BeeToolNotFoundPrompt,
BeeUserEmptyPrompt,
BeeUserPrompt,
} from "@/agents/bee/prompts.js";
import { BeeIterationToolResult, BeeOutputParser } from "@/agents/bee/parser.js";
Expand All @@ -53,7 +54,7 @@ export class BeeAgentRunner {
this.failedAttemptsCounter = new RetryCounter(options?.execution?.totalMaxRetries, AgentError);
}

static async create(input: BeeInput, options: BeeRunOptions, prompt: string) {
static async create(input: BeeInput, options: BeeRunOptions, prompt: string | null) {
const memory = new TokenMemory({
llm: input.llm,
capacityThreshold: 0.85,
Expand Down Expand Up @@ -103,14 +104,22 @@ export class BeeAgentRunner {
}),
}),
...input.memory.messages,
BaseMessage.of({
role: Role.USER,
text: (input.templates?.user ?? BeeUserPrompt).render({
input: prompt.trim() ? prompt : "Empty message.",
}),
}),
]);

if (prompt !== null || input.memory.isEmpty()) {
const isEmpty = prompt?.trim?.() || input.memory.isEmpty();
const template = isEmpty
? (input.templates?.userEmpty ?? BeeUserEmptyPrompt)
: (input.templates?.user ?? BeeUserPrompt);

await memory.add(
BaseMessage.of({
role: Role.USER,
text: template.render({ input: prompt }),
}),
);
}

return new BeeAgentRunner(input, options, memory);
}

Expand Down
4 changes: 3 additions & 1 deletion src/agents/bee/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ import {
BeeToolInputErrorPrompt,
BeeToolNoResultsPrompt,
BeeToolNotFoundPrompt,
BeeUserEmptyPrompt,
BeeUserPrompt,
} from "@/agents/bee/prompts.js";

export interface BeeRunInput {
prompt: string;
prompt: string | null;
}

export interface BeeRunOutput {
Expand Down Expand Up @@ -124,6 +125,7 @@ export interface BeeCallbacks {
export interface BeeAgentTemplates {
system: typeof BeeSystemPrompt;
user: typeof BeeUserPrompt;
userEmpty: typeof BeeUserEmptyPrompt;
toolError: typeof BeeToolErrorPrompt;
toolInputError: typeof BeeToolInputErrorPrompt;
toolNoResultError: typeof BeeToolNoResultsPrompt;
Expand Down

0 comments on commit 6f9ddc8

Please sign in to comment.