UNPKG

node-agency

Version:
186 lines 8 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Agency = void 0; const agent_1 = require("./agent"); const utils_1 = require("./utils"); const colors_1 = __importDefault(require("colors")); const fs_1 = __importDefault(require("fs")); const Agency = function ({ agents, tasks, llm, process = "hierarchical", memory = false, resources, humanFeedback = true, outFile, selfReflect = true, }) { let manager; let store; if (memory) { store = (0, utils_1.VectorStore)(); } if (llm && process === "hierarchical") { llm.isManager = true; llm.selfReflect = selfReflect; manager = (0, agent_1.Agent)({ role: "Supervising Manager", goal: "Complete the task with the of agents, delegating tasks as needed. Please use the content from tool calls to come up with your final response.", tools: (0, utils_1.getManagerTools)(agents, humanFeedback), model: llm, }); if (store) { manager.memory(store); } } if (store) { agents.forEach((agent) => { agent.memory(store); if (agent.model) { agent.model.selfReflect = selfReflect; } }); } else { agents.forEach((agent) => { if (agent.model) { agent.model.selfReflect = selfReflect; } }); } if (resources && !store) { throw new Error("Resources can only be used with memory enabled. Please enable memory to use resources."); } if (resources && store) { resources.forEach((resource) => __awaiter(this, void 0, void 0, function* () { if (store) { const resourceContent = yield (0, utils_1.getContent)(resource); for (const content of resourceContent) { const embeddings = yield (0, utils_1.getEmbeddings)(content); const emb = embeddings.data.map((e) => e.embedding); store.addVectors(emb, [ { pageContent: content, metadata: { type: "resource", tags: [resource], }, }, ]); } } })); } const kickoff = () => __awaiter(this, void 0, void 0, function* () { console.log(colors_1.default.green("Starting Agency...\n\n")); let context = ""; let finalOutput = ""; const startTime = new Date().getTime(); for (const task of tasks) { const coworkerTools = (0, utils_1.getCoworkerTools)(agents.filter((agent) => { var _a; return agent.role !== ((_a = task.agent) === null || _a === void 0 ? void 0 : _a.role); }), humanFeedback); const out = yield task.execute({ agent: manager, context, tools: process !== "hierarchical" ? coworkerTools : undefined, }); if (memory) { // TODO: store in longterm memory using sqlite } context += `${out}\n-----------------\n`; finalOutput = out; } const endTime = new Date().getTime(); const runTime = endTime - startTime; const formattedRunTime = `${Math.floor(runTime / 60000)} minutes and ${Math.floor((runTime % 60000) / 1000)} seconds`; console.log(colors_1.default.green("\nAgency Completed!\n\n")); if (outFile) { console.log(colors_1.default.green(`Writing results to file: ${outFile}`)); // delete file if exists if (fs_1.default.existsSync(outFile)) { fs_1.default.unlinkSync(outFile); } // create directory if not exists const paths = outFile.split("/"); if (paths.length > 1) { const dir = paths.slice(0, paths.length - 1).join("/"); if (!fs_1.default.existsSync(dir)) { fs_1.default.mkdirSync(dir, { recursive: true }); } } // write to file fs_1.default.writeFileSync(outFile, finalOutput, "utf-8"); } return (0, utils_1.generateOutput)(finalOutput, formattedRunTime); }); const run = (prompt, stream, history) => __awaiter(this, void 0, void 0, function* () { if (!manager) { throw new Error("Manager is not defined. Please provide a manager model to run the agency in chatbot mode."); } const executeMethod = stream ? "executeStream" : "execute"; if (history) { const newHistory = history.map((item) => { return { role: item.role, content: item.content }; }); // const currentToolCalls = manager.model.history.filter( // (item) => // // For OpenAI // (item.role === "assistant" && // "tool_calls" in item && // item.tool_calls && // item.tool_calls.length > 0) || // // For Claude // (item.role === "assistant" && // item.content && // typeof item.content === "object" && // item.content.filter((c) => c.type === "tool_use").length > 0) // ); // const [firstHistoryItems, middleHistoryItems, lastHistoryItems] = // groupIntoNChunks(newHistory, 3); // manager.model.history = [ // ...firstHistoryItems, // ...currentToolCalls, // ...middleHistoryItems, // ...lastHistoryItems, // ]; manager.model.history = newHistory; } const result = (yield manager[executeMethod](prompt)); // if history is passed, clear the history after execution if (history) { for (const agent of agents) { if (agent.model) { agent.model.history = []; } } } else { // pune if history too large if (manager.model.history.length > 100) { manager.model.history = manager.model.history.slice(manager.model.history.length - 50); } // prune each agent's history for (const agent of agents) { if (agent.model && agent.model.history.length > 100) { agent.model.history = agent.model.history.slice(agent.model.history.length - 50); } } } return result; }); const execute = (prompt, history) => __awaiter(this, void 0, void 0, function* () { return run(prompt, false, history); }); const executeStream = (prompt, history) => __awaiter(this, void 0, void 0, function* () { return run(prompt, true, history); }); return { kickoff, execute, executeStream, }; }; exports.Agency = Agency; //# sourceMappingURL=agency.js.map