Skip to content

Commit

Permalink
feat: cli chat
Browse files Browse the repository at this point in the history
Signed-off-by: Enrico Toniato <tonxipad+github@gmail.com>
  • Loading branch information
tonxxd committed Nov 19, 2024
1 parent 1bbd63f commit 1cdfd42
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/agent_chat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { WatsonXChatLLM } from "bee-agent-framework/adapters/watsonx/chat";
import { BeeAgent } from "bee-agent-framework/agents/bee/agent";
import { TokenMemory } from "bee-agent-framework/memory/tokenMemory";
import { DuckDuckGoSearchTool } from "bee-agent-framework/tools/search/duckDuckGoSearch";
import { OpenMeteoTool } from "bee-agent-framework/tools/weather/openMeteo";
import chalk from "chalk";
import "dotenv/config";
import cliChat from "./helpers/cli_chat.js";

const llm = WatsonXChatLLM.fromPreset("meta-llama/llama-3-70b-instruct", {
apiKey: process.env.WATSONX_API_KEY,
projectId: process.env.WATSONX_PROJECT_ID,
parameters: {
decoding_method: "greedy",
max_new_tokens: 1500,
},
});

try {
console.clear();

console.log(
`\nHi! I am ${chalk.yellow("Bee")} 🐝 Agent!\n${chalk.gray("type 'exit' at anytime to quit")}\n`,
);

const agent = new BeeAgent({
llm,
memory: new TokenMemory({ llm }),
tools: [new DuckDuckGoSearchTool(), new OpenMeteoTool()],
});

await cliChat(agent);
} catch (error) {
console.error(error);
}
57 changes: 57 additions & 0 deletions src/helpers/cli_chat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { BeeAgent } from "bee-agent-framework/agents/bee/agent";
import chalk from "chalk";
import "dotenv/config";
import readline from "readline";

async function cliChat(agent: BeeAgent) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});

rl.on("SIGINT", () => {
console.log("\nGoodbye! 👋");
rl.close();
process.exit(0);
});

try {
const userQuestion = await new Promise<string>((resolve) => {
rl.question("Ask a question: ", (answer) => {
resolve(answer);
});
});

if (["exit", "q", "quit"].includes(userQuestion.trim().toLowerCase())) {
console.log("Exiting...");
rl.close();
process.exit(0);
}

const response = await agent
.run(
{ prompt: userQuestion.trim() },
{
execution: {
maxRetriesPerStep: 3,
totalMaxRetries: 10,
maxIterations: 20,
},
},
)
.observe((emitter) => {
emitter.on("update", async ({ update }) => {
console.log(chalk.gray(`(${update.key}): `, update.value));
});
});

console.log(`🐝: ` + chalk.hex("#9AEDFE")(response.result.text));
rl.close();
await cliChat(agent);
} catch (error) {
rl.close();
throw error;
}
}

export default cliChat;

0 comments on commit 1cdfd42

Please sign in to comment.