improve monologue (#157)

* improve monologue

* Update monologue.py

---------

Co-authored-by: Hesham Haroon <heshamharoon@Heshams-MacBook-Pro.local>
This commit is contained in:
Hesham
2024-03-26 01:15:40 +02:00
committed by GitHub
parent 847c2148a6
commit 6a1197f5c0

View File

@@ -1,3 +1,4 @@
import agenthub.langchains_agent.utils.json as json
import agenthub.langchains_agent.utils.llm as llm
@@ -7,17 +8,29 @@ class Monologue:
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}")