-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Expand file tree
/
Copy pathrich_table_formatter.py
More file actions
171 lines (149 loc) · 7.34 KB
/
rich_table_formatter.py
File metadata and controls
171 lines (149 loc) · 7.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
"""Rich table formatter for comparison results."""
from rich.console import Console
from rich.table import Table
from rich import box
def print_comparison_rich(headers: list[str], rows: list[list[str]]) -> None:
"""Render a beautiful comparison table using Rich library."""
console = Console()
# Create the main table
table = Table(
title="🤖 Parlant Guidelines vs Traditional Prompt: Life Insurance Agent Comparison",
box=box.ROUNDED,
show_header=True,
header_style="bold magenta",
title_style="bold blue",
show_lines=True
)
# Add columns with different styles
table.add_column("📝 Query", style="cyan", width=30, no_wrap=False, overflow="fold")
table.add_column("🤖 Traditional LLM", style="dim red", width=50, no_wrap=False, overflow="fold")
table.add_column("🎯 Parlant Agent", style="green", width=50, no_wrap=False, overflow="fold")
table.add_column("🧠 Reasoning", style="yellow1", width=35, no_wrap=False, overflow="fold")
for row in rows:
query, traditional, parlant, reasoning = row
# Format reasoning with better styling
if reasoning and reasoning != "(no explicit tools/guidelines recorded)":
parts = reasoning.split(" | ")
reasoning_text = ""
for part in parts:
if part.startswith("Guidelines:"):
guideline_text = part.replace("Guidelines: ", "")
reasoning_text += f"[bold green]📋 Guidelines:[/bold green]\n[dim]{guideline_text}[/dim]\n\n"
elif part.startswith("Tools:"):
tool_text = part.replace("Tools: ", "")
reasoning_text += f"[bold blue]🔧 Tools:[/bold blue]\n[dim]{tool_text}[/dim]"
else:
reasoning_text = "[dim]No explicit guidelines/tools recorded[/dim]"
# Format text based on column type for optimal display
def format_query_text(text):
"""Format query text - keep it concise and readable."""
text = text.strip()
if len(text) > 50:
words = text.split()
lines = []
current_line = ""
for word in words:
if len(current_line + " " + word) > 50:
if current_line:
lines.append(current_line)
current_line = word
else:
current_line += " " + word if current_line else word
if current_line:
lines.append(current_line)
return '\n'.join(lines)
return text
def format_traditional_text(text):
"""Format traditional LLM response - handle verbose content."""
text = text.strip()
# Break at natural sentence boundaries first
text = text.replace('. ', '.\n').replace('! ', '!\n').replace('? ', '?\n')
text = text.replace('\n\n', '\n')
lines = text.split('\n')
lines = [line.strip() for line in lines if line.strip()]
# Format each line to fit column width
formatted_lines = []
for line in lines:
if len(line) > 45: # Shorter lines for traditional column
words = line.split()
current_line = ""
for word in words:
if len(current_line + " " + word) > 45:
if current_line and len(current_line) > 15:
formatted_lines.append(current_line)
current_line = word
else:
current_line += " " + word if current_line else word
else:
current_line += " " + word if current_line else word
if current_line:
formatted_lines.append(current_line)
else:
formatted_lines.append(line)
return '\n'.join(formatted_lines)
def format_parlant_text(text):
"""Format Parlant response - handle structured content."""
text = text.strip()
# Break at natural sentence boundaries
text = text.replace('. ', '.\n').replace('! ', '!\n').replace('? ', '?\n')
text = text.replace('\n\n', '\n')
lines = text.split('\n')
lines = [line.strip() for line in lines if line.strip()]
# Format each line to fit column width
formatted_lines = []
for line in lines:
if len(line) > 45: # Shorter lines for Parlant column
words = line.split()
current_line = ""
for word in words:
if len(current_line + " " + word) > 45:
if current_line and len(current_line) > 15:
formatted_lines.append(current_line)
current_line = word
else:
current_line += " " + word if current_line else word
else:
current_line += " " + word if current_line else word
if current_line:
formatted_lines.append(current_line)
else:
formatted_lines.append(line)
return '\n'.join(formatted_lines)
def format_reasoning_text(text):
"""Format reasoning text - handle structured guidelines and tools."""
text = text.strip()
# Handle structured reasoning content
if "📋 Guidelines:" in text and "🔧 Tools:" in text:
# Split guidelines and tools
parts = text.split("🔧 Tools:")
guidelines_part = parts[0].replace("📋 Guidelines:", "").strip()
tools_part = parts[1].strip() if len(parts) > 1 else ""
formatted = []
if guidelines_part:
formatted.append(f"📋 Guidelines: {guidelines_part}")
if tools_part:
formatted.append(f"🔧 Tools: {tools_part}")
return '\n'.join(formatted)
# For other reasoning content
if len(text) > 35: # Shorter lines for reasoning column
words = text.split()
lines = []
current_line = ""
for word in words:
if len(current_line + " " + word) > 35:
if current_line:
lines.append(current_line)
current_line = word
else:
current_line += " " + word if current_line else word
if current_line:
lines.append(current_line)
return '\n'.join(lines)
return text
table.add_row(
format_query_text(query),
format_traditional_text(traditional),
format_parlant_text(parlant),
format_reasoning_text(reasoning_text)
)
console.print(table)