feat: add agent interaction heatmap visualization
Browse files- Added a new tab for visualizing agent interactions
- Implemented heatmap generation from swarm logs
- Updated requirements with pandas, seaborn, and matplotlib
- Improved UI with better layout and instructions
- Fixed model name to use latest Claude 3.5 Sonnet
- .gitignore +4 -1
- app.py +69 -0
- requirements.txt +4 -1
.gitignore
CHANGED
@@ -1,2 +1,5 @@
|
|
1 |
# Environment variables
|
2 |
-
.env
|
|
|
|
|
|
|
|
1 |
# Environment variables
|
2 |
+
.env
|
3 |
+
|
4 |
+
# Local tests
|
5 |
+
local_tests
|
app.py
CHANGED
@@ -5,6 +5,9 @@ import anthropic
|
|
5 |
import yaml
|
6 |
import hashlib
|
7 |
import json
|
|
|
|
|
|
|
8 |
from dotenv import load_dotenv
|
9 |
|
10 |
load_dotenv()
|
@@ -121,6 +124,48 @@ def discover_agents_from_registry():
|
|
121 |
print(f"Successfully loaded {len(tools)} agents from registry")
|
122 |
return tools, cards, index
|
123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
# ---- Claude Orchestrator ----
|
125 |
|
126 |
def match_agents_by_vector(input_text, tools, index):
|
@@ -374,6 +419,30 @@ if __name__ == "__main__":
|
|
374 |
chatbot
|
375 |
)
|
376 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
377 |
mcp_interface = gr.Interface(
|
378 |
fn=mcp_entry_point,
|
379 |
inputs=gr.Textbox(label="User Input"),
|
|
|
5 |
import yaml
|
6 |
import hashlib
|
7 |
import json
|
8 |
+
import io
|
9 |
+
import matplotlib.pyplot as plt
|
10 |
+
import seaborn as sns
|
11 |
from dotenv import load_dotenv
|
12 |
|
13 |
load_dotenv()
|
|
|
124 |
print(f"Successfully loaded {len(tools)} agents from registry")
|
125 |
return tools, cards, index
|
126 |
|
127 |
+
# ---- Heatmap Generator ----
|
128 |
+
|
129 |
+
def generate_swarm_heatmap(log_path="swarm_log.jsonl"):
|
130 |
+
from itertools import combinations
|
131 |
+
from collections import defaultdict
|
132 |
+
import pandas as pd
|
133 |
+
|
134 |
+
agent_pairs = defaultdict(int)
|
135 |
+
|
136 |
+
try:
|
137 |
+
with open(log_path, "r", encoding="utf-8") as f:
|
138 |
+
for line in f:
|
139 |
+
entry = json.loads(line)
|
140 |
+
agents = entry.get("agents", [])
|
141 |
+
for a, b in combinations(sorted(agents), 2):
|
142 |
+
agent_pairs[(a, b)] += 1
|
143 |
+
|
144 |
+
all_agents = sorted({agent for pair in agent_pairs for agent in pair})
|
145 |
+
matrix = pd.DataFrame(0, index=all_agents, columns=all_agents)
|
146 |
+
|
147 |
+
for (a, b), count in agent_pairs.items():
|
148 |
+
matrix.at[a, b] = count
|
149 |
+
matrix.at[b, a] = count
|
150 |
+
|
151 |
+
# Create heatmap figure
|
152 |
+
plt.figure(figsize=(8, 6))
|
153 |
+
sns.heatmap(matrix, annot=True, fmt="d", cmap="YlGnBu", linewidths=0.5)
|
154 |
+
plt.title("Swarm Agent Co-occurrence")
|
155 |
+
plt.xticks(rotation=45, ha="right")
|
156 |
+
plt.yticks(rotation=0)
|
157 |
+
plt.tight_layout()
|
158 |
+
|
159 |
+
buf = io.BytesIO()
|
160 |
+
plt.savefig(buf, format="png")
|
161 |
+
plt.close()
|
162 |
+
buf.seek(0)
|
163 |
+
return buf
|
164 |
+
|
165 |
+
except Exception as e:
|
166 |
+
print(f"Error generating heatmap: {e}")
|
167 |
+
return None
|
168 |
+
|
169 |
# ---- Claude Orchestrator ----
|
170 |
|
171 |
def match_agents_by_vector(input_text, tools, index):
|
|
|
419 |
chatbot
|
420 |
)
|
421 |
|
422 |
+
# Add Heatmap Tab
|
423 |
+
with gr.Tab("Agent Swarm Heatmap"):
|
424 |
+
with gr.Row():
|
425 |
+
with gr.Column():
|
426 |
+
heatmap_btn = gr.Button("Show Heatmap")
|
427 |
+
heatmap_output = gr.Image(type="pil", label="Agent Interaction Heatmap")
|
428 |
+
|
429 |
+
def show_heatmap():
|
430 |
+
heatmap = generate_swarm_heatmap()
|
431 |
+
if heatmap is None:
|
432 |
+
return None
|
433 |
+
from PIL import Image
|
434 |
+
return Image.open(heatmap)
|
435 |
+
|
436 |
+
heatmap_btn.click(show_heatmap, outputs=heatmap_output)
|
437 |
+
|
438 |
+
gr.Markdown("""
|
439 |
+
### How to read this heatmap:
|
440 |
+
- The heatmap shows how often different agents interact together
|
441 |
+
- Darker cells indicate more frequent interactions between agents
|
442 |
+
- The diagonal shows self-interactions (typically not used)
|
443 |
+
- Use this to understand which agents work together most frequently
|
444 |
+
""")
|
445 |
+
|
446 |
mcp_interface = gr.Interface(
|
447 |
fn=mcp_entry_point,
|
448 |
inputs=gr.Textbox(label="User Input"),
|
requirements.txt
CHANGED
@@ -2,4 +2,7 @@ gradio==5.33.1
|
|
2 |
requests==2.32.3
|
3 |
pyyaml==6.0.2
|
4 |
anthropic==0.53.0
|
5 |
-
gradio[mcp]
|
|
|
|
|
|
|
|
2 |
requests==2.32.3
|
3 |
pyyaml==6.0.2
|
4 |
anthropic==0.53.0
|
5 |
+
gradio[mcp]
|
6 |
+
pandas==2.2.3
|
7 |
+
seaborn==0.13.2
|
8 |
+
matplotlib==3.10.3
|