Skip to content

Commit

Permalink
feat: auto-continue/recover after code execution/system messages
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBjare committed Oct 25, 2023
1 parent 8b2342a commit c23a5ce
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
5 changes: 4 additions & 1 deletion gptme/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ def loop(
# If last message was from the user (such as from crash/edited log),
# then skip asking for input and generate response
last_msg = log[-1] if log else None
if not last_msg or (last_msg.role in ["system", "assistant"]):
if not last_msg or (last_msg.role in ["assistant"]):
inquiry = prompt_user()
if not inquiry:
# Empty command, ask for input again
Expand All @@ -275,6 +275,9 @@ def loop(
# performs reduction/context trimming, if necessary
msgs = log.prepare_messages()

for m in msgs:
logger.debug(f"Prepared message: {m}")

# generate response
msg_response = reply(msgs, model, stream)

Expand Down
18 changes: 11 additions & 7 deletions gptme/tools/reduce.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,23 @@ def reduce_log(
log: list[Message], limit=TOKEN_LIMIT_SOFT
) -> Generator[Message, None, None]:
"""Reduces log until it is below `limit` tokens by continually summarizing the longest messages until below the limit."""
# filter out pinned messages
tokens_total = len_tokens("".join(m.content for m in log))
if tokens_total <= limit:
yield from log
return

# filter out pinned messages, skip the latest 2 messages
i, longest_msg = max(
[(i, m) for i, m in enumerate(log) if not m.pinned],
[(i, m) for i, m in enumerate(log) if not m.pinned and i < len(log) - 2],
key=lambda t: len_tokens(t[1].content),
)
longest_msg = summarize(longest_msg)
logger.info("Reducing log: %s", longest_msg.content)
log = log[:i] + [longest_msg] + log[i + 1 :]
tokens_total = len_tokens("".join(m.content for m in log))
if tokens_total > limit:
# recurse until we are below the limit
yield from reduce_log(log, limit)
else:
yield from log

# recurse until we are below the limit
yield from reduce_log(log, limit)


def limit_log(log: list[Message]) -> list[Message]:
Expand Down

0 comments on commit c23a5ce

Please sign in to comment.