From dcdfa04fc0966df3ee81b6417832e1a283ccb201 Mon Sep 17 00:00:00 2001 From: Hans Raaf Date: Sat, 4 Nov 2023 04:57:43 +0100 Subject: [PATCH] I added commands to shape the conversation: (#218) * I added commands to shape the conversation: `/rethink ` will change the internal dialog of the last assistant message. `/rewrite ` will change the last answer of the assistant. Both commands can be used to change how the conversation continues in some pretty drastic and powerfull ways. * remove magic numbers * add disclaimer --------- Co-authored-by: cpacker --- README.md | 4 ++++ memgpt/main.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/README.md b/README.md index 9b0e79de..82cd1192 100644 --- a/README.md +++ b/README.md @@ -297,6 +297,10 @@ While using MemGPT via the CLI (not Discord!) you can run various commands: print the current contents of agent memory /pop undo the last message in the conversation +/rethink + will replace the inner dialog of the last assistant message with the to help shaping the conversation +/rewrite + will replace the last assistant answer with the given text to correct or force the answer /heartbeat send a heartbeat system message to the agent /memorywarning diff --git a/memgpt/main.py b/memgpt/main.py index 0fb0b227..fda6155d 100644 --- a/memgpt/main.py +++ b/memgpt/main.py @@ -8,6 +8,7 @@ import os import sys import pickle import traceback +import json import questionary import typer @@ -509,6 +510,32 @@ async def run_agent_loop(memgpt_agent, first, no_verify=False, cfg=None, strip_u memgpt_agent.messages.pop() continue + elif user_input.lower() == "/rethink" or user_input.lower().startswith("/rethink "): + # TODO this needs to also modify the persistence manager + if len(user_input) < len("/rethink "): + print("Missing text after the command") + continue + for x in range(len(memgpt_agent.messages) - 1, 0, -1): + if memgpt_agent.messages[x].get("role") == "assistant": + text = user_input[len("/rethink ") :].strip() + memgpt_agent.messages[x].update({"content": text}) + break + continue + + elif user_input.lower() == "/rewrite" or user_input.lower().startswith("/rewrite "): + # TODO this needs to also modify the persistence manager + if len(user_input) < len("/rewrite "): + print("Missing text after the command") + continue + for x in range(len(memgpt_agent.messages) - 1, 0, -1): + if memgpt_agent.messages[x].get("role") == "assistant": + text = user_input[len("/rewrite ") :].strip() + args = json.loads(memgpt_agent.messages[x].get("function_call").get("arguments")) + args["message"] = text + memgpt_agent.messages[x].get("function_call").update({"arguments": json.dumps(args)}) + break + continue + # No skip options elif user_input.lower() == "/wipe": memgpt_agent = agent.AgentAsync(memgpt.interface) @@ -589,6 +616,8 @@ USER_COMMANDS = [ ("/dump ", "view the last messages (all if is omitted)"), ("/memory", "print the current contents of agent memory"), ("/pop", "undo the last message in the conversation"), + ("/rethink ", "changes the inner thoughts of the last agent message"), + ("/rewrite ", "changes the reply of the last agent message"), ("/heartbeat", "send a heartbeat system message to the agent"), ("/memorywarning", "send a memory warning system message to the agent"), ("/attach", "attach data source to agent"),