from rich.panel import Panel
from rich.text import Text
from rich.prompt import Prompt
from rich.progress import Progress, SpinnerColumn, TextColumn
from rich.table import Table
from rich.markdown import Markdown
from rich.layout import Layout
from rich.align import Align
from rich import box
from rich.columns import Columns
import time
def display_welcome_banner(console):
"""Display a beautiful welcome banner"""
title = Text("π€ RAG ASSISTANT", style="bold magenta")
subtitle = Text("Retrieval-Augmented Generation System", style="dim cyan")
welcome_panel = Panel.fit(
Align.center(title + "\n" + subtitle),
border_style="bright_blue",
padding=(1, 2),
)
console.print()
console.print(welcome_panel)
console.print()
def display_collection_info(collection_name, console):
"""Display collection information in a styled panel"""
info_text = Text()
info_text.append("π Active Collection: ", style="bold cyan")
info_text.append(collection_name, style="bold yellow")
info_panel = Panel(
info_text, title="Configuration", border_style="green", padding=(0, 1)
)
console.print(info_panel)
console.print()
def display_instructions(console):
"""Display usage instructions"""
instructions = Table(show_header=False, box=None, padding=(0, 1))
instructions.add_column(style="cyan", width=12)
instructions.add_column(style="white")
instructions.add_row("π‘ Tip:", "Ask questions about your documents")
instructions.add_row("πͺ Exit:", "Type 'exit' or 'quit' to leave")
instructions.add_row("π Clear:", "Type 'clear' to clear the screen")
console.print(
Panel(instructions, title="Instructions", border_style="blue", padding=(0, 1))
)
console.print()
def get_user_question(console):
"""Get user input with Rich prompt styling"""
return Prompt.ask(
"[bold cyan]β Your Question[/bold cyan]", default="", show_default=False
).strip()
def show_processing_step(step_name, console, color="blue"):
"""Display processing steps with spinner"""
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
console=console,
transient=True,
) as progress:
task = progress.add_task(f"[{color}]{step_name}...", total=None)
time.sleep(0.5) # Simulate processing time for demo
return task
def display_search_results(results, console):
"""Display search results in a beautiful format"""
if not results:
console.print(
Panel(
"[yellow]β οΈ No relevant documents found for your query.[/yellow]",
title="Search Results",
border_style="yellow",
)
)
return []
# Create results table
results_table = Table(
title=f"π Found {len(results)} relevant documents",
box=box.ROUNDED,
title_style="bold green",
)
results_table.add_column("Rank", style="cyan", width=6)
results_table.add_column("Relevance", style="yellow", width=10)
results_table.add_column("Preview", style="white")
documents = []
for i, hit in enumerate(results[:5], 1):
if isinstance(hit, dict) and "payload" in hit:
text = hit["payload"].get("text", "No text available")
documents.append(text)
# Get relevance score if available
score = hit.get("score", "N/A")
score_str = (
f"{score:.3f}" if isinstance(score, (int, float)) else str(score)
)
# Preview (first 80 characters)
preview = text[:80] + "..." if len(text) > 80 else text
results_table.add_row(str(i), score_str, preview)
console.print()
console.print(results_table)
return documents
def display_reranking_info(reranked_results, console):
"""Display reranking information"""
if not reranked_results or "results" not in reranked_results:
return
rerank_table = Table(
title="π― Reranked Results (Top 3)",
box=box.SIMPLE_HEAD,
title_style="bold magenta",
)
rerank_table.add_column("Position", style="cyan", width=8)
rerank_table.add_column("Relevance Score", style="green", width=15)
rerank_table.add_column("Original Index", style="yellow", width=13)
# Sort by relevance score and take top 3
top_results = sorted(
reranked_results["results"], key=lambda x: x["relevance_score"], reverse=True
)[:3]
for i, result in enumerate(top_results, 1):
rerank_table.add_row(
str(i), f"{result['relevance_score']:.3f}", str(result["index"] + 1)
)
console.print()
console.print(rerank_table)
def display_answer(answer, console):
"""Display the final answer in a beautiful format"""
# Create answer panel with markdown support
answer_markdown = Markdown(answer)
answer_panel = Panel(
answer_markdown,
title="π€ AI Assistant Answer",
border_style="bright_green",
padding=(1, 2),
)
console.print()
console.print(answer_panel)
console.print()
def display_error(error_message, console):
"""Display errors in a styled format"""
error_panel = Panel(
f"[red]β {error_message}[/red]", title="Error", border_style="red"
)
console.print()
console.print(error_panel)
def display_separator(console):
"""Display a visual separator between queries"""
console.print("\n" + "β" * console.width + "\n")