UNPKG

auto-gpt-ts

Version:

my take of Auto-GPT in typescript

107 lines (104 loc) 5.13 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.updateRunningSummary = exports.getNewlyTrimmedMessages = void 0; const config_1 = require("../config/config"); const llm_utils_1 = require("../llm/llm-utils"); function getNewlyTrimmedMessages(fullMessageHistory, currentContext, lastMemoryIndex) { // Select messages in full_message_history with an index higher than last_memory_index const new_messages = fullMessageHistory.filter((_, i) => i > lastMemoryIndex); // Remove messages that are already present in current_context const newMessagesNotInContext = new_messages.filter((msg) => !currentContext.includes(msg)); // Find the index of the last message processed let new_index = lastMemoryIndex; if (newMessagesNotInContext.length > 0) { const lastMessage = newMessagesNotInContext[newMessagesNotInContext.length - 1]; new_index = fullMessageHistory.indexOf(lastMessage); } return [newMessagesNotInContext, new_index]; } exports.getNewlyTrimmedMessages = getNewlyTrimmedMessages; /** * This function takes a list of dictionaries representing new events and combines them with the current summary, * focusing on key and potentially important information to remember. The updated summary is returned in a message * formatted in the 1st person past tense. * * @param {string} currentMemory - The current summary of actions. * @param {Array<Record<string, any>>} newEvents - A list of dictionaries containing the latest events to be added to the summary. * * @returns {string} - A message containing the updated summary of actions, formatted in the 1st person past tense. * * @example * const newEvents = [ * {"event": "entered the kitchen."}, * {"event": "found a scrawled note with the number 7"} * ]; * const currentMemory = "I cooked dinner and ate it."; * update_running_summary(currentMemory, newEvents); * // Returns: "This reminds you of these events from your past: * // I entered the kitchen and found a scrawled note saying 7." */ function updateRunningSummary(currentMemory, newEvents) { return __awaiter(this, void 0, void 0, function* () { // Replace "assistant" with "you". This produces much better first person past tense results. for (const event of newEvents) { if (event.role.toLowerCase() === "assistant") { event.role = "you"; // Remove "thoughts" dictionary from "content" const contentDict = JSON.parse(event.content); if (contentDict.thoughts) { delete contentDict.thoughts; } event.content = JSON.stringify(contentDict); } else if (event.role.toLowerCase() === "system") { event.role = "your computer"; } else if (event.role.toLowerCase() === "user") { // Delete all user messages newEvents.splice(newEvents.indexOf(event), 1); } } // This can happen at any point during execturion, not just the beginning if (newEvents.length === 0) { newEvents = [{ event: "Nothing new happened." }]; } const prompt = `Your task is to create a concise running summary of actions and information results in the provided text, focusing on key and potentially important information to remember. You will receive the current summary and the your latest actions. Combine them, adding relevant key information from the latest development in 1st person past tense and keeping the summary concise. Summary So Far: """ ${currentMemory} """ Latest Development: """ ${JSON.stringify(newEvents, null, 2)} """ `; const messages = [ { role: "user", content: prompt, }, ]; const cfg = new config_1.Config(); const completionMemory = yield (0, llm_utils_1.createChatCompletion)(messages, cfg.fastLlmModel); if (completionMemory.trim().length) { const messageToReturn = { role: "system", content: `This reminds you of these events from your past: \n${completionMemory}`, }; return messageToReturn.content; } return ''; }); } exports.updateRunningSummary = updateRunningSummary; //# sourceMappingURL=summary-memory.js.map