Skip to content

Commit

Permalink
Fix how /retry works to more gracefully handle errors
Browse files Browse the repository at this point in the history
  • Loading branch information
njbbaer committed Aug 17, 2024
1 parent a378a5e commit c85a7de
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 15 deletions.
3 changes: 0 additions & 3 deletions src/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ def save(self):
def add_message(self, role, message, image_url=None):
self._conversation.add_message(role, message, image_url)

def trim_messages(self, n=None):
self._conversation.trim_messages(n)

def reset_conversation(self):
self._conversation.reset()

Expand Down
3 changes: 0 additions & 3 deletions src/conversation.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ def add_message(self, role, message, image_url=None):
}
)

def trim_messages(self, n=None):
self._data["messages"] = self.messages[:-n]

def reset(self):
self._data = {
"cost": 0,
Expand Down
14 changes: 7 additions & 7 deletions src/simulacrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ async def chat(self, user_input, user_name, image_url):
if user_input:
user_input = self._apply_attribution(user_input, user_name)
self.context.add_message("user", user_input, image_url)
self.context.save()
completion = await ChatExecutor(self.context).execute()
content = self._strip_tag(completion.content.strip(), "note")
self.last_completion = completion
Expand All @@ -29,11 +30,6 @@ async def new_conversation(self):
self.context.save()
self.cost_warning_sent = False

def trim_messages(self, n=None):
self.context.load()
self.context.trim_messages(n)
self.context.save()

def reset_conversation(self):
self.context.load()
self.context.reset_conversation()
Expand All @@ -45,12 +41,12 @@ def add_conversation_fact(self, fact):
self.context.add_conversation_fact(fact)
self.context.save()

def undo_last_user_message(self):
def undo_last_messages_by_role(self, role):
self.context.load()
num_messages = len(self.context.conversation_messages)
for _ in range(num_messages):
message = self.context.conversation_messages.pop()
if message["role"] == "user":
if message["role"] == role:
break
self.context.save()

Expand All @@ -62,6 +58,10 @@ def get_conversation_cost(self):
self.context.load()
return self.context.conversation_cost

@property
def last_message_role(self):
return self.context.conversation_messages[-1]["role"]

def _strip_tag(self, content, tag):
content = re.sub(rf"<{tag}.*?>.*?</{tag}>", "", content, flags=re.DOTALL)
content = re.sub(r"\n{3,}", "\n\n", content)
Expand Down
5 changes: 3 additions & 2 deletions src/telegram/telegram_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ async def new_conversation_command_handler(self, ctx):

@message_handler
async def retry_command_handler(self, ctx):
self.sim.trim_messages(1)
if self.sim.last_message_role == "assistant":
self.sim.undo_last_messages_by_role("assistant")
await self._chat(ctx, user_message=None)

@message_handler
Expand All @@ -83,7 +84,7 @@ async def reply_command_handler(self, ctx):

@message_handler
async def undo_command_handler(self, ctx):
self.sim.undo_last_user_message()
self.sim.undo_last_messages_by_role("user")
await ctx.send_message("🗑️ Last message undone")

@message_handler
Expand Down

0 comments on commit c85a7de

Please sign in to comment.