Spaces:
Sleeping
Sleeping
yc1838 commited on
Commit ·
6283426
1
Parent(s): 5a60732
feat: add episodic task summarization
Browse files- src/lilith_agent/memory.py +38 -3
src/lilith_agent/memory.py
CHANGED
|
@@ -12,6 +12,41 @@ lilith_home = Path(os.getenv("LILITH_HOME", ".lilith"))
|
|
| 12 |
# langmem.init(local_dir=str(lilith_home / "memory")) # Placeholder, SDK API may vary
|
| 13 |
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
def extract_and_compress_facts(messages: List[BaseMessage], model) -> None:
|
| 16 |
"""
|
| 17 |
Extracts new facts from the conversation and merges/compresses them
|
|
@@ -36,11 +71,11 @@ def extract_and_compress_facts(messages: List[BaseMessage], model) -> None:
|
|
| 36 |
|
| 37 |
response = model.invoke(prompt)
|
| 38 |
|
| 39 |
-
# Placeholder for langmem save_fact logic
|
| 40 |
-
# In a full langmem cloud setup, you might use memory_manager.
|
| 41 |
-
# Here we just log it as a stub until local vector is fully set up.
|
| 42 |
log.info(f"[memory] Facts extracted: {response.content[:100]}...")
|
| 43 |
|
| 44 |
log.info("[memory] Extraction complete.")
|
| 45 |
except Exception as e:
|
| 46 |
log.error(f"[memory] Failed to extract facts: {e}")
|
|
|
|
|
|
|
|
|
| 12 |
# langmem.init(local_dir=str(lilith_home / "memory")) # Placeholder, SDK API may vary
|
| 13 |
|
| 14 |
|
| 15 |
+
def summarize_episode(messages: List[BaseMessage], model) -> None:
|
| 16 |
+
"""
|
| 17 |
+
Summarizes the trajectory of the task to learn from past experiences.
|
| 18 |
+
"""
|
| 19 |
+
log.info("[memory] Summarizing task episode...")
|
| 20 |
+
try:
|
| 21 |
+
# Extract the initial question
|
| 22 |
+
initial_question = ""
|
| 23 |
+
for m in messages:
|
| 24 |
+
if isinstance(m, HumanMessage):
|
| 25 |
+
initial_question = str(m.content)
|
| 26 |
+
break
|
| 27 |
+
|
| 28 |
+
conv_str = "\n".join([f"{m.type}: {m.content[:200]}..." for m in messages if m.content])
|
| 29 |
+
|
| 30 |
+
prompt = f"""
|
| 31 |
+
Summarize the trajectory of this task to help a future agent avoid mistakes and repeat successes.
|
| 32 |
+
Include:
|
| 33 |
+
1. Task description
|
| 34 |
+
2. Tools used and why
|
| 35 |
+
3. Errors encountered and how they were bypassed
|
| 36 |
+
4. Final outcome
|
| 37 |
+
|
| 38 |
+
Initial Question: {initial_question}
|
| 39 |
+
Trajectory:
|
| 40 |
+
{conv_str}
|
| 41 |
+
"""
|
| 42 |
+
|
| 43 |
+
response = model.invoke(prompt)
|
| 44 |
+
|
| 45 |
+
# Placeholder for langmem save_episode logic
|
| 46 |
+
log.info(f"[memory] Episode summarized: {response.content[:100]}...")
|
| 47 |
+
except Exception as e:
|
| 48 |
+
log.error(f"[memory] Failed to summarize episode: {e}")
|
| 49 |
+
|
| 50 |
def extract_and_compress_facts(messages: List[BaseMessage], model) -> None:
|
| 51 |
"""
|
| 52 |
Extracts new facts from the conversation and merges/compresses them
|
|
|
|
| 71 |
|
| 72 |
response = model.invoke(prompt)
|
| 73 |
|
| 74 |
+
# Placeholder for langmem save_fact logic
|
|
|
|
|
|
|
| 75 |
log.info(f"[memory] Facts extracted: {response.content[:100]}...")
|
| 76 |
|
| 77 |
log.info("[memory] Extraction complete.")
|
| 78 |
except Exception as e:
|
| 79 |
log.error(f"[memory] Failed to extract facts: {e}")
|
| 80 |
+
|
| 81 |
+
summarize_episode(messages, model)
|