Skip to content

Commit

Permalink
refactor: example agents such that the default agent does not require…
Browse files Browse the repository at this point in the history
… secrets
  • Loading branch information
grahamwhiteuk committed Oct 10, 2024
1 parent 3c9fdcb commit 3a7daa6
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 7 deletions.
157 changes: 157 additions & 0 deletions examples/agents/airTableToolAgent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/**
* Copyright 2024 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import "dotenv/config.js";
import { BAMChatLLM } from "bee-agent-framework/adapters/bam/chat";
import { BeeAgent } from "bee-agent-framework/agents/bee/agent";
import { createConsoleReader } from "../helpers/io.js";
import { FrameworkError } from "bee-agent-framework/errors";
import { TokenMemory } from "bee-agent-framework/memory/tokenMemory";
import { Logger } from "bee-agent-framework/logger/logger";
import { OllamaChatLLM } from "bee-agent-framework/adapters/ollama/chat";
import { OpenAIChatLLM } from "bee-agent-framework/adapters/openai/chat";
import { PromptTemplate } from "bee-agent-framework/template";
import { WatsonXChatLLM } from "bee-agent-framework/adapters/watsonx/chat";
import { parseEnv } from "bee-agent-framework/internals/env";
import { z } from "zod";
import {
BeeSystemPrompt,
BeeToolErrorPrompt,
BeeToolInputErrorPrompt,
BeeToolNoResultsPrompt,
} from "bee-agent-framework/agents/bee/prompts";

// core tools
import { DuckDuckGoSearchTool } from "bee-agent-framework/tools/search/duckDuckGoSearch";
import { WikipediaTool } from "bee-agent-framework/tools/search/wikipedia";

// AirTable tool
import { AirtableTool } from "bee-community-tools/tools/airtable";

const AIRTABLE_TOKEN: string = parseEnv("AIRTABLE_TOKEN", z.string());
const AIRTABLE_BASE: string = parseEnv("AIRTABLE_BASE", z.string());

Logger.root.level = "silent"; // disable internal logs
const logger = new Logger({ name: "app", level: "trace" });

async function runBeeAgent() {
// use BAM if GENAI_API_KEY env var is defined
// else use Watsonx if WATSONX_API_KEY and WATSONX_PROJECT_ID env vars are defined
// else use OpenAI if OPENAI_API_KEY env var is defined
// else use Ollama
const llm =
process.env.GENAI_API_KEY !== undefined
? BAMChatLLM.fromPreset("meta-llama/llama-3-1-70b-instruct")
: process.env.WATSONX_API_KEY !== undefined && process.env.WATSONX_PROJECT_ID !== undefined
? WatsonXChatLLM.fromPreset("meta-llama/llama-3-1-70b-instruct", {
apiKey: process.env.WATSONX_API_KEY, //pragma: allowlist secret
projectId: process.env.WATSONX_PROJECT_ID,
parameters: {
decoding_method: "greedy",
min_new_tokens: 5,
max_new_tokens: 50,
},
})
: process.env.OPENAI_API_KEY !== undefined
? new OpenAIChatLLM()
: new OllamaChatLLM({ modelId: "llama3.1" });

const agent = new BeeAgent({
llm,
memory: new TokenMemory({ llm }),
tools: [
new DuckDuckGoSearchTool(),
new WikipediaTool(),
new AirtableTool({ apiToken: AIRTABLE_TOKEN, baseId: AIRTABLE_BASE }),
],
templates: {
user: new PromptTemplate({
schema: z
.object({
input: z.string(),
})
.passthrough(),
template: `User: {{input}}`,
}),
system: BeeSystemPrompt.fork((old) => ({
...old,
defaults: {
instructions: "You are a helpful assistant that uses tools to answer questions.",
},
})),
toolError: BeeToolErrorPrompt,
toolInputError: BeeToolInputErrorPrompt,
toolNoResultError: BeeToolNoResultsPrompt.fork((old) => ({
...old,
template: `${old.template}\nPlease reformat your input.`,
})),
toolNotFoundError: new PromptTemplate({
schema: z
.object({
tools: z.array(z.object({ name: z.string() }).passthrough()),
})
.passthrough(),
template: `Tool does not exist!
{{#tools.length}}
Use one of the following tools: {{#trim}}{{#tools}}{{name}},{{/tools}}{{/trim}}
{{/tools.length}}`,
}),
},
});

const reader = createConsoleReader();

try {
for await (const { prompt } of reader) {
const response = await agent
.run(
{ prompt },
{
execution: {
maxRetriesPerStep: 3,
totalMaxRetries: 10,
maxIterations: 20,
},
signal: AbortSignal.timeout(2 * 60 * 1000),
},
)
.observe((emitter) => {
emitter.on("start", () => {
reader.write(`Agent 🤖 : `, "Starting new iteration");
});
emitter.on("error", ({ error }) => {
reader.write(`Agent 🤖 : `, FrameworkError.ensure(error).dump());
});
emitter.on("retry", () => {
reader.write(`Agent 🤖 : `, "retrying the action...");
});
emitter.on("update", async ({ update }) => {
// log 'data' to see the whole state
// to log only valid runs (no errors), check if meta.success === true
reader.write(`Agent (${update.key}) 🤖 : `, update.value);
});
});

reader.write(`Agent 🤖 : `, response.result.text);
}
} catch (error) {
logger.error(FrameworkError.ensure(error).dump());
} finally {
process.exit(0);
}
}

void runBeeAgent();
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import { OllamaChatLLM } from "bee-agent-framework/adapters/ollama/chat";
import { OpenAIChatLLM } from "bee-agent-framework/adapters/openai/chat";
import { PromptTemplate } from "bee-agent-framework/template";
import { WatsonXChatLLM } from "bee-agent-framework/adapters/watsonx/chat";
import { parseEnv } from "bee-agent-framework/internals/env";
import { z } from "zod";
import {
BeeSystemPrompt,
Expand All @@ -44,10 +43,6 @@ import { WikipediaTool } from "bee-agent-framework/tools/search/wikipedia";
// import { HelloWorldTool } from "@/tools/helloWorld.js";
import { OpenLibraryTool } from "bee-community-tools/tools/openLibrary";
import { ImageDescriptionTool } from "bee-community-tools/tools/imageDescription";
import { AirtableTool } from "bee-community-tools/tools/airtable";

const AIRTABLE_TOKEN: string = parseEnv("AIRTABLE_TOKEN", z.string());
const AIRTABLE_BASE: string = parseEnv("AIRTABLE_BASE", z.string());

Logger.root.level = "silent"; // disable internal logs
const logger = new Logger({ name: "app", level: "trace" });
Expand Down Expand Up @@ -85,7 +80,6 @@ async function runBeeAgent() {
// new HelloWorldTool(),
new OpenLibraryTool(),
new ImageDescriptionTool(),
new AirtableTool({ apiToken: AIRTABLE_TOKEN, baseId: AIRTABLE_BASE }),
],
templates: {
user: new PromptTemplate({
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"clean": "rimraf dist",
"build": "yarn clean && yarn ts:check && tsup",
"ts:check": "tsc --noEmit && tsc -p tsconfig.examples.json --noEmit",
"start": "tsx --tsconfig tsconfig.examples.json examples/agents/allToolsAgent.ts",
"start": "tsx --tsconfig tsconfig.examples.json examples/agents/manyToolsAgent.ts",
"lint": "yarn eslint",
"lint:fix": "yarn eslint --fix",
"format": "yarn prettier --check .",
Expand Down

0 comments on commit 3a7daa6

Please sign in to comment.