feat: add function to print communication between agents
Browse files- utils/__init__.py +3 -0
- utils/pretty_print_messages.py +46 -0
utils/__init__.py
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from utils.pretty_print_messages import pretty_print_messages
|
| 2 |
+
|
| 3 |
+
__all__ = ["pretty_print_messages"]
|
utils/pretty_print_messages.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Any, Dict, List, Tuple, Union
|
| 2 |
+
|
| 3 |
+
from langchain_core.messages import BaseMessage, convert_to_messages
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def pretty_print_message(message: BaseMessage, indent: bool = False) -> None:
|
| 7 |
+
pretty_message = message.pretty_repr(html=True)
|
| 8 |
+
if not indent:
|
| 9 |
+
print(pretty_message)
|
| 10 |
+
return
|
| 11 |
+
|
| 12 |
+
indented = "\n".join("\t" + c for c in pretty_message.split("\n"))
|
| 13 |
+
print(indented)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def pretty_print_messages(
|
| 17 |
+
update: Union[Dict[str, Any], Tuple[List[str], Dict[str, Any]]],
|
| 18 |
+
last_message: bool = False,
|
| 19 |
+
) -> None:
|
| 20 |
+
is_subgraph = False
|
| 21 |
+
if isinstance(update, tuple):
|
| 22 |
+
ns, update = update
|
| 23 |
+
# skip parent graph updates in the printouts
|
| 24 |
+
if len(ns) == 0:
|
| 25 |
+
return
|
| 26 |
+
|
| 27 |
+
graph_id = ns[-1].split(":")[0]
|
| 28 |
+
print(f"Update from subgraph {graph_id}:")
|
| 29 |
+
print("\n")
|
| 30 |
+
is_subgraph = True
|
| 31 |
+
|
| 32 |
+
for node_name, node_update in update.items():
|
| 33 |
+
update_label = f"Update from node {node_name}:"
|
| 34 |
+
if is_subgraph:
|
| 35 |
+
update_label = "\t" + update_label
|
| 36 |
+
|
| 37 |
+
print(update_label)
|
| 38 |
+
print("\n")
|
| 39 |
+
|
| 40 |
+
messages = convert_to_messages(node_update["messages"])
|
| 41 |
+
if last_message:
|
| 42 |
+
messages = messages[-1:]
|
| 43 |
+
|
| 44 |
+
for m in messages:
|
| 45 |
+
pretty_print_message(m, indent=is_subgraph)
|
| 46 |
+
print("\n")
|