π OpenHands: AI-Driven Development
improve monologue (#157)
Hesham6a1197f5c026
diff --git a/agenthub/langchains_agent/utils/monologue.py b/agenthub/langchains_agent/utils/monologue.pyindex c9de0c79ff..99e0de6b67 100644--- a/agenthub/langchains_agent/utils/monologue.py+++ b/agenthub/langchains_agent/utils/monologue.py@@ -1,3 +1,4 @@+import agenthub.langchains_agent.utils.json as jsonimport agenthub.langchains_agent.utils.llm as llm@@ -7,17 +8,29 @@ class Monologue:self.model_name = model_namedef 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.thoughtsdef 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_lengthdef 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}")