Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement] CLI Chat #10

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;