node-agency
Version:
A node package for building AI agents
143 lines • 7.07 kB
JavaScript
;
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Agent = void 0;
const openai_1 = require("./models/openai");
const ollama_1 = require("./models/ollama");
const utils_1 = require("./utils");
const logger_1 = require("./logger");
const Agent = function ({ role, goal, tools, model }) {
let systemMessage = `As a ${role}, your goal is to ${goal}.`;
if (tools && model instanceof ollama_1.Model) {
throw new Error("OllamaModel cannot have tools");
}
model = model || new openai_1.Model();
let vectorStore = null;
const getShortTermMemory = (prompt, currentTask) => __awaiter(this, void 0, void 0, function* () {
if (vectorStore && currentTask) {
const embeddings = yield (0, utils_1.getEmbeddings)(currentTask);
const em = embeddings.data.map((e) => e.embedding);
const vectors = vectorStore.similaritySearchVectorWithScore(em[0], 3);
const hist = vectors.map((v) => {
const [content] = v;
return content.pageContent;
});
if (hist.length) {
(0, logger_1.Logger)({
type: "info",
payload: "Found memories for task: " +
currentTask +
"\n\nMemories:\n" +
hist.join("\n\n"),
});
}
prompt += hist.length
? "\n\n## Previous History:\n\n" + hist.join("\n\n")
: "";
return prompt;
}
return prompt;
});
const saveShortTermMemories = (agentResults, currentTask) => __awaiter(this, void 0, void 0, function* () {
(0, logger_1.Logger)({
type: "results",
payload: JSON.stringify({ role, agentResults }),
});
if (vectorStore) {
const embeddings = yield (0, utils_1.getEmbeddings)(agentResults);
const em = embeddings.data.map((e) => e.embedding);
vectorStore.addVectors(em, [
{
pageContent: agentResults,
metadata: {
role,
task: currentTask,
},
},
]);
}
});
return {
role,
goal,
model,
memory: (store) => {
vectorStore = store;
},
execute: function (prompt, workerTools) {
return __awaiter(this, void 0, void 0, function* () {
let newPrompt = prompt;
let currentTask = "";
//combine tools
tools = tools === null || tools === void 0 ? void 0 : tools.concat(workerTools || []);
try {
const { task, input } = JSON.parse(prompt);
currentTask = `${task}`;
newPrompt = `Complete the following task: ${task}\n\n## Here is some context to help you with your task:\n${input}`;
// attach planning prompt
const containHumanFeedbackTool = tools === null || tools === void 0 ? void 0 : tools.some((tool) => tool.function.name === "human_feedback");
if (!containHumanFeedbackTool) {
newPrompt += `\n\n## Please start by planning your approach to the task, and the next steps your should take. If all steps have been completed, please indicate that you are done.`;
}
else {
newPrompt += `\n\n## Please start by planning your approach to the task, and the next steps your should take. Verify which next steps you should take with the 'human_feedback' tool. If all steps have been completed, please indicate that you are done.`;
}
}
catch (e) { }
(0, logger_1.Logger)({
type: "agent",
payload: JSON.stringify({ role, systemMessage, newPrompt }),
});
// add memories to prompt
newPrompt = yield getShortTermMemory(newPrompt, currentTask);
const agentResults = yield model.call(systemMessage, { role: "user", content: newPrompt }, currentTask
? tools
: tools === null || tools === void 0 ? void 0 : tools.filter((tool) => tool.function.name !== "human_feedback"), // remove human feedback tool if executed diirectly
(0, utils_1.getContext)());
// model.selfReflected = 0;
// create short term memory
saveShortTermMemories(agentResults, currentTask);
return agentResults;
});
},
executeStream: (prompt) => __awaiter(this, void 0, void 0, function* () {
if ("callStream" in model) {
let newPrompt = prompt;
let currentTask = "";
try {
const { task, input } = JSON.parse(prompt);
currentTask = `${task}`;
newPrompt = `Complete the following task: ${task}\n\nHere is some context to help you:\n${input}`;
}
catch (e) { }
// add memories to prompt
newPrompt = yield getShortTermMemory(newPrompt, currentTask);
(0, logger_1.Logger)({
type: "agent",
payload: JSON.stringify({ role, systemMessage, newPrompt }),
});
const agentResults = yield model.callStream(systemMessage, { role: "user", content: newPrompt }, (agentResults) => __awaiter(this, void 0, void 0, function* () {
// create short term memory
saveShortTermMemories(agentResults, currentTask);
}), currentTask
? tools
: tools === null || tools === void 0 ? void 0 : tools.filter((tool) => tool.function.name !== "human_feedback"), // remove human feedback tool if executed diirectly
(0, utils_1.getContext)());
return agentResults;
}
else {
throw new Error("Model does not support streaming");
}
}),
};
};
exports.Agent = Agent;
//# sourceMappingURL=agent.js.map