Skip to content

Commit 252fd28

Browse files
Fix: Mem0 graph data handling (#248)
* Update handling logic Signed-off-by: Andy Kwok <[email protected]> * Update lint Signed-off-by: Andy Kwok <[email protected]> --------- Signed-off-by: Andy Kwok <[email protected]>
1 parent 55354a1 commit 252fd28

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

src/strands_tools/mem0_memory.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,47 @@ def format_retrieve_response(memories: List[Dict]) -> Panel:
454454
return Panel(table, title="[bold green]Search Results", border_style="green")
455455

456456

457+
def format_retrieve_graph_response(memories: List[Dict]) -> Panel:
458+
"""Format retrieve response for graph data"""
459+
if not memories:
460+
return Panel("No graph memories found matching the query.",
461+
title="[bold yellow]No Matches", border_style="yellow")
462+
463+
table = Table(title="Search Results", show_header=True, header_style="bold magenta")
464+
table.add_column("Source", style="cyan")
465+
table.add_column("Relationship", style="yellow", width=50)
466+
table.add_column("Destination", style="green")
467+
468+
for memory in memories:
469+
source = memory.get("source", "N/A")
470+
relationship = memory.get("relationship", "N/A")
471+
destination = memory.get("destination", "N/A")
472+
473+
table.add_row(source, relationship, destination)
474+
475+
return Panel(table, title="[bold green]Search Results (Graph)", border_style="green")
476+
477+
478+
def format_list_graph_response(memories: List[Dict]) -> Panel:
479+
"""Format list response for graph data"""
480+
if not memories:
481+
return Panel("No graph memories found.", title="[bold yellow]No Memories", border_style="yellow")
482+
483+
table = Table(title="Graph Memories", show_header=True, header_style="bold magenta")
484+
table.add_column("Source", style="cyan")
485+
table.add_column("Relationship", style="yellow", width=50)
486+
table.add_column("Target", style="green")
487+
488+
for memory in memories:
489+
source = memory.get("source", "N/A")
490+
relationship = memory.get("relationship", "N/A")
491+
destination = memory.get("target", "N/A")
492+
493+
table.add_row(source, relationship, destination)
494+
495+
return Panel(table, title="[bold green]Memories List (Graph)", border_style="green")
496+
497+
457498
def format_history_response(history: List[Dict]) -> Panel:
458499
"""Format memory history response."""
459500
if not history:
@@ -637,6 +678,14 @@ def mem0_memory(tool: ToolUse, **kwargs: Any) -> ToolResult:
637678
results_list = memories if isinstance(memories, list) else memories.get("results", [])
638679
panel = format_list_response(results_list)
639680
console.print(panel)
681+
682+
# Process graph relations (If any)
683+
if "relations" in memories:
684+
relationships_list = memories.get("relations", [])
685+
results_list.extend(relationships_list)
686+
panel_graph = format_list_graph_response(relationships_list)
687+
console.print(panel_graph)
688+
640689
return ToolResult(
641690
toolUseId=tool_use_id,
642691
status="success",
@@ -656,6 +705,14 @@ def mem0_memory(tool: ToolUse, **kwargs: Any) -> ToolResult:
656705
results_list = memories if isinstance(memories, list) else memories.get("results", [])
657706
panel = format_retrieve_response(results_list)
658707
console.print(panel)
708+
709+
# Process graph relations (If any)
710+
if "relations" in memories:
711+
relationships_list = memories.get("relations", [])
712+
results_list.extend(relationships_list)
713+
panel_graph = format_retrieve_graph_response(relationships_list)
714+
console.print(panel_graph)
715+
659716
return ToolResult(
660717
toolUseId=tool_use_id,
661718
status="success",

0 commit comments

Comments
 (0)