Skip to content

Commit

Permalink
improve monologue (All-Hands-AI#157)
Browse files Browse the repository at this point in the history
* improve monologue

* Update monologue.py

---------

Co-authored-by: Hesham Haroon <heshamharoon@Heshams-MacBook-Pro.local>
  • Loading branch information
h9-tect and Hesham Haroon committed Mar 25, 2024
1 parent 847c214 commit 6a1197f
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions agenthub/langchains_agent/utils/monologue.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

import agenthub.langchains_agent.utils.json as json
import agenthub.langchains_agent.utils.llm as llm

Expand All @@ -7,17 +8,29 @@ def __init__(self, model_name):
self.model_name = model_name

def add_event(self, t: dict):
if not isinstance(t, dict):
raise ValueError("Event must be a dictionary")
self.thoughts.append(t)

def get_thoughts(self):
return self.thoughts

def get_total_length(self):
return sum([len(json.dumps(t)) for t in self.thoughts])
total_length = 0
for t in self.thoughts:
try:
total_length += len(json.dumps(t))
except TypeError as e:
print(f"Error serializing thought: {e}")
return total_length

def condense(self):
new_thoughts = llm.summarize_monologue(self.thoughts, self.model_name)
# self.thoughts = [Event(t['action'], t['args']) for t in new_thoughts]
self.thoughts = new_thoughts


try:
new_thoughts = llm.summarize_monologue(self.thoughts, self.model_name)
# Ensure new_thoughts is not empty or significantly malformed before assigning
if not new_thoughts or len(new_thoughts) > len(self.thoughts):
raise ValueError("Condensing resulted in invalid state.")
self.thoughts = new_thoughts
except Exception as e:
# Consider logging the error here instead of or in addition to raising an exception
raise RuntimeError(f"Error condensing thoughts: {e}")

0 comments on commit 6a1197f

Please sign in to comment.