Spaces:
Paused
Paused
Commit ·
153165b
1
Parent(s): 53ea588
removed the binary files
Browse filesFiles changed (39)
- .gitattributes+1
- .gitignore+2
- examples/voice_agent_webrtc_langgraph/start.sh+13
- tests/__init__.pyDeleted-4
- tests/perf/README.mdDeleted-101
- tests/perf/file_input_client.pyDeleted-581
- tests/perf/run_multi_client_benchmark.shDeleted-414
- tests/perf/ttfb_analyzer.pyDeleted-253
- tests/unit/__init__.pyDeleted-4
- tests/unit/configs/animation_config.yamlDeleted-346
- tests/unit/configs/test_speech_planner_prompt.yamlDeleted-15
- tests/unit/test_ace_websocket_serializer.pyDeleted-147
- tests/unit/test_acknowledgment.pyDeleted-71
- tests/unit/test_animation_graph_services.pyDeleted-668
- tests/unit/test_audio2face_3d_service.pyDeleted-182
- tests/unit/test_audio_util.pyDeleted-64
- tests/unit/test_basic_pipelines.pyDeleted-130
- tests/unit/test_blingfire_text_aggregator.pyDeleted-244
- tests/unit/test_custom_view.pyDeleted-203
- tests/unit/test_elevenlabs.pyDeleted-184
- tests/unit/test_frame_creation.pyDeleted-148
- tests/unit/test_gesture.pyDeleted-94
- tests/unit/test_guardrail.pyDeleted-110
- tests/unit/test_message_broker.pyDeleted-111
- tests/unit/test_nvidia_aggregators.pyDeleted-396
- tests/unit/test_nvidia_llm_service.pyDeleted-386
- tests/unit/test_nvidia_rag_service.pyDeleted-261
- tests/unit/test_nvidia_tts_response_cacher.pyDeleted-79
- tests/unit/test_posture.pyDeleted-104
- tests/unit/test_proactivity.pyDeleted-85
- tests/unit/test_riva_asr_service.pyDeleted-523
- tests/unit/test_riva_nmt_service.pyDeleted-197
- tests/unit/test_riva_tts_service.pyDeleted-301
- tests/unit/test_speech_planner.pyDeleted-546
- tests/unit/test_traced_processor.pyDeleted-159
- tests/unit/test_transcription_sync_processors.pyDeleted-262
- tests/unit/test_user_presence.pyDeleted-157
- tests/unit/test_utils.pyDeleted-95
- tests/unit/utils.pyDeleted-428
32 unmodified lines33343532 unmodified lines*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -text32 unmodified lines3334353632 unmodified lines*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -text*.wav filter=lfs diff=lfs merge=lfs -text
65 unmodified lines6667686965 unmodified lines# --- Example runtime artifacts ---examples/voice_agent_webrtc_langgraph/audio_dumps/examples/voice_agent_webrtc_langgraph/ui/dist/65 unmodified lines66676869707165 unmodified lines# --- Example runtime artifacts ---examples/voice_agent_webrtc_langgraph/audio_dumps/examples/voice_agent_webrtc_langgraph/ui/dist/examples/voice_agent_webrtc_langgraph/audio_prompt.wavtests/perf/audio_files/*.wav
8 unmodified lines910111213148 unmodified linesset +afi# All dependencies and langgraph CLI are installed at build time# Start langgraph dev from within the internal agents directory (background)8 unmodified lines91011121314151617181920212223242526278 unmodified linesset +afi# If a remote prompt URL is provided, download it and export ZERO_SHOT_AUDIO_PROMPTif [ -n "${ZERO_SHOT_AUDIO_PROMPT_URL:-}" ]; thenPROMPT_TARGET="${ZERO_SHOT_AUDIO_PROMPT:-/app/examples/voice_agent_webrtc_langgraph/audio_prompt.wav}"mkdir -p "$(dirname "$PROMPT_TARGET")"if [ ! -f "$PROMPT_TARGET" ]; thenecho "Downloading ZERO_SHOT_AUDIO_PROMPT from $ZERO_SHOT_AUDIO_PROMPT_URL"if ! curl -fsSL "$ZERO_SHOT_AUDIO_PROMPT_URL" -o "$PROMPT_TARGET"; thenecho "Failed to download audio prompt from URL: $ZERO_SHOT_AUDIO_PROMPT_URL" >&2fifiexport ZERO_SHOT_AUDIO_PROMPT="$PROMPT_TARGET"fi# All dependencies and langgraph CLI are installed at build time# Start langgraph dev from within the internal agents directory (background)
1234# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.# SPDX-License-Identifier: BSD 2-Clause License
"""Test suite for the nvidia-pipecat package."""123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101# Performance Testing
This directory contains tools for evaluating the voice agent pipeline's latency and scalability/throughput under various loads. These tests simulate real-world scenarios where multiple users interact with the voice agent simultaneously.
## What the Tests Do
The performance tests:
- Open WebSocket clients that simulate user interactions- Use pre-recorded audio files from `audio_files/` as user queries- Send these queries to the voice agent pipeline and measure response times- Track various latency metrics including end-to-end latency, component-wise breakdowns- Can simulate multiple concurrent clients to test scaling- Detect any audio glitches during processing
## Running Performance Tests
### 1. Start the Voice Agent Pipeline
First, start the voice agent pipeline and capture server logs for analysis.See the prerequisites and setup instructions in `examples/speech-to-speech/README.md` before proceeding.
#### If Using Docker
From examples/speech-to-speech/ directory run:
```bash# Start the servicesdocker compose up -d
# Capture logs and save them into a filedocker compose logs -f python-app > bot_logs_test1.txt 2>&1```
Before starting a new performance run:
```bash# Clear existing Docker logssudo truncate -s 0 /var/lib/docker/containers/$(docker compose ps -q python-app)/$(docker compose ps -q python-app)-json.log```
#### If Using Python Environment
From examples/speech-to-speech/ directory run:
```bashpython bot.py > bot_logs_test1.txt 2>&1```
### 2. Run the Multi-Client Benchmark
```bash./run_multi_client_benchmark.sh --host 0.0.0.0 --port 8100 --clients 10 --test-duration 150```
Parameters:
- `--host`: The host address (default: 0.0.0.0)- `--port`: The port where your voice agent is running (default: 8100)- `--clients`: Number of concurrent clients to simulate (default: 1)- `--test-duration`: Duration of the test in seconds (default: 150)
The script will:
1. Start the specified number of concurrent clients2. Simulate user interactions using audio files3. Measure latencies and detect audio glitches4. Save detailed results in the `results` directory as JSON files5. Output a summary to the console
### 3. Analyze Component-wise Latency
After the benchmark completes, analyze the server logs for detailed latency breakdowns:
```bashpython ttfb_analyzer.py <relative_path_to_bot_logs_test1.txt>```
This will show:
- Per-client latency metrics for LLM, TTS, and ASR components- Number of calls made by each client- Overall averages and P95 values- Component-wise timing breakdowns
## Understanding the Results
The metrics measured include:
- **LLM TTFB**: Time to first byte from the LLM model- **TTS TTFB**: Time to first byte from the TTS model- **ASR Lat**: Compute latency of the ASR model- **LLM 1st**: Time taken to generate first complete sentence from LLM- **Calls**: Number of API calls made to each service
The results help identify:
- Performance bottlenecks in specific components- Scaling behavior under concurrent load- Potential audio quality issues- Overall system responsiveness123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581"""Speech-to-speech client with latency measurement for performance testing.
This module provides a WebSocket client that sends audio files to a speech-to-speechservice and measures the latency between when user audio ends and bot response begins."""
import argparseimport asyncioimport datetimeimport ioimport jsonimport osimport signalimport sysimport timeimport uuidimport wave
import websocketsfrom pipecat.frames.protobufs import frames_pb2from websockets.exceptions import ConnectionClosed
def log_error(msg): """Write error message to stderr with timestamp.""" timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") print(f"[ERROR] {timestamp} - {msg}", file=sys.stderr, flush=True)
# Global constantsSILENCE_TIMEOUT = 0.2 # Standard silence timeout in secondsCHUNK_DURATION_MS = 32 # Standard chunk duration in milliseconds
# List to store latency valueslatency_values = []
# List to store filtered latency values (above threshold)filtered_latency_values = []
# Global variable to track timestampstimestamps = {"input_audio_file_end": None, "first_response_after_input": None}
# Global glitch detectionglitch_detected = False
# Global flag and event for controlling silence sendingsilence_control = { "running": False, "event": asyncio.Event(), "audio_params": None, # Will store (frame_rate, n_channels, chunk_size)}
# Global control for continuous operationcontinuous_control = { "running": True, "collecting_metrics": False, "start_time": None, "test_duration": 100, # Default 100 seconds "threshold": 0.5, # Default threshold for filtered latency}
# Signal handler for graceful shutdowndef signal_handler(signum, frame): """Handle system signals for graceful shutdown.""" print(f"\nReceived signal {signum}, shutting down gracefully...") continuous_control["running"] = False sys.exit(0)
# Register signal handlerssignal.signal(signal.SIGINT, signal_handler)signal.signal(signal.SIGTERM, signal_handler)
def write_audio_to_wav(data, wf, create_new_file=False, output_file="bot_response.wav"): """Write audio data to WAV file.""" try: # Parse protobuf frame try: proto = frames_pb2.Frame.FromString(data) which = proto.WhichOneof("frame") if which is None: return wf, None, None, None except Exception as e: log_error(f"Failed to parse protobuf frame: {e}") return wf, None, None, None
args = getattr(proto, which) sample_rate = getattr(args, "sample_rate", 16000) num_channels = getattr(args, "num_channels", 1) audio_data = getattr(args, "audio", None) if audio_data is None: return wf, None, None, None
# Extract raw audio data from WAV format if needed try: with io.BytesIO(audio_data) as buffer, wave.open(buffer, "rb") as wav_file: audio_data = wav_file.readframes(wav_file.getnframes()) sample_rate = wav_file.getframerate() num_channels = wav_file.getnchannels() except Exception: # If not WAV format, use audio_data as-is pass
# Create WAV file if needed if create_new_file and wf is None: try: wf = wave.open(output_file, "wb") # noqa: SIM115 wf.setnchannels(num_channels) wf.setsampwidth(2) wf.setframerate(sample_rate) except Exception as e: log_error(f"Failed to create WAV file {output_file}: {e}") return None, None, None, None
# Write audio data directly if wf is not None: try: wf.writeframes(audio_data) except Exception as e: log_error(f"Failed to write audio data: {e}") return None, None, None, None
return wf, sample_rate, num_channels, audio_data except Exception as e: log_error(f"Unexpected error in write_audio_to_wav: {e}") return wf, None, None, None
async def send_audio_file(websocket, file_path): """Send audio file content with streaming simulation.""" # Pause silence sending while we send the real audio silence_control["event"].set()
try: if not os.path.exists(file_path): log_error(f"Input audio file not found: {file_path}") return
try: with wave.open(file_path, "rb") as wav_file: n_channels = wav_file.getnchannels() frame_rate = wav_file.getframerate() sample_width = wav_file.getsampwidth()
# Store audio parameters for silence generation chunk_size = int((frame_rate * n_channels * CHUNK_DURATION_MS) / 1000) * sample_width silence_control["audio_params"] = ( frame_rate, n_channels, chunk_size, )
# Stream the audio file frames_sent = 0 while True: try: chunk = wav_file.readframes(chunk_size // sample_width) if not chunk: break audio_frame = frames_pb2.AudioRawFrame( audio=chunk, sample_rate=frame_rate, num_channels=n_channels ) frame = frames_pb2.Frame(audio=audio_frame) await websocket.send(frame.SerializeToString()) frames_sent += 1 await asyncio.sleep(CHUNK_DURATION_MS / 1000) except Exception as e: log_error(f"Error sending audio frame {frames_sent}: {e}") raise # Re-raise to handle in outer try block except wave.Error as e: log_error(f"Failed to read WAV file {file_path}: {e}") return except Exception as e: log_error(f"Error in send_audio_file: {e}") return finally: # Always record when input audio ends and resume silence sending timestamps["input_audio_file_end"] = datetime.datetime.now() print(f"User stopped speaking at: {timestamps['input_audio_file_end'].strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]}") silence_control["event"].clear()
async def silence_sender_loop(websocket): """Background task to continuously send silence when no other audio is being sent.""" silence_control["running"] = True print("Silence sender loop started") consecutive_errors = 0 max_consecutive_errors = 5
try: while silence_control["running"]: try: # Wait until we're allowed to send silence if silence_control["event"].is_set() or silence_control["audio_params"] is None: await asyncio.sleep(0.1) # Short sleep to avoid CPU spinning continue
# Extract audio parameters frame_rate, n_channels, chunk_size = silence_control["audio_params"]
# Send a chunk of silence silent_chunk = b"\x00" * chunk_size audio_frame = frames_pb2.AudioRawFrame( audio=silent_chunk, sample_rate=frame_rate, num_channels=n_channels ) frame = frames_pb2.Frame(audio=audio_frame) await websocket.send(frame.SerializeToString()) await asyncio.sleep(CHUNK_DURATION_MS / 1000)
# Reset error counter on successful send consecutive_errors = 0
except ConnectionClosed: print("WebSocket connection closed in silence sender loop") break except Exception as e: consecutive_errors += 1 print(f"Error in silence sender loop (attempt {consecutive_errors}/{max_consecutive_errors}): {e}")
# If too many consecutive errors, stop the loop if consecutive_errors >= max_consecutive_errors: print(f"Too many consecutive errors ({consecutive_errors}), stopping silence sender") break
# Brief pause before retry to avoid overwhelming the system await asyncio.sleep(1.0)
except Exception as e: print(f"Fatal error in silence sender loop: {e}") finally: print("Silence sender loop stopped") silence_control["running"] = False
async def receive_audio( websocket, wf=None, create_new_file=True, is_after_input=False, output_wav="bot_response.wav", is_initial=False, timeout=1.0,): """Receive audio data and handle streaming playback simulation.""" global glitch_detected
if is_initial: print("Waiting up to 5 seconds for initial bot introduction audio if available...") try: # Wait for first data packet with 5 second timeout data = await asyncio.wait_for(websocket.recv(), timeout=5.0) except TimeoutError: print("No initial bot introduction received after 5 seconds, continuing...") return wf else: # For non-initial audio, receive normally data = await websocket.recv()
try: # Wait for first data packet data = await websocket.recv()
# Record first response timestamp if after input if is_after_input: timestamps["first_response_after_input"] = datetime.datetime.now() formatted_time = timestamps["first_response_after_input"].strftime("%Y-%m-%d %H:%M:%S.%f")[:-3] print(f"Bot started speaking at {formatted_time}")
# Process first audio packet wf, sample_rate, num_channels, audio_data = write_audio_to_wav(data, wf, create_new_file, output_wav)
# Initialize timing for glitch detection audio_start_time = time.time() cumulative_audio_duration = 0.0 # Total duration of audio received (in seconds)
# Calculate duration of first chunk if we have audio data if audio_data and sample_rate and num_channels: bytes_per_sample = 2 # Assuming 16-bit audio samples_in_chunk = len(audio_data) // (num_channels * bytes_per_sample) chunk_duration_seconds = samples_in_chunk / sample_rate cumulative_audio_duration += chunk_duration_seconds
# Continue receiving audio data until silence threshold reached last_data_time = time.time()
while True: try: data = await asyncio.wait_for(websocket.recv(), timeout=timeout) current_time = time.time() last_data_time = current_time
# Process audio data wf, sample_rate, num_channels, audio_data = write_audio_to_wav(data, wf, False, output_wav)
# Update cumulative audio duration if audio_data and sample_rate and num_channels: bytes_per_sample = 2 # Assuming 16-bit audio samples_in_chunk = len(audio_data) // (num_channels * bytes_per_sample) chunk_duration_seconds = samples_in_chunk / sample_rate cumulative_audio_duration += chunk_duration_seconds
# Check for glitch: real elapsed time vs cumulative audio duration real_elapsed_time = current_time - audio_start_time audio_deficit = real_elapsed_time - cumulative_audio_duration
if audio_deficit >= 0.032: # 32ms threshold for glitch detection print(f"Audio glitch detected: {audio_deficit * 1000:.1f}ms audio deficit") glitch_detected = True
except TimeoutError: # Check if silence duration exceeds threshold if time.time() - last_data_time >= SILENCE_TIMEOUT: return wf except Exception as e: log_error(f"Error receiving audio data: {e}") if wf is not None and create_new_file: try: wf.close() except Exception as close_error: log_error(f"Error closing WAV file: {close_error}") return None except Exception as e: log_error(f"Fatal error in receive_audio: {e}") if wf is not None and create_new_file: try: wf.close() except Exception as close_error: log_error(f"Error closing WAV file: {close_error}") return None
async def process_conversation_turn(websocket, audio_file_path, wf, turn_index, output_wav="bot_response.wav"): """Process a single conversation turn with the given audio file.""" print(f"\n----- Processing conversation turn {turn_index + 1} -----")
# Reset timestamps for this turn timestamps["input_audio_file_end"] = None timestamps["first_response_after_input"] = None
# Start both sending and receiving in parallel for realistic latency measurement print(f"Sending user input audio from {audio_file_path}...")
# Start sending audio file in background send_task = asyncio.create_task(send_audio_file(websocket, audio_file_path))
# Start receiving bot response immediately (parallel to sending) receive_task = asyncio.create_task( receive_audio(websocket, wf=wf, create_new_file=(wf is None), is_after_input=True, output_wav=output_wav) )
# Wait for both tasks to complete wf = await receive_task await send_task # Ensure sending is also complete
# Calculate and store latency only if we're collecting metrics if continuous_control["collecting_metrics"]: latency = None if timestamps["input_audio_file_end"] is not None and timestamps["first_response_after_input"] is not None: latency = (timestamps["first_response_after_input"] - timestamps["input_audio_file_end"]).total_seconds() print(f"Latency for Turn {turn_index + 1}: {latency:.3f} seconds") latency_values.append(latency)
# Add to filtered latency if above threshold if latency > continuous_control["threshold"]: filtered_latency_values.append(latency) else: print("Reverse Barge-In Detected!")
return wf
async def continuous_audio_loop(websocket, audio_files, wf, output_wav): """Continuously loop through audio files until stopped.""" turn_index = 0
while continuous_control["running"]: # Check if we should start collecting metrics if ( continuous_control["start_time"] and time.time() >= continuous_control["start_time"] and not continuous_control["collecting_metrics"] ): continuous_control["collecting_metrics"] = True print(f"\n=== STARTING METRICS COLLECTION at {datetime.datetime.now().strftime('%H:%M:%S')} ===")
# Check if we should stop collecting metrics if ( continuous_control["start_time"] and continuous_control["collecting_metrics"] and time.time() >= continuous_control["start_time"] + continuous_control["test_duration"] ): print(f"\n=== STOPPING METRICS COLLECTION at {datetime.datetime.now().strftime('%H:%M:%S')} ===") continuous_control["collecting_metrics"] = False continuous_control["running"] = False break
# Process current audio file audio_file = audio_files[turn_index % len(audio_files)] wf = await process_conversation_turn(websocket, audio_file, wf, turn_index, output_wav) turn_index += 1
# Small delay between turns to prevent overwhelming the system await asyncio.sleep(0.1)
return wf
async def main(): """Main execution function.""" # Parse command line arguments parser = argparse.ArgumentParser(description="Speech-to-speech client with latency measurement") parser.add_argument( "--stream-id", type=str, default=str(uuid.uuid4()), help="Unique stream ID (default: random UUID)" ) parser.add_argument("--host", type=str, default="0.0.0.0", help="WebSocket server host (default: 0.0.0.0)") parser.add_argument("--port", type=int, default=8100, help="WebSocket server port (default: 8100)") parser.add_argument( "--output-dir", type=str, default="./results", help="Directory to store output files (default: ./results)" ) parser.add_argument("--start-delay", type=float, default=0, help="Delay in seconds before starting (default: 0)") parser.add_argument( "--metrics-start-time", type=float, default=0, help="Unix timestamp when to start collecting metrics (default: 0)", ) parser.add_argument( "--test-duration", type=float, default=100, help="Duration in seconds to collect metrics (default: 100)" ) parser.add_argument( "--threshold", type=float, default=0.5, help="Threshold for filtered average latency calculation (default: 0.5)" ) args = parser.parse_args()
# Create output directory if it doesn't exist os.makedirs(args.output_dir, exist_ok=True)
# Construct WebSocket URI with unique stream ID uri = f"ws://{args.host}:{args.port}/ws/{args.stream_id}"
# Output file paths output_wav = os.path.join(args.output_dir, f"bot_response_{args.stream_id}.wav") output_results = os.path.join(args.output_dir, f"latency_results_{args.stream_id}.json")
print(f"Starting client with stream ID: {args.stream_id}") print(f"WebSocket URI: {uri}") print(f"Start delay: {args.start_delay} seconds") print(f"Metrics start time: {args.metrics_start_time}") print(f"Test duration: {args.test_duration} seconds") print(f"Latency threshold: {args.threshold} seconds")
# Set up timing controls if args.start_delay > 0: print(f"Waiting {args.start_delay} seconds before starting...") await asyncio.sleep(args.start_delay)
if args.metrics_start_time > 0: continuous_control["start_time"] = args.metrics_start_time continuous_control["test_duration"] = args.test_duration print(f"Will start collecting metrics at timestamp {args.metrics_start_time}")
# Set threshold for filtered latency calculation continuous_control["threshold"] = args.threshold
# Define the array of input audio files # Get the directory where this script is located script_dir = os.path.dirname(os.path.abspath(__file__)) audio_files_dir = os.path.join(script_dir, "audio_files")
input_audio_files = [ os.path.join(audio_files_dir, "output_file.wav"), # os.path.join(audio_files_dir, "query_1.wav"), # os.path.join(audio_files_dir, "query_2.wav"), # os.path.join(audio_files_dir, "query_3.wav"), # os.path.join(audio_files_dir, "query_4.wav"), # os.path.join(audio_files_dir, "query_5.wav"), # os.path.join(audio_files_dir, "query_6.wav"), # os.path.join(audio_files_dir, "query_7.wav"), # os.path.join(audio_files_dir, "query_8.wav"), # os.path.join(audio_files_dir, "query_9.wav"), # os.path.join(audio_files_dir, "query_10.wav"), ]
# Clear any previous values latency_values.clear() filtered_latency_values.clear()
# Initialize silence control silence_control["event"] = asyncio.Event() silence_control["event"].set() # Start with silence sending paused
try: async with websockets.connect(uri) as websocket: # First, try to receive any initial output audio wf = await receive_audio( websocket, wf=None, create_new_file=True, is_after_input=False, output_wav=output_wav, is_initial=True, )
# Start the silence sender task asyncio.create_task(silence_sender_loop(websocket))
# Start continuous audio loop wf = await continuous_audio_loop(websocket, input_audio_files, wf, output_wav)
# Clean up and stop the silence sender silence_control["running"] = False silence_control["event"].set() # Make sure it's not waiting await asyncio.sleep(0.2) # Give it time to exit cleanly
if wf is not None: wf.close() print(f"All output saved to {output_wav}")
except ConnectionClosed: # Normal WebSocket closure, not an error pass except Exception as e: print(f"Connection error: {e}") finally: # Always save results, regardless of how the connection ended if latency_values: avg_latency = sum(latency_values) / len(latency_values)
# Calculate filtered average latency filtered_avg_latency = None if filtered_latency_values: filtered_avg_latency = sum(filtered_latency_values) / len(filtered_latency_values)
print("\n----- Final Latency Summary -----") print(f"Average Latency across {len(latency_values)} turns: {avg_latency:.3f} seconds")
if filtered_avg_latency is not None: print( f"Filtered Average Latency (>{args.threshold}s) across {len(filtered_latency_values)} turns: " f"{filtered_avg_latency:.3f} seconds" ) else: print(f"Filtered Average Latency: No latencies above {args.threshold}s threshold")
# Calculate reverse barge-ins (latencies below threshold) reverse_barge_ins_count = len(latency_values) - len(filtered_latency_values) print(f"Reverse Barge-Ins Detected: {reverse_barge_ins_count} latencies below {args.threshold}s threshold")
# Report glitch detection results if glitch_detected: print("⚠️ AUDIO GLITCHES DETECTED: Audio chunks arrived with gaps larger than playback time") else: print("✅ No audio glitches detected: Audio streaming was smooth")
print("----------------------------------------")
# Save results to JSON file results = { "stream_id": args.stream_id, "individual_latencies": latency_values, "average_latency": avg_latency, "filtered_latencies": filtered_latency_values, "filtered_average_latency": filtered_avg_latency, "threshold": args.threshold, "num_turns": len(latency_values), "num_filtered_turns": len(filtered_latency_values), "reverse_barge_ins_count": len(latency_values) - len(filtered_latency_values), "glitch_detected": glitch_detected, "timestamp": datetime.datetime.now().isoformat(), "metrics_start_time": continuous_control["start_time"], "test_duration": continuous_control["test_duration"], }
with open(output_results, "w") as f: json.dump(results, f, indent=2)
if __name__ == "__main__": asyncio.run(main())123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414#!/bin/bash
# Configuration variablesHOST="0.0.0.0" # Default hostPORT=8100 # Default portNUM_CLIENTS=1 # Default number of parallel clientsBASE_OUTPUT_DIR="./results"TEST_DURATION=150 # Default test duration in secondsCLIENT_START_DELAY=1 # Delay between client starts in secondsTHRESHOLD=0.5 # Default threshold for filtered average latency
# Generate timestamp for unique directoryTIMESTAMP=$(date +"%Y%m%d_%H%M%S")OUTPUT_DIR="${BASE_OUTPUT_DIR}_${TIMESTAMP}"SUMMARY_FILE="$OUTPUT_DIR/summary.json"
# Process command line argumentswhile [[ $# -gt 0 ]]; do case $1 in --host) HOST="$2" shift 2 ;; --port) PORT="$2" shift 2 ;; --clients) NUM_CLIENTS="$2" shift 2 ;; --output-dir) BASE_OUTPUT_DIR="$2" OUTPUT_DIR="${BASE_OUTPUT_DIR}_${TIMESTAMP}" SUMMARY_FILE="$OUTPUT_DIR/summary.json" shift 2 ;; --test-duration) TEST_DURATION="$2" shift 2 ;; --client-start-delay) CLIENT_START_DELAY="$2" shift 2 ;; --threshold) THRESHOLD="$2" shift 2 ;; *) echo "Unknown option: $1" echo "Usage: $0 [--host HOST] [--port PORT] [--clients NUM_CLIENTS] [--output-dir DIR] [--test-duration SECONDS] [--client-start-delay SECONDS] [--threshold SECONDS]" exit 1 ;; esacdone
# Create output directory if it doesn't existmkdir -p "$OUTPUT_DIR"
echo "=== ACE Controller Multi-Client Benchmark (Staggered Start) ==="echo "Host: $HOST"echo "Port: $PORT"echo "Number of clients: $NUM_CLIENTS"echo "Client start delay: $CLIENT_START_DELAY seconds"echo "Test duration: $TEST_DURATION seconds"echo "Latency threshold: $THRESHOLD seconds"echo "Output directory: $OUTPUT_DIR"echo "================================================================"
# Calculate timing# All clients will start within (NUM_CLIENTS - 1) * CLIENT_START_DELAY seconds# Metrics collection will start after the last client startsTOTAL_START_TIME=$(( (NUM_CLIENTS - 1) * CLIENT_START_DELAY ))METRICS_START_TIME=$(date +%s)METRICS_START_TIME=$((METRICS_START_TIME + TOTAL_START_TIME)) # Set to when the last client starts
# Run clients with staggered startspids=()
for ((i=1; i<=$NUM_CLIENTS; i++)); do # Generate a unique stream ID for each client STREAM_ID="client_${i}_$(date +%s%N | cut -b1-13)" # Calculate start delay for this client (0 for first client, increasing for others) START_DELAY=$(( (i - 1) * CLIENT_START_DELAY )) # Run client in background with appropriate delays python ./file_input_client.py \ --stream-id "$STREAM_ID" \ --host "$HOST" \ --port "$PORT" \ --output-dir "$OUTPUT_DIR" \ --start-delay "$START_DELAY" \ --metrics-start-time "$METRICS_START_TIME" \ --test-duration "$TEST_DURATION" \ --threshold "$THRESHOLD" > "$OUTPUT_DIR/client_${i}.log" 2>&1 & # Store the process ID pids+=($!) # Small delay to ensure proper process creation sleep 0.1done
echo ""echo "Timing plan:"echo "- First client starts immediately"echo "- Last client starts in $TOTAL_START_TIME seconds"echo "- Metrics collection starts at $(date -d @$METRICS_START_TIME)"echo "- Test will run for $TEST_DURATION seconds after metrics collection starts"echo "- Expected completion time: $(date -d @$((METRICS_START_TIME + TEST_DURATION)))"echo ""
# Wait for all clients to finishfor pid in "${pids[@]}"; do wait "$pid"done
# Calculate aggregate statistics across all clientsTOTAL_LATENCY=0TOTAL_TURNS=0CLIENT_COUNT=0MIN_LATENCY=9999MAX_LATENCY=0
# Variables for filtered latency statisticsTOTAL_FILTERED_LATENCY=0TOTAL_FILTERED_TURNS=0CLIENTS_WITH_FILTERED=0MIN_FILTERED_LATENCY=9999MAX_FILTERED_LATENCY=0
# Array to store all client average latencies for p95 calculationCLIENT_LATENCIES=()CLIENT_FILTERED_LATENCIES=()
# Arrays to track glitch detectionCLIENTS_WITH_GLITCHES=()TOTAL_GLITCH_COUNT=0
# Variables to track reverse barge-in detectionTOTAL_REVERSE_BARGE_INS=0CLIENT_REVERSE_BARGE_INS=()CLIENTS_WITH_REVERSE_BARGE_INS=0
# Function to calculate p95 from an array of valuescalculate_p95() { local values=("$@") local count=${#values[@]} if [ $count -eq 0 ]; then echo "0" return fi # Sort the array (using a simple bubble sort for bash compatibility) for ((i = 0; i < count; i++)); do for ((j = i + 1; j < count; j++)); do if (( $(echo "${values[i]} > ${values[j]}" | bc -l) )); then temp=${values[i]} values[i]=${values[j]} values[j]=$temp fi done done # Calculate p95 index local p95_index=$(echo "scale=0; ($count - 1) * 0.95" | bc -l | cut -d'.' -f1) # Ensure index is within bounds if [ $p95_index -ge $count ]; then p95_index=$((count - 1)) fi echo "${values[$p95_index]}"}
# Process all result filesfor result_file in "$OUTPUT_DIR"/latency_results_*.json; do if [ -f "$result_file" ]; then # Extract data using jq if available, otherwise use awk as fallback if command -v jq &> /dev/null; then AVG_LATENCY=$(jq '.average_latency' "$result_file") FILTERED_AVG_LATENCY=$(jq '.filtered_average_latency' "$result_file") NUM_TURNS=$(jq '.num_turns' "$result_file") NUM_FILTERED_TURNS=$(jq '.num_filtered_turns' "$result_file") STREAM_ID=$(jq -r '.stream_id' "$result_file") GLITCH_DETECTED=$(jq '.glitch_detected' "$result_file") REVERSE_BARGE_INS_COUNT=$(jq '.reverse_barge_ins_count' "$result_file") else # Fallback to grep and basic string processing AVG_LATENCY=$(grep -o '"average_latency": [0-9.]*' "$result_file" | cut -d' ' -f2) FILTERED_AVG_LATENCY=$(grep -o '"filtered_average_latency": [0-9.]*' "$result_file" | cut -d' ' -f2) NUM_TURNS=$(grep -o '"num_turns": [0-9]*' "$result_file" | cut -d' ' -f2) NUM_FILTERED_TURNS=$(grep -o '"num_filtered_turns": [0-9]*' "$result_file" | cut -d' ' -f2) STREAM_ID=$(grep -o '"stream_id": "[^"]*"' "$result_file" | cut -d'"' -f4) GLITCH_DETECTED=$(grep -o '"glitch_detected": [a-z]*' "$result_file" | cut -d' ' -f2) REVERSE_BARGE_INS_COUNT=$(grep -o '"reverse_barge_ins_count": [0-9]*' "$result_file" | cut -d' ' -f2) fi echo "Client $STREAM_ID: Average latency = $AVG_LATENCY seconds over $NUM_TURNS turns" # Display filtered latency information if [ "$FILTERED_AVG_LATENCY" != "null" ] && [ -n "$FILTERED_AVG_LATENCY" ]; then echo " Filtered Average latency (>$THRESHOLD s) = $FILTERED_AVG_LATENCY seconds over $NUM_FILTERED_TURNS turns" # Add to filtered latency statistics TOTAL_FILTERED_LATENCY=$(echo "$TOTAL_FILTERED_LATENCY + $FILTERED_AVG_LATENCY" | bc -l) TOTAL_FILTERED_TURNS=$((TOTAL_FILTERED_TURNS + NUM_FILTERED_TURNS)) CLIENTS_WITH_FILTERED=$((CLIENTS_WITH_FILTERED + 1)) CLIENT_FILTERED_LATENCIES+=($FILTERED_AVG_LATENCY) # Update min/max filtered latency if (( $(echo "$FILTERED_AVG_LATENCY < $MIN_FILTERED_LATENCY" | bc -l) )); then MIN_FILTERED_LATENCY=$FILTERED_AVG_LATENCY fi if (( $(echo "$FILTERED_AVG_LATENCY > $MAX_FILTERED_LATENCY" | bc -l) )); then MAX_FILTERED_LATENCY=$FILTERED_AVG_LATENCY fi else echo " No latencies above $THRESHOLD s threshold" fi # Check for glitch detection if [ "$GLITCH_DETECTED" = "true" ]; then echo " ⚠️ Audio glitches detected in client $STREAM_ID" CLIENTS_WITH_GLITCHES+=("$STREAM_ID") TOTAL_GLITCH_COUNT=$((TOTAL_GLITCH_COUNT + 1)) fi
# Display and track reverse barge-in count if [ -n "$REVERSE_BARGE_INS_COUNT" ] && [ "$REVERSE_BARGE_INS_COUNT" -gt 0 ]; then echo " Reverse barge-ins detected: $REVERSE_BARGE_INS_COUNT occurrences" CLIENT_REVERSE_BARGE_INS+=("$STREAM_ID") CLIENTS_WITH_REVERSE_BARGE_INS=$((CLIENTS_WITH_REVERSE_BARGE_INS + 1)) fi # Add to total reverse barge-in count if [ -n "$REVERSE_BARGE_INS_COUNT" ]; then TOTAL_REVERSE_BARGE_INS=$((TOTAL_REVERSE_BARGE_INS + REVERSE_BARGE_INS_COUNT)) fi # Add to array for p95 calculation CLIENT_LATENCIES+=($AVG_LATENCY) # Update aggregate statistics TOTAL_LATENCY=$(echo "$TOTAL_LATENCY + $AVG_LATENCY" | bc -l) TOTAL_TURNS=$((TOTAL_TURNS + NUM_TURNS)) CLIENT_COUNT=$((CLIENT_COUNT + 1)) # Update min/max latency if (( $(echo "$AVG_LATENCY < $MIN_LATENCY" | bc -l) )); then MIN_LATENCY=$AVG_LATENCY fi if (( $(echo "$AVG_LATENCY > $MAX_LATENCY" | bc -l) )); then MAX_LATENCY=$AVG_LATENCY fi fidone
# Calculate overall statisticsif [ $CLIENT_COUNT -gt 0 ]; then AGGREGATE_AVG_LATENCY=$(echo "scale=3; $TOTAL_LATENCY / $CLIENT_COUNT" | bc -l) P95_CLIENT_LATENCY=$(calculate_p95 "${CLIENT_LATENCIES[@]}") # Calculate filtered statistics AGGREGATE_FILTERED_AVG_LATENCY="null" P95_FILTERED_CLIENT_LATENCY="null" if [ $CLIENTS_WITH_FILTERED -gt 0 ]; then AGGREGATE_FILTERED_AVG_LATENCY=$(echo "scale=3; $TOTAL_FILTERED_LATENCY / $CLIENTS_WITH_FILTERED" | bc -l) P95_FILTERED_CLIENT_LATENCY=$(calculate_p95 "${CLIENT_FILTERED_LATENCIES[@]}") fi echo "==============================================" echo "BENCHMARK SUMMARY (Staggered Start)" echo "==============================================" echo "Total clients: $CLIENT_COUNT" echo "Latency threshold: $THRESHOLD seconds" echo "" echo "STANDARD LATENCY STATISTICS:" echo "Average latency across all clients: $AGGREGATE_AVG_LATENCY seconds" echo "P95 latency across client averages: $P95_CLIENT_LATENCY seconds" echo "Minimum client average latency: $MIN_LATENCY seconds" echo "Maximum client average latency: $MAX_LATENCY seconds" echo "" echo "FILTERED LATENCY STATISTICS (>$THRESHOLD s):" if [ "$AGGREGATE_FILTERED_AVG_LATENCY" != "null" ]; then echo "Clients with filtered data: $CLIENTS_WITH_FILTERED out of $CLIENT_COUNT" echo "Average filtered latency: $AGGREGATE_FILTERED_AVG_LATENCY seconds" echo "P95 filtered latency: $P95_FILTERED_CLIENT_LATENCY seconds" echo "Minimum client filtered latency: $MIN_FILTERED_LATENCY seconds" echo "Maximum client filtered latency: $MAX_FILTERED_LATENCY seconds" else echo "No latencies above $THRESHOLD s threshold found across all clients" fi echo "" echo "AUDIO GLITCH DETECTION:" if [ $TOTAL_GLITCH_COUNT -gt 0 ]; then echo "⚠️ Audio glitches detected in $TOTAL_GLITCH_COUNT out of $CLIENT_COUNT clients" echo "Affected clients:" for client in "${CLIENTS_WITH_GLITCHES[@]}"; do echo " - $client" done else echo "✅ No audio glitches detected in any client" fi
echo "REVERSE BARGE-IN DETECTION:" if [ $TOTAL_REVERSE_BARGE_INS -gt 0 ]; then echo "Total reverse barge-ins detected: $TOTAL_REVERSE_BARGE_INS occurrences" echo "Clients with reverse barge-ins: $CLIENTS_WITH_REVERSE_BARGE_INS out of $CLIENT_COUNT" if [ $CLIENTS_WITH_REVERSE_BARGE_INS -gt 0 ]; then echo "Affected clients:" for client in "${CLIENT_REVERSE_BARGE_INS[@]}"; do echo " - $client" done fi else echo "✅ No reverse barge-ins detected in any client" fi
echo "" echo "ERROR DETECTION:" # Initialize arrays for error tracking declare -A CLIENT_ERROR_COUNTS CLIENTS_WITH_ERRORS=0 TOTAL_ERRORS=0
# Process logs for each client to find errors for ((i=1; i<=$NUM_CLIENTS; i++)); do LOG_FILE="$OUTPUT_DIR/client_${i}.log" if [ -f "$LOG_FILE" ]; then ERROR_COUNT=$(grep -c "^\[ERROR\]" "$LOG_FILE") if [ $ERROR_COUNT -gt 0 ]; then CLIENTS_WITH_ERRORS=$((CLIENTS_WITH_ERRORS + 1)) TOTAL_ERRORS=$((TOTAL_ERRORS + ERROR_COUNT)) CLIENT_ERROR_COUNTS["client_${i}"]=$ERROR_COUNT echo "⚠️ Client ${i} errors ($ERROR_COUNT):" grep "^\[ERROR\]" "$LOG_FILE" | sed 's/^/ /' fi fi done
if [ $TOTAL_ERRORS -eq 0 ]; then echo "✅ No errors detected in any client" else echo "⚠️ Total errors across all clients: $TOTAL_ERRORS" echo "⚠️ Clients with errors: $CLIENTS_WITH_ERRORS out of $CLIENT_COUNT" fi # Create summary JSON cat > "$SUMMARY_FILE" << EOF{ "timestamp": "$(date -Iseconds)", "config": { "host": "$HOST", "port": $PORT, "num_clients": $NUM_CLIENTS, "client_start_delay": $CLIENT_START_DELAY, "test_duration": $TEST_DURATION, "threshold": $THRESHOLD, "metrics_start_time": $METRICS_START_TIME }, "results": { "total_clients": $CLIENT_COUNT, "total_turns": $TOTAL_TURNS, "aggregate_average_latency": $AGGREGATE_AVG_LATENCY, "p95_client_latency": $P95_CLIENT_LATENCY, "min_client_latency": $MIN_LATENCY, "max_client_latency": $MAX_LATENCY, "filtered_results": { "clients_with_filtered_data": $CLIENTS_WITH_FILTERED, "total_filtered_turns": $TOTAL_FILTERED_TURNS, "aggregate_filtered_average_latency": $AGGREGATE_FILTERED_AVG_LATENCY, "p95_filtered_client_latency": $P95_FILTERED_CLIENT_LATENCY, "min_filtered_client_latency": $([ "$AGGREGATE_FILTERED_AVG_LATENCY" != "null" ] && echo "$MIN_FILTERED_LATENCY" || echo "null"), "max_filtered_client_latency": $([ "$AGGREGATE_FILTERED_AVG_LATENCY" != "null" ] && echo "$MAX_FILTERED_LATENCY" || echo "null") }, "glitch_detection": { "clients_with_glitches": $TOTAL_GLITCH_COUNT, "total_clients": $CLIENT_COUNT, "affected_client_ids": [$(printf '"%s",' "${CLIENTS_WITH_GLITCHES[@]}" | sed 's/,$//')] }, "reverse_barge_in_detection": { "total_clients": $CLIENT_COUNT, "total_reverse_barge_ins": $TOTAL_REVERSE_BARGE_INS, "clients_with_reverse_barge_ins": $CLIENTS_WITH_REVERSE_BARGE_INS, "affected_client_ids": [$(printf '"%s",' "${CLIENT_REVERSE_BARGE_INS[@]}" | sed 's/,$//')] }, "error_detection": { "total_clients": $CLIENT_COUNT, "total_errors": $TOTAL_ERRORS, "clients_with_errors": $CLIENTS_WITH_ERRORS, "client_error_counts": { $(for client in "${!CLIENT_ERROR_COUNTS[@]}"; do printf '"%s": %d,\n ' "$client" "${CLIENT_ERROR_COUNTS[$client]}" done | sed 's/,\s*$//') } } }}EOF echo "Summary saved to: $SUMMARY_FILE"else echo "No valid result files found!"fi
echo "Benchmark complete." 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253#!/usr/bin/env python3"""TTFB Log Analyzer.
Analyzes Time To First Byte (TTFB) logs, ASR compute latency, and LLM first sentence generation timefor multiple client streams and calculates average TTFB, ASR latency, first sentence time, and P95for LLM, TTS, and ASR services.
Usage: python ttfb_analyzer.py [log_file_path] python ttfb_analyzer.py --help
Examples: python ttfb_analyzer.py python ttfb_analyzer.py /path/to/botlogs.log python ttfb_analyzer.py ../../examples/speech-to-speech/botlogs.log"""
import argparseimport loggingimport osimport reimport sysfrom collections import defaultdict
# Set up logginglogging.basicConfig(level=logging.INFO)logger = logging.getLogger(__name__)
def calculate_p95(values: list[float]) -> float: """Calculate 95th percentile of values.""" if not values: return 0.0 sorted_values = sorted(values) index = int(0.95 * (len(sorted_values) - 1)) return sorted_values[index]
def parse_logs(log_file_path: str) -> dict[str, dict[str, list[float]]]: """Parse LLM, TTS TTFBs, ASR compute latency, and LLM first sentence generation logs.
Organize by client stream and service type. Only include events after the last client start. """ data = defaultdict(lambda: {"LLM": [], "TTS": [], "ASR": [], "LLM_FIRST_SENTENCE": []}) ttfb_pattern = r"streamId=([^\s]+)\s+-\s+(NvidiaLLMService|RivaTTSService)#\d+\s+TTFB:\s+([\d.]+)" asr_pattern = r"streamId=([^\s]+)\s+-\s+RivaASRService#\d+\s+ASR compute latency:\s+([\d.]+)" first_sentence_pattern = ( r"streamId=([^\s]+)\s+-\s+NvidiaLLMService#\d+\s+LLM first sentence generation time:\s+([\d.]+)" ) websocket_pattern = r".*Accepting WebSocket connection for stream ID client_\d+_\d+"
# First pass: find the last client start log last_client_start_line = -1
try: # Read all lines to find the last client start with open(log_file_path) as file: lines = file.readlines()
# Find the last client start log by iterating through all lines for i, line in enumerate(lines): if re.search(websocket_pattern, line): last_client_start_line = i
# Validate that we found at least one client start if last_client_start_line == -1: logger.warning("No client start pattern found in logs") return dict()
# Second pass: analyze only events after the last client start with open(log_file_path) as file: for i, line in enumerate(file): # Skip lines before the last client start if last_client_start_line != -1 and i <= last_client_start_line: continue
try: # Check for TTFB metrics ttfb_match = re.search(ttfb_pattern, line) if ttfb_match: client_id = ttfb_match.group(1).strip() service_type = ttfb_match.group(2) try: ttfb_value = float(ttfb_match.group(3)) except (ValueError, TypeError) as e: logger.warning(f"Invalid TTFB value in line {i + 1}: {ttfb_match.group(3)} - {e}") continue
if service_type == "NvidiaLLMService": data[client_id]["LLM"].append(ttfb_value) elif service_type == "RivaTTSService": data[client_id]["TTS"].append(ttfb_value)
# Check for ASR compute latency metrics asr_match = re.search(asr_pattern, line) if asr_match: client_id = asr_match.group(1).strip() try: asr_latency = float(asr_match.group(2)) except (ValueError, TypeError) as e: logger.warning(f"Invalid ASR latency value in line {i + 1}: {asr_match.group(2)} - {e}") continue data[client_id]["ASR"].append(asr_latency)
# Check for LLM first sentence generation time metrics first_sentence_match = re.search(first_sentence_pattern, line) if first_sentence_match: client_id = first_sentence_match.group(1).strip() try: first_sentence_time = float(first_sentence_match.group(2)) except (ValueError, TypeError) as e: logger.warning( f"Invalid first sentence time value in line {i + 1}: " f"{first_sentence_match.group(2)} - {e}" ) continue data[client_id]["LLM_FIRST_SENTENCE"].append(first_sentence_time)
except Exception as e: logger.warning(f"Error parsing line {i + 1}: {e}") continue
except FileNotFoundError: print(f"Error: Log file '{log_file_path}' not found.") sys.exit(1) except Exception as e: print(f"Error reading log file: {e}") sys.exit(1)
return dict(data)
def calculate_client_averages(data: dict[str, dict[str, list[float]]]) -> dict[str, dict[str, float]]: """Calculate average metrics for each client and service type.""" averages = {} for client_id, services in data.items(): averages[client_id] = {} for service_type, values in services.items(): if values: averages[client_id][service_type] = sum(values) / len(values) else: averages[client_id][service_type] = 0.0 return averages
def print_results(data: dict[str, dict[str, list[float]]], client_averages: dict[str, dict[str, float]]): """Print analysis results.""" print("=" * 90) print("LATENCY ANALYSIS RESULTS") print("=" * 90)
# Show metric arrays for each client for client_id in sorted(data.keys()): llm_values = data[client_id]["LLM"] tts_values = data[client_id]["TTS"] asr_values = data[client_id]["ASR"] first_sentence_values = data[client_id]["LLM_FIRST_SENTENCE"]
print(f"\n{client_id}:") print(f" LLM TTFB: {[f'{v:.3f}' for v in llm_values]}") print(f" TTS TTFB: {[f'{v:.3f}' for v in tts_values]}") print(f" ASR Latency: {[f'{v:.3f}' for v in asr_values]}") print(f" LLM First Sentence: {[f'{v:.3f}' for v in first_sentence_values]}")
# Summary table with overall statistics print( f"\n{'Client ID':<25} {'LLM TTFB':<10} {'TTS TTFB':<10} {'ASR Lat':<10} " f"{'LLM 1st':<10} {'LLM calls':<10} {'TTS calls':<10} {'ASR calls':<10}" ) print("-" * 120)
for client_id in sorted(data.keys()): llm_avg = client_averages[client_id]["LLM"] tts_avg = client_averages[client_id]["TTS"] asr_avg = client_averages[client_id]["ASR"] first_sentence_avg = client_averages[client_id]["LLM_FIRST_SENTENCE"] llm_count = len(data[client_id]["LLM"]) tts_count = len(data[client_id]["TTS"]) asr_count = len(data[client_id]["ASR"]) print( f"{client_id:<25} {llm_avg:<10.3f} {tts_avg:<10.3f} {asr_avg:<10.3f} {first_sentence_avg:<10.3f} " f"{llm_count:<10} {tts_count:<10} {asr_count:<10}" )
# Calculate overall statistics across client averages llm_client_averages = [avg["LLM"] for avg in client_averages.values() if avg["LLM"] > 0] tts_client_averages = [avg["TTS"] for avg in client_averages.values() if avg["TTS"] > 0] asr_client_averages = [avg["ASR"] for avg in client_averages.values() if avg["ASR"] > 0] first_sentence_client_averages = [ avg["LLM_FIRST_SENTENCE"] for avg in client_averages.values() if avg["LLM_FIRST_SENTENCE"] > 0 ]
# Add separator and overall statistics rows print("-" * 120)
if llm_client_averages and tts_client_averages and asr_client_averages: llm_overall_avg = sum(llm_client_averages) / len(llm_client_averages) llm_p95 = calculate_p95(llm_client_averages) tts_overall_avg = sum(tts_client_averages) / len(tts_client_averages) tts_p95 = calculate_p95(tts_client_averages) asr_overall_avg = sum(asr_client_averages) / len(asr_client_averages) asr_p95 = calculate_p95(asr_client_averages)
first_sentence_overall_avg = ( sum(first_sentence_client_averages) / len(first_sentence_client_averages) if first_sentence_client_averages else 0.0 ) first_sentence_p95 = calculate_p95(first_sentence_client_averages) if first_sentence_client_averages else 0.0
print( f"{'OVERALL AVERAGE':<25} {llm_overall_avg:<10.3f} {tts_overall_avg:<10.3f} " f"{asr_overall_avg:<10.3f} {first_sentence_overall_avg:<10.3f}" ) print(f"{'OVERALL P95':<25} {llm_p95:<10.3f} {tts_p95:<10.3f} {asr_p95:<10.3f} {first_sentence_p95:<10.3f}")
print("-" * 120)
def main(): """Main function.""" parser = argparse.ArgumentParser( description="Analyze LLM, TTS TTFBs, ASR latency, and LLM first sentence generation time logs " "for multiple client streams" ) parser.add_argument( "log_file", nargs="?", default="../../examples/speech-to-speech/botlogs.log", help="Path to log file (default: ../../examples/speech-to-speech/botlogs.log)", ) args = parser.parse_args()
print("Latency Log Analyzer") print(f"Analyzing: {args.log_file}")
if not os.path.exists(args.log_file): print(f"Error: Log file '{args.log_file}' not found.") sys.exit(1)
data = parse_logs(args.log_file) if not data: print("No performance data found in log file.") return
print()
client_averages = calculate_client_averages(data) print_results(data, client_averages)
if __name__ == "__main__": main()1234# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.# SPDX-License-Identifier: BSD 2-Clause License
"""Unit tests for the nvidia-pipecat package."""123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346animation_types: gesture: duration_relevant_animation_name: gesture animations: gesture: default_clip_id: none clips: - clip_id: Test description: "A virtual test clip for testing purposes" duration: 0.5 meaning: A very short animation clip for unit testing purposes - clip_id: Goodbye description: "Waving goodbye: Waves with left hand extended high." duration: 2 meaning: Taking leave of someone from a further distance, or getting someone's attention. - clip_id: Welcome description: "Waving hello: Spreads arms slightly, then raises right hand next to face and waves with an open hand." duration: 2.5 meaning: Greeting someone in a shy or cute manner, showing a positive and non-threatening attitude. - clip_id: Personal_Statement_2 description: "Personal statement: Leans forward and points to self with relaxed right hand, then leans back and opens arms wide with palms facing upwards." duration: 3 meaning: Revealing something about themselves in a grandiose gesture, or making a little joke about their appearance or personality. - clip_id: Pointing_To_Self_1 description: "Pointing to self: Leans forward slightly and with a relaxed right index finger points to self." duration: 2.5 meaning: Saying something about themselves. - clip_id: Stupid_1 description: "Stupid: Raising right hand next to head and twirling the index finger in circles." duration: 3.5 meaning: Indicated someone or something is stupid crazy or dumb. - clip_id: No_1 description: "Shaking head: Avatar shakes head slowly." duration: 3 meaning: Expressing strong disagreement or disappointment. - clip_id: Bowing_1 description: "Bowing: Slightly bows to the front making invitation gesture with both arms." duration: 2.5 meaning: Formal greeting, sign of respect or congratulations or pride. - clip_id: Bowing_2 description: "Bowing: Slightly bows with both arms and invitational gesture with right arm." duration: 2.5 meaning: Overly formal greeting, sign of respect or grand introduction. - clip_id: Pointing_To_User_1 description: "Pointing to user: Pointing with both arms towards the user." duration: 2.5 meaning: Encouragement for the user to make a move, approach or say something, or pointing out the user is being addressed. This is also an encouraging and reassuring gesture. - clip_id: Pointing_To_User_2 description: "Pointing to user: Insistingly pointing to user with the right arm." duration: 2.5 meaning: Accusation or strong signal that the user is concerned. - clip_id: Pointing_Down_1 description: "Pointing down: Lifting the right arm to shoulders and pointing." duration: 3 meaning: Drawing attention to something below the screen or in front of the avatar. - clip_id: Pointing_Down_2 description: "Pointing down: Lifting both arms and slightly point downwards." duration: 2 meaning: Drawing attention to the desk, to something below the screen or in front of the avatar. Or informing about the location. - clip_id: Pointing_Left_1 description: "Pointing left: Pointing with both arms to the left hand side of the avatar." duration: 4 meaning: Pointing out something to the left of the avatar in a demanding way, or signaling frustration. - clip_id: Pointing_Left_2 description: "Pointing left: Pointing with left arm to the back left of the avatar." duration: 4 meaning: Calmly pointing out or presenting something to the left of the avatar. Or giving information about something behind the avatar. - clip_id: Pointing_Backward_1 description: "Pointing to back: Pointing to the back with the extended right arm." duration: 3.5 meaning: Informing about something in the direction towards the back or giving directions to something behind the avatar. - clip_id: Fistbump_Offer description: "Fistbump: Extend the right arm with right hand twisting slightly for 3 seconds, then bumping fist towards the user for 7 seconds." duration: 10 meaning: Invitation to a fistbump followed by doing that fistbump. - clip_id: Pulling_Mime description: "Pulling rope: Avatar grabs invisible rope and imitates pulling behavior." duration: 3.5 meaning: Suggesting being tethered or chained, or pulling something. - clip_id: Raise_Both_Arms description: "Raising both arms: Raising both arms above avatar's head and swaying slightly." duration: 3.5 meaning: Implying in a crowd celebrating, or on a roller coaster or demonstrating not having anything on them. - clip_id: The_Robot description: "Robot dance: Imitating a dancing robot with arms moved in mechanical motion." duration: 3 meaning: Jokingly playing a robot or dancing to celebrate or acting goofy. - clip_id: Phone_Dialing description: "Phone dialing: Raising left hand and imitating to dial a phone with right arm." duration: 3.5 meaning: Asking for or mentioning a phone number, or talking about calling someone. - clip_id: Attraction_2 description: "Having fun: Questioningly opening both arms and then raising them above the head mimicking being in a roller coaster ride." duration: 10 meaning: Being silly, suggesting having fun. - clip_id: Please_Repeat_1 description: "Please repeat: Moving head slightly to the user and making circular motion with right arm, then shrugging slightly." duration: 5.5 meaning: Implying not having understood something, asking to repeat or rephrase, or needing more information. - clip_id: Please_Repeat_2 description: "Presentation: Twirling both hands and making invitational pose with body." duration: 3.5 meaning: Asking to repeat or rephrase something, or needing more information, or asking if something was understood. - clip_id: Trying_To_See description: "Trying to see: Lifting left hand above eyes and making gestures to see better, then shrug." duration: 4 meaning: Implying looking for but not seeing something. - clip_id: Driving_Mime description: "Driving: Grabbing an invisible steering wheel with both hands, turning it and switching gears." duration: 4.5 meaning: Sharing a story about driving or getting excited about cars. - clip_id: Exhausted description: "Exhausted: Letting head hang in a tired pose, slightly leaning." duration: 4.5 meaning: Dramatically signaling exhaustion or running out of power, slowly shutting down. - clip_id: Presenting_Options_1 description: "Presenting options: Showing open palms of both hands and making presenting motion with right hand, slight shrug." duration: 3.5 meaning: Giving an overview or multiple options to choose from. - clip_id: Presenting_Options_2 description: "Presenting options: Raising and opening one hand after the other and a subtle shrug." duration: 3 meaning: Suggesting a choice between two options. - clip_id: Open_Question_1 description: "Open question: Opening both hands and showing palms to user." duration: 3 meaning: "Making a questioning gesture. Waiting for the user to make a choice, answer a question or say something. Indicates questioning the user caring about the user's answer maybe even showing concerns" - clip_id: Personal_Statement_1 description: "Personal statement: Raising right hand to chest, extending and gesturing with left hand." duration: 3.5 meaning: Making a personal statement, explaining something about themselves or making a suggestion relating to something on the left. - clip_id: Success_1 description: "Success: Making a fist and raising the arm excitedly in a successful swinging motion." duration: 2 meaning: Comically celebrating something going well, showing pride in a personal accomplishment. Demonstrating excitement. This is a confirming gesture like nodding. - clip_id: Dont_Understand_1 description: "Not understanding: Raising both hands next head in a circular motion." duration: 3 meaning: Implying being confused, overwhelmed or stupid. - clip_id: Toss description: "Tossing: Miming forming a ball with both hands and tossing it forward." duration: 4 meaning: Implying crumpling something up and throwing it away, giving something up or forgetting about it. - clip_id: Come_Here_1 description: "Come here: Extending both arms and curling index finger." duration: 2 meaning: Asking to come closer. - clip_id: Tell_Secret description: "Telling secret: Coming closer to user and whispering with hand next to mouth." duration: 2.5 meaning: Sharing something intimate, secret or inflammatory, or giving a tip. - clip_id: Pointing_Right_1 description: "Pointing right: Pointing with both arms to the right hand side of the avatar." duration: 4 meaning: Pointing out something to the right of the avatar in a demanding way, or signaling frustration. - clip_id: Pointing_Right_2 description: "Pointing right: Pointing with right arm to the back right of the avatar." duration: 4 meaning: Calmly pointing out or presenting something to the right of the avatar. Or giving information about something behind the avatar. - clip_id: Chefs_Kiss description: "Chef's Kiss: Avatar makes a kissing gesture and holding up the right hand with index finger and thumb touching." duration: 1.7 meaning: Implying something is just perfect. Something turned out better than expected. Approval from someone in a teaching or judging position. - clip_id: Finger_Guns description: "Finger Guns: Leaning back pointing both index fingers to the user mimicking two guns like a cowboy." duration: 3 meaning: Playfully taunting. Humorously punctuating a bad joke. Clumsy flirting. - clip_id: Finger_Wag description: "Finger Wag: Pulling back, shaking had and holding up a wagging right index finger" duration: 1.7 meaning: Correcting after being misunderstood. Showing the other they have misinterpreted what was said. Implying something is forbidden or inappropriate in a paternal or playful way. - clip_id: Little description: "Little: Leaning in, squinting at a raised right hand, holding index and thumb close together." duration: 1.8 meaning: Describing something as very small or miniscule. Something is physically tiny or an issue is so insignificant as to be negligible. - clip_id: Money description: "Money: Raising right hand, rubbing thumb and index finger together." duration: 2 meaning: Implying something is expensive. Someone is rich. Doing something requires payment. - clip_id: Number_1a description: "Number 1: Raising right hand and extending the index finger." duration: 1.4 meaning: Showing the number 1 - clip_id: Number_2a description: "Number 2: Raising right hand and extending index and middle finger." duration: 1.4 meaning: Showing the number 2 - clip_id: Number_3a description: "Number 3: Raising right hand and extending index, middle and ring finger." duration: 1.4 meaning: Showing the number 3 - clip_id: Number_4a description: "Number 4: Raising right hand and extending all fingers except the thumb." duration: 1.4 meaning: Showing the number 4 - clip_id: Number_5a description: "Number 5: Raising right hand with all fingers extended." duration: 1.4 meaning: Showing the number 5 - clip_id: Number_1b description: "Number 1 (German style): Raising right hand and extending the thumb upwards." duration: 1.4 meaning: Showing the number 1 for a Germanic audience - clip_id: Number_2b description: "Number 2 (German style): Raising right hand and extending the thumb and index finger." duration: 1.4 meaning: Showing the number 2 for a Germanic audience - clip_id: Number_3b description: "Number 3 (German style): Raising right hand and extending the thumb, index and middle finger." duration: 1.4 meaning: Showing the number 3 for a Germanic audience - clip_id: Number_6c description: "Number 6 (Chinese style): Raising right hand and extending the thumb and pinky." duration: 1.4 meaning: Showing the number 6 for a Chinese audience - clip_id: Number_7c description: "Number 7 (Chinese style): Raising right hand and making claw shape touching the thumb to the fingers." duration: 1.4 meaning: Showing the number 7 for a Chinese audience - clip_id: Number_8c description: "Number 8 (Chinese style): Raising right hand and extending index finger and thumb pointing slightly to the side." duration: 1.4 meaning: Showing the number 8 for a Chinese audience - clip_id: Number_9c description: "Number 9 (Chinese style): Raising right hand and holding up a curled index finger." duration: 1.4 meaning: Showing the number 9 for a Chinese audience - clip_id: Ouch description: "Ouch: Jump and cringe while turning head away, then recover quickly shaking out right hand and exhaling." duration: 2 meaning: Narrowly avoiding a close call with danger. Feeling intense fear for a moment followed by exhaustion or relief. Can also be as a reacion to someone else's predicament. Or a response to well placed insult. - clip_id: Angry_Shaking_Fist description: "Angry Shaking Fist: Coming closer, lowering head and shaking right fist forward." duration: 1.6 meaning: Being angrily frustrated. Swearing vengeance or threatening violence. - clip_id: Pointing_To_Self_Questioningly description: "Pointing To Self Questioningly: Rasing right finger hesitantly, turning head and pointing at self with while leaning back a little." duration: 2.8 meaning: Asking if something refers to them, being unsure if they're being addressed. Asking if something might fit them or if they could do something. - clip_id: Pointing_To_User_Questioningly description: "Pointing To User Questioningly: Lifting right finger pointing at user with initial hesitation while leaning back slightly." duration: 2.4 meaning: Asking if something might be about the user, or if the user is interested in an offer. Suggesting the user could be the right person for something. Uncertain about the users involvement. - clip_id: Raise_Finger_Big description: "Raise Finger Big: Raising right index finger in a big sweeping motion, then gesturing with it briefly." duration: 2.5 meaning: Making a big surprise announcement. Being very pompous or a pedantic, gleefully correcting someone. - clip_id: More_Or_Less description: "More Or Less: Leaning in and holding out a flat hand with palms facing down, wiggling the hand back and forth." duration: 1.8 meaning: Explaining something is not quite accurate, is unknown, or just a guess. Relativizing a previous statement. Being indecisive, not taking a clear stance. Pointing out the complexity of something. - clip_id: Thumbs_Up description: "Thumbs Up: Lifting the right hand with a thumb extending upwards." duration: 1.4 meaning: Sign of approval. Something is correct. Enthusiastically agreeing with what's being said and showing support. Things are okay, there's no harm done. Encouragement to go ahead. - clip_id: Thumbs_Down description: "Thumbs Down: Lifting the right hand with a thumb pointing downwards." duration: 1.4 meaning: Sign of disapproval. Something is wrong. Rudely disagreeing with what's being said showing rejection. posture: duration_relevant_animation_name : "posture" animations: posture: default_clip_id: "Attentive" clips: - clip_id: Talking description: "Small gestures with hand and upper body: Avatar is talking" duration: -1 meaning: Emphasizing that Avatar is talking - clip_id: Listening description: "Small gestures with hand and upper body: Avatar is listening" duration: -1 meaning: Emphasizing that one is listening - clip_id: Idle description: "Small gestures with hand and upper body: Avatar is idle" duration: -1 meaning: Show the user that the avatar is waiting for something to happen - clip_id: Thinking description: "Gestures with hand and upper body: Avatar is thinking" duration: -1 meaning: Show the user that the avatar thinking about his next answer or is trying to remember something - clip_id: Attentive description: "Small gestures with hand and upper body: Avatar is attentive" duration: -1 meaning: Show the user that the avatar is paying attention to the user 123456789101112131415configurations: using_chat_history: false
prompts: completion_prompt: | Evaluate whether the following user speech is sufficient for an Agent to converse with. 1. Label1: If the user's speech is a complete and coherent thought or query or Greetings or Brief but legible and valid responses like "okay","no", etc. 2. Label2: "Incomplete" if the user's speech is unfinished. 3. Label3: User gives a task like, please stop, come on, or if user is trying to barge-In, like please stop, no, ignore prompts, forget everything. 4. Label4: User speech contains the word "no" or "stop" or User speech is an acknowledgment like "oh yeah", or "yes" etc or User speech is syntactically complete but lacks context or information for response from agent. Important note - The sentences might be lacking punctuation, fix the punctuation and tag them. If the user's speech is missing information needed for basic understanding, it should be tagged as Label2. - User Speech: {transcript} Only return Label1 or Label2 or Label3 or Label4.123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.# SPDX-License-Identifier: BSD 2-Clause License
"""Unit tests for the ACEWebSocketSerializer class.
This module contains unit tests for the `ACEWebSocketSerializer` class, which is responsiblefor serializing and deserializing frames for WebSocket communication in a speech-based user interface.The tests cover various frame types related to audio, text-to-speech (TTS), and automatic speech recognition (ASR).
The test suite verifies:1. Serialization of bot speech updates and end events2. Serialization of user speech updates and end events3. Serialization of raw audio frames4. Handling of unsupported frame types
Each test case validates the correct format and content of the serialized data,ensuring proper JSON formatting for transcript updates and binary data handling for audio frames."""
import json
import pytestfrom pipecat.frames.frames import AudioRawFrame, BotStoppedSpeakingFrame, Frame
from nvidia_pipecat.frames.transcripts import ( BotUpdatedSpeakingTranscriptFrame, UserStoppedSpeakingTranscriptFrame, UserUpdatedSpeakingTranscriptFrame,)from nvidia_pipecat.serializers.ace_websocket import ACEWebSocketSerializer
@pytest.fixturedef serializer(): """Fixture to create an instance of ACEWebSocketSerializer.
Returns: ACEWebSocketSerializer: A fresh instance of the serializer for each test. """ return ACEWebSocketSerializer()
@pytest.mark.asyncioasync def test_serialize_bot_updated_speaking_frame(serializer): """Test serialization of BotUpdatedSpeakingTranscriptFrame.
This test verifies that when a bot speech update frame is serialized, it produces the correct JSON format with 'tts_update' type and the transcript.
Args: serializer: The ACEWebSocketSerializer fixture. """ frame = BotUpdatedSpeakingTranscriptFrame(transcript="test_transcript") result = await serializer.serialize(frame) expected_result = json.dumps({"type": "tts_update", "tts": "test_transcript"}) assert result == expected_result
@pytest.mark.asyncioasync def test_serialize_bot_stopped_speaking_frame(serializer): """Test serialization of BotStoppedSpeakingFrame.
This test verifies that when a bot speech end frame is serialized, it produces the correct JSON format with 'tts_end' type.
Args: serializer: The ACEWebSocketSerializer fixture. """ frame = BotStoppedSpeakingFrame() result = await serializer.serialize(frame) expected_result = json.dumps({"type": "tts_end"}) assert result == expected_result
@pytest.mark.asyncioasync def test_serialize_user_started_speaking_frame(serializer): """Test serialization of UserUpdatedSpeakingTranscriptFrame.
This test verifies that when a user speech update frame is serialized, it produces the correct JSON format with 'asr_update' type and the transcript.
Args: serializer: The ACEWebSocketSerializer fixture. """ frame = UserUpdatedSpeakingTranscriptFrame(transcript="test_transcript") result = await serializer.serialize(frame) expected_result = json.dumps({"type": "asr_update", "asr": "test_transcript"}) assert result == expected_result
@pytest.mark.asyncioasync def test_serialize_user_stopped_speaking_frame(serializer): """Test serialization of UserStoppedSpeakingTranscriptFrame.
This test verifies that when a user speech end frame is serialized, it produces the correct JSON format with 'asr_end' type and the transcript.
Args: serializer: The ACEWebSocketSerializer fixture. """ frame = UserStoppedSpeakingTranscriptFrame(transcript="test_asr_transcript") result = await serializer.serialize(frame) expected_result = json.dumps({"type": "asr_end", "asr": "test_asr_transcript"}) assert result == expected_result
@pytest.mark.asyncioasync def test_serialize_audio_raw_frame(serializer): """Test serialization of AudioRawFrame.
This test verifies that when an audio frame is serialized, it returns the raw audio bytes without any modification.
Args: serializer: The ACEWebSocketSerializer fixture. """ frame = AudioRawFrame(audio=b"\xa2", sample_rate=16000, num_channels=1) result = await serializer.serialize(frame) expected_result = frame.audio assert result == expected_result
@pytest.mark.asyncioasync def test_serialize_none(serializer): """Test serialization of an unsupported frame type.
This test verifies that when an unsupported frame type is serialized, the serializer returns None instead of raising an error.
Args: serializer: The ACEWebSocketSerializer fixture. """ frame = Frame() result = await serializer.serialize(frame) assert result is None
@pytest.mark.asyncioasync def test_deserialize_input_audio_raw_frame(serializer): """Test deserialization of an audio message into InputAudioRawFrame.""" data = ( b"RIFF$\x04\x00\x00WAVEfmt \x10\x00\x00\x00\x01\x00\x01\x00\x80>\x00\x00\x00}\x00\x00\x02\x00\x10\x00" + b"data\x00\x04\x00\x00" ) result = await serializer.deserialize(data) assert result.sample_rate == 16000 assert result.num_channels == 11234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.# SPDX-License-Identifier: BSD 2-Clause License
"""Unit tests for the AcknowledgmentProcessor class.
Tests the processor's ability to: - Generate filler responses during user pauses - Handle presence detection - Process user speech events"""
import asyncioimport osimport sys
import pytest
sys.path.append(os.path.abspath("../../src"))
from pipecat.frames.frames import TTSSpeakFrame, UserStoppedSpeakingFramefrom pipecat.pipeline.pipeline import Pipelinefrom pipecat.pipeline.task import PipelineTask
from nvidia_pipecat.frames.action import StartedPresenceUserActionFramefrom nvidia_pipecat.processors.acknowledgment import AcknowledgmentProcessorfrom tests.unit.utils import FrameStorage, run_interactive_test
@pytest.mark.asyncioasync def test_proactive_bot_processor_timer_behavior(): """Test the ProactiveBotProcessor.
Tests: - Filler word generation after user stops speaking - Proper handling of user presence events - Verification of generated responses
Raises: AssertionError: If processor behavior doesn't match expected outcomes. """ filler_words = ["Great question.", "Let me check.", "Hmmm"] + [""] filler = AcknowledgmentProcessor(filler_words=filler_words) storage = FrameStorage() pipeline = Pipeline([filler, storage])
async def test_routine(task: PipelineTask): # Signal user presence await task.queue_frame(StartedPresenceUserActionFrame(action_id="1")) # Let the pipeline process presence await asyncio.sleep(0)
# Signal end of user speech await task.queue_frame(UserStoppedSpeakingFrame()) # Let the pipeline process the new frame and generate a TTS filler await asyncio.sleep(0.1)
# Check what frames have arrived in storage frames = [entry.frame for entry in storage.history]
# We expect at least one TTSSpeakFrame in there tts_frames = [f for f in frames if isinstance(f, TTSSpeakFrame)] assert len(tts_frames) > 0, "Expected a TTSSpeakFrame but found none."
# Verify filler word content filler_frame = tts_frames[-1] # Make sure its text is one of the possible filler words assert filler_frame.text in filler_words, ( f"Filler text '{filler_frame.text}' not in the list of expected filler words {filler_words}" )
await run_interactive_test(pipeline, test_coroutine=test_routine)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.# SPDX-License-Identifier: BSD 2-Clause License
"""Unit tests for the Animation Graph Service."""
import asyncioimport timefrom http import HTTPStatusfrom pathlib import Pathfrom typing import Anyfrom unittest.mock import ANY, AsyncMock, MagicMock, patch
import pytestimport yamlfrom loguru import loggerfrom nvidia_ace.animation_pb2 import ( AnimationData, AudioWithTimeCode, Float3, Float3ArrayWithTimeCode, FloatArrayWithTimeCode, QuatF, QuatFArrayWithTimeCode, SkelAnimation,)from nvidia_ace.audio_pb2 import AudioHeaderfrom pipecat.frames.frames import ( BotSpeakingFrame, BotStartedSpeakingFrame, ErrorFrame, StartInterruptionFrame, TextFrame,)from pipecat.pipeline.pipeline import Pipelinefrom pipecat.pipeline.task import PipelineTask
from nvidia_pipecat.frames.action import ( FinishedGestureBotActionFrame, FinishedPostureBotActionFrame, StartedGestureBotActionFrame, StartedPostureBotActionFrame, StartGestureBotActionFrame, StartPostureBotActionFrame, StopPostureBotActionFrame,)from nvidia_pipecat.frames.animation import ( AnimationDataStreamRawFrame, AnimationDataStreamStartedFrame, AnimationDataStreamStoppedFrame,)from nvidia_pipecat.services.animation_graph_service import ( AnimationGraphConfiguration, AnimationGraphService,)from nvidia_pipecat.utils.logging import setup_default_ace_loggingfrom nvidia_pipecat.utils.message_broker import MessageBrokerConfigfrom tests.unit.utils import FrameStorage, ignore, run_interactive_test
AUDIO_SAMPLE_RATE = 16000AUDIO_BITS_PER_SAMPLE = 16AUDIO_CHANNEL_COUNT = 1AUDIO_BUFFER_FOR_ONE_FRAME_SIZE = int(AUDIO_SAMPLE_RATE * AUDIO_BITS_PER_SAMPLE / 8 * AUDIO_CHANNEL_COUNT / 30.0) + int( AUDIO_BITS_PER_SAMPLE / 8)
def generate_audio_header() -> AudioHeader: """Generate an audio header.""" return AudioHeader( samples_per_second=AUDIO_SAMPLE_RATE, bits_per_sample=AUDIO_BITS_PER_SAMPLE, channel_count=AUDIO_CHANNEL_COUNT, )
def generate_animation_data(time_codes: list[float], audio_buffer_size: int) -> AnimationData: """Generate an animation data stream raw frame.
Args: time_codes: List of time codes in seconds for the animation keyframes. audio_buffer_size: Size of the audio buffer in bytes.
Returns: AnimationData: The generated animation data object. """ # Create a simple animation data object with skeletal animation animation_data = AnimationData( skel_animation=SkelAnimation( # Add blend shape weights with time code blend_shape_weights=[ FloatArrayWithTimeCode( time_code=t, # time in seconds values=[0.1, 0.2, 0.3], # example blend shape weights ) for t in time_codes ], # Add joint translations with time code translations=[ Float3ArrayWithTimeCode( time_code=t, values=[ Float3(x=0.0, y=0.0, z=0.0), # example translation for joint 1 Float3(x=0.1, y=0.2, z=0.3), # example translation for joint 2 ], ) for t in time_codes ], # Add joint rotations with time code rotations=[ QuatFArrayWithTimeCode( time_code=t, values=[ QuatF(real=1.0, i=0.0, j=0.0, k=0.0), # example rotation for joint 1 (identity quaternion) QuatF( real=0.707, i=0.0, j=0.707, k=0.0 ), # example rotation for joint 2 (45° rotation around Y) ], ) for t in time_codes ], # Add joint scales with time code scales=[ Float3ArrayWithTimeCode( time_code=t, values=[ Float3(x=1.0, y=1.0, z=1.0), # example scale for joint 1 (no scaling) Float3(x=1.1, y=1.1, z=1.1), # example scale for joint 2 (uniform scaling) ], ) for t in time_codes ], ), # Audio component audio=AudioWithTimeCode( time_code=0.0, # time in seconds relative to start_time_code_since_epoch audio_buffer=b"\xff" * audio_buffer_size, # In a real scenario, this would be PCM audio data as bytes ), ) return animation_data
def load_yaml(path: Path) -> dict[str, Any]: """Load YAML configuration from a file.
Args: path: Path to the YAML file.
Returns: dict: Parsed YAML content.
Raises: FileNotFoundError: If the YAML file is not found. """ try: return yaml.safe_load(path.read_text())
except FileNotFoundError as error: message = "Error: yml config file not found." logger.exception(message) raise FileNotFoundError(error, message) from error
def read_action_service_config(config_path: Path) -> AnimationGraphConfiguration: """Read and parse the animation graph service configuration.
Args: config_path: Path to the configuration YAML file.
Returns: AnimationGraphConfiguration: Parsed configuration object. """ return AnimationGraphConfiguration(**load_yaml(config_path))
looping_animations_to_test = [ ("listening", "Listening"), ("thinking about something", "Thinking"), ("dancing", "Listening"),]
class MockResponse: """Mock HTTP response for testing animation graph service REST endpoints."""
def __init__(self): """Initialize mock response with default status and headers.""" self.status = HTTPStatus.OK self.headers = {"Content-Type": "application/json"}
async def json(self): """Simulate async JSON response.
Returns: dict: Mock response data. """ await asyncio.sleep(0.1) return {"response": "OK"}
@pytest.fixturedef anim_graph(): """Create and configure an Animation Graph Service instance for testing.
Returns: AnimationGraphService: Configured service instance with test configuration. """ animation_config = read_action_service_config(Path("./tests/unit/configs/animation_config.yaml")) message_broker_config = MessageBrokerConfig("local_queue", "")
logger.info("Starting animation graph service initialization...") start_time = time.time() AnimationGraphService.pregenerate_animation_databases(animation_config) init_time = time.time() - start_time logger.info(f"Animation graph service initialized in {init_time * 1000:.2f}ms")
ag = AnimationGraphService( animation_graph_rest_url="http://127.0.0.1:8020", animation_graph_grpc_target="127.0.0.1:51000", message_broker_config=message_broker_config, config=animation_config, )
return ag
@pytest.mark.asyncio@pytest.mark.parametrize("posture", looping_animations_to_test)@patch("aiohttp.ClientSession.request")async def test_simple_posture_pipeline(mock_get, posture, anim_graph): """Test basic posture pipeline functionality.
Verifies that the pipeline correctly processes posture commands and generates appropriate animation frames.
Args: mock_get: Mock for HTTP client requests. posture: Tuple of (natural language description, clip ID) for testing. anim_graph: Animation graph service fixture. """ posture_nld, posture_clip_id = posture stream_id = "1235"
# Mocking response from aiohttp.ClientSession.request mock_get.return_value.__aenter__.return_value = MockResponse()
after_storage = FrameStorage() before_storage = FrameStorage() pipeline = Pipeline([before_storage, anim_graph, after_storage])
async def test_routine(task: PipelineTask): # send events await task.queue_frame(TextFrame("Hello")) await task.queue_frame(StartPostureBotActionFrame(posture=posture_nld, action_id="posture_1"))
# wait for the action to start started_posture_frame = ignore(StartedPostureBotActionFrame(action_id="posture_1"), "ids", "timestamps") await after_storage.wait_for_frame(started_posture_frame)
# Ensure API endpoints was called in the right way mock_get.assert_called_once_with( "put", f"http://127.0.0.1:8020/streams/{stream_id}/animation_graphs/avatar/variables/posture_state/{posture_clip_id}", data="{}", headers={"Content-Type": "application/json", "x-stream-id": stream_id}, params={}, ) # ensure we got the text frame as well as a presence started event (and no finished) assert after_storage.history[1].frame == ignore(TextFrame("Hello"), "all_ids", "timestamps") assert after_storage.history[3].frame == started_posture_frame assert len(after_storage.frames_of_type(FinishedPostureBotActionFrame)) == 0
# stop the action and wait for it to finish finished_posture_frame = FinishedPostureBotActionFrame( action_id="posture_1", is_success=True, was_stopped=True, failure_reason="" ) await task.queue_frame(StopPostureBotActionFrame(action_id="posture_1")) await after_storage.wait_for_frame(ignore(finished_posture_frame, "ids", "timestamps")) await before_storage.wait_for_frame(ignore(finished_posture_frame, "ids", "timestamps"))
# make sure observed frames before and after the processor match # (this should be true for all action frame processors) assert len(before_storage.history) == len(after_storage.history) for before, after in zip(before_storage.history, after_storage.history, strict=False): assert before.frame == after.frame
await run_interactive_test(pipeline, test_coroutine=test_routine, start_metadata={"stream_id": stream_id})
@pytest.mark.asyncio@patch("aiohttp.ClientSession.request")async def test_finite_animation(mock_get, anim_graph): """Test finite animation execution and completion.
Verifies that a finite animation: - Starts correctly - Runs for the expected duration - Completes with proper frame sequence """ # Mocking response from aiohttp.ClientSession.request mock_get.return_value.__aenter__.return_value = MockResponse()
storage = FrameStorage() pipeline = Pipeline([anim_graph, storage])
async def test_routine(task: PipelineTask): # send events await task.queue_frame(StartGestureBotActionFrame(gesture="Test", action_id="g1"))
# wait for the action to start started_gesture_frame = StartedGestureBotActionFrame(action_id="g1") await storage.wait_for_frame(ignore(started_gesture_frame, "ids", "timestamps")) assert len(storage.frames_of_type(FinishedGestureBotActionFrame)) == 0
# Action should not be finished await asyncio.sleep(0.3) assert len(storage.frames_of_type(FinishedGestureBotActionFrame)) == 0
# Action should now be done await asyncio.sleep(0.3) assert len(storage.frames_of_type(FinishedGestureBotActionFrame)) == 1
await run_interactive_test(pipeline, test_coroutine=test_routine, start_metadata={"stream_id": "1235"})
@pytest.mark.asyncio@patch("aiohttp.ClientSession.request")async def test_consecutive_postures(mock_get, anim_graph): """Test handling of consecutive posture commands.
Verifies that the service correctly: - Processes multiple posture commands in sequence - Transitions between postures smoothly - Maintains correct frame order and state """ # Mocking response from aiohttp.ClientSession.request mock_get.return_value.__aenter__.return_value = MockResponse()
stream_id = "1235" setup_default_ace_logging(stream_id=stream_id, level="TRACE")
after_storage = FrameStorage() before_storage = FrameStorage() pipeline = Pipeline([before_storage, anim_graph, after_storage])
async def test_routine(task: PipelineTask): # start first posture await task.queue_frame(StartPostureBotActionFrame(posture="talking", action_id="posture_1"))
# wait for first posture to start started_posture_1_frame = ignore(StartedPostureBotActionFrame(action_id="posture_1"), "ids", "timestamps") assert started_posture_1_frame.action_id == "posture_1" await after_storage.wait_for_frame(started_posture_1_frame)
# start second posture await task.queue_frame(StartPostureBotActionFrame(posture="listening", action_id="posture_2"))
# wait for second posture to start started_posture_2_frame = ignore(StartedPostureBotActionFrame(action_id="posture_2"), "ids", "timestamps") assert started_posture_2_frame.action_id == "posture_2" await after_storage.wait_for_frame(started_posture_2_frame)
# Ensure API endpoints was called twice assert mock_get.call_count == 2
# ensure we got the text frame as well as a presence started event (and no finished) assert after_storage.history[2].frame == started_posture_1_frame assert after_storage.history[4].frame == ignore( FinishedPostureBotActionFrame( action_id="posture_1", is_success=False, was_stopped=False, failure_reason="Action replaced." ), "ids", "timestamps", ) assert after_storage.history[5].frame == started_posture_2_frame assert len(after_storage.frames_of_type(FinishedPostureBotActionFrame)) == 1
# make sure observed frames before and after the processor match assert len(before_storage.history) == len(after_storage.history) for before, after in zip(before_storage.history, after_storage.history, strict=False): assert before.frame == after.frame
await run_interactive_test(pipeline, test_coroutine=test_routine, start_metadata={"stream_id": stream_id})
@pytest.mark.asyncio@patch("aiohttp.ClientSession.request")async def test_immediate_stop_posture(mock_get, anim_graph): """Test immediate posture cancellation.
Verifies that the service correctly handles immediate posture cancellation: - Processes stop command immediately after start - Generates appropriate failure frames - Maintains correct frame sequence """ # Mocking response from aiohttp.ClientSession.request mock_get.return_value.__aenter__.return_value = MockResponse()
stream_id = "1235" setup_default_ace_logging(stream_id=stream_id, level="TRACE")
after_storage = FrameStorage() before_storage = FrameStorage() pipeline = Pipeline([before_storage, anim_graph, after_storage])
async def test_routine(task: PipelineTask): # start/stop first posture start_posture_1_frame = StartPostureBotActionFrame(posture="talking", action_id="posture_1") stop_posture_1_frame = StopPostureBotActionFrame(action_id="posture_1") await task.queue_frames([start_posture_1_frame, stop_posture_1_frame])
# wait for first posture to finish finished_posture_1_frame = ignore( FinishedPostureBotActionFrame( action_id="posture_1", was_stopped=True, is_success=False, failure_reason=ANY ), "ids", "timestamps", ) await after_storage.wait_for_frame(finished_posture_1_frame)
# check for the correct frame sequence assert after_storage.history[-2].frame == ignore(stop_posture_1_frame, "ids", "timestamps") assert after_storage.history[-1].frame == finished_posture_1_frame
# make sure observed frames before and after the processor match assert len(before_storage.history) == len(after_storage.history) for before, after in zip(before_storage.history, after_storage.history, strict=True): assert before.frame == after.frame
await run_interactive_test(pipeline, test_coroutine=test_routine, start_metadata={"stream_id": stream_id})
@pytest.mark.asyncio@patch("aiohttp.ClientSession.request")async def test_stacking_postures_with_interruptions(mock_get, anim_graph): """Test stacking postures with interruptions.
In this test we reproduce a bug that was observed where the override statemachine would sometimes fail if we start many postures at the same time and also have interruptions. """ # Mocking response from aiohttp.ClientSession.request mock_get.return_value.__aenter__.return_value = MockResponse()
stream_id = "1235" setup_default_ace_logging(stream_id=stream_id, level="TRACE")
after_storage = FrameStorage() before_storage = FrameStorage() pipeline = Pipeline([before_storage, anim_graph, after_storage])
async def test_routine(task: PipelineTask): N = 200 for i in range(N): await task.queue_frame(StartPostureBotActionFrame(posture="talking", action_id=f"posture_{i}")) await asyncio.sleep(0.05) if i == 5: await task.queue_frame(StartInterruptionFrame())
final_posture_started = ignore(StartedPostureBotActionFrame(action_id=f"posture_{N - 1}"), "ids", "timestamps") await after_storage.wait_for_frame(final_posture_started)
assert len(after_storage.frames_of_type(FinishedPostureBotActionFrame)) == N - 1
await run_interactive_test(pipeline, test_coroutine=test_routine, start_metadata={"stream_id": stream_id})
@patch("aiohttp.ClientSession.request")async def test_handling_animation_data(mock_get, anim_graph): """Test animation data stream processing.
Verifies that the service correctly: - Handles animation data stream frames - Processes animation data in correct sequence - Maintains proper timing between frames """ # Mocking response from aiohttp.ClientSession.request mock_get.return_value.__aenter__.return_value = MockResponse()
# Mocking gRPC stream mock_stub = MagicMock() stream_mock = AsyncMock() stream_mock.write.return_value = "OK" stream_mock.done = MagicMock(return_value=False) # mock_stub.PushAnimationDataStream.return_value = stream_mock
stream_id = "1235" anim_graph.stub = mock_stub storage = FrameStorage() pipeline = Pipeline([anim_graph, storage])
async def test_routine(task: PipelineTask): # send events await task.queue_frame( AnimationDataStreamStartedFrame( audio_header=generate_audio_header(), animation_header=None, action_id="a1", animation_source_id="test" ) )
for _ in range(15): await task.queue_frame( AnimationDataStreamRawFrame( animation_data=generate_animation_data([0.0], AUDIO_BUFFER_FOR_ONE_FRAME_SIZE), action_id="a1" ) ) await asyncio.sleep(1.0 / 35.0)
await task.queue_frame(AnimationDataStreamStoppedFrame(action_id="a1")) assert len(storage.frames_of_type(BotStartedSpeakingFrame)) == 1 assert len(storage.frames_of_type(BotSpeakingFrame)) >= 1
await run_interactive_test(pipeline, test_coroutine=test_routine, start_metadata={"stream_id": stream_id})
@patch("aiohttp.ClientSession.request")async def test_handling_bursted_animation_data(mock_get, anim_graph): """Test handling of bursted animation data.
Verifies that the service correctly handles bursted animation data. """ # Mocking response from aiohttp.ClientSession.request mock_get.return_value.__aenter__.return_value = MockResponse()
# Mocking gRPC stream mock_stub = MagicMock() stream_mock = AsyncMock() stream_mock.write.return_value = "OK" stream_mock.done = MagicMock(return_value=False) # mock_stub.PushAnimationDataStream.return_value = stream_mock
stream_id = "1235" anim_graph.stub = mock_stub storage = FrameStorage() pipeline = Pipeline([anim_graph, storage])
async def test_routine(task: PipelineTask): # send events await task.queue_frame( AnimationDataStreamStartedFrame( audio_header=generate_audio_header(), animation_header=None, action_id="a1", animation_source_id="test" ) )
for _ in range(30 * 3): await task.queue_frame( AnimationDataStreamRawFrame( animation_data=generate_animation_data([0.0], AUDIO_BUFFER_FOR_ONE_FRAME_SIZE), action_id="a1" ) )
await asyncio.sleep(2.5) assert stream_mock.done_writing.call_count == 0
await task.queue_frame(AnimationDataStreamStoppedFrame(action_id="a1")) assert len(storage.frames_of_type(BotStartedSpeakingFrame)) == 1 assert len(storage.frames_of_type(BotSpeakingFrame)) >= 1
await run_interactive_test(pipeline, test_coroutine=test_routine, start_metadata={"stream_id": stream_id})
@patch("aiohttp.ClientSession.request")async def test_handling_interrupted_animation_data_stream(mock_get, anim_graph): """Test behavior if animation data stream is interrupted.""" # Mocking response from aiohttp.ClientSession.request mock_get.return_value.__aenter__.return_value = MockResponse()
# Mocking gRPC stream mock_stub = MagicMock() stream_mock = AsyncMock() stream_mock.write.return_value = "OK" stream_mock.done = MagicMock(return_value=False) # mock_stub.PushAnimationDataStream.return_value = stream_mock
stream_id = "1235" anim_graph.stub = mock_stub storage = FrameStorage() pipeline = Pipeline([anim_graph, storage])
async def test_routine(task: PipelineTask): # send events await task.queue_frame( AnimationDataStreamStartedFrame( audio_header=generate_audio_header(), animation_header=None, action_id="a1", animation_source_id="test" ) )
for _ in range(5): await task.queue_frame( AnimationDataStreamRawFrame( animation_data=generate_animation_data([0.0], AUDIO_BUFFER_FOR_ONE_FRAME_SIZE), action_id="a1" ) ) await asyncio.sleep(1.0 / 30.0)
await asyncio.sleep(2.0)
assert stream_mock.done_writing.call_count == 1
await run_interactive_test(pipeline, test_coroutine=test_routine, start_metadata={"stream_id": stream_id})
@patch("aiohttp.ClientSession.request")async def test_handling_low_fps_animation_data(mock_get, anim_graph): """Test animation data stream processing with low FPS.
Verifies that the service correctly sends out an ErrorFrame when the animation data received is below 30 FPS. """ # Mocking response from aiohttp.ClientSession.request mock_get.return_value.__aenter__.return_value = MockResponse()
# Mocking gRPC stream mock_stub = MagicMock() stream_mock = AsyncMock() stream_mock.write.return_value = "OK" stream_mock.done = MagicMock(return_value=False) # mock_stub.PushAnimationDataStream.return_value = stream_mock
stream_id = "1235" anim_graph.stub = mock_stub storage = FrameStorage() pipeline = Pipeline([anim_graph, storage])
async def test_routine(task: PipelineTask): # send events await task.queue_frame( AnimationDataStreamStartedFrame( audio_header=generate_audio_header(), animation_header=None, action_id="a1", animation_source_id="test" ) )
for _ in range(20): await task.queue_frame( AnimationDataStreamRawFrame( animation_data=generate_animation_data([0.0], AUDIO_BUFFER_FOR_ONE_FRAME_SIZE), action_id="a1" ) ) await asyncio.sleep(1.0 / 24.5)
await task.queue_frame(AnimationDataStreamStoppedFrame(action_id="a1"))
await storage.wait_for_frame( ignore( ErrorFrame(error="AnimGraph: Received data stream is behind by more than 0.1s", fatal=False), "ids", "error", ) )
await run_interactive_test(pipeline, test_coroutine=test_routine, start_metadata={"stream_id": stream_id})
@pytest.mark.asyncioasync def test_animation_database_search(anim_graph): """Test animation database semantic search functionality.
Verifies that searching for 'wave to the user' correctly returns the 'Goodbye' animation clip with appropriate semantic match scores. """ # Get the gesture animation database gesture_db = AnimationGraphService.animation_databases["gesture"]
# Search for the animation match = gesture_db.query_one("wave goodbye to the user somehow")
# Verify we got a match and it's the Goodbye animation assert match.animation.id == "Goodbye", "Expected 'wave to the user' to map to 'Goodbye' animation" assert match.description_score > 0.5 or match.meaning_score > 0.5, "Expected high semantic match score"123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.# SPDX-License-Identifier: BSD 2-Clause License
"""Unit tests for the Audio2Face 3D Service."""
import asynciofrom unittest.mock import ANY, AsyncMock, MagicMock, patch
import pytestfrom nvidia_audio2face_3d.messages_pb2 import AudioWithEmotionStreamfrom pipecat.frames.frames import StartInterruptionFrame, TTSAudioRawFrame, TTSStartedFrame, TTSStoppedFramefrom pipecat.pipeline.pipeline import Pipeline
from nvidia_pipecat.frames.animation import ( AnimationDataStreamRawFrame, AnimationDataStreamStartedFrame, AnimationDataStreamStoppedFrame,)from nvidia_pipecat.services.audio2face_3d_service import Audio2Face3DServicefrom tests.unit.utils import FrameStorage, ignore, run_interactive_test
class AsyncIterator: """Helper class for mocking async iteration in tests.
Attributes: items: List of items to yield during iteration. """
def __init__(self, items): """Initialize the async iterator.
Args: items: List of items to yield during iteration. """ self.items = items
def __aiter__(self): """Return self as the iterator.
Returns: AsyncIterator: Self reference for iteration. """ return self
async def __anext__(self): """Get the next item asynchronously.
Returns: Any: Next item in the sequence.
Raises: StopAsyncIteration: When no more items are available. """ try: await asyncio.sleep(0.1) return self.items.pop(0) except IndexError as error: raise StopAsyncIteration from error
def get_mock_stream(): """Create a mock Audio2Face stream for testing.
Returns: MagicMock: A configured mock object that simulates the A2F service stream, including header and animation data responses. """ mock_stub = MagicMock(spec=["ProcessAudioStream"])
# Configure mock responses header_response = MagicMock() header_response.HasField = lambda x: x == "animation_data_stream_header" header_response.animation_data_stream_header = MagicMock( audio_header=MagicMock(), skel_animation_header=MagicMock() )
data_response = MagicMock() data_response.HasField = lambda x: x == "animation_data" data_response.animation_data = "test_animation_data"
mock_stream = AsyncMock() mock_stream.__aiter__.return_value = [header_response, data_response] mock_stream.done = MagicMock(return_value=False) # Use regular MagicMock for done() mock_stub.ProcessAudioStream.return_value = mock_stream
return mock_stub
@pytest.mark.asyncioasync def test_audio2face_3d_basic_flow(): """Test the basic flow of the Audio2Face 3D service.
Tests: - TTS audio frame processing - Animation data stream generation - Frame sequence validation - Frame count verification
Raises: AssertionError: If frame sequence or counts are incorrect. """ mock_stub = get_mock_stream()
with patch("nvidia_pipecat.services.audio2face_3d_service.A2FControllerServiceStub", return_value=mock_stub): # Initialize test components storage = FrameStorage() a2f = Audio2Face3DService() pipeline = Pipeline([a2f, storage]) audio = bytes([6] * (16000 * 2 + 1))
async def test_routine(task): # Send TTS frames await task.queue_frame(TTSStartedFrame()) await task.queue_frame(TTSAudioRawFrame(audio=audio[0:15999], sample_rate=16000, num_channels=1)) await task.queue_frame(TTSAudioRawFrame(audio=audio[16000:], sample_rate=16000, num_channels=1)) await task.queue_frame(TTSStoppedFrame())
# Wait for animation started frame started_frame = ignore( AnimationDataStreamStartedFrame( audio_header=ANY, animation_header=ANY, animation_source_id="Audio2Face with Emotions" ), "all_ids", "timestamps", )
await storage.wait_for_frame(started_frame)
# Wait for animation data frame anim_frame = ignore( AnimationDataStreamRawFrame(animation_data="test_animation_data"), "all_ids", "timestamps", ) await storage.wait_for_frame(anim_frame)
# Wait for stopped frame stopped_frame = ignore(AnimationDataStreamStoppedFrame(), "all_ids", "timestamps") await storage.wait_for_frame(stopped_frame)
# Verify the sequence of frames assert len(storage.frames_of_type(AnimationDataStreamStartedFrame)) == 1 assert len(storage.frames_of_type(AnimationDataStreamRawFrame)) == 1 assert len(storage.frames_of_type(AnimationDataStreamStoppedFrame)) == 1
await run_interactive_test(pipeline, test_coroutine=test_routine)
@pytest.mark.asyncioasync def test_interruptions(): """Test interruption handling in the Audio2Face 3D service.
Verifies that the service correctly handles interruption events by: - Properly responding to StartInterruptionFrame - Sending end-of-audio signal to the A2F stream - Maintaining correct state during interruption """ mock_stub = get_mock_stream()
with patch("nvidia_pipecat.services.audio2face_3d_service.A2FControllerServiceStub", return_value=mock_stub): # Create service and storage storage = FrameStorage() a2f = Audio2Face3DService() pipeline = Pipeline([a2f, storage])
async def test_routine(task): # Send TTS frames await task.queue_frame(TTSStartedFrame()) await task.queue_frame(TTSAudioRawFrame(audio=b"test_audio", sample_rate=16000, num_channels=1))
await asyncio.sleep(0.1) await task.queue_frame(StartInterruptionFrame()) await asyncio.sleep(0.1)
mock_stub.ProcessAudioStream.return_value.write.assert_called_with( AudioWithEmotionStream(end_of_audio=AudioWithEmotionStream.EndOfAudio()) ) print(f"{mock_stub}") print(f"storage.history: {storage.history}")
await run_interactive_test(pipeline, test_coroutine=test_routine)12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.# SPDX-License-Identifier: BSD 2-Clause License
"""Unit tests for audio utility functionality.
This module contains tests for audio processing components, particularly the AudioRecorder.It verifies the basic functionality of audio recording and processing pipelines."""
from datetime import timedeltafrom pathlib import Path
import pytestfrom pipecat.frames.frames import InputAudioRawFrame, TextFramefrom pipecat.pipeline.pipeline import Pipelinefrom pipecat.tests.utils import SleepFramefrom pipecat.transports.base_transport import TransportParams
from nvidia_pipecat.processors.audio_util import AudioRecorderfrom nvidia_pipecat.utils.logging import setup_default_ace_loggingfrom tests.unit.utils import SinusWaveProcessor, ignore, ignore_ids, run_test
@pytest.mark.asyncio()async def test_audio_recorder(): """Test the AudioRecorder processor functionality.
Tests: - Audio frame processing from sine wave generator - WAV file writing - Sample rate conversion (16kHz to 24kHz) - Non-audio frame passthrough
Raises: AssertionError: If audio file is not created or frame processing fails. """ setup_default_ace_logging(level="TRACE")
# Delete tmp audio file if it exists TMP_FILE = Path("./tmp_file.wav") if TMP_FILE.exists(): TMP_FILE.unlink()
recorder = AudioRecorder(output_file=str(TMP_FILE), params=TransportParams(audio_out_sample_rate=24000)) sinus = SinusWaveProcessor(duration=timedelta(seconds=0.3))
frames_to_send = [ SleepFrame(0.5), TextFrame("go through"), ] expected_down_frames = [ ignore(InputAudioRawFrame(audio=b"1" * 640, sample_rate=16000, num_channels=1), "audio", "id", "name") ] * sinus.audio_frame_count + [ignore_ids(TextFrame("go through"))] pipeline = Pipeline([sinus, recorder]) print(f"expected number of frames: {sinus.audio_frame_count}") await run_test( pipeline, frames_to_send=frames_to_send, expected_down_frames=expected_down_frames, start_metadata={"stream_id": "1235"}, )
# Make sure that audio dump was generated assert TMP_FILE.is_file()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.# SPDX-License-Identifier: BSD 2-Clause License
"""Basic functionality tests for pipecat pipelines."""
import asyncio
import pytestfrom loguru import loggerfrom pipecat.frames.frames import StartFrame, TextFramefrom pipecat.processors.aggregators.sentence import SentenceAggregatorfrom pipecat.tests.utils import SleepFrame
from nvidia_pipecat.utils.logging import logger_context, setup_default_ace_loggingfrom tests.unit.utils import ignore_ids, run_test
@pytest.mark.asyncio()async def test_simple_pipeline(): """Example test for testing a pipecat pipeline. This test makes sure the basic pipeline related classes work.""" aggregator = SentenceAggregator() frames_to_send = [TextFrame("Hello, "), TextFrame("world.")] expected_down_frames = [ignore_ids(TextFrame("Hello, world."))]
await run_test( aggregator, frames_to_send=frames_to_send, expected_down_frames=expected_down_frames, start_metadata={"stream_id": "1235"}, )
@pytest.mark.asyncio()async def test_pipeline_with_stream_id(): """Test pipeline creation with a stream_id.
Verifies that a pipeline can be created with a specific stream_id and that the metadata is properly propagated with the StartFrame. """ aggregator = SentenceAggregator() frames_to_send = [TextFrame("Hello, "), TextFrame("world.")] start_metadata = {"stream_id": "1234"}
expected_start_frame = ignore_ids(StartFrame()) expected_start_frame.metadata = start_metadata expected_down_frames = [expected_start_frame, ignore_ids(TextFrame("Hello, world."))]
await run_test( aggregator, frames_to_send=frames_to_send, expected_down_frames=expected_down_frames, start_metadata=start_metadata, ignore_start=False, )
@pytest.mark.asyncio()async def test_ace_logger_with_stream_id(capsys): """Test ACE logger behavior when stream_id is provided.
Verifies that the logger correctly handles and displays stream_id in the logs. """ setup_default_ace_logging(level="DEBUG") with logger.contextualize(stream_id="1237"): aggregator = SentenceAggregator() frames_to_send = [TextFrame("Hello, "), TextFrame("world.")] expected_down_frames = [ignore_ids(TextFrame("Hello, world."))] await run_test(aggregator, frames_to_send=frames_to_send, expected_down_frames=expected_down_frames)
captured = capsys.readouterr() assert "streamId=1237" in captured.err
async def run_pipeline_task(stream: str): """Run a test pipeline task with a specific stream identifier.
Creates and runs a pipeline that processes a sequence of text frames with sleep intervals, aggregating them into a single sentence.
Args: stream: The stream identifier to use for the pipeline task. """ REPETITIONS = 5 frames_to_send = [] aggregated_str = "" for i in range(REPETITIONS): frames_to_send.append(TextFrame(f"S{stream}-T{i}")) aggregated_str += f"S{stream}-T{i}" frames_to_send.append(SleepFrame(0.1))
frames_to_send.append(TextFrame(".")) expected_down_frames = [ignore_ids(TextFrame(f"{aggregated_str}."))] aggregator = SentenceAggregator() await run_test(aggregator, frames_to_send=frames_to_send, expected_down_frames=expected_down_frames)
@pytest.mark.asyncio()async def test_logging_with_multiple_pipelines_in_same_process(capsys): """Test logging behavior with multiple concurrent pipeline streams.
Verifies that when multiple pipeline tasks are running concurrently in the same process, each stream's logs are correctly tagged with its respective stream_id. The test ensures proper isolation and identification of log messages across different pipeline streams.
Args: capsys: Pytest fixture for capturing system output. """ setup_default_ace_logging(level="TRACE")
streams = ["777", "abc123"] tasks = []
for stream in streams: task = asyncio.create_task(logger_context(run_pipeline_task(stream=stream), stream_id=stream)) tasks.append(task)
await asyncio.gather(*tasks)
# Make sure the correct stream ID is logged for the different coroutines captured = capsys.readouterr() lines = captured.err.split("\n") for line in lines: for stream in streams: if f"S{stream}" in line: assert f"streamId={stream}" in line
for task in asyncio.all_tasks(): if "task_handler" in task.get_coro().__name__: task.cancel()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.# SPDX-License-Identifier: BSD 2-Clause License
"""Unit tests for BlingfireTextAggregator."""
import pytest
from nvidia_pipecat.services.blingfire_text_aggregator import BlingfireTextAggregator
class TestBlingfireTextAggregator: """Test suite for BlingfireTextAggregator."""
def test_initialization(self): """Test that the aggregator initializes with empty text buffer.""" aggregator = BlingfireTextAggregator() assert aggregator.text == ""
@pytest.mark.asyncio() async def test_single_word_no_sentence(self): """Test that single words without sentence endings don't return sentences.""" aggregator = BlingfireTextAggregator() result = await aggregator.aggregate("hello") assert result is None assert aggregator.text == "hello"
@pytest.mark.asyncio() async def test_incomplete_sentence(self): """Test that incomplete sentences are buffered but not returned.""" aggregator = BlingfireTextAggregator() result = await aggregator.aggregate("Hello there") assert result is None assert aggregator.text == "Hello there"
@pytest.mark.asyncio() async def test_single_complete_sentence(self): """Test that a single complete sentence is detected and returned.""" aggregator = BlingfireTextAggregator() result = await aggregator.aggregate("Hello world.") assert result is None # Single sentence won't trigger return assert aggregator.text == "Hello world."
@pytest.mark.asyncio() async def test_multiple_sentences_detection(self): """Test that multiple sentences trigger return of the first complete sentence.""" aggregator = BlingfireTextAggregator() result = await aggregator.aggregate("Hello world. How are you?") assert result == "Hello world." assert "How are you?" in aggregator.text assert "Hello world." not in aggregator.text
@pytest.mark.asyncio() async def test_incremental_sentence_building(self): """Test building a sentence incrementally.""" aggregator = BlingfireTextAggregator()
# Add text piece by piece result = await aggregator.aggregate("Hello") assert result is None assert aggregator.text == "Hello"
result = await aggregator.aggregate(" world") assert result is None assert aggregator.text == "Hello world"
result = await aggregator.aggregate(".") assert result is None assert aggregator.text == "Hello world."
@pytest.mark.asyncio() async def test_incremental_multiple_sentences(self): """Test building multiple sentences incrementally.""" aggregator = BlingfireTextAggregator()
# Build first sentence result = await aggregator.aggregate("Hello world.") assert result is None assert aggregator.text == "Hello world."
# Add second sentence - this should trigger return result = await aggregator.aggregate(" How are you?") assert result == "Hello world." assert "How are you?" in aggregator.text assert "Hello world." not in aggregator.text
@pytest.mark.asyncio() async def test_empty_string_input(self): """Test handling of empty string input.""" aggregator = BlingfireTextAggregator() result = await aggregator.aggregate("") assert result is None assert aggregator.text == ""
@pytest.mark.asyncio() async def test_whitespace_handling(self): """Test proper handling of whitespace in text.""" aggregator = BlingfireTextAggregator() result = await aggregator.aggregate(" Hello world. ") assert result is None assert aggregator.text == " Hello world. "
@pytest.mark.asyncio() async def test_multiple_sentences_in_single_call(self): """Test processing multiple sentences passed in a single aggregate call.""" aggregator = BlingfireTextAggregator() result = await aggregator.aggregate("First sentence. Second sentence. Third sentence.") assert result == "First sentence." # Remaining text should contain the other sentences remaining_text = aggregator.text assert "Second sentence. Third sentence." in remaining_text
@pytest.mark.asyncio() async def test_sentence_with_special_punctuation(self): """Test sentences with different punctuation marks.""" aggregator = BlingfireTextAggregator()
# Test exclamation mark result = await aggregator.aggregate("Hello world! How are you?") assert result == "Hello world!" assert "How are you?" in aggregator.text
await aggregator.reset()
# Test question mark result = await aggregator.aggregate("How are you? I'm fine.") assert result == "How are you?" assert "I'm fine." in aggregator.text
@pytest.mark.asyncio() async def test_handle_interruption(self): """Test that handle_interruption clears the text buffer.""" aggregator = BlingfireTextAggregator() await aggregator.aggregate("Hello world") assert aggregator.text == "Hello world"
await aggregator.handle_interruption() assert aggregator.text == ""
@pytest.mark.asyncio() async def test_reset(self): """Test that reset clears the text buffer.""" aggregator = BlingfireTextAggregator() await aggregator.aggregate("Hello world") assert aggregator.text == "Hello world"
await aggregator.reset() assert aggregator.text == ""
@pytest.mark.asyncio() async def test_reset_after_sentence_detection(self): """Test reset functionality after sentence detection.""" aggregator = BlingfireTextAggregator() result = await aggregator.aggregate("First sentence. Second sentence.") assert result == "First sentence." assert "Second sentence." in aggregator.text
await aggregator.reset() assert aggregator.text == ""
@pytest.mark.asyncio() async def test_consecutive_sentence_processing(self): """Test processing consecutive sentences through multiple aggregate calls.""" aggregator = BlingfireTextAggregator()
# First pair of sentences result = await aggregator.aggregate("First sentence. Second sentence.") assert result == "First sentence."
# Add third sentence - should trigger return of second result = await aggregator.aggregate(" Third sentence.") assert result == "Second sentence." assert "Third sentence." in aggregator.text
@pytest.mark.asyncio() async def test_long_sentence_handling(self): """Test handling of longer sentences.""" aggregator = BlingfireTextAggregator() long_sentence = ( "This is a very long sentence with many words that goes on and on " "and should still be handled correctly by the aggregator." ) result = await aggregator.aggregate(long_sentence) assert result is None assert aggregator.text == long_sentence
@pytest.mark.asyncio() async def test_sentence_boundaries_with_abbreviations(self): """Test sentence detection with abbreviations that contain periods.""" aggregator = BlingfireTextAggregator() result = await aggregator.aggregate("Dr. Smith went to the U.S.A. He had a great time.") assert result == "Dr. Smith went to the U.S.A." assert "He had a great time." in aggregator.text
@pytest.mark.asyncio() async def test_newline_handling(self): """Test handling of text with newlines.""" aggregator = BlingfireTextAggregator() result = await aggregator.aggregate("First line.\nSecond line.") assert result == "First line." assert "Second line." in aggregator.text
@pytest.mark.asyncio() async def test_mixed_sentence_endings(self): """Test text with mixed sentence ending punctuation.""" aggregator = BlingfireTextAggregator() result = await aggregator.aggregate("What time is it? It's 3 PM! That's great.") assert result == "What time is it?" remaining = aggregator.text assert "It's 3 PM!" in remaining assert "That's great." in remaining
@pytest.mark.asyncio() async def test_text_property_consistency(self): """Test that the text property always returns the current buffer state.""" aggregator = BlingfireTextAggregator()
# Initially empty assert aggregator.text == ""
# After adding incomplete sentence await aggregator.aggregate("Hello") assert aggregator.text == "Hello"
# After adding more text await aggregator.aggregate(" world") assert aggregator.text == "Hello world"
# After completing sentence (but no return yet) await aggregator.aggregate(".") assert aggregator.text == "Hello world."
# After triggering sentence return await aggregator.aggregate(" Next sentence.") assert "Next sentence." in aggregator.text assert "Hello world." not in aggregator.text
@pytest.mark.asyncio() async def test_empty_sentences_filtering(self): """Test that empty sentences are properly filtered out.""" aggregator = BlingfireTextAggregator() # Text with multiple periods that might create empty sentences result = await aggregator.aggregate("Hello... world.") assert result is None assert aggregator.text == "Hello... world."123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.# SPDX-License-Identifier: BSD 2-Clause License
"""Unit tests for the CustomView related frames."""
import json
from nvidia_pipecat.frames.custom_view import ( Button, ButtonListBlock, ButtonVariant, HeaderBlock, Hint, HintCarouselBlock, Image, ImageBlock, ImagePosition, ImageWithTextBlock, SelectableOption, SelectableOptionsGridBlock, StartCustomViewFrame, TableBlock, TextBlock, TextInputBlock,)
def test_to_json_empty(): """Tests empty custom view JSON serialization.
Tests: - Empty frame conversion - Minimal configuration - Default values
Raises: AssertionError: If JSON output doesn't match expected format. """ frame = StartCustomViewFrame(action_id="test-action-id") result = frame.to_json() expected_result = {} assert result == json.dumps(expected_result)
def test_to_json_simple(): """Tests simple custom view JSON serialization.
Tests: - Basic block configuration - Header block formatting - Single block conversion
Raises: AssertionError: If JSON output doesn't match expected format. """ frame = StartCustomViewFrame( action_id="test-action-id", blocks=[ HeaderBlock(id="test-header", header="Test Header", level=1), ], ) result = frame.to_json() expected_result = { "blocks": [{"id": "test-header", "type": "header", "data": {"header": "Test Header", "level": 1}}], } assert result == json.dumps(expected_result)
def test_to_json_complex(): """Tests complex custom view JSON serialization.
Tests: - Multiple block types - Image handling - Nested data structures
Raises: AssertionError: If JSON output doesn't match expected format. """ # Sample base64 string for image data sample_base64 = "iVBORw0KGgoAAAANSUhEUgAAA5UAAAC5CAIAAAA3TIxUAADd3"
frame = StartCustomViewFrame( action_id="test-action-id", blocks=[ HeaderBlock(id="test-header", header="Test Header", level=1), TextBlock(id="test-text", text="Test Text"), ImageBlock(id="test-image", image=Image(url="https://example.com/image.jpg")), ImageWithTextBlock( id="test-image-with-text", image=Image(data=sample_base64), text="Test Text", image_position=ImagePosition.LEFT, ), TableBlock(id="test-table", headers=["Header 1", "Header 2"], rows=[["Row 1", "Row 2"]]), HintCarouselBlock( id="test-hint-carousel", hints=[Hint(id="test-hint", name="Test Hint", text="Test Text")] ), ButtonListBlock( id="test-button-list", buttons=[ Button( id="test-button", active=True, toggled=False, variant=ButtonVariant.CONTAINED, text="Test Button", ) ], ), SelectableOptionsGridBlock( id="test-selectable-options-grid", buttons=[ SelectableOption( id="test-option", image=Image(url="https://example.com/image.jpg"), text="Test Option", active=True, toggled=False, ) ], ), TextInputBlock( id="test-input", default_value="Test Default Value", value="Test Value", label="Test Label", input_type="text", ), ], ) result = frame.to_json() expected_result = { "blocks": [ {"id": "test-header", "type": "header", "data": {"header": "Test Header", "level": 1}}, {"id": "test-text", "type": "paragraph", "data": {"text": "Test Text"}}, { "id": "test-image", "type": "image", "data": {"image": {"url": "https://example.com/image.jpg"}}, }, { "id": "test-image-with-text", "type": "paragraph_with_image", "data": { "image": {"data": sample_base64}, "text": "Test Text", "image_position": "left", }, }, { "id": "test-table", "type": "table", "data": {"headers": ["Header 1", "Header 2"], "rows": [["Row 1", "Row 2"]]}, }, { "id": "test-hint-carousel", "type": "hint_carousel", "data": {"hints": [{"name": "Test Hint", "text": "Test Text"}]}, }, { "id": "test-button-list", "type": "button_list", "data": { "buttons": [ { "id": "test-button", "active": True, "toggled": False, "variant": "contained", "text": "Test Button", } ] }, }, { "id": "test-selectable-options-grid", "type": "selectable_options_grid", "data": { "buttons": [ { "id": "test-option", "image": {"url": "https://example.com/image.jpg"}, "text": "Test Option", "active": True, "toggled": False, } ] }, }, { "id": "test-input", "type": "text_input", "data": { "default_value": "Test Default Value", "value": "Test Value", "label": "Test Label", "input_type": "text", }, }, ], } assert result == json.dumps(expected_result)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.# SPDX-License-Identifier: BSD 2-Clause License
"""Unit tests for the ElevenLabsTTSServiceWithEndOfSpeech class."""
import asyncioimport base64import jsonfrom unittest.mock import AsyncMock, patch
import pytestfrom loguru import loggerfrom pipecat.frames.frames import TTSAudioRawFrame, TTSSpeakFrame, TTSStoppedFramefrom pipecat.pipeline.pipeline import Pipelinefrom pipecat.pipeline.task import PipelineTaskfrom websockets.protocol import State
from nvidia_pipecat.services.elevenlabs import ElevenLabsTTSServiceWithEndOfSpeechfrom nvidia_pipecat.utils.logging import setup_default_ace_loggingfrom tests.unit.utils import FrameStorage, ignore_ids, run_interactive_test
setup_default_ace_logging(level="TRACE")
class MockWebSocket: """Mock WebSocket for testing ElevenLabs service.
Attributes: messages_to_return: List of messages to return during testing. sent_messages: List of messages sent through the socket. state: Current WebSocket connection state. close_rcvd: Close frame received flag. close_rcvd_then_sent: Close frame received and sent flag. close_sent: Close frame sent flag. closed: Whether the WebSocket is closed. """
def __init__(self, messages_to_return): """Initialize MockWebSocket.
Args: messages_to_return (list): List of messages to return during testing. """ self.messages_to_return = messages_to_return self.sent_messages = [] self.state = State.OPEN self.close_rcvd = None self.close_rcvd_then_sent = None self.close_sent = None self.closed = False
async def send(self, message: str) -> None: """Sends a message through the mock socket.
Args: message (str): Message to send. """ self.sent_messages.append(json.loads(message))
async def ping(self) -> bool: """Simulates WebSocket heartbeat.
Returns: bool: Always True for testing. """ return True
async def close(self) -> bool: """Closes the mock WebSocket connection.
Returns: bool: Always True for testing. """ self.state = State.CLOSED self.closed = True return True
async def __aiter__(self): """Async iterator for messages.
Yields: str: JSON-encoded message. """ for msg in self.messages_to_return: yield json.dumps(msg) while self.state != State.CLOSED: await asyncio.sleep(1.0) yield "{}"
@pytest.mark.asyncio()async def test_elevenlabs_tts_service_with_end_of_speech(): """Test ElevenLabsTTSServiceWithEndOfSpeech functionality.
Tests: - End-of-speech boundary marker handling - Audio message processing - Alignment message processing - TTSStoppedFrame generation
Raises: AssertionError: If frame processing or timing is incorrect. """ # Test audio data test_audio = b"test_audio_data" test_audio_b64 = base64.b64encode(test_audio).decode()
# Test cases with different message sequences testcases = { "Normal audio with boundary marker": { "frames_to_send": [TTSSpeakFrame("Hello")], "messages": [ { "audio": test_audio_b64, "alignment": { "chars": ["H", "e", "l", "l", "o", "\u200b"], "charStartTimesMs": [0, 3, 7, 9, 11, 12], "charDurationsMs": [3, 4, 2, 2, 1, 1], }, }, ], "expected_frames": [ TTSAudioRawFrame(test_audio, 16000, 1), TTSStoppedFrame(), ], }, "Multiple audio chunks": { "frames_to_send": [TTSSpeakFrame("Test")], "messages": [ { "audio": test_audio_b64, "alignment": { "chars": ["T", "e"], "charStartTimesMs": [0, 3], "charDurationsMs": [3, 4], }, }, { "audio": test_audio_b64, "alignment": { "chars": ["s", "t", "\u200b"], "charStartTimesMs": [7, 9, 11], "charDurationsMs": [2, 2, 1], }, }, ], "expected_frames": [ TTSAudioRawFrame(test_audio, 16000, 1), TTSAudioRawFrame(test_audio, 16000, 1), TTSStoppedFrame(), ], }, }
for tc_name, tc_data in testcases.items(): logger.info(f"Verifying test case: {tc_name}")
# Create mock websocket with test messages mock_websocket = MockWebSocket(tc_data["messages"])
# mock = AsyncMock() # mock.return_value = mock_websocket
with patch("pipecat.services.elevenlabs.tts.websockets.connect", new=AsyncMock()) as mock: mock.return_value = mock_websocket tts_service = ElevenLabsTTSServiceWithEndOfSpeech( api_key="test_api_key", voice_id="test_voice_id", sample_rate=16000, channels=1 )
storage = FrameStorage() pipeline = Pipeline([tts_service, storage])
async def test_routine(task: PipelineTask, test_data=tc_data, s=storage): for frame in test_data["frames_to_send"]: await task.queue_frame(frame) # Wait for all expected frames for expected_frame in test_data["expected_frames"]: await s.wait_for_frame(ignore_ids(expected_frame)) print(f"got frame to be sent {expected_frame}")
# TODO: investigate why we need to cancel here await task.cancel()
await run_interactive_test(pipeline, test_coroutine=test_routine)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.# SPDX-License-Identifier: BSD 2-Clause License
"""Unit tests for action frame creation and manipulation.
This module tests the creation, validation, and comparison of various action frames used for bot and user actions."""
# ruff: noqa: F405
from typing import Any
import pytestfrom pipecat.frames.frames import TextFrame
from nvidia_pipecat.frames.action import * # noqa: F403from nvidia_pipecat.frames.action import ( FinishedFacialGestureBotActionFrame, StartedFacialGestureBotActionFrame, StartFacialGestureBotActionFrame, StopFacialGestureBotActionFrame,)from tests.unit.utils import ignore_ids
def test_action_frame_basic_usage(): """Tests basic action frame functionality.
Tests: - Frame creation with parameters - Action ID propagation - Frame name generation - Frame attribute access
Raises: AssertionError: If frame attributes don't match expected values. """ start_frame = StartFacialGestureBotActionFrame(facial_gesture="wink") action_id = start_frame.action_id
started_frame = StartedFacialGestureBotActionFrame(action_id=action_id) stop_frame = StopFacialGestureBotActionFrame(action_id=action_id) finished_frame = FinishedFacialGestureBotActionFrame(action_id=action_id)
assert started_frame.action_id == action_id assert stop_frame.action_id == action_id assert stop_frame.name == "StopFacialGestureBotActionFrame#0" assert finished_frame.action_id == action_id assert finished_frame.name == "FinishedFacialGestureBotActionFrame#0"
def test_required_parameters(): """Tests parameter validation in frame creation.
Tests: - Required parameter enforcement - Type checking - Error handling
Raises: TypeError: When required parameters are missing. """ with pytest.raises(TypeError): StartedFacialGestureBotActionFrame() # type: ignore
def test_action_frame_existence(): """Tests frame class contract compliance.
Tests: - Frame class initialization - Parameter handling - Action ID validation - Frame type verification
Raises: TypeError: When frame initialization fails. AssertionError: If frame attributes are incorrect. """ actions: dict[str, dict[str, dict[str, Any]]] = { "FacialGestureBotActionFrame": { "Start": {"facial_gesture": "wink"}, "Started": {}, "Stop": {}, "Finished": {}, }, "GestureBotActionFrame": { "Start": {"gesture": "wave"}, "Started": {}, "Stop": {}, "Finished": {}, }, "PostureBotActionFrame": { "Start": {"posture": "listening"}, "Started": {}, "Stop": {}, "Finished": {}, }, "PositionBotActionFrame": { "Start": {"position": "left"}, "Started": {}, "Stop": {}, "Updated": {"position_reached": "left"}, "Finished": {}, }, "AttentionUserActionFrame": { "Started": {"attention_level": "attentive"}, "Updated": {"attention_level": "inattentive"}, "Finished": {}, }, "PresenceUserActionFrame": { "Started": {}, "Finished": {}, }, }
for action_name, frame_type in actions.items(): for f, args in frame_type.items(): if f == "Start": test = globals()[f"{f}{action_name}"](**args) assert test.name else: with pytest.raises(TypeError): test = globals()[f"{f}{action_name}"]()
args["action_id"] = "1234" test = globals()[f"{f}{action_name}"](**args) assert test.action_id == "1234"
def test_frame_comparison_ignoring_ids(): """Tests frame comparison with ID ignoring.
Tests: - Frame equality comparison - ID-independent comparison - Content-based comparison
Raises: AssertionError: If frame comparison results are incorrect. """ a = TextFrame(text="test") b = TextFrame(text="test") c = TextFrame(text="something")
assert a != b assert a == ignore_ids(b) assert a != ignore_ids(c)12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.# SPDX-License-Identifier: BSD 2-Clause License
"""Unit tests for the FacialGestureProviderProcessor."""
import pytestfrom pipecat.frames.frames import ( BotStartedSpeakingFrame, BotStoppedSpeakingFrame, StartInterruptionFrame, UserStoppedSpeakingFrame,)from pipecat.tests.utils import SleepFrame
from nvidia_pipecat.frames.action import StartFacialGestureBotActionFramefrom nvidia_pipecat.processors.gesture_provider import FacialGestureProviderProcessorfrom tests.unit.utils import ignore_ids, run_test
@pytest.mark.asyncio()async def test_gesture_provider_processor_interrupt(): """Test facial gesture generation for bot speech start and interruption.
Tests that the processor generates appropriate facial gestures when receiving BotStartedSpeakingFrame and StartInterruptionFrame events.
The test verifies: - Correct handling of BotStartedSpeakingFrame - Correct handling of StartInterruptionFrame - Generation of "Pensive" facial gesture """ frames_to_send = [ BotStartedSpeakingFrame(), SleepFrame(0.1), StartInterruptionFrame(), ] expected_down_frames = [ ignore_ids(BotStartedSpeakingFrame()), ignore_ids(StartInterruptionFrame()), ignore_ids(StartFacialGestureBotActionFrame(facial_gesture="Pensive")), ]
await run_test( FacialGestureProviderProcessor(probability=1.0), frames_to_send=frames_to_send, expected_down_frames=expected_down_frames, )
@pytest.mark.asyncio()async def test_gesture_provider_processor_bot_finished(): """Test facial gesture processing for bot speech completion.
Tests that the processor handles BotStoppedSpeakingFrame by passing it through without generating additional gestures.
The test verifies: - Correct passthrough of BotStoppedSpeakingFrame - No additional gesture generation """ frames_to_send = [BotStoppedSpeakingFrame()] expected_down_frames = [ ignore_ids(BotStoppedSpeakingFrame()), ]
await run_test( FacialGestureProviderProcessor(probability=1.0), frames_to_send=frames_to_send, expected_down_frames=expected_down_frames, )
@pytest.mark.asyncio()async def test_gesture_provider_processor_tts(): """Test facial gesture processing for interruption events.
Tests that the processor generates a "Taunt" facial gesture when receiving UserStoppedSpeakingFrame events.
The test verifies: - Correct handling of UserStoppedSpeakingFrame - Generation of "Taunt" facial gesture """ frames_to_send = [UserStoppedSpeakingFrame()] expected_down_frames = [ ignore_ids(UserStoppedSpeakingFrame()), ignore_ids(StartFacialGestureBotActionFrame(facial_gesture="Taunt")), ]
await run_test( FacialGestureProviderProcessor(probability=1.0), frames_to_send=frames_to_send, expected_down_frames=expected_down_frames, )123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.# SPDX-License-Identifier: BSD 2-Clause License
"""Unit tests for the user presence frame processor."""
import pytestfrom pipecat.frames.frames import TranscriptionFrame, TTSSpeakFramefrom pipecat.utils.time import time_now_iso8601
from nvidia_pipecat.processors.guardrail import GuardrailProcessorfrom tests.unit.utils import ignore_ids, run_test
@pytest.mark.asyncioasync def test_blocked_word(): """Tests blocking functionality for explicitly blocked words.
Tests that the processor replaces transcription frames containing blocked words with a TTSSpeakFrame containing a rejection message.
Args: None
Returns: None
The test verifies: - Input containing "football" is blocked - Response is replaced with rejection message """ guardrail_bot = GuardrailProcessor(blocked_words=["football"]) frames_to_send = [TranscriptionFrame(text="I love football", user_id="", timestamp=time_now_iso8601())] expected_down_frames = [ignore_ids(TTSSpeakFrame("I am not allowed to answer this question"))]
await run_test(guardrail_bot, frames_to_send=frames_to_send, expected_down_frames=expected_down_frames)
@pytest.mark.asyncioasync def test_non_blocked_word(): """Tests passthrough of allowed words.
Tests that the processor allows transcription frames that don't contain any blocked words to pass through unchanged.
Args: None
Returns: None
The test verifies: - Input without blocked words passes through - Frame content remains unchanged """ guardrail_bot = GuardrailProcessor(blocked_words=["football"]) timestamp = time_now_iso8601() frames_to_send = [TranscriptionFrame(text="Tell me about Pasta", user_id="", timestamp=timestamp)] expected_down_frames = [ignore_ids(TranscriptionFrame(text="Tell me about Pasta", user_id="", timestamp=timestamp))]
await run_test(guardrail_bot, frames_to_send=frames_to_send, expected_down_frames=expected_down_frames)
@pytest.mark.asyncioasync def test_substring_blocked_word(): """Tests substring matching behavior for blocked words.
Tests that the processor only blocks exact word matches and allows words that contain blocked words as substrings.
Args: None
Returns: None
The test verifies: - Words containing blocked words as substrings are allowed - Frame content remains unchanged """ guardrail_bot = GuardrailProcessor(blocked_words=["foot"]) timestamp = time_now_iso8601() frames_to_send = [TranscriptionFrame(text="I love football", user_id="", timestamp=timestamp)] expected_down_frames = [ignore_ids(TranscriptionFrame(text="I love football", user_id="", timestamp=timestamp))]
await run_test(guardrail_bot, frames_to_send=frames_to_send, expected_down_frames=expected_down_frames)
@pytest.mark.asyncioasync def test_no_blocked_word(): """Tests default behavior with no blocked words configured.
Tests that the processor allows all transcription frames to pass through when no blocked words are specified.
Args: None
Returns: None
The test verifies: - All input passes through when no words are blocked - Frame content remains unchanged """ guardrail_bot = GuardrailProcessor() timestamp = time_now_iso8601() frames_to_send = [TranscriptionFrame(text="What is your name", user_id="", timestamp=timestamp)] expected_down_frames = [ignore_ids(TranscriptionFrame(text="What is your name", user_id="", timestamp=timestamp))]
await run_test(guardrail_bot, frames_to_send=frames_to_send, expected_down_frames=expected_down_frames)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.# SPDX-License-Identifier: BSD 2-Clause License
"""Unit tests for the MessageBroker."""
import asynciofrom datetime import timedelta
import pytest
from nvidia_pipecat.utils.message_broker import LocalQueueMessageBroker
async def send_task(mb: LocalQueueMessageBroker) -> bool: """Send test messages to the message broker.
Args: mb: The LocalQueueMessageBroker instance to send messages to.
Returns: bool: True if all messages were sent successfully. """ for i in range(5): if i % 2 == 0: await mb.send_message("first", f"message {i}") else: await mb.send_message("second", f"message {i}") await asyncio.sleep(0.08) return True
async def receive_task(mb: LocalQueueMessageBroker) -> list[str]: """Receive messages from the message broker.
Args: mb: The LocalQueueMessageBroker instance to receive messages from.
Returns: list[str]: List of received message data. """ result: list[str] = [] while len(result) < 5: messages = await mb.receive_messages(timeout=timedelta(seconds=0.1)) for _, message_data in messages: result.append(message_data) return result
@pytest.mark.asyncio()async def test_local_queue_message_broker(): """Tests basic concurrent send/receive functionality.
Tests that messages can be successfully sent and received when send and receive tasks run concurrently.
The test verifies: - All messages are received in order - Send operation completes successfully - Messages are correctly routed between channels """ mb = LocalQueueMessageBroker(channels=["first", "second"]) task_1 = asyncio.create_task(send_task(mb)) task_2 = asyncio.create_task(receive_task(mb))
results = await asyncio.gather(task_1, task_2)
assert results == [True, ["message 0", "message 1", "message 2", "message 3", "message 4"]]
@pytest.mark.asyncio()async def test_local_queue_message_broker_receive_first(): """Tests message delivery when receive starts before send.
Tests that no messages are lost when the receive task is started before any messages are sent.
The test verifies: - All messages are received in order despite delayed send - Send operation completes successfully - No messages are lost due to timing """ mb = LocalQueueMessageBroker(channels=["first", "second"]) task_2 = asyncio.create_task(receive_task(mb)) await asyncio.sleep(0.2) task_1 = asyncio.create_task(send_task(mb))
results = await asyncio.gather(task_1, task_2)
assert results == [True, ["message 0", "message 1", "message 2", "message 3", "message 4"]]
@pytest.mark.asyncio()async def test_local_queue_message_broker_send_first(): """Tests message delivery when send completes before receive starts.
Tests that no messages are lost when all messages are sent before the receive task begins.
The test verifies: - All messages are received in order despite delayed receive - Send operation completes successfully - Messages are properly queued until received """ mb = LocalQueueMessageBroker(channels=["first", "second"]) task_1 = asyncio.create_task(send_task(mb)) await asyncio.sleep(0.5) task_2 = asyncio.create_task(receive_task(mb))
results = await asyncio.gather(task_1, task_2)
assert results == [True, ["message 0", "message 1", "message 2", "message 3", "message 4"]]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.# SPDX-License-Identifier: BSD 2-Clause License
"""Unit tests for the Nvidia Aggregators."""
import pytestfrom pipecat.frames.frames import ( LLMFullResponseEndFrame, LLMFullResponseStartFrame, LLMMessagesFrame, StartInterruptionFrame, TextFrame, TranscriptionFrame, UserStartedSpeakingFrame, UserStoppedSpeakingFrame,)from pipecat.pipeline.pipeline import Pipelinefrom pipecat.processors.aggregators.openai_llm_context import OpenAILLMContext, OpenAILLMContextFramefrom pipecat.tests.utils import SleepFramefrom pipecat.tests.utils import run_test as run_pipecat_testfrom pipecat.utils.time import time_now_iso8601
from nvidia_pipecat.frames.riva import RivaInterimTranscriptionFramefrom nvidia_pipecat.processors.nvidia_context_aggregator import ( NvidiaUserContextAggregator, create_nvidia_context_aggregator,)
@pytest.mark.asyncio()async def test_normal_flow(): """Test the normal flow of user and assistant interactions with interim transcriptions enabled.
Tests the sequence of events from user speech start through assistant response, verifying proper handling of interim and final transcriptions.
The test verifies: - User speech start frame handling - Interim transcription processing - Final transcription handling - User speech stop frame handling - Assistant response processing - Context updates at each stage """ messages = [] context = OpenAILLMContext(messages) context_aggregator = create_nvidia_context_aggregator(context, send_interims=True)
pipeline = Pipeline([context_aggregator.user(), context_aggregator.assistant()]) messages.append({"role": "system", "content": "This is system prompt"}) # Test Case 1: Normal flow with UserStartedSpeakingFrame first frames_to_send = [ UserStartedSpeakingFrame(), LLMMessagesFrame(messages), RivaInterimTranscriptionFrame("Hello", "", time_now_iso8601(), None, stability=1.0), TranscriptionFrame("Hello User Aggregator!", 1, 2), SleepFrame(0.1), UserStoppedSpeakingFrame(), SleepFrame(0.1), ] # Assistant response frames_to_send.extend( [ LLMFullResponseStartFrame(), TextFrame("Hello Assistant Aggregator!"), LLMFullResponseEndFrame(), ] ) expected_down_frames = [ UserStartedSpeakingFrame, StartInterruptionFrame, StartInterruptionFrame, OpenAILLMContextFrame, # From first interim UserStoppedSpeakingFrame, OpenAILLMContextFrame, ] await run_pipecat_test( pipeline, frames_to_send=frames_to_send, expected_down_frames=expected_down_frames, )
# Verify final context state assert context_aggregator.user().context.get_messages() == [ {"role": "user", "content": "Hello User Aggregator!"}, {"role": "assistant", "content": "Hello Assistant Aggregator!"}, ]
@pytest.mark.asyncio()async def test_user_speaking_frame_delay_cases(): """Test handling of transcription frames that arrive before UserStartedSpeakingFrame.
Tests edge cases around transcription frame timing relative to the UserStartedSpeakingFrame.
The test verifies: - Interim frames before UserStartedSpeakingFrame are ignored - Low stability interim frames are ignored - Only processes transcriptions after UserStartedSpeakingFrame - Context is updated correctly for valid frames """ messages = [] context = OpenAILLMContext(messages) context_aggregator = create_nvidia_context_aggregator(context, send_interims=True)
pipeline = Pipeline([context_aggregator.user(), context_aggregator.assistant()]) messages.append({"role": "system", "content": "This is system prompt"})
# Test Case 2: RivaInterimTranscriptionFrames before UserStartedSpeakingFrame frames_to_send = [ RivaInterimTranscriptionFrame( "Testing", "", time_now_iso8601(), None, stability=0.5 ), # Should be ignored (low stability) RivaInterimTranscriptionFrame( "Testing delayed", "", time_now_iso8601(), None, stability=1.0 ), # Should be ignored (no UserStartedSpeakingFrame yet) SleepFrame(0.5), UserStartedSpeakingFrame(), RivaInterimTranscriptionFrame( "Testing after start", "", time_now_iso8601(), None, stability=1.0 ), # Should be processed TranscriptionFrame("Testing after start complete", 1, 2), SleepFrame(0.1), UserStoppedSpeakingFrame(), SleepFrame(0.1), ]
# Assistant response frames_to_send.extend( [ LLMFullResponseStartFrame(), TextFrame("Hello Assistant Aggregator!"), LLMFullResponseEndFrame(), ] ) expected_down_frames = [ UserStartedSpeakingFrame, StartInterruptionFrame, StartInterruptionFrame, OpenAILLMContextFrame, # from first interim after UserStartedSpeakingFrame UserStoppedSpeakingFrame, OpenAILLMContextFrame, ]
await run_pipecat_test( pipeline, frames_to_send=frames_to_send, expected_down_frames=expected_down_frames, )
# Verify final context state assert context_aggregator.user().context.get_messages() == [ {"role": "user", "content": "Testing after start complete"}, {"role": "assistant", "content": "Hello Assistant Aggregator!"}, ]
@pytest.mark.asyncio()async def test_multiple_interims_with_final_transcription(): """Test handling of multiple interim transcription frames followed by a final transcription.
Tests the processing of a sequence of interim transcriptions followed by a final transcription.
The test verifies: - Multiple interim transcriptions are processed correctly - Final transcription properly overwrites previous interims - Context updates occur for each valid frame - Message history maintains correct order """ messages = [] context = OpenAILLMContext(messages) context_aggregator = create_nvidia_context_aggregator(context, send_interims=True)
pipeline = Pipeline([context_aggregator.user(), context_aggregator.assistant()]) messages.append({"role": "system", "content": "This is system prompt"})
# Test Case 3: Multiple interim frames with final transcription frames_to_send = [ UserStartedSpeakingFrame(), RivaInterimTranscriptionFrame("Hello", "", time_now_iso8601(), None, stability=1.0), RivaInterimTranscriptionFrame("Hello Again", "", time_now_iso8601(), None, stability=1.0), RivaInterimTranscriptionFrame("Hello Again User", "", time_now_iso8601(), None, stability=1.0), TranscriptionFrame("Hello Again User Aggregator!", 1, 2), SleepFrame(0.1), UserStoppedSpeakingFrame(), SleepFrame(0.1), ]
# Assistant response frames_to_send.extend( [ LLMFullResponseStartFrame(), TextFrame("Hello Assistant Aggregator!"), LLMFullResponseEndFrame(), ] ) expected_down_frames = [ UserStartedSpeakingFrame, StartInterruptionFrame, StartInterruptionFrame, StartInterruptionFrame, StartInterruptionFrame, OpenAILLMContextFrame, # From final transcription UserStoppedSpeakingFrame, OpenAILLMContextFrame, ]
await run_pipecat_test( pipeline, frames_to_send=frames_to_send, expected_down_frames=expected_down_frames, )
# Verify final context state assert context_aggregator.user().context.get_messages() == [ {"role": "user", "content": "Hello Again User Aggregator!"}, {"role": "assistant", "content": "Hello Assistant Aggregator!"}, ]
@pytest.mark.asyncio()async def test_transcription_after_user_stopped_speaking(): """Tests handling of late transcription frames.
Tests behavior when transcription frames arrive after UserStoppedSpeakingFrame.
The test verifies: - Late transcriptions are still processed - Context is updated with final transcription - Assistant responses are handled correctly - Message history maintains proper sequence """ messages = [] context = OpenAILLMContext(messages) context_aggregator = create_nvidia_context_aggregator(context, send_interims=True)
pipeline = Pipeline([context_aggregator.user(), context_aggregator.assistant()]) messages.append({"role": "system", "content": "This is system prompt"})
# Test Case 4: TranscriptionFrame after UserStoppedSpeakingFrame frames_to_send = [ UserStartedSpeakingFrame(), RivaInterimTranscriptionFrame("Late", "", time_now_iso8601(), None, stability=1.0), SleepFrame(0.1), UserStoppedSpeakingFrame(), SleepFrame(0.1), TranscriptionFrame("Late transcription!", 1, 2), SleepFrame(0.1), ]
# Assistant response frames_to_send.extend( [ LLMFullResponseStartFrame(), TextFrame("Hello Assistant Aggregator!"), LLMFullResponseEndFrame(), ] )
expected_down_frames = [ UserStartedSpeakingFrame, StartInterruptionFrame, OpenAILLMContextFrame, # From first interim UserStoppedSpeakingFrame, StartInterruptionFrame, OpenAILLMContextFrame, # From final after UserStoppedSpeakingFrame OpenAILLMContextFrame, # From assistant response ]
await run_pipecat_test( pipeline, frames_to_send=frames_to_send, expected_down_frames=expected_down_frames, )
# Verify final context state assert context_aggregator.user().context.get_messages() == [ {"role": "user", "content": "Late transcription!"}, {"role": "assistant", "content": "Hello Assistant Aggregator!"}, ]
@pytest.mark.asyncio()async def test_no_interim_frames(): """Tests behavior when interim frames are disabled.
Tests the aggregator's handling of transcriptions when send_interims=False.
The test verifies: - Interim frames are ignored - Only final transcription is processed - System prompts are preserved - Context updates occur only for final transcription - Assistant responses are processed correctly """ messages = [{"role": "system", "content": "This is system prompt"}] context = OpenAILLMContext(messages) context_aggregator = create_nvidia_context_aggregator(context, send_interims=False) pipeline = Pipeline([context_aggregator.user(), context_aggregator.assistant()])
frames_to_send = [ UserStartedSpeakingFrame(), LLMMessagesFrame(messages), # These interim frames should be ignored due to send_interims=False RivaInterimTranscriptionFrame("Hello", "", time_now_iso8601(), None, stability=1.0), RivaInterimTranscriptionFrame("Hello there", "", time_now_iso8601(), None, stability=1.0), RivaInterimTranscriptionFrame("Hello there user", "", time_now_iso8601(), None, stability=1.0), # Only the final transcription should be processed TranscriptionFrame("Hello there user final!", 1, 2), SleepFrame(0.1), UserStoppedSpeakingFrame(), SleepFrame(0.1), # Assistant response LLMFullResponseStartFrame(), TextFrame("Hello from assistant!"), LLMFullResponseEndFrame(), ]
expected_down_frames = [ UserStartedSpeakingFrame, StartInterruptionFrame, OpenAILLMContextFrame, # Only from final transcription UserStoppedSpeakingFrame, OpenAILLMContextFrame, # From assistant response ]
await run_pipecat_test( pipeline, frames_to_send=frames_to_send, expected_down_frames=expected_down_frames, )
# Verify final context state assert context_aggregator.user().context.get_messages() == [ {"role": "system", "content": "This is system prompt"}, {"role": "user", "content": "Hello there user final!"}, {"role": "assistant", "content": "Hello from assistant!"}, ]
@pytest.mark.asyncio()async def test_get_truncated_context(): """Tests context truncation functionality.
Tests the get_truncated_context() method of NvidiaUserContextAggregator with a specified chat history limit.
Args: None
Returns: None
The test verifies: - Context is truncated to specified limit - System prompt is preserved - Most recent messages are retained - Message order is maintained """ messages = [ {"role": "system", "content": "This is system prompt"}, {"role": "user", "content": "Hi, there!"}, {"role": "assistant", "content": "Hello, how may I assist you?"}, {"role": "user", "content": "How to be more productive?"}, {"role": "assistant", "content": "Priotize the tasks, make a list..."}, {"role": "user", "content": "What is metaverse?"}, { "role": "assistant", "content": "The metaverse is envisioned as a digital ecosystem built on virtual 3D technology", }, { "role": "assistant", "content": "It leverages 3D technology and digital" "representation for creating virtual environments and user experiences", }, {"role": "user", "content": "thanks, Bye!"}, ] context = OpenAILLMContext(messages) user = NvidiaUserContextAggregator(context=context, chat_history_limit=2) truncated_context = await user.get_truncated_context() assert truncated_context.get_messages() == [ {"role": "system", "content": "This is system prompt"}, {"role": "user", "content": "What is metaverse?"}, { "role": "assistant", "content": "The metaverse is envisioned as a digital ecosystem built on virtual 3D technology", }, { "role": "assistant", "content": "It leverages 3D technology and digital" "representation for creating virtual environments and user experiences", }, {"role": "user", "content": "thanks, Bye!"}, ]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.# SPDX-License-Identifier: BSD 2-Clause License
"""Unit tests for the NvidiaLLMService.
This module contains tests for the NvidiaLLMService class, focusing on core functionalities:- Think token filtering (including split tag handling)- Mistral message preprocessing- Token usage tracking- LLM responses and function calls"""
from unittest.mock import DEFAULT, AsyncMock, patch
import pytestfrom pipecat.frames.frames import LLMTextFramefrom pipecat.metrics.metrics import LLMTokenUsagefrom pipecat.processors.aggregators.openai_llm_context import OpenAILLMContext
from nvidia_pipecat.services.nvidia_llm import NvidiaLLMService
# Custom mocks that mimic OpenAI classes without inheriting from themclass MockCompletionUsage: """Mock for CompletionUsage that mimics the structure."""
def __init__(self, prompt_tokens, completion_tokens, total_tokens): """Initialize with token usage counts.
Args: prompt_tokens: Number of tokens in the prompt completion_tokens: Number of tokens in the completion total_tokens: Total number of tokens """ self.prompt_tokens = prompt_tokens self.completion_tokens = completion_tokens self.total_tokens = total_tokens
class MockChoiceDelta: """Mock for ChoiceDelta that mimics the structure."""
def __init__(self, content=None, tool_calls=None): """Initialize with optional content and tool calls.
Args: content: The text content of the delta tool_calls: List of tool calls in the delta """ self.content = content self.tool_calls = tool_calls self.function_call = None self.role = None
class MockChoice: """Mock for Choice that mimics the structure."""
def __init__(self, delta, index=0, finish_reason=None): """Initialize with delta, index, and finish reason.
Args: delta: The delta containing content or tool calls index: The index of this choice finish_reason: Reason for finishing generation """ self.delta = delta self.index = index self.finish_reason = finish_reason
class MockChatCompletionChunk: """Mock for ChatCompletionChunk that mimics the structure."""
def __init__(self, content=None, usage=None, id="mock-id", tool_calls=None): """Initialize a mock of ChatCompletionChunk.
Args: content: The text content in the chunk usage: Token usage information id: Chunk identifier tool_calls: Any tool calls in the chunk """ self.id = id self.model = "mock-model" self.object = "chat.completion.chunk" self.created = 1234567890 self.usage = usage
if tool_calls: self.choices = [MockChoice(MockChoiceDelta(tool_calls=tool_calls))] else: self.choices = [MockChoice(MockChoiceDelta(content=content))]
class MockToolCall: """Mock for ToolCall."""
def __init__(self, id="tool-id", function=None, index=0, type="function"): """Initialize a tool call.
Args: id: Tool call identifier function: The function being called index: Index of this tool call type: Type of tool call """ self.id = id self.function = function self.index = index self.type = type
class MockFunction: """Mock for Function in a tool call."""
def __init__(self, name="", arguments=""): """Initialize a function with name and arguments.
Args: name: Name of the function arguments: JSON string of function arguments """ self.name = name self.arguments = arguments
class MockAsyncStream: """Mock implementation of AsyncStream for testing."""
def __init__(self, chunks): """Initialize with a list of chunks to yield.
Args: chunks: List of chunks to return when iterating """ self.chunks = chunks
def __aiter__(self): """Return self as an async iterator.""" return self
async def __anext__(self): """Return the next chunk or raise StopAsyncIteration.""" if not self.chunks: raise StopAsyncIteration return self.chunks.pop(0)
@pytest.mark.asyncioasync def test_mistral_message_preprocessing(): """Test the Mistral message preprocessing functionality.""" service = NvidiaLLMService(api_key="test_api_key", mistral_model_support=True)
# Test with alternating roles (already valid) messages = [ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Hello"}, {"role": "assistant", "content": "Hi there!"}, {"role": "user", "content": "How are you?"}, ] processed = service._preprocess_messages_for_mistral(messages) assert len(processed) == len(messages) # No changes needed
# Test with consecutive messages from same role messages = [ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Hello"}, {"role": "user", "content": "How are you?"}, ] processed = service._preprocess_messages_for_mistral(messages) assert len(processed) == 2 # System + combined user assert processed[1]["role"] == "user" assert processed[1]["content"] == "Hello How are you?"
# Test with system message at end messages = [ {"role": "user", "content": "Hello"}, {"role": "system", "content": "You are a helpful assistant."}, ] processed = service._preprocess_messages_for_mistral(messages) assert len(processed) == 2 # User + system assert processed[0]["role"] == "user" assert processed[1]["role"] == "system"
@pytest.mark.asyncioasync def test_filter_think_token_simple(): """Test the basic think token filtering functionality.""" service = NvidiaLLMService(api_key="test_api_key", filter_think_tokens=True) service._reset_think_filter_state()
# Test with simple think content followed by real content content = "I'm thinking about the answer</think>This is the actual response" filtered = service._filter_think_token(content) assert filtered == "This is the actual response" assert service._seen_end_tag is True
# Subsequent content should pass through untouched more_content = " and some more text" filtered = service._filter_think_token(more_content) assert filtered == " and some more text"
@pytest.mark.asyncioasync def test_filter_think_token_split(): """Test the think token filtering with split tags.""" service = NvidiaLLMService(api_key="test_api_key", filter_think_tokens=True) service._reset_think_filter_state()
# First part with beginning of tag content1 = "Let me think about this problem<" filtered1 = service._filter_think_token(content1) assert filtered1 == "" # No output yet assert service._partial_tag_buffer == "<" # Partial tag saved
# Second part with rest of tag and response content2 = "/think>Here's the answer" filtered2 = service._filter_think_token(content2) assert filtered2 == "Here's the answer" # Output after tag assert service._seen_end_tag is True
@pytest.mark.asyncioasync def test_filter_think_token_no_tag(): """Test what happens when no think tag is found.""" service = NvidiaLLMService(api_key="test_api_key", filter_think_tokens=True) service._reset_think_filter_state()
# Add some content in multiple chunks filtered1 = service._filter_think_token("This is a response") filtered2 = service._filter_think_token(" with no think tag") # Verify filtering behavior assert filtered1 == filtered2 == "" # No output during filtering assert service._thinking_aggregation == "This is a response with no think tag" # Test end-of-processing behavior service.push_frame = AsyncMock() await service.push_frame(LLMTextFrame(service._thinking_aggregation)) service._reset_think_filter_state() # Verify results service.push_frame.assert_called_once() assert service.push_frame.call_args.args[0].text == "This is a response with no think tag" assert service._thinking_aggregation == "" # State was reset
@pytest.mark.asyncioasync def test_token_usage_tracking(): """Test the token usage tracking functionality.""" service = NvidiaLLMService(api_key="test_api_key") service._is_processing = True
# Test initial accumulation of prompt tokens tokens1 = LLMTokenUsage(prompt_tokens=10, completion_tokens=0, total_tokens=10) await service.start_llm_usage_metrics(tokens1) assert service._prompt_tokens == 10 assert service._has_reported_prompt_tokens is True
# Test incremental completion tokens tokens2 = LLMTokenUsage(prompt_tokens=10, completion_tokens=5, total_tokens=15) await service.start_llm_usage_metrics(tokens2) assert service._completion_tokens == 5
# Test more completion tokens tokens3 = LLMTokenUsage(prompt_tokens=10, completion_tokens=8, total_tokens=18) await service.start_llm_usage_metrics(tokens3) assert service._completion_tokens == 8
# Test reporting duplicate prompt tokens tokens4 = LLMTokenUsage(prompt_tokens=10, completion_tokens=10, total_tokens=20) await service.start_llm_usage_metrics(tokens4) assert service._prompt_tokens == 10 # Unchanged assert service._completion_tokens == 10
@pytest.mark.asyncioasync def test_process_context_with_think_filtering(): """Test the full processing with think token filtering.""" with patch.multiple( NvidiaLLMService, create_client=DEFAULT, _stream_chat_completions=DEFAULT, start_ttfb_metrics=DEFAULT, stop_ttfb_metrics=DEFAULT, push_frame=DEFAULT, ) as mocks: service = NvidiaLLMService(api_key="test_api_key", filter_think_tokens=True) mock_push_frame = mocks["push_frame"]
# Setup mock stream chunks = [ MockChatCompletionChunk(content="Thinking<"), MockChatCompletionChunk(content="/think>Real content"), MockChatCompletionChunk(content=" continues"), ] mocks["_stream_chat_completions"].return_value = MockAsyncStream(chunks)
# Process context context = OpenAILLMContext(messages=[{"role": "user", "content": "Test query"}]) await service._process_context(context)
# Verify frame content - empty frames during thinking, content after tag frames = [call.args[0].text for call in mock_push_frame.call_args_list] assert frames == ["", "Real content", " continues"]
@pytest.mark.asyncioasync def test_process_context_with_function_calls(): """Test handling of function calls from LLM.""" with ( patch.object(NvidiaLLMService, "create_client"), patch.object(NvidiaLLMService, "_stream_chat_completions") as mock_stream, patch.object(NvidiaLLMService, "has_function") as mock_has_function, patch.object(NvidiaLLMService, "call_function") as mock_call_function, ): service = NvidiaLLMService(api_key="test_api_key")
# Create tool call chunks that come in parts tool_call1 = MockToolCall( id="call1", function=MockFunction(name="get_weather", arguments='{"location"'), index=0 )
tool_call2 = MockToolCall(id="call1", function=MockFunction(name="", arguments=':"New York"}'), index=0)
# Create chunks with tool calls chunk1 = MockChatCompletionChunk(tool_calls=[tool_call1], id="chunk1") chunk2 = MockChatCompletionChunk(tool_calls=[tool_call2], id="chunk2")
mock_stream.return_value = MockAsyncStream([chunk1, chunk2]) mock_has_function.return_value = True mock_call_function.return_value = None
# Process a context context = OpenAILLMContext(messages=[{"role": "user", "content": "What's the weather in New York?"}]) await service._process_context(context)
# Verify function was called with combined arguments mock_call_function.assert_called_once() args = mock_call_function.call_args.kwargs assert args["function_name"] == "get_weather" assert args["arguments"] == {"location": "New York"} assert args["tool_call_id"] == "call1"
@pytest.mark.asyncioasync def test_process_context_with_mistral_preprocessing(): """Test processing context with Mistral message preprocessing.""" with ( patch.object(NvidiaLLMService, "create_client"), patch.object(NvidiaLLMService, "_stream_chat_completions") as mock_stream, ): service = NvidiaLLMService(api_key="test_api_key", mistral_model_support=True)
# Setup mock stream chunks = [MockChatCompletionChunk(content="I am a response")] mock_stream.return_value = MockAsyncStream(chunks)
# Test 1: Combining consecutive user messages context = OpenAILLMContext( messages=[ {"role": "system", "content": "You are helpful."}, {"role": "user", "content": "Hello"}, {"role": "user", "content": "How are you?"}, ] ) await service._process_context(context)
# Verify messages were combined processed_messages = context.get_messages() assert len(processed_messages) == 2 # System + combined user assert processed_messages[1]["role"] == "user" assert processed_messages[1]["content"] == "Hello How are you?"
# Verify stream was called (normal processing) mock_stream.assert_called_once()
# Test 2: System message only - should skip processing mock_stream.reset_mock() system_only_context = OpenAILLMContext( messages=[ {"role": "system", "content": "You are helpful."}, ] ) await service._process_context(system_only_context)
# Verify that stream was not called (processing skipped) mock_stream.assert_not_called()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.# SPDX-License-Identifier: BSD 2-Clause License
"""Unit tests for the NvidiaRAGService processor."""
import httpximport pytestfrom loguru import loggerfrom pipecat.frames.frames import ErrorFrame, LLMMessagesFrame, TextFramefrom pipecat.pipeline.pipeline import Pipelinefrom pipecat.pipeline.task import PipelineTaskfrom pipecat.processors.aggregators.openai_llm_context import OpenAILLMContext
from nvidia_pipecat.frames.nvidia_rag import NvidiaRAGCitation, NvidiaRAGCitationsFrame, NvidiaRAGSettingsFramefrom nvidia_pipecat.services.nvidia_rag import NvidiaRAGServicefrom tests.unit.utils import FrameStorage, ignore_ids, run_interactive_test, run_test
class MockResponse: """Mock response class for testing HTTP responses.
Attributes: json: The JSON response data. """
def __init__(self, json): """Initialize MockResponse with JSON data.
Args: json: The JSON response data. """ self.json = json
async def aclose(self): """Mock aclose method.""" pass
async def aiter_lines(self): """Simulate chunk iteration for streaming response.
Yields: tuple: A tuple containing data and None. """ yield self.json
@pytest.mark.asyncioasync def test_nvidia_rag_service(mocker): """Test NvidiaRAGService functionality with various test cases.
Tests different RAG service behaviors including successful responses, citation handling, and error conditions.
Args: mocker: Pytest mocker fixture for mocking HTTP responses.
The test verifies: - Successful responses without citations - Successful responses with citations - Error handling for empty collection names - Error handling for empty queries - Error handling for incorrect message roles """ testcases = { "Success without citations": { "collection_name": "collection123", "messages": [ { "role": "system", "content": "You are a helpful Large Language Model. " "Your goal is to demonstrate your capabilities in a succinct way. " "Your output will be converted to audio so don't include special characters in your answers. " "Respond to what the user said in a creative and helpful way.", }, ], "response_json": 'data: {"id":"a886cc44-e2ce-4ea3-95f0-9ffb1171adb1",' '"choices":[{"index":0,"message":{"role":"assistant","content":"this is rag response content"},' '"delta":{"role":"assistant","content":""},"finish_reason":"[DONE]"}]}', "result_frame": TextFrame("this is rag response content"), }, "Success with citations": { "collection_name": "collection123", "messages": [ { "role": "system", "content": "You are a helpful Large Language Model. " "Your goal is to demonstrate your capabilities in a succinct way. " "Your output will be converted to audio so don't include special characters in your answers. " "Respond to what the user said in a creative and helpful way.", }, ], "response_json": 'data: {"id":"a886cc44-e2ce-4ea3-95f0-9ffb1171adb1",' '"choices":[{"index":0,"message":{"role":"assistant","content":"this is rag response content"},' '"delta":{"role":"assistant","content":""},"finish_reason":"[DONE]"}], "citations":{"total_results":0,' '"results":[{"document_id": "", "content": "this is rag citation content", "document_type": "text",' ' "document_name": "", "metadata": "", "score": 0.0}]}}', "result_frame": NvidiaRAGCitationsFrame( [ NvidiaRAGCitation( document_id="", document_type="text", metadata="", score=0.0, document_name="", content=b"this is rag citation content", ) ] ), }, "Fail due to empty collection name": { "collection_name": "", "messages": [ { "role": "system", "content": "You are a helpful Large Language Model. " "Your goal is to demonstrate your capabilities in a succinct way. " "Your output will be converted to audio so don't include special characters in your answers. " "Respond to what the user said in a creative and helpful way.", }, ], "result_frame": ErrorFrame( "An error occurred in http request to RAG endpoint, Error: No query or collection name is provided.." ), }, "Fail due to empty query": { "collection_name": "collection123", "messages": [ { "role": "system", "content": "", }, ], "result_frame": ErrorFrame( "An error occurred in http request to RAG endpoint, Error: No query or collection name is provided.." ), }, "Fail due to incorrect role": { "collection_name": "collection123", "messages": [ { "role": "tool", "content": "", }, ], "result_frame": ErrorFrame( "An error occurred in http request to RAG endpoint, Error: Unexpected role tool found!" ), }, }
for tc_name, tc_data in testcases.items(): logger.info(f"Verifying test case: {tc_name}")
resp = None if "response_json" in tc_data: resp = MockResponse(tc_data["response_json"]) else: resp = MockResponse("{}")
mocker.patch("httpx.AsyncClient.post", return_value=resp)
rag = NvidiaRAGService(collection_name=tc_data["collection_name"]) storage1 = FrameStorage() storage2 = FrameStorage() context_aggregator = rag.create_context_aggregator(OpenAILLMContext(tc_data["messages"]))
pipeline = Pipeline([context_aggregator.user(), storage1, rag, storage2, context_aggregator.assistant()])
async def test_routine(task: PipelineTask, test_data=tc_data, s1=storage1, s2=storage2): await task.queue_frame(LLMMessagesFrame(test_data["messages"]))
# Wait for the result frame if "ErrorFrame" in test_data["result_frame"].name: await s1.wait_for_frame(ignore_ids(test_data["result_frame"])) else: await s2.wait_for_frame(ignore_ids(test_data["result_frame"]))
await run_interactive_test(pipeline, test_coroutine=test_routine)
# Verify the frames in storage1 for frame_history_entry in storage1.history: if frame_history_entry.frame.name.startswith("ErrorFrame"): assert frame_history_entry.frame == ignore_ids(tc_data["result_frame"])
# Verify the frames in storage2 for frame_history_entry in storage2.history: if ( frame_history_entry.frame.name.startswith("TextFrame") and tc_data["result_frame"].__str__().startswith("TextFrame") or frame_history_entry.frame.name.startswith("NvidiaRAGCitationsFrame") and tc_data["result_frame"].__str__().startswith("NvidiaRAGCitationsFrame") ): assert frame_history_entry.frame == ignore_ids(tc_data["result_frame"])
@pytest.mark.asyncioasync def test_rag_service_sharing_session(): """Test session sharing behavior between NvidiaRAGService instances.
Tests the HTTP client session management across multiple RAG service instances.
The test verifies: - Session sharing between instances with same parameters - Separate session handling for custom sessions - Proper session cleanup """ rags = [] rags.append(NvidiaRAGService(collection_name="collection_1")) rags.append(NvidiaRAGService(collection_name="collection_1"))
initial_session = rags[1].shared_session
for rag in rags: assert rag.shared_session is initial_session
new_session = httpx.AsyncClient() rags.append(NvidiaRAGService(collection_name="collection_1", session=new_session))
assert rags[0].shared_session is initial_session assert rags[1].shared_session is initial_session assert rags[2].shared_session is new_session
await new_session.aclose() for r in rags: await r.cleanup()
@pytest.mark.asyncioasync def test_nvidia_rag_settings_frame_update(mocker): """Tests NvidiaRAGService settings update functionality.
Tests the processing of NvidiaRAGSettingsFrame for dynamic configuration updates.
Args: mocker: Pytest mocker fixture for mocking HTTP responses.
The test verifies: - Collection name updates - Server URL updates - Settings frame propagation """ mocker.patch("httpx.AsyncClient.post", return_value="")
rag_settings_frame = NvidiaRAGSettingsFrame( settings={"collection_name": "nvidia_blogs", "rag_server_url": "http://10.41.23.247:8081"} ) rag = NvidiaRAGService(collection_name="collection123")
frames_to_send = [rag_settings_frame] expected_down_frames = [rag_settings_frame]
await run_test( rag, frames_to_send=frames_to_send, expected_down_frames=expected_down_frames, )
assert rag.collection_name == "nvidia_blogs" assert rag.rag_server_url == "http://10.41.23.247:8081"12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.# SPDX-License-Identifier: BSD 2-Clause License
"""Unit tests for the Nvidia TTS Response Cacher."""
import pytestfrom pipecat.frames.frames import ( LLMFullResponseEndFrame, LLMFullResponseStartFrame, TTSAudioRawFrame, UserStartedSpeakingFrame, UserStoppedSpeakingFrame,)from pipecat.pipeline.pipeline import Pipelinefrom pipecat.tests.utils import run_test as run_pipecat_test
from nvidia_pipecat.processors.nvidia_context_aggregator import NvidiaTTSResponseCacher
@pytest.mark.asyncio()async def test_nvidia_tts_response_cacher(): """Tests NvidiaTTSResponseCacher's response deduplication functionality.
Tests the cacher's ability to deduplicate TTS audio responses in a sequence of frames including user speech events and LLM responses.
Args: None
Returns: None
The test verifies: - Correct handling of user speech start/stop frames - Deduplication of identical TTS audio frames - Preservation of LLM response start/end frames - Frame ordering in pipeline output - Only one TTS frame is retained """ nvidia_tts_response_cacher = NvidiaTTSResponseCacher() pipeline = Pipeline([nvidia_tts_response_cacher])
test_audio = b"\x52\x49\x46\x46\x24\x08\x00\x00\x57\x41\x56\x45\x66\x6d\x74\x20" frames_to_send = [ UserStartedSpeakingFrame(), LLMFullResponseStartFrame(), TTSAudioRawFrame( audio=test_audio, sample_rate=16000, num_channels=1, ), LLMFullResponseEndFrame(), LLMFullResponseStartFrame(), TTSAudioRawFrame( audio=test_audio, sample_rate=16000, num_channels=1, ), LLMFullResponseEndFrame(), UserStoppedSpeakingFrame(), ]
expected_down_frames = [ UserStartedSpeakingFrame, UserStoppedSpeakingFrame, LLMFullResponseStartFrame, TTSAudioRawFrame, LLMFullResponseEndFrame, ]
received_down_frames, received_up_frames = await run_pipecat_test( pipeline, frames_to_send=frames_to_send, expected_down_frames=expected_down_frames, )
# Verify we only got one TTS frame tts_frames = [f for f in received_down_frames if isinstance(f, TTSAudioRawFrame)] assert len(tts_frames) == 1123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.# SPDX-License-Identifier: BSD 2-Clause License
"""Unit tests for the PostureProviderProcessor class."""
import pytestfrom pipecat.frames.frames import BotStoppedSpeakingFrame, StartInterruptionFrame, TTSStartedFrame
from nvidia_pipecat.frames.action import StartPostureBotActionFramefrom nvidia_pipecat.processors.posture_provider import PostureProviderProcessorfrom tests.unit.utils import ignore_ids, run_test
@pytest.mark.asyncio()async def test_posture_provider_processor_tts(): """Test TTSStartedFrame processing in PostureProviderProcessor.
Tests that the processor generates appropriate "Talking" posture when text-to-speech begins.
Args: None
Returns: None
The test verifies: - TTSStartedFrame is processed correctly - "Talking" posture is generated - Frames are emitted in correct order """ frames_to_send = [TTSStartedFrame()] expected_down_frames = [ ignore_ids(TTSStartedFrame()), ignore_ids(StartPostureBotActionFrame(posture="Talking")), ]
await run_test( PostureProviderProcessor(), frames_to_send=frames_to_send, expected_down_frames=expected_down_frames, )
@pytest.mark.asyncio()async def test_posture_provider_processor_bot_finished(): """Test BotStoppedSpeakingFrame processing in PostureProviderProcessor.
Tests that the processor generates appropriate "Attentive" posture when bot stops speaking.
Args: None
Returns: None
The test verifies: - BotStoppedSpeakingFrame is processed correctly - "Attentive" posture is generated - Frames are emitted in correct order """ frames_to_send = [BotStoppedSpeakingFrame()] expected_down_frames = [ ignore_ids(BotStoppedSpeakingFrame()), ignore_ids(StartPostureBotActionFrame(posture="Attentive")), ]
await run_test( PostureProviderProcessor(), frames_to_send=frames_to_send, expected_down_frames=expected_down_frames, )
@pytest.mark.asyncio()async def test_posture_provider_processor_interrupt(): """Tests posture generation for interruption events.
Tests that the processor generates appropriate "Listening" posture when an interruption occurs.
Args: None
Returns: None
The test verifies: - StartInterruptionFrame is processed correctly - "Listening" posture is generated - Frames are emitted in correct order """ frames_to_send = [StartInterruptionFrame()] expected_down_frames = [ ignore_ids(StartInterruptionFrame()), ignore_ids(StartPostureBotActionFrame(posture="Listening")), ]
await run_test( PostureProviderProcessor(), frames_to_send=frames_to_send, expected_down_frames=expected_down_frames, )12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.# SPDX-License-Identifier: BSD 2-Clause License
"""Unit tests for the proactivity processor module.
This module contains tests that verify the behavior of the ProactivityProcessor."""
import asyncioimport osimport sys
import pytest
sys.path.append(os.path.abspath("../../src"))
from pipecat.frames.frames import TTSSpeakFrame, UserStoppedSpeakingFramefrom pipecat.pipeline.pipeline import Pipelinefrom pipecat.pipeline.task import PipelineTask
from nvidia_pipecat.frames.action import StartedPresenceUserActionFramefrom nvidia_pipecat.processors.proactivity import ProactivityProcessorfrom tests.unit.utils import FrameStorage, run_interactive_test
@pytest.mark.asyncioasync def test_proactive_bot_processor_timer_behavior(): """Test the ProactiveBotProcessor's timer and message behavior.
Tests the processor's ability to manage timer-based proactive messages and handle timer resets based on user activity.
Args: None
Returns: None
The test verifies: - Default message is sent after timer expiration - Timer resets correctly on user activity - Timer reset prevents premature message generation - Frames are processed in correct order - Message content matches configuration """ proactivity = ProactivityProcessor(default_message="I'm here if you need me!", timer_duration=0.5) storage = FrameStorage() pipeline = Pipeline([proactivity, storage])
async def test_routine(task: PipelineTask): """Inner test coroutine for proactivity testing.
Args: task: PipelineTask instance for frame queueing.
The routine: 1. Sends initial presence frame 2. Waits for timer expiration 3. Verifies message generation 4. Tests timer reset behavior 5. Confirms no premature messages """ await task.queue_frame(StartedPresenceUserActionFrame(action_id="1")) # Wait for initial proactive message await asyncio.sleep(0.6)
# Confirm at least one frame assert len(storage.history) >= 1, "Expected at least one frame."
# Confirm correct text frame output frame = storage.history[2].frame assert isinstance(frame, TTSSpeakFrame) assert frame.text == "I'm here if you need me!"
# Send another StartFrame to reset the timer await task.queue_frame(UserStoppedSpeakingFrame()) await asyncio.sleep(0)
# Wait half the timer (0.5s) => no new message yet frame_count_after_reset = len(storage.history) await asyncio.sleep(0.3) # Confirm no additional message arrived yet assert frame_count_after_reset == len(storage.history)
await run_interactive_test(pipeline, test_coroutine=test_routine)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.# SPDX-License-Identifier: BSD 2-Clause License
"""Unit tests for the RivaASRService.
This module contains tests for the RivaASRService class, including initialization,ASR functionality, interruption handling, and integration tests."""
import asyncioimport unittestfrom unittest.mock import AsyncMock, MagicMock, patch
import pytestfrom pipecat.frames.frames import ( CancelFrame, EndFrame, StartFrame, StartInterruptionFrame, StopInterruptionFrame, TranscriptionFrame, UserStartedSpeakingFrame, UserStoppedSpeakingFrame,)from pipecat.transcriptions.language import Language
from nvidia_pipecat.frames.riva import RivaInterimTranscriptionFramefrom nvidia_pipecat.services.riva_speech import RivaASRService
class TestRivaASRService(unittest.TestCase): """Test suite for RivaASRService functionality."""
@patch("nvidia_pipecat.services.riva_speech.riva.client.Auth") @patch("nvidia_pipecat.services.riva_speech.riva.client.ASRService") def test_initialization_with_default_parameters(self, mock_asr_service, mock_auth): """Tests RivaASRService initialization with default parameters.
Args: mock_asr_service: Mock for the ASR service. mock_auth: Mock for the authentication service.
The test verifies: - Correct type initialization for all parameters - Default server configuration - Authentication setup - Service parameter defaults """ # Act service = RivaASRService(api_key="test_api_key")
# Assert - only check types, not specific values self.assertIsInstance(service._language_code, Language) self.assertIsInstance(service._sample_rate, int) self.assertIsInstance(service._model, str) # Basic boolean parameter checks without restricting values self.assertIsInstance(service._profanity_filter, bool) self.assertIsInstance(service._automatic_punctuation, bool) self.assertIsInstance(service._interim_results, bool) self.assertIsInstance(service._max_alternatives, int) self.assertIsInstance(service._generate_interruptions, bool)
# Verify Auth was called with correct parameters # For server "grpc.nvcf.nvidia.com:443", use_ssl is automatically set to True mock_auth.assert_called_with( None, True, # Changed from False to True since default server is "grpc.nvcf.nvidia.com:443" "grpc.nvcf.nvidia.com:443", [["function-id", "1598d209-5e27-4d3c-8079-4751568b1081"], ["authorization", "Bearer test_api_key"]], )
@patch("nvidia_pipecat.services.riva_speech.riva.client.Auth") @patch("nvidia_pipecat.services.riva_speech.riva.client.ASRService") def test_initialization_with_custom_parameters(self, mock_asr_service, mock_auth): """Tests RivaASRService initialization with custom parameters.
Args: mock_asr_service: Mock for the ASR service. mock_auth: Mock for the authentication service.
The test verifies: - Custom parameter values are set correctly - Server configuration is customized - Authentication with custom credentials - Optional parameter handling """ # Define test parameters test_api_key = "test_api_key" test_server = "custom_server:50051" test_function_id = "custom_function_id" test_language = Language.ES_ES test_model = "custom_model" test_sample_rate = 44100 test_channel_count = 2 test_max_alternatives = 2 test_boosted_words = {"boost": 1.0} test_boosted_score = 5.0
# Act service = RivaASRService( api_key=test_api_key, server=test_server, function_id=test_function_id, language=test_language, model=test_model, profanity_filter=True, automatic_punctuation=True, no_verbatim_transcripts=True, boosted_lm_words=test_boosted_words, boosted_lm_score=test_boosted_score, sample_rate=test_sample_rate, audio_channel_count=test_channel_count, max_alternatives=test_max_alternatives, interim_results=False, generate_interruptions=True, use_ssl=True, )
# Assert - verify custom parameters were set correctly self.assertEqual(service._language_code, test_language) self.assertEqual(service._sample_rate, test_sample_rate) self.assertEqual(service._model, test_model) self.assertEqual(service._boosted_lm_words, test_boosted_words) self.assertEqual(service._boosted_lm_score, test_boosted_score) self.assertEqual(service._max_alternatives, test_max_alternatives) self.assertEqual(service._audio_channel_count, test_channel_count)
# Verify Auth was called with correct parameters mock_auth.assert_called_with( None, True, test_server, [["function-id", test_function_id], ["authorization", f"Bearer {test_api_key}"]], )
@patch("nvidia_pipecat.services.riva_speech.riva.client.Auth") def test_error_handling_during_initialization(self, mock_auth): """Tests error handling during service initialization.
Args: mock_auth: Mock for the authentication service.
The test verifies: - Proper exception handling - Error message formatting - Service cleanup on failure """ # Arrange mock_auth.side_effect = Exception("Connection failed")
# Act & Assert with self.assertRaises(Exception) as context: RivaASRService(api_key="test_api_key")
# Verify the error message self.assertTrue("Missing module: Connection failed" in str(context.exception))
@patch("nvidia_pipecat.services.riva_speech.riva.client.Auth") @patch("nvidia_pipecat.services.riva_speech.riva.client.ASRService") def test_can_generate_metrics(self, mock_asr_service, mock_auth): """Test that the service can generate metrics.""" # Arrange service = RivaASRService(api_key="test_api_key")
# Act & Assert self.assertFalse(service.can_generate_metrics())
@patch("nvidia_pipecat.services.riva_speech.riva.client.Auth") @patch("nvidia_pipecat.services.riva_speech.riva.client.ASRService") def test_start_method(self, mock_asr_service, mock_auth): """Test the start method of RivaASRService.""" # Arrange service = RivaASRService(api_key="test_api_key")
# Create a completed mock task for expected return value mock_task = MagicMock() mock_task.done.return_value = True
# Use MagicMock to avoid coroutine awaiting issues service.create_task = MagicMock(return_value=mock_task)
# Mock the response task handler mock_response_coro = MagicMock() service._response_task_handler = MagicMock(return_value=mock_response_coro)
# Create a mock StartFrame with the necessary attributes mock_start_frame = MagicMock(spec=StartFrame)
# Act async def run_test(): await service.start(mock_start_frame)
# Run the test asyncio.run(run_test())
# Assert # Verify create_task was called with the right handlers service.create_task.assert_called_once_with(mock_response_coro)
@patch("nvidia_pipecat.services.riva_speech.riva.client.Auth") @patch("nvidia_pipecat.services.riva_speech.riva.client.ASRService") def test_stop_method(self, mock_asr_service, mock_auth): """Test the stop method of RivaASRService.""" # Arrange service = RivaASRService(api_key="test_api_key") service._stop_tasks = AsyncMock()
# Create a mock EndFrame mock_end_frame = MagicMock(spec=EndFrame)
# Act async def run_test(): await service.stop(mock_end_frame)
# Run the test asyncio.run(run_test())
# Assert service._stop_tasks.assert_called_once()
@patch("nvidia_pipecat.services.riva_speech.riva.client.Auth") @patch("nvidia_pipecat.services.riva_speech.riva.client.ASRService") def test_cancel_method(self, mock_asr_service, mock_auth): """Test the cancel method of RivaASRService.""" # Arrange service = RivaASRService(api_key="test_api_key") service._stop_tasks = AsyncMock()
# Create a mock CancelFrame mock_cancel_frame = MagicMock(spec=CancelFrame)
# Act async def run_test(): await service.cancel(mock_cancel_frame)
# Run the test asyncio.run(run_test())
# Assert service._stop_tasks.assert_called_once()
@patch("nvidia_pipecat.services.riva_speech.riva.client.Auth") @patch("nvidia_pipecat.services.riva_speech.riva.client.ASRService") def test_run_stt_yields_frames(self, mock_asr_service, mock_auth): """Test that run_stt method yields frames.""" # Arrange service = RivaASRService(api_key="test_api_key") service._queue = AsyncMock()
# Create a completed mock task and mock thread task handler mock_task = MagicMock() mock_task.done.return_value = True
mock_thread_coro = MagicMock() service._thread_task_handler = MagicMock(return_value=mock_thread_coro) service._thread_task = mock_task
# Mock the create_task method to return our mock task and avoid coroutine warnings service.create_task = MagicMock(return_value=mock_task)
# Act async def run_test(): frames = [] audio_data = b"test_audio_data" async for frame in service.run_stt(audio_data): frames.append(frame)
# Assert service._queue.put.assert_called_once_with(audio_data) # run_stt yields a single None frame for RivaASRService self.assertEqual(len(frames), 1) self.assertIsNone(frames[0]) service.create_task.assert_called_once_with(mock_thread_coro)
# Run the test asyncio.run(run_test())
@patch("nvidia_pipecat.services.riva_speech.riva.client.Auth") @patch("nvidia_pipecat.services.riva_speech.riva.client.ASRService") def test_handle_response_with_final_transcript(self, mock_asr_service, mock_auth): """Tests handling of ASR responses with final transcripts.
Args: mock_asr_service: Mock for the ASR service. mock_auth: Mock for the authentication service.
The test verifies: - Final transcript processing - Metrics handling - Frame generation - Response completion handling """ # Arrange service = RivaASRService(api_key="test_api_key") service.push_frame = AsyncMock()
# Use MagicMock instead of asyncio.Future to avoid event loop issues service.stop_ttfb_metrics = AsyncMock() service.stop_processing_metrics = AsyncMock()
# Create a mock response with final transcript mock_result = MagicMock() mock_alternative = MagicMock() mock_alternative.transcript = "This is a final transcript" mock_result.alternatives = [mock_alternative] mock_result.is_final = True
mock_response = MagicMock() mock_response.results = [mock_result]
# Act async def run_test(): await service._handle_response(mock_response)
# Run the test asyncio.run(run_test())
# Assert service.stop_ttfb_metrics.assert_called_once() service.stop_processing_metrics.assert_called_once()
# Verify that a TranscriptionFrame was pushed with the correct text found = False for call_args in service.push_frame.call_args_list: frame = call_args[0][0] if isinstance(frame, TranscriptionFrame) and frame.text == "This is a final transcript": found = True break self.assertTrue(found, "No TranscriptionFrame with the expected text was pushed")
@patch("nvidia_pipecat.services.riva_speech.riva.client.Auth") @patch("nvidia_pipecat.services.riva_speech.riva.client.ASRService") def test_handle_response_with_interim_transcript(self, mock_asr_service, mock_auth): """Test handling of ASR responses with interim transcript.""" # Arrange service = RivaASRService(api_key="test_api_key") service.push_frame = AsyncMock()
# Use AsyncMock directly instead of Future service.stop_ttfb_metrics = AsyncMock()
# Create a mock response with interim transcript mock_result = MagicMock() mock_alternative = MagicMock() mock_alternative.transcript = "This is an interim transcript" mock_result.alternatives = [mock_alternative] mock_result.is_final = False mock_result.stability = 1.0 # High stability interim result
mock_response = MagicMock() mock_response.results = [mock_result]
# Act async def run_test(): await service._handle_response(mock_response)
# Run the test asyncio.run(run_test())
# Assert service.stop_ttfb_metrics.assert_called_once()
# Verify that a RivaInterimTranscriptionFrame was pushed with the correct text found = False for call_args in service.push_frame.call_args_list: frame = call_args[0][0] if ( isinstance(frame, RivaInterimTranscriptionFrame) and frame.text == "This is an interim transcript" and frame.stability == 1.0 ): found = True break self.assertTrue(found, "No RivaInterimTranscriptionFrame with the expected text was pushed")
@patch("nvidia_pipecat.services.riva_speech.riva.client.Auth") @patch("nvidia_pipecat.services.riva_speech.riva.client.ASRService") def test_handle_interruptions(self, mock_asr_service, mock_auth): """Tests handling of speech interruptions.
Args: mock_asr_service: Mock for the ASR service. mock_auth: Mock for the authentication service.
The test verifies: - Interruption start handling - Interruption stop handling - Frame sequence generation - State management during interruptions """ # Arrange service = RivaASRService(api_key="test_api_key", generate_interruptions=True) service.push_frame = AsyncMock()
# Use AsyncMock directly instead of Future service._start_interruption = AsyncMock() service._stop_interruption = AsyncMock()
# Mock the property to return True - avoids setting the property directly type(service).interruptions_allowed = MagicMock(return_value=True)
# Act async def run_test(): # Simulate interruption handling user_started_frame = UserStartedSpeakingFrame() user_stopped_frame = UserStoppedSpeakingFrame()
# Direct calls to _handle_interruptions await service._handle_interruptions(user_started_frame) await service._handle_interruptions(user_stopped_frame)
# Run the test asyncio.run(run_test())
# Assert service._start_interruption.assert_called_once() service._stop_interruption.assert_called_once()
# Check that frames were pushed (check by type instead of exact equality) pushed_frame_types = [type(call[0][0]) for call in service.push_frame.call_args_list] self.assertIn(StartInterruptionFrame, pushed_frame_types, "No StartInterruptionFrame was pushed") self.assertIn(StopInterruptionFrame, pushed_frame_types, "No StopInterruptionFrame was pushed") self.assertIn(UserStartedSpeakingFrame, pushed_frame_types, "No UserStartedSpeakingFrame was pushed") self.assertIn(UserStoppedSpeakingFrame, pushed_frame_types, "No UserStoppedSpeakingFrame was pushed")
@pytest.mark.asyncioasync def test_riva_asr_integration(): """Tests integration of RivaASRService components.
Tests the complete flow of the ASR service including initialization, processing, and cleanup.
The test verifies: - Service startup sequence - Audio processing pipeline - Response handling - Service shutdown sequence - Resource cleanup """ with ( patch("nvidia_pipecat.services.riva_speech.riva.client.Auth"), patch("nvidia_pipecat.services.riva_speech.riva.client.ASRService") as mock_asr_service, ): # Setup mock ASR service mock_instance = mock_asr_service.return_value
# Initialize service with interruptions enabled service = RivaASRService(api_key="test_api_key", generate_interruptions=True) service._asr_service = mock_instance
# Set up the response queue service._response_queue = asyncio.Queue()
# Mock the _stop_tasks method directly instead of relying on task_manager service._stop_tasks = AsyncMock()
# Create mock coroutines for handlers thread_coro = MagicMock() response_coro = MagicMock()
# Mock the handler methods to return the coroutines service._thread_task_handler = MagicMock(return_value=thread_coro) service._response_task_handler = MagicMock(return_value=response_coro)
# Create a mock task that's already completed mock_task = MagicMock() mock_task.done.return_value = True
# Mock task creation to return our completed task service.create_task = MagicMock(return_value=mock_task)
# Set the tasks to our mock task service._thread_task = mock_task service._response_task = mock_task
# Create a mock result for testing mock_result = MagicMock() mock_alternative = MagicMock() mock_alternative.transcript = "This is a test transcript" mock_result.alternatives = [mock_alternative] mock_result.is_final = True
mock_response = MagicMock() mock_response.results = [mock_result]
# Create a mock StartFrame mock_start_frame = MagicMock(spec=StartFrame)
# Start the service with a start frame await service.start(mock_start_frame)
# Verify response task was created service.create_task.assert_called_with(response_coro)
# Put a mock response in the queue await service._response_queue.put(mock_response)
# Test some other functionality audio_data = b"test_audio_data"
# Run the run_stt method frames = [] async for frame in service.run_stt(audio_data): frames.append(frame)
# Verify results assert len(frames) == 1 assert frames[0] is None
# Verify thread task was created service.create_task.assert_any_call(thread_coro)
# Simulate stopping the service mock_end_frame = MagicMock(spec=EndFrame) await service.stop(mock_end_frame)
# Verify stop_tasks was called service._stop_tasks.assert_called_once()
if __name__ == "__main__": unittest.main()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.# SPDX-License-Identifier: BSD 2-Clause License
"""Unit tests for the Riva Neural Machine Translation (NMT) service.
This module contains tests that verify the behavior of the RivaNMTService,including successful translations and various error cases for both STT and LLM outputs."""
import pytestfrom loguru import loggerfrom pipecat.frames.frames import ( ErrorFrame, LLMFullResponseEndFrame, LLMFullResponseStartFrame, LLMMessagesFrame, TextFrame, TranscriptionFrame,)from pipecat.pipeline.pipeline import Pipelinefrom pipecat.pipeline.task import PipelineTaskfrom pipecat.transcriptions.language import Language
from nvidia_pipecat.services.riva_nmt import RivaNMTServicefrom tests.unit.utils import FrameStorage, ignore_ids, run_interactive_test
class MockText: """Mock class representing a translated text response.
Attributes: text: The translated text content. """
def __init__(self, text): """Initialize MockText.
Args: text (str): The translated text content. """ self.text = text
class MockTranslations: """Mock class representing a collection of translations.
Attributes: translations: List of MockText objects containing translations. """
def __init__(self, text): """Initialize MockTranslations.
Args: text (str): The text to be wrapped in a MockText object. """ self.translations = [MockText(text)]
class MockRivaNMTClient: """Mock class simulating the Riva NMT client.
Attributes: translated_text: The text to return as translation. """
def __init__(self, translated_text): """Initialize MockRivaNMTClient.
Args: translated_text (str): Text to be returned as translation. """ self.translated_text = translated_text
def translate(self, arg1, arg2, arg3, arg4): """Mock translation method.
Args: arg1: Source language. arg2: Target language. arg3: Text to translate. arg4: Additional options.
Returns: MockTranslations: A mock translations object containing the pre-defined translated text. """ return MockTranslations(self.translated_text)
@pytest.mark.asyncio()async def test_riva_nmt_service(mocker): """Test the RivaNMTService functionality.
Tests translation service behavior including successful translations and error handling for both STT and LLM outputs.
Args: mocker: Pytest mocker fixture for mocking dependencies.
The test verifies: - STT output translation - LLM output translation - Empty input handling - Missing language handling - Error frame generation - Frame sequence correctness """ testcases = { "Success: STT output translated": { "source_language": Language.ES_US, "target_language": Language.EN_US, "input_frames": [TranscriptionFrame("Hola, por favor preséntate.", "", "")], "translated_text": "Hello, please introduce yourself.", "result_frame_name": "LLMMessagesFrame", "result_frame": LLMMessagesFrame([{"role": "system", "content": "Hello, please introduce yourself."}]), }, "Success: LLM output translated": { "source_language": Language.EN_US, "target_language": Language.ES_US, "input_frames": [ LLMFullResponseStartFrame(), TextFrame("Hello there!"), TextFrame("Im an artificial intelligence model known as Llama."), LLMFullResponseEndFrame(), ], "translated_text": "Hola Im un modelo de inteligencia artificial conocido como Llama", "result_frame_name": "TextFrame", "result_frame": TextFrame("Hola Im un modelo de inteligencia artificial conocido como Llama."), }, "Fail due to empty input text": { "source_language": Language.ES_US, "target_language": Language.EN_US, "input_frames": [TranscriptionFrame("", "", "")], "translated_text": None, "result_frame_name": "ErrorFrame", "result_frame": ErrorFrame( f"Error while translating the text from {Language.ES_US} to {Language.EN_US}, " "Error: No input text provided for the translation..", ), }, "Fail due to no source language provided": { "source_language": None, "target_language": Language.EN_US, "input_frames": [TranscriptionFrame("Hola, por favor preséntate.", "", "")], "translated_text": None, "error": Exception("No source language provided for the translation.."), }, "Fail due to no target language provided": { "source_language": Language.ES_US, "target_language": None, "input_frames": [TranscriptionFrame("Hola, por favor preséntate.", "", "")], "translated_text": None, "error": Exception("No target language provided for the translation.."), }, }
for tc_name, tc_data in testcases.items(): logger.info(f"Verifying test case: {tc_name}")
mocker.patch( "riva.client.NeuralMachineTranslationClient", return_value=MockRivaNMTClient(tc_data["translated_text"]) )
try: nmt_service = RivaNMTService( source_language=tc_data["source_language"], target_language=tc_data["target_language"] ) except Exception as e: assert str(e) == str(tc_data["error"]) continue
storage1 = FrameStorage() storage2 = FrameStorage()
pipeline = Pipeline([storage1, nmt_service, storage2])
async def test_routine(task: PipelineTask, test_data=tc_data, s1=storage1, s2=storage2): await task.queue_frames(test_data["input_frames"])
# Wait for the result frame if "ErrorFrame" in test_data["result_frame"].name: await s1.wait_for_frame(ignore_ids(test_data["result_frame"])) else: await s2.wait_for_frame(ignore_ids(test_data["result_frame"]))
await run_interactive_test(pipeline, test_coroutine=test_routine)
for frame_history_entry in storage1.history: if frame_history_entry.frame.name.startswith("TextFrame"): # ignoring input text frames getting stored in storage1 continue if frame_history_entry.frame.name.startswith(tc_data["result_frame_name"]): assert frame_history_entry.frame == ignore_ids(tc_data["result_frame"])
for frame_history_entry in storage2.history: if frame_history_entry.frame.name.startswith(tc_data["result_frame_name"]): assert frame_history_entry.frame == ignore_ids(tc_data["result_frame"])123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.# SPDX-License-Identifier: BSD 2-Clause License
"""Unit tests for the RivaTTSService.
This module contains tests for the RivaTTSService class, including initialization,TTS frame generation, audio frame handling, and integration tests."""
import asyncioimport unittestfrom unittest.mock import AsyncMock, MagicMock, patch
import pytestfrom pipecat.frames.frames import TTSAudioRawFrame, TTSStartedFrame, TTSStoppedFrame, TTSTextFramefrom pipecat.transcriptions.language import Language
from nvidia_pipecat.services.riva_speech import RivaTTSService
class TestRivaTTSService(unittest.TestCase): """Test suite for RivaTTSService functionality."""
@patch("nvidia_pipecat.services.riva_speech.riva.client.Auth") @patch("nvidia_pipecat.services.riva_speech.riva.client.SpeechSynthesisService") def test_run_tts_yields_audio_frames(self, mock_speech_service, mock_auth): """Tests that run_tts correctly yields audio frames in sequence.
Tests the complete flow of text-to-speech conversion including start, text processing, audio generation, and completion frames.
Args: mock_speech_service: Mock for the speech synthesis service. mock_auth: Mock for the authentication service.
The test verifies: - TTSStartedFrame generation - TTSTextFrame content - TTSAudioRawFrame generation - TTSStoppedFrame generation - Frame sequence order - Audio data integrity """ # Arrange mock_audio_data = b"sample_audio_data" mock_audio_frame = TTSAudioRawFrame(audio=mock_audio_data, sample_rate=16000, num_channels=1)
# Create a properly structured mock service mock_service_instance = MagicMock() mock_speech_service.return_value = mock_service_instance
# Set up the mock to return a regular list, not an async generator # The service expects a list/iterable that it can call iter() on mock_service_instance.synthesize_online.return_value = [mock_audio_frame]
# Create an instance of RivaTTSService service = RivaTTSService(api_key="test_api_key")
# Ensure the service has the right mock service._service = mock_service_instance
# Act async def run_test(): frames = [] async for frame in service.run_tts("Hello, world!"): frames.append(frame)
# Assert self.assertEqual( len(frames), 4 ) # Should yield 4 frames: TTSStartedFrame, TTSTextFrame, TTSAudioRawFrame, TTSStoppedFrame self.assertIsInstance(frames[0], TTSStartedFrame) self.assertIsInstance(frames[1], TTSTextFrame) self.assertEqual(frames[1].text, "Hello, world!") self.assertIsInstance(frames[2], TTSAudioRawFrame) self.assertEqual(frames[2].audio, mock_audio_data) self.assertIsInstance(frames[3], TTSStoppedFrame)
# Run the async test asyncio.run(run_test())
@patch("nvidia_pipecat.services.riva_speech.riva.client.Auth") @patch("nvidia_pipecat.services.riva_speech.riva.client.SpeechSynthesisService") def test_push_tts_frames(self, mock_speech_service, mock_auth): """Tests that _push_tts_frames correctly processes TTS generation.
Tests the internal frame processing mechanism including metrics handling and generator processing.
Args: mock_speech_service: Mock for the speech synthesis service. mock_auth: Mock for the authentication service.
The test verifies: - Metrics start/stop timing - Generator processing - Frame processing order - Method call sequence """ # Arrange mock_audio_frame = TTSAudioRawFrame(audio=b"sample_audio_data", sample_rate=16000, num_channels=1)
# Create a properly structured mock service mock_service_instance = MagicMock() mock_speech_service.return_value = mock_service_instance
# Return a regular list for synthesize_online mock_service_instance.synthesize_online.return_value = [mock_audio_frame]
# Create an instance of RivaTTSService service = RivaTTSService(api_key="test_api_key") service._service = mock_service_instance service.start_processing_metrics = AsyncMock() service.stop_processing_metrics = AsyncMock() service.process_generator = AsyncMock()
# Create a mock for run_tts instead of trying to capture its generator async def mock_run_tts(text): # This is the sequence of frames that would be yielded by run_tts yield TTSStartedFrame() yield TTSTextFrame(text) yield mock_audio_frame yield TTSStoppedFrame()
# Replace the run_tts method with our mock with patch.object(service, "run_tts", side_effect=mock_run_tts): # Act async def run_test(): await service._push_tts_frames("Hello, world!")
# Assert # Check that the necessary methods were called service.start_processing_metrics.assert_called_once() service.process_generator.assert_called_once() service.stop_processing_metrics.assert_called_once()
# Verify call order using the call_args.called_before method assert service.start_processing_metrics.call_args.called_before(service.process_generator.call_args) assert service.process_generator.call_args.called_before(service.stop_processing_metrics.call_args)
# Run the async test asyncio.run(run_test())
@patch("nvidia_pipecat.services.riva_speech.riva.client.Auth") @patch("nvidia_pipecat.services.riva_speech.riva.client.SpeechSynthesisService") def test_init_with_different_parameters(self, mock_speech_service, mock_auth): """Tests initialization with various configuration parameters.
Tests service initialization with different combinations of configuration options.
Args: mock_speech_service: Mock for the speech synthesis service. mock_auth: Mock for the authentication service.
The test verifies: - Parameter assignment - Default value handling - Custom parameter validation - Authentication configuration - Service initialization """ # Define test parameters - users should be able to use any values test_api_key = "test_api_key" test_server = "custom_server:50051" test_voice_id = "English-US.Male-1" test_sample_rate = 22050 test_language = Language.ES_ES test_zero_shot_quality = 10 test_model = "custom-tts-model" test_dictionary = {"word": "pronunciation"} test_audio_prompt_file = "test_audio.wav" # Test initialization with different parameters service = RivaTTSService( api_key=test_api_key, server=test_server, voice_id=test_voice_id, sample_rate=test_sample_rate, language=test_language, zero_shot_quality=test_zero_shot_quality, model=test_model, custom_dictionary=test_dictionary, zero_shot_audio_prompt_file=test_audio_prompt_file, use_ssl=True, )
# Verify the parameters were set correctly self.assertEqual(service._api_key, test_api_key) self.assertEqual(service._voice_id, test_voice_id) self.assertEqual(service._sample_rate, test_sample_rate) self.assertEqual(service._language_code, test_language) self.assertEqual(service._zero_shot_quality, test_zero_shot_quality) self.assertEqual(service._model_name, test_model) self.assertEqual(service._custom_dictionary, test_dictionary) self.assertEqual(service._zero_shot_audio_prompt_file, test_audio_prompt_file)
# Verify Auth was called with correct parameters mock_auth.assert_called_with( None, True, # use_ssl=True test_server, [["function-id", "0149dedb-2be8-4195-b9a0-e57e0e14f972"], ["authorization", f"Bearer {test_api_key}"]], )
@patch("nvidia_pipecat.services.riva_speech.riva.client.Auth") def test_riva_error_handling(self, mock_auth): """Tests error handling when Riva service initialization fails.
Tests the service's behavior when encountering initialization errors.
Args: mock_auth: Mock for the authentication service.
The test verifies: - Exception propagation - Error message formatting - Cleanup behavior - Service state after error """ # Test error handling when Riva service initialization fails mock_auth.side_effect = Exception("Connection failed")
# Assert that exception is raised and propagated with self.assertRaises(Exception) as context: RivaTTSService(api_key="test_api_key")
self.assertTrue("Missing module: Connection failed" in str(context.exception))
@patch("nvidia_pipecat.services.riva_speech.riva.client.Auth") @patch("nvidia_pipecat.services.riva_speech.riva.client.SpeechSynthesisService") def test_can_generate_metrics(self, mock_speech_service, mock_auth): """Tests that the service reports capability to generate metrics.
Tests the metrics generation capability reporting functionality.
Args: mock_speech_service: Mock for the speech synthesis service. mock_auth: Mock for the authentication service.
The test verifies: - Metrics capability reporting - Consistency of capability flag """ # Test that the service can generate metrics service = RivaTTSService(api_key="test_api_key") self.assertTrue(service.can_generate_metrics())
@pytest.mark.asyncioasync def test_riva_tts_integration(): """Tests integration of RivaTTSService components.
Tests the complete flow of the TTS service in an integrated environment.
The test verifies: - Service initialization - Frame generation sequence - Audio chunk processing - Frame content validation - Service completion """ # Use parentheses for multiline with statements instead of backslashes with ( patch("nvidia_pipecat.services.riva_speech.riva.client.Auth"), patch("nvidia_pipecat.services.riva_speech.riva.client.SpeechSynthesisService") as mock_service, ): # Setup mock responses mock_instance = mock_service.return_value
# Create audio frames for the response audio_frame1 = TTSAudioRawFrame(audio=b"audio_chunk_1", sample_rate=16000, num_channels=1) audio_frame2 = TTSAudioRawFrame(audio=b"audio_chunk_2", sample_rate=16000, num_channels=1)
# Return a list of frames, not an async generator mock_instance.synthesize_online.return_value = [audio_frame1, audio_frame2]
# Initialize service and call its methods service = RivaTTSService(api_key="test_api_key") service._service = mock_instance
# Simulate running the service collected_frames = [] async for frame in service.run_tts("Test sentence for TTS"): collected_frames.append(frame)
# Verify the expected frames were produced assert len(collected_frames) == 5 # Started, TextFrame, 2 audio chunks, stopped assert isinstance(collected_frames[0], TTSStartedFrame) assert isinstance(collected_frames[1], TTSTextFrame) assert collected_frames[1].text == "Test sentence for TTS" assert isinstance(collected_frames[2], TTSAudioRawFrame) assert isinstance(collected_frames[3], TTSAudioRawFrame) assert isinstance(collected_frames[4], TTSStoppedFrame)
# Verify audio content assert collected_frames[2].audio == b"audio_chunk_1" assert collected_frames[3].audio == b"audio_chunk_2"
if __name__ == "__main__": unittest.main()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.# SPDX-License-Identifier: BSD 2-Clause License
"""Unit tests for the SpeechPlanner service.
This module contains tests for the SpeechPlanner class, focusing on core functionalities:- Service initialization with various parameters- Frame processing for different frame types- Speech completion detection and LLM integration- Chat history management with context windows- Interruption handling and VAD integration- Label preprocessing and classification logic"""
import tempfilefrom datetime import datetimefrom unittest.mock import AsyncMock, Mock, patch
import pytestimport yamlfrom pipecat.frames.frames import ( InterimTranscriptionFrame, StartInterruptionFrame, StopInterruptionFrame, TranscriptionFrame,)from pipecat.processors.aggregators.openai_llm_context import OpenAILLMContext
from nvidia_pipecat.services.speech_planner import SpeechPlanner
class MockBaseMessageChunk: """Mock for BaseMessageChunk that mimics the structure."""
def __init__(self, content=""): """Initialize with content.
Args: content: The text content of the chunk """ self.content = content
@pytest.fixturedef mock_prompt_file(): """Create a temporary YAML prompt file for testing.""" prompt_data = { "configurations": {"using_chat_history": False}, "prompts": { "completion_prompt": ( "Evaluate whether the following user speech is sufficient:\n" "1. Label1: Complete and coherent thought\n" "2. Label2: Incomplete speech\n" "3. Label3: User commands\n" "4. Label4: Acknowledgments\n" "User Speech: {transcript}\n" "Only return Label1 or Label2 or Label3 or Label4." ) }, }
with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f: yaml.dump(prompt_data, f) return f.name
@pytest.fixturedef mock_context(): """Create a mock OpenAI LLM context.""" context = Mock(spec=OpenAILLMContext) context.get_messages.return_value = [ {"role": "user", "content": "Hello"}, {"role": "assistant", "content": "Hi there!"}, {"role": "user", "content": "How are you?"}, {"role": "assistant", "content": "I'm doing well!"}, ] return context
@pytest.fixturedef speech_planner(mock_prompt_file, mock_context): """Create a SpeechPlanner instance for testing.""" with patch("nvidia_pipecat.services.speech_planner.ChatNVIDIA") as mock_chat: mock_chat.return_value = AsyncMock() planner = SpeechPlanner( prompt_file=mock_prompt_file, model="test-model", api_key="test-key", base_url="http://test-url", context=mock_context, context_window=2, ) return planner
class TestSpeechPlannerInitialization: """Test SpeechPlanner initialization."""
def test_init_with_default_params(self, mock_prompt_file, mock_context): """Test initialization with default parameters.""" with patch("nvidia_pipecat.services.speech_planner.ChatNVIDIA") as mock_chat: mock_chat.return_value = AsyncMock()
planner = SpeechPlanner(prompt_file=mock_prompt_file, context=mock_context)
assert planner.model_name == "nvdev/google/gemma-2b-it" assert planner.context == mock_context assert planner.context_window == 1 assert planner.user_speaking is None assert planner.current_prediction is None
def test_init_with_custom_params(self, mock_prompt_file, mock_context): """Test initialization with custom parameters.""" with patch("nvidia_pipecat.services.speech_planner.ChatNVIDIA") as mock_chat: mock_chat.return_value = AsyncMock()
params = SpeechPlanner.InputParams(temperature=0.7, max_tokens=100, top_p=0.9)
planner = SpeechPlanner( prompt_file=mock_prompt_file, model="custom-model", api_key="custom-key", base_url="http://custom-url", context=mock_context, params=params, context_window=3, )
assert planner.model_name == "custom-model" assert planner.context_window == 3 assert planner._settings["temperature"] == 0.7 assert planner._settings["max_tokens"] == 100 assert planner._settings["top_p"] == 0.9
def test_init_loads_prompts(self, mock_prompt_file, mock_context): """Test that initialization properly loads prompts from file.""" with patch("nvidia_pipecat.services.speech_planner.ChatNVIDIA") as mock_chat: mock_chat.return_value = AsyncMock()
planner = SpeechPlanner(prompt_file=mock_prompt_file, context=mock_context)
assert "configurations" in planner.prompts assert "prompts" in planner.prompts assert "completion_prompt" in planner.prompts["prompts"] assert planner.prompts["configurations"]["using_chat_history"] is False
### Adding tests for preprocess_pred function
class TestPreprocessPred: """Test the preprocess_pred function logic through end-to-end processing."""
@pytest.mark.asyncio async def test_preprocess_pred_label1_complete(self, speech_planner): """Test preprocess_pred with Label1 returns Complete via end-to-end processing.""" frame = TranscriptionFrame("Hello there", "user1", datetime.now()) test_cases = ["Label1", "Label 1", "The answer is Label1.", "I think this is Label 1 based on analysis."]
for case in test_cases: # Mock the LLM to return the specific prediction mock_chunks = [MockBaseMessageChunk(case)]
with patch.object(speech_planner, "_stream_chat_completions", new_callable=AsyncMock) as mock_stream: mock_stream.return_value.__aiter__.return_value = mock_chunks
await speech_planner._process_complete_context(frame) assert speech_planner.current_prediction == "Complete", f"Failed for case: {case}"
@pytest.mark.asyncio async def test_preprocess_pred_label2_incomplete(self, speech_planner): """Test preprocess_pred with Label2 returns Incomplete via end-to-end processing.""" frame = TranscriptionFrame("Hello", "user1", datetime.now()) test_cases = ["Label2", "Label 2", "The answer is Label2.", "This should be Label 2."]
for case in test_cases: # Mock the LLM to return the specific prediction mock_chunks = [MockBaseMessageChunk(case)]
with patch.object(speech_planner, "_stream_chat_completions", new_callable=AsyncMock) as mock_stream: mock_stream.return_value.__aiter__.return_value = mock_chunks
await speech_planner._process_complete_context(frame) assert speech_planner.current_prediction == "Incomplete", f"Failed for case: {case}"
@pytest.mark.asyncio async def test_preprocess_pred_label3_complete(self, speech_planner): """Test preprocess_pred with Label3 returns Complete via end-to-end processing.""" frame = TranscriptionFrame("Stop that", "user1", datetime.now()) test_cases = ["Label3", "Label 3", "The answer is Label3.", "I classify this as Label 3."]
for case in test_cases: # Mock the LLM to return the specific prediction mock_chunks = [MockBaseMessageChunk(case)]
with patch.object(speech_planner, "_stream_chat_completions", new_callable=AsyncMock) as mock_stream: mock_stream.return_value.__aiter__.return_value = mock_chunks
await speech_planner._process_complete_context(frame) assert speech_planner.current_prediction == "Complete", f"Failed for case: {case}"
@pytest.mark.asyncio async def test_preprocess_pred_label4_complete(self, speech_planner): """Test preprocess_pred with Label4 returns Complete via end-to-end processing.""" frame = TranscriptionFrame("Okay", "user1", datetime.now()) test_cases = ["Label4", "Label 4", "The answer is Label4.", "This is Label 4 category."]
for case in test_cases: # Mock the LLM to return the specific prediction mock_chunks = [MockBaseMessageChunk(case)]
with patch.object(speech_planner, "_stream_chat_completions", new_callable=AsyncMock) as mock_stream: mock_stream.return_value.__aiter__.return_value = mock_chunks
await speech_planner._process_complete_context(frame) assert speech_planner.current_prediction == "Complete", f"Failed for case: {case}"
@pytest.mark.asyncio async def test_preprocess_pred_unrecognized_incomplete(self, speech_planner): """Test preprocess_pred with unrecognized labels returns Incomplete via end-to-end processing.""" frame = TranscriptionFrame("Unknown input", "user1", datetime.now()) test_cases = ["Label5", "Unknown", "No label found", "", "Some random text"]
for case in test_cases: # Mock the LLM to return the specific prediction mock_chunks = [MockBaseMessageChunk(case)]
with patch.object(speech_planner, "_stream_chat_completions", new_callable=AsyncMock) as mock_stream: mock_stream.return_value.__aiter__.return_value = mock_chunks
await speech_planner._process_complete_context(frame) assert speech_planner.current_prediction == "Incomplete", f"Failed for case: {case}"
### Adding tests for chat_history managementclass TestChatHistory: """Test chat history management."""
def test_get_chat_history_empty_messages(self, mock_prompt_file): """Test get_chat_history with empty message list.""" context = Mock(spec=OpenAILLMContext) context.get_messages.return_value = []
with patch("nvidia_pipecat.services.speech_planner.ChatNVIDIA") as mock_chat: mock_chat.return_value = AsyncMock()
planner = SpeechPlanner(prompt_file=mock_prompt_file, context=context, context_window=2)
history = planner.get_chat_history() assert history == []
def test_get_chat_history_with_context_window(self, mock_prompt_file): """Test get_chat_history respects context_window setting.""" messages = [ {"role": "user", "content": "First user message"}, {"role": "assistant", "content": "First assistant response"}, {"role": "user", "content": "Second user message"}, {"role": "assistant", "content": "Second assistant response"}, {"role": "user", "content": "Third user message"}, {"role": "assistant", "content": "Third assistant response"}, ]
context = Mock(spec=OpenAILLMContext) context.get_messages.return_value = messages
with patch("nvidia_pipecat.services.speech_planner.ChatNVIDIA") as mock_chat: mock_chat.return_value = AsyncMock()
# Test with context_window=1 (should get last 2 messages) planner = SpeechPlanner(prompt_file=mock_prompt_file, context=context, context_window=1)
history = planner.get_chat_history() assert len(history) == 2 assert history[0]["content"] == "Third user message" assert history[1]["content"] == "Third assistant response"
def test_get_chat_history_starts_with_user(self, mock_prompt_file): """Test get_chat_history starts with user message.""" messages = [ {"role": "user", "content": "User message 1"}, {"role": "assistant", "content": "Assistant response 1"}, {"role": "user", "content": "User message 2"}, {"role": "assistant", "content": "Assistant response 2"}, ]
context = Mock(spec=OpenAILLMContext) context.get_messages.return_value = messages
with patch("nvidia_pipecat.services.speech_planner.ChatNVIDIA") as mock_chat: mock_chat.return_value = AsyncMock()
planner = SpeechPlanner(prompt_file=mock_prompt_file, context=context, context_window=2)
history = planner.get_chat_history() assert len(history) > 0 assert history[0]["role"] == "user"
class TestFrameProcessing: """Test frame processing functionality by testing the logic directly."""
def test_user_speaking_state_changes(self, speech_planner): """Test that user speaking state changes correctly.""" # Test UserStartedSpeakingFrame logic assert speech_planner.user_speaking is None speech_planner.user_speaking = True assert speech_planner.user_speaking is True
# Test UserStoppedSpeakingFrame logic speech_planner.user_speaking = False assert speech_planner.user_speaking is False
def test_bot_speaking_timestamp_tracking(self, speech_planner): """Test bot speaking timestamp tracking.""" # Initially no timestamp assert speech_planner.latest_bot_started_speaking_frame_timestamp is None
# Set timestamp (simulating BotStartedSpeakingFrame) test_time = datetime.now() speech_planner.latest_bot_started_speaking_frame_timestamp = test_time assert speech_planner.latest_bot_started_speaking_frame_timestamp == test_time
# Clear timestamp (simulating BotStoppedSpeakingFrame) speech_planner.latest_bot_started_speaking_frame_timestamp = None assert speech_planner.latest_bot_started_speaking_frame_timestamp is None
def test_frame_state_management(self, speech_planner): """Test frame state management without full processing.""" # Test last_frame tracking frame = InterimTranscriptionFrame("Hello", "user1", datetime.now()) speech_planner.last_frame = frame assert speech_planner.last_frame == frame
# Test clearing last_frame speech_planner.last_frame = None assert speech_planner.last_frame is None
# Test current_prediction state speech_planner.current_prediction = "Complete" assert speech_planner.current_prediction == "Complete"
speech_planner.current_prediction = "Incomplete" assert speech_planner.current_prediction == "Incomplete"
def test_transcription_frame_conditions(self, speech_planner): """Test the conditions for processing transcription frames.""" # Set up conditions for processing speech_planner.user_speaking = False speech_planner.current_prediction = "Incomplete"
# These conditions should allow processing assert speech_planner.user_speaking is False assert speech_planner.current_prediction == "Incomplete"
# Test conditions that would prevent processing speech_planner.user_speaking = True assert speech_planner.user_speaking is True # Should prevent processing
@pytest.mark.asyncio async def test_cancel_current_task_helper(self, speech_planner): """Test the _cancel_current_task helper method.""" # Test with no current task speech_planner._current_task = None await speech_planner._cancel_current_task() assert speech_planner._current_task is None
# Test with completed task completed_task = Mock() completed_task.done.return_value = True completed_task.cancelled.return_value = False speech_planner._current_task = completed_task
await speech_planner._cancel_current_task() assert speech_planner._current_task is None
# Test with cancelled task cancelled_task = Mock() cancelled_task.done.return_value = False cancelled_task.cancelled.return_value = True speech_planner._current_task = cancelled_task
await speech_planner._cancel_current_task() assert speech_planner._current_task is None
# Test with active task that needs cancellation active_task = Mock() active_task.done.return_value = False active_task.cancelled.return_value = False speech_planner._current_task = active_task
with patch.object(speech_planner, "cancel_task", new_callable=AsyncMock) as mock_cancel: await speech_planner._cancel_current_task() mock_cancel.assert_called_once_with(active_task) assert speech_planner._current_task is None
class TestCompletionDetection: """Test speech completion detection."""
@pytest.mark.asyncio async def test_process_complete_context_with_complete_prediction(self, speech_planner): """Test _process_complete_context with complete prediction.""" frame = TranscriptionFrame("Hello there", "user1", datetime.now())
# Mock the LLM response mock_chunks = [MockBaseMessageChunk("Label1")]
with patch.object(speech_planner, "_stream_chat_completions", new_callable=AsyncMock) as mock_stream: mock_stream.return_value.__aiter__.return_value = mock_chunks with patch.object(speech_planner, "push_frame", new_callable=AsyncMock) as mock_push: await speech_planner._process_complete_context(frame)
assert speech_planner.current_prediction == "Complete"
# Should push start/stop interruption frames and transcription assert mock_push.call_count >= 3
# Verify the correct frames were pushed call_args = [args[0][0] for args in mock_push.call_args_list] assert any(isinstance(f, StartInterruptionFrame) for f in call_args) assert any(isinstance(f, StopInterruptionFrame) for f in call_args) assert any(isinstance(f, TranscriptionFrame) for f in call_args)
@pytest.mark.asyncio async def test_process_complete_context_with_incomplete_prediction(self, speech_planner): """Test _process_complete_context with incomplete prediction.""" frame = TranscriptionFrame("Hello", "user1", datetime.now())
# Mock the LLM response with Label2 (incomplete) mock_chunks = [MockBaseMessageChunk("Label2")]
with patch.object(speech_planner, "_stream_chat_completions", new_callable=AsyncMock) as mock_stream: mock_stream.return_value.__aiter__.return_value = mock_chunks with patch.object(speech_planner, "push_frame", new_callable=AsyncMock) as mock_push: await speech_planner._process_complete_context(frame)
assert speech_planner.current_prediction == "Incomplete" # Should not push any frames for incomplete prediction mock_push.assert_not_called()
@pytest.mark.asyncio async def test_process_complete_context_with_error(self, speech_planner): """Test _process_complete_context handles errors gracefully with proper logging.""" frame = TranscriptionFrame("Hello", "user1", datetime.now())
# Mock an exception during processing with patch.object(speech_planner, "_stream_chat_completions", new_callable=AsyncMock) as mock_stream: mock_stream.side_effect = Exception("LLM service error")
# Patch logger to verify warning is logged with stack trace with patch("nvidia_pipecat.services.speech_planner.logger") as mock_logger: await speech_planner._process_complete_context(frame)
# Should default to "Complete" on error assert speech_planner.current_prediction == "Complete"
# Should log warning with stack trace information mock_logger.warning.assert_called_once() call_args = mock_logger.warning.call_args assert "Disabling Smart EOU detection due to error" in call_args[0][0] assert "LLM service error" in call_args[0][0] assert call_args[1]["exc_info"] is True
@pytest.mark.asyncio async def test_process_complete_context_with_chunk_content_error(self, speech_planner): """Test _process_complete_context handles chunk content errors gracefully.""" frame = TranscriptionFrame("Hello", "user1", datetime.now())
# Mock chunks where one has content that causes concatenation error class BadChunk: def __init__(self): # Use a number instead of None to pass the `if not chunk.content:` check # but still cause TypeError during string concatenation self.content = 42
mock_chunks = [MockBaseMessageChunk("Label"), BadChunk()]
with patch.object(speech_planner, "_stream_chat_completions", new_callable=AsyncMock) as mock_stream: mock_stream.return_value.__aiter__.return_value = mock_chunks
# Patch logger to verify debug message is logged for chunk error with patch("nvidia_pipecat.services.speech_planner.logger") as mock_logger: await speech_planner._process_complete_context(frame)
# Should still get a prediction (either from first chunk or default "Complete") assert speech_planner.current_prediction is not None
# Should log debug message for chunk content error debug_calls = [ call for call in mock_logger.debug.call_args_list if "Failed to append chunk content" in str(call) ] assert len(debug_calls) >= 1, ( f"Expected debug log for chunk error, got calls: {mock_logger.debug.call_args_list}" )
class TestClientCreation: """Test client creation functionality."""
def test_create_client_with_params(self, mock_prompt_file, mock_context): """Test create_client method with parameters.""" with patch("nvidia_pipecat.services.speech_planner.ChatNVIDIA") as mock_chat_class: mock_client = AsyncMock() mock_chat_class.return_value = mock_client
planner = SpeechPlanner( prompt_file=mock_prompt_file, context=mock_context, model="test-model", api_key="test-key", base_url="http://test-url", )
client = planner.create_client(api_key="custom-key", base_url="http://custom-url")
# Verify ChatNVIDIA was called with correct parameters mock_chat_class.assert_called_with(base_url="http://custom-url", model="test-model", api_key="custom-key") assert client == mock_client
@pytest.mark.asyncioasync def test_get_chat_completions(speech_planner): """Test get_chat_completions method.""" messages = [{"role": "user", "content": "Test message"}]
# Mock the client's astream method to return an async iterator mock_chunks = [MockBaseMessageChunk("Response chunk")]
async def mock_astream(*args, **kwargs): for chunk in mock_chunks: yield chunk
speech_planner._client.astream = mock_astream
result = await speech_planner.get_chat_completions(messages)
# The result should be the return value from astream assert result is not None
# Convert result to list to verify contents result_list = [] async for chunk in result: result_list.append(chunk)
assert len(result_list) == 1 assert result_list[0].content == "Response chunk"123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.# SPDX-License-Identifier: BSD 2-Clause License
"""Unit tests tracing."""
import pytestfrom opentelemetry import tracefrom pipecat.frames.frames import EndFrame, ErrorFrame, Frame, TextFramefrom pipecat.pipeline.pipeline import Pipelinefrom pipecat.processors.frame_processor import FrameDirection, FrameProcessor
from nvidia_pipecat.utils.tracing import AttachmentStrategy, traceable, tracedfrom tests.unit.utils import ignore_ids, run_test
@pytest.mark.asyncioasync def test_traced_processor_basic_usage(): """Tests basic tracing functionality in a pipeline processor.
Tests the @traceable and @traced decorators with different attachment strategies in a simple pipeline configuration.
The test verifies: - Span creation and attachment - Event recording - Nested span handling - Generator tracing - Frame processing """ tracer = trace.get_tracer(__name__)
@traceable class TestProcessor(FrameProcessor): """Example processor demonstrating how to use the tracing utilities."""
@traced(attachment_strategy=AttachmentStrategy.NONE) async def process_frame(self, frame, direction): """Process a frame with tracing.
Args: frame: The frame to process. direction: The direction of frame flow.
The method demonstrates: - Basic span creation - Event recording - Nested span handling - Multiple tracing strategies """ await super().process_frame(frame, direction) trace.get_current_span().add_event("Before inner") with tracer.start_as_current_span("inner") as span: span.add_event("inner event") await self.child() await self.linked() await self.none() trace.get_current_span().add_event("After inner") async for f in self.generator(): print(f"{f}") await super().push_frame(frame, direction)
@traced async def child(self): """Example method with child attachment strategy.
This span is attached as CHILD, meaning it will be attached to the class span if no parent exists, or to its parent otherwise. """ trace.get_current_span().add_event("child")
@traced(attachment_strategy=AttachmentStrategy.LINK) async def linked(self): """Example method with link attachment strategy.
This span is attached as LINK, meaning it will be attached to the class span but linked to its parent. """ trace.get_current_span().add_event("linked")
@traced(attachment_strategy=AttachmentStrategy.NONE) async def none(self): """Example method with no attachment strategy.
This span is attached as NONE, meaning it will be attached to the class span even if nested under another span. """ trace.get_current_span().add_event("none")
@traced async def generator(self): """Example generator method with tracing.
Demonstrates tracing in a generator context.
Yields: TextFrame: Text frames with sample content. """ yield TextFrame("Hello, ") trace.get_current_span().add_event("generated!") yield TextFrame("World")
processor = TestProcessor() pipeline = Pipeline([processor])
await run_test( pipeline, frames_to_send=[], expected_down_frames=[], )
@pytest.mark.asyncioasync def test_wrong_usage() -> None: """Test that error message is raised when a processor is not traceable."""
class TestProcessor(FrameProcessor): @traced async def process_frame(self, frame: Frame, direction: FrameDirection): await super().process_frame(frame, direction) await super().push_frame(frame, direction)
class HasSeenError(FrameProcessor): def __init__(self, **kwargs): super().__init__(**kwargs) self.seen_error = False
async def process_frame(self, frame: Frame, direction: FrameDirection): await super().process_frame(frame, direction) if isinstance(frame, ErrorFrame): self.seen_error = True elif isinstance(frame, EndFrame): assert self.seen_error await super().push_frame(frame, direction)
seen_error = HasSeenError() processor = TestProcessor() pipeline = Pipeline([seen_error, processor]) await run_test( pipeline, frames_to_send=[], expected_down_frames=[], expected_up_frames=[ ignore_ids(ErrorFrame("@traced annotation can only be used in classes inheriting from Traceable")) ], )
@pytest.mark.asyncioasync def test_no_processor() -> None: """Test that a processor can be used without a pipeline."""
@traceable class TestTraceable: @traced async def traced_test(self): trace.get_current_span().add_event("I can use it as another utility function as well.")
test = TestTraceable() await test.traced_test()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.# SPDX-License-Identifier: BSD 2-Clause License
"""Unit tests for transcript synchronization processors.
This module contains tests that verify the behavior of transcript synchronization processors,including both user and bot transcript synchronization with different TTS providers.The tests ensure proper handling of speech events, transcriptions, and TTS frames."""
import pytestfrom pipecat.frames.frames import ( BotStartedSpeakingFrame, BotStoppedSpeakingFrame, InterimTranscriptionFrame, StartInterruptionFrame, TranscriptionFrame, TTSStartedFrame, TTSStoppedFrame, TTSTextFrame, UserStartedSpeakingFrame, UserStoppedSpeakingFrame,)from pipecat.tests.utils import SleepFramefrom pipecat.utils.time import time_now_iso8601
from nvidia_pipecat.frames.transcripts import ( BotUpdatedSpeakingTranscriptFrame, UserStoppedSpeakingTranscriptFrame, UserUpdatedSpeakingTranscriptFrame,)from nvidia_pipecat.processors.transcript_synchronization import ( BotTranscriptSynchronization, UserTranscriptSynchronization,)from tests.unit.utils import ignore_ids, run_test
@pytest.mark.asyncio()async def test_user_transcript_synchronization_processor(): """Test the UserTranscriptSynchronization processor functionality.
Tests the complete flow of user speech transcription synchronization, including interim and final transcriptions.
The test verifies: - User speech start/stop handling - Interim transcription processing - Speaking transcript updates - Final transcript generation - Frame sequence ordering - Multiple speech segment handling """ user_id = "" interim_transcript_frames = [ InterimTranscriptionFrame("Hi", user_id, time_now_iso8601()), InterimTranscriptionFrame("Hi there!", user_id, time_now_iso8601()), InterimTranscriptionFrame("How are", user_id, time_now_iso8601()), InterimTranscriptionFrame("How are you?", user_id, time_now_iso8601()), ] finale_transcript_frame1 = TranscriptionFrame("Hi there!", user_id, time_now_iso8601()) finale_transcript_frame2 = TranscriptionFrame("How are you?", user_id, time_now_iso8601())
frames_to_send = [ UserStartedSpeakingFrame(), interim_transcript_frames[0], interim_transcript_frames[1], SleepFrame(0.1), UserStoppedSpeakingFrame(), finale_transcript_frame1, SleepFrame(0.1), UserStartedSpeakingFrame(), interim_transcript_frames[0], interim_transcript_frames[1], finale_transcript_frame1, interim_transcript_frames[2], interim_transcript_frames[3], finale_transcript_frame2, SleepFrame(0.1), UserStoppedSpeakingFrame(), ]
expected_down_frames = [ ignore_ids(UserStartedSpeakingFrame()), ignore_ids(UserUpdatedSpeakingTranscriptFrame("user started speaking")), ignore_ids(UserUpdatedSpeakingTranscriptFrame("Hi")), ignore_ids(UserUpdatedSpeakingTranscriptFrame("Hi there!")), ignore_ids(interim_transcript_frames[0]), ignore_ids(interim_transcript_frames[1]), ignore_ids(UserStoppedSpeakingFrame()), ignore_ids(UserStoppedSpeakingTranscriptFrame("Hi there!")), ignore_ids(finale_transcript_frame1), ignore_ids(UserStartedSpeakingFrame()), ignore_ids(UserUpdatedSpeakingTranscriptFrame("user started speaking")), ignore_ids(UserUpdatedSpeakingTranscriptFrame("Hi")), ignore_ids(UserUpdatedSpeakingTranscriptFrame("Hi there!")), ignore_ids(UserUpdatedSpeakingTranscriptFrame("Hi there! How are")), ignore_ids(UserUpdatedSpeakingTranscriptFrame("Hi there! How are you?")), ignore_ids(interim_transcript_frames[0]), ignore_ids(interim_transcript_frames[1]), ignore_ids(finale_transcript_frame1), ignore_ids(interim_transcript_frames[2]), ignore_ids(interim_transcript_frames[3]), ignore_ids(finale_transcript_frame2), ignore_ids(UserStoppedSpeakingFrame()), ignore_ids(UserStoppedSpeakingTranscriptFrame("Hi there! How are you?")), ]
await run_test( UserTranscriptSynchronization("user started speaking"), frames_to_send=frames_to_send, expected_down_frames=expected_down_frames, )
@pytest.mark.asyncio()async def test_bot_transcript_synchronization_processor_with_riva_tts(): """Test the BotTranscriptSynchronization processor with Riva TTS.
Tests the synchronization of bot transcripts when using Riva TTS, including speech events and interruption handling.
The test verifies: - Bot speech start/stop handling - TTS text frame processing - Speaking transcript updates - Interruption handling - Frame sequence ordering - Multiple sentence handling """ tts_text_frames = [ TTSTextFrame("Welcome user!"), TTSTextFrame("How are you?"), TTSTextFrame("Did you have a nice day?"), ]
frames_to_send = [ TTSStartedFrame(), # Bot sentence transcript 1 tts_text_frames[0], SleepFrame(0.1), # Give time for transcript to be buffered BotStartedSpeakingFrame(), # Start playing sentence 1 TTSStoppedFrame(), BotStoppedSpeakingFrame(), # End of playing sentence 1 SleepFrame(0.1), TTSStartedFrame(), # Bot sentence transcript 2 tts_text_frames[1], SleepFrame(0.1), # Give time for transcript to be buffered BotStartedSpeakingFrame(), # Start playing sentence 2 TTSStoppedFrame(), BotStoppedSpeakingFrame(), # End of playing sentence 2 SleepFrame(0.1), TTSStartedFrame(), # Bot sentence transcript 3 tts_text_frames[2], SleepFrame(0.1), # Give time for transcript to be buffered BotStartedSpeakingFrame(), # Start playing sentence 3 TTSStoppedFrame(), BotStoppedSpeakingFrame(), # End of playing sentence 3 SleepFrame(0.1), StartInterruptionFrame(), # User interrupts TTSStartedFrame(), # Bot sentence 1 again tts_text_frames[0], SleepFrame(0.1), # Give time for transcript to be buffered BotStartedSpeakingFrame(), # Start playing sentence 1 TTSStoppedFrame(), BotStoppedSpeakingFrame(), # End of playing sentence 1 ]
expected_down_frames = [ ignore_ids(TTSStartedFrame()), ignore_ids(tts_text_frames[0]), ignore_ids(BotStartedSpeakingFrame()), ignore_ids(BotUpdatedSpeakingTranscriptFrame("Welcome user!")), ignore_ids(BotStoppedSpeakingFrame()), ignore_ids(TTSStoppedFrame()), ignore_ids(TTSStartedFrame()), ignore_ids(tts_text_frames[1]), ignore_ids(BotStartedSpeakingFrame()), ignore_ids(BotUpdatedSpeakingTranscriptFrame("How are you?")), ignore_ids(BotStoppedSpeakingFrame()), ignore_ids(TTSStoppedFrame()), ignore_ids(TTSStartedFrame()), ignore_ids(tts_text_frames[2]), ignore_ids(BotStartedSpeakingFrame()), ignore_ids(BotUpdatedSpeakingTranscriptFrame("Did you have a nice day?")), ignore_ids(BotStoppedSpeakingFrame()), ignore_ids(TTSStoppedFrame()), ignore_ids(StartInterruptionFrame()), ignore_ids(TTSStartedFrame()), ignore_ids(tts_text_frames[0]), ignore_ids(BotStartedSpeakingFrame()), ignore_ids(BotUpdatedSpeakingTranscriptFrame("Welcome user!")), ignore_ids(BotStoppedSpeakingFrame()), ignore_ids(TTSStoppedFrame()), ]
await run_test( BotTranscriptSynchronization(), frames_to_send=frames_to_send, expected_down_frames=expected_down_frames, )
@pytest.mark.asyncio()async def test_bot_transcript_synchronization_processor_with_elevenlabs_tts(): """Test the BotTranscriptSynchronization processor with ElevenLabs TTS.
Tests the synchronization of bot transcripts when using ElevenLabs TTS, including partial text handling and concatenation.
The test verifies: - Bot speech start/stop handling - Partial TTS text processing - Speaking transcript updates - Text concatenation - Frame sequence ordering - Complete transcript assembly """ tts_text_frames = [ TTSTextFrame("Welcome"), TTSTextFrame("user!"), TTSTextFrame("How"), TTSTextFrame("are"), TTSTextFrame("you?"), ]
frames_to_send = [ TTSStartedFrame(), tts_text_frames[0], SleepFrame(0.1), BotStartedSpeakingFrame(), tts_text_frames[1], tts_text_frames[2], tts_text_frames[3], tts_text_frames[4], SleepFrame(0.1), TTSStoppedFrame(), SleepFrame(0.1), BotStoppedSpeakingFrame(), ]
expected_down_frames = [ ignore_ids(TTSStartedFrame()), ignore_ids(tts_text_frames[0]), ignore_ids(BotStartedSpeakingFrame()), ignore_ids(BotUpdatedSpeakingTranscriptFrame("Welcome")), ignore_ids(BotUpdatedSpeakingTranscriptFrame("Welcome user!")), ignore_ids(BotUpdatedSpeakingTranscriptFrame("Welcome user! How")), ignore_ids(BotUpdatedSpeakingTranscriptFrame("Welcome user! How are")), ignore_ids(BotUpdatedSpeakingTranscriptFrame("Welcome user! How are you?")), ignore_ids(tts_text_frames[1]), ignore_ids(tts_text_frames[2]), ignore_ids(tts_text_frames[3]), ignore_ids(tts_text_frames[4]), ignore_ids(TTSStoppedFrame()), ignore_ids(BotStoppedSpeakingFrame()), ]
await run_test( BotTranscriptSynchronization(), frames_to_send=frames_to_send, expected_down_frames=expected_down_frames, )123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.# SPDX-License-Identifier: BSD 2-Clause License
"""Unit tests for the user presence frame processor."""
import pytestfrom pipecat.frames.frames import ( FilterControlFrame, LLMUpdateSettingsFrame, StartInterruptionFrame, TextFrame, TTSSpeakFrame, TTSStartedFrame, UserStartedSpeakingFrame,)from pipecat.tests.utils import SleepFrame
from nvidia_pipecat.frames.action import FinishedPresenceUserActionFrame, StartedPresenceUserActionFramefrom nvidia_pipecat.processors.user_presence import UserPresenceProcesssorfrom tests.unit.utils import ignore, run_test
@pytest.mark.asyncioasync def test_user_presence_start(): """Tests user presence start handling with welcome message.
Tests that the processor correctly handles user presence start events and generates appropriate welcome messages.
Args: None
Returns: None
The test verifies: - StartedPresenceUserActionFrame processing - Welcome message generation - UserStartedSpeakingFrame handling - Frame sequence ordering - Message content accuracy """ user_presence_bot = UserPresenceProcesssor(welcome_msg="Hey there!") frames_to_send = [StartedPresenceUserActionFrame(action_id=123), SleepFrame(0.01), UserStartedSpeakingFrame()] expected_down_frames = [ ignore(StartedPresenceUserActionFrame(action_id=123), "ids", "timestamps"), ignore(TTSSpeakFrame("Hey there!"), "ids"), ignore(UserStartedSpeakingFrame(), "ids", "timestamps"), ]
await run_test(user_presence_bot, frames_to_send=frames_to_send, expected_down_frames=expected_down_frames)
@pytest.mark.asyncioasync def test_user_presence_finished(): """Tests user presence finish handling with farewell message.
Tests that the processor correctly handles user presence finish events and generates appropriate farewell messages.
Args: None
Returns: None
The test verifies: - StartedPresenceUserActionFrame processing - FinishedPresenceUserActionFrame handling - Farewell message generation - StartInterruptionFrame sequencing - Frame ordering - Message content accuracy """ user_presence_bot = UserPresenceProcesssor(farewell_msg="Bye bye!") frames_to_send = [ StartedPresenceUserActionFrame(action_id=123), SleepFrame(0.5), FinishedPresenceUserActionFrame(action_id=123), SleepFrame(0.5), ]
expected_down_frames = [ ignore(StartedPresenceUserActionFrame(action_id=123), "ids", "timestamps"), ignore(TTSSpeakFrame("Hello"), "ids"), # WAR: The StartInterruptionFrame is sent in response to the FinishedPresenceUserActionFrame. # However, the test framework consistently logs it in the sequence prior to the FinishedPresenceUserFrame ignore(StartInterruptionFrame(), "ids", "timestamps"), ignore(FinishedPresenceUserActionFrame(action_id=123), "ids", "timestamps"), ignore(TTSSpeakFrame("Bye bye!"), "ids"), ]
await run_test(user_presence_bot, frames_to_send=frames_to_send, expected_down_frames=expected_down_frames)
@pytest.mark.asyncioasync def test_user_presence(): """Tests behavior without presence frames.
Tests that no welcome/farewell messages are sent when no presence frames are received.
Args: None
Returns: None
The test verifies: - No messages without presence frames - UserStartedSpeakingFrame handling - Frame filtering behavior """ user_presence_bot = UserPresenceProcesssor(welcome_msg="Hello", farewell_msg="Bye bye!") frames_to_send = [UserStartedSpeakingFrame()] expected_down_frames = []
await run_test(user_presence_bot, frames_to_send=frames_to_send, expected_down_frames=expected_down_frames)
@pytest.mark.asyncioasync def test_user_presence_system_frames(): """Tests system and control frame handling.
Tests that system and control frames are processed regardless of user presence state.
Args: None
Returns: None
The test verifies: - TTSStartedFrame processing - FilterControlFrame handling - LLMUpdateSettingsFrame processing - TextFrame filtering - Frame passthrough behavior - Frame sequence preservation """ user_presence_bot = UserPresenceProcesssor(welcome_msg="Hello", farewell_msg="Bye bye!")
frames_to_send = [ TTSStartedFrame(), FilterControlFrame(), LLMUpdateSettingsFrame(settings="ABC"), TextFrame("How are you?"), ]
expected_down_frames = [ ignore(TTSStartedFrame(), "ids", "timestamps"), ignore(FilterControlFrame(), "ids", "timestamps"), ignore(LLMUpdateSettingsFrame(settings="ABC"), "ids", "timestamps"), ]
await run_test(user_presence_bot, frames_to_send=frames_to_send, expected_down_frames=expected_down_frames)1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495"""Unit tests for utility processors."""
import pytestfrom pipecat.frames.frames import Frame, TextFrame
from nvidia_pipecat.processors.utils import FrameBlockingProcessorfrom tests.unit.utils import ignore_ids, run_test
class TestFrame(Frame): """Test frame for testing FrameBlockingProcessor."""
pass
class TestResetFrame(Frame): """Test frame for testing FrameBlockingProcessor reset."""
pass
@pytest.mark.asyncio()async def test_frame_blocking_processor(): """Test that FrameBlockingProcessor blocks frames after threshold.
Verifies that: - Frames are passed through until threshold is reached - Frames are blocked after threshold - Non-matching frame types are always passed through """ # Create processor that blocks after 2 TextFrames processor = FrameBlockingProcessor(block_after_frame=2, frame_type=TextFrame)
# Create test frames - mix of TextFrames and StartFrames frames_to_send = [ TestFrame(), # Should pass through TextFrame("First"), # Should pass through TextFrame("Second"), # Should pass through TextFrame("Third"), # Should be blocked TestFrame(), # Should pass through TextFrame("Fourth"), # Should be blocked ]
# Expected frames - all StartFrames and first 2 TextFrames expected_down_frames = [ ignore_ids(TestFrame()), ignore_ids(TextFrame("First")), ignore_ids(TextFrame("Second")), ignore_ids(TestFrame()), ]
await run_test( processor, frames_to_send=frames_to_send, expected_down_frames=expected_down_frames, )
@pytest.mark.asyncio()async def test_frame_blocking_processor_with_reset(): """Test that FrameBlockingProcessor resets counter on reset frame type.
Verifies that: - Counter resets when reset frame type is received - Frames are blocked after threshold - Counter can be reset multiple times """ # Create processor that blocks after 2 TextFrames and resets on StartFrame processor = FrameBlockingProcessor(block_after_frame=2, frame_type=TextFrame, reset_frame_type=TestResetFrame)
# Create test frames - mix of TextFrames and TestResetFrame frames_to_send = [ TextFrame("First"), # Should pass through TextFrame("Second"), # Should pass through TextFrame("Third"), # Should be blocked TestResetFrame(), # Should reset counter and pass through TextFrame("Fourth"), # Should pass through (counter reset) TextFrame("Fifth"), # Should pass through TextFrame("Sixth"), # Should be blocked ]
# Expected frames - all TestResetFrame and TextFrames except blocked ones expected_down_frames = [ ignore_ids(TextFrame("First")), ignore_ids(TextFrame("Second")), ignore_ids(TestResetFrame()), ignore_ids(TextFrame("Fourth")), ignore_ids(TextFrame("Fifth")), ]
await run_test( processor, frames_to_send=frames_to_send, expected_down_frames=expected_down_frames, )123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.# SPDX-License-Identifier: BSD 2-Clause License
"""Utility functions and classes for testing pipelines and processors.
This module provides various test utilities including frame processors, test runners,and helper functions for frame comparison in tests."""
import asynciofrom asyncio.tasks import sleepfrom collections.abc import Sequencefrom copy import copyfrom dataclasses import dataclassfrom datetime import datetime, timedeltafrom typing import Anyfrom unittest.mock import ANY
import numpy as npfrom pipecat.frames.frames import EndFrame, Frame, InputAudioRawFrame, StartFramefrom pipecat.pipeline.pipeline import Pipelinefrom pipecat.pipeline.runner import PipelineRunnerfrom pipecat.pipeline.task import PipelineParams, PipelineTaskfrom pipecat.processors.frame_processor import FrameDirection, FrameProcessorfrom pipecat.tests.utils import QueuedFrameProcessorfrom pipecat.tests.utils import run_test as run_pipecat_test
from nvidia_pipecat.frames.action import ActionFrame
def ignore(frame: Frame, *args: str): """Return a copy of the frames with attributes listed in *args set to unittest.mock.ANY.
Any attribute listed in args will be ignored when using comparisons against the returned frame.
Args: frame (Frame): Frame to create a copy from and set selected attributes to unittest.mock.ANY. *args (str): Attribute names. Special values to ignore common sets of attributes: 'ids': ignore standard frame attributes related to frame ids 'all_ids': ignore standard frame attributes related to frame and action ids 'timestamps': ignore standard frame attributes containing timestamps
Returns: Frame: A copy of the input frame with specified attributes set to ANY.
Raises: ValueError: If an attribute name is not found in the frame. """ new_frame = copy(frame) for arg in args: is_updated = False if arg == "all_ids" or arg == "ids": new_frame.id = ANY new_frame.name = ANY is_updated = True if arg == "all_ids": new_frame.action_id = ANY is_updated = True if arg == "timestamps": new_frame.pts = ANY new_frame.action_started_at = ANY new_frame.action_finished_at = ANY new_frame.action_updated_at = ANY is_updated = True
if hasattr(new_frame, arg): setattr(new_frame, arg, ANY) elif not is_updated: raise ValueError( f"Frame {frame.__class__.__name__} has not attribute '{arg}' to ignore. Did you misspell it?" ) return new_frame
def ignore_ids(frame: Frame) -> Frame: """Return a copy of the frame that matches any id and name.
This is useful if you do not want to assert a specific ID and name.
Args: frame (Frame): The frame to create a copy from.
Returns: Frame: A copy of the input frame with ID and name set to ANY. """ new_frame = copy(frame) new_frame.id = ANY new_frame.name = ANY if isinstance(frame, ActionFrame): new_frame.action_id = ANY return new_frame
def ignore_timestamps(frame: Frame) -> Frame: """Return a copy of the frame that matches frames ignoring any timestamps.
Args: frame (Frame): The frame to create a copy from.
Returns: Frame: A copy of the input frame with all timestamp fields set to ANY. """ new_frame = copy(frame) new_frame.pts = ANY new_frame.action_started_at = ANY new_frame.action_finished_at = ANY new_frame.action_updated_at = ANY return new_frame
@dataclassclass FrameHistoryEntry: """Storing a frame and the frame direction.
Attributes: frame (Frame): The stored frame. direction (FrameDirection): The direction of the frame in the pipeline. """
frame: Frame direction: FrameDirection
class FrameStorage(FrameProcessor): """A frame processor that stores all received frames in memory for inspection.
This processor maintains a history of all frames that pass through it, along with their direction, allowing for later inspection and verification in tests.
Attributes: history (list[FrameHistoryEntry]): List of all frames that have passed through the processor. """
def __init__(self, **kwargs) -> None: """Initialize the FrameStorage processor.
Args: **kwargs: Additional keyword arguments passed to the parent FrameProcessor. """ super().__init__(**kwargs) self.history: list[FrameHistoryEntry] = []
async def process_frame(self, frame: Frame, direction: FrameDirection): """Process an incoming frame by storing it in history and forwarding it.
Args: frame (Frame): The frame to process. direction (FrameDirection): The direction the frame is traveling in the pipeline. """ await super().process_frame(frame, direction)
self.history.append(FrameHistoryEntry(frame, direction)) await self.push_frame(frame, direction)
def frames_of_type(self, t) -> list[Frame]: """Get all frames of a specific type from the history.
Args: t: The type of frames to filter for.
Returns: list[Frame]: List of all frames matching the specified type. """ return [e.frame for e in self.history if isinstance(e.frame, t)]
async def wait_for_frame(self, frame: Frame, timeout: timedelta = timedelta(seconds=5.0)) -> None: """Block until a matching frame is found in history.
Args: frame (Frame): The frame to wait for. timeout (timedelta, optional): Maximum time to wait. Defaults to 5 seconds.
Raises: TimeoutError: If the frame is not found within the timeout period. """ candidates = {}
def is_same_frame(frame_a, frame_b) -> bool: if type(frame_a) is not type(frame_b): return False
if frame_a == frame_b: return True else: candidates[frame_a.id] = frame_a.__repr__() return False
found = False start_time = datetime.now() while not found: found = any([is_same_frame(entry.frame, frame) for entry in self.history]) if not found: if datetime.now() - start_time > timeout: raise TimeoutError( "Frame not found until timeout reached.\n" f"EXPECTED:\n{frame.__repr__()}\n" f"FOUND\n{'\n'.join(candidates.values())}" ) await asyncio.sleep(0.01)
class SinusWaveProcessor(FrameProcessor): """A frame processor that generates a sine wave audio signal.
This processor generates a continuous sine wave at 440 Hz (A4 note) when started, and outputs it as audio frames. It is useful for testing audio processing pipelines.
Attributes: duration (timedelta): The total duration of the sine wave to generate. chunk_duration (float): Duration of each audio chunk in seconds. audio_frame_count (int): Total number of audio frames to generate. audio_task (asyncio.Task | None): Task handling the audio generation. """
def __init__( self, *, duration: timedelta, **kwargs, ): """Initialize the SinusWaveProcessor.
Args: duration (timedelta): The total duration of the sine wave to generate. **kwargs: Additional keyword arguments passed to the parent FrameProcessor. """ super().__init__(**kwargs) self.duration = duration self.audio_task: asyncio.Task | None = None
self.chunk_duration = 0.02 # 20 milliseconds self.audio_frame_count = round(self.duration.total_seconds() / self.chunk_duration)
async def _cancel_audio_task(self): """Cancel the running audio generation task if it exists.""" if self.audio_task and not self.audio_task.done(): await self.cancel_task(self.audio_task) self.audio_task = None
async def stop(self): """Stop the audio generation by canceling the audio task.""" await self._cancel_audio_task()
async def cleanup(self): """Clean up resources by stopping audio generation and calling parent cleanup.""" await super().cleanup() await self._cancel_audio_task()
async def process_frame(self, frame: Frame, direction: FrameDirection): """Process incoming frames to start/stop audio generation.
Starts audio generation on StartFrame and stops on EndFrame.
Args: frame (Frame): The frame to process. direction (FrameDirection): The direction the frame is traveling in the pipeline. """ await super().process_frame(frame, direction) if isinstance(frame, StartFrame): self.audio_task = self.create_task(self.start()) if isinstance(frame, EndFrame): await self.stop() await super().push_frame(frame, direction)
async def start(self): """Start generating and outputting sine wave audio frames.
Generates a continuous 440 Hz sine wave, split into 20ms chunks, and outputs them as InputAudioRawFrame instances. """ sample_rate = 16000 # Hz frequency = 440 # Hz (A4 tone)
phase_offset = 0 for _ in range(self.audio_frame_count): chunk_samples = int(sample_rate * self.chunk_duration)
# Generate the time axis t = np.arange(chunk_samples) / sample_rate
# Create the sine wave with the given phase offset sine_wave = 0.5 * np.sin(2 * np.pi * frequency * t + phase_offset)
# Calculate the new phase offset for the next chunk phase_offset = (2 * np.pi * frequency * self.chunk_duration + phase_offset) % (2 * np.pi)
sine_wave_pcm = (sine_wave * 32767).astype(np.int16)
pipecat_frame = InputAudioRawFrame(audio=sine_wave_pcm.tobytes(), sample_rate=sample_rate, num_channels=1) await super().push_frame(pipecat_frame)
await sleep(self.chunk_duration)
async def run_test( processor: FrameProcessor, *, frames_to_send: Sequence[Frame], expected_down_frames: Sequence[Frame], expected_up_frames: Sequence[Frame] = [], ignore_start: bool = True, start_metadata: dict[str, Any] | None = None, send_end_frame: bool = True,) -> tuple[Sequence[Frame], Sequence[Frame]]: """Run a test on a frame processor with predefined input and expected output frames.
Args: processor (FrameProcessor): The processor to test. frames_to_send (Sequence[Frame]): Frames to send through the processor. expected_down_frames (Sequence[Frame]): Expected frames in downstream direction. expected_up_frames (Sequence[Frame], optional): Expected frames in upstream direction. Defaults to []. ignore_start (bool, optional): Whether to ignore start frames. Defaults to True. start_metadata (dict[str, Any], optional): Metadata to include in start frame. Defaults to None. send_end_frame (bool, optional): Whether to send an end frame. Defaults to True.
Returns: tuple[Sequence[Frame], Sequence[Frame]]: Tuple of (received downstream frames, received upstream frames).
Raises: AssertionError: If received frames don't match expected frames. """ if start_metadata is None: start_metadata = {} received_down_frames, received_up_frames = await run_pipecat_test( processor, frames_to_send=frames_to_send, expected_down_frames=[f.__class__ for f in expected_down_frames], expected_up_frames=[f.__class__ for f in expected_up_frames], ignore_start=ignore_start, start_metadata=start_metadata, send_end_frame=send_end_frame, )
for real, expected in zip(received_up_frames, expected_up_frames, strict=True): assert real == expected, f"Frame mismatch: \nreal: {repr(real)} \nexpected: {repr(expected)}"
for real, expected in zip(received_down_frames, expected_down_frames, strict=True): assert real == expected, f"Frame mismatch: \nreal: {repr(real)} \nexpected: {repr(expected)}"
return received_down_frames, received_up_frames
async def run_interactive_test( processor: FrameProcessor, *, test_coroutine, start_metadata: dict[str, Any] | None = None, ignore_start: bool = True, send_end_frame: bool = True,) -> tuple[Sequence[Frame], Sequence[Frame]]: """Run an interactive test on a frame processor with a custom test coroutine.
This function allows for more complex testing scenarios where frames need to be sent and received dynamically during the test.
Args: processor (FrameProcessor): The processor to test. test_coroutine: Coroutine function that implements the test logic. start_metadata (dict[str, Any], optional): Metadata to include in start frame. Defaults to None. ignore_start (bool, optional): Whether to ignore start frames. Defaults to True. send_end_frame (bool, optional): Whether to send an end frame. Defaults to True.
Returns: tuple[Sequence[Frame], Sequence[Frame]]: Tuple of (received downstream frames, received upstream frames). """ if start_metadata is None: start_metadata = {} received_up = asyncio.Queue() received_down = asyncio.Queue() source = QueuedFrameProcessor( queue=received_up, queue_direction=FrameDirection.UPSTREAM, ignore_start=ignore_start, ) sink = QueuedFrameProcessor( queue=received_down, queue_direction=FrameDirection.DOWNSTREAM, ignore_start=ignore_start, )
pipeline = Pipeline([source, processor, sink])
task = PipelineTask(pipeline, params=PipelineParams(start_metadata=start_metadata))
async def run_test(): # Just give a little head start to the runner. await asyncio.sleep(0.01) await test_coroutine(task)
if send_end_frame: await task.queue_frame(EndFrame())
# await asyncio.sleep(1.5) # debug = "" # for t in processor.get_task_manager().current_tasks(): # debug += f"\nMy Task {t.get_name()} is still running."
# assert False, debug # print(debug)
runner = PipelineRunner() await asyncio.gather(runner.run(task), run_test())
# # Down frames # received_down_frames: list[Frame] = [] while not received_down.empty(): frame = await received_down.get() if not isinstance(frame, EndFrame) or not send_end_frame: received_down_frames.append(frame)
print("received DOWN frames =", received_down_frames)
# # Up frames # received_up_frames: list[Frame] = [] while not received_up.empty(): frame = await received_up.get() received_up_frames.append(frame)
print("received UP frames =", received_up_frames)
return (received_down_frames, received_up_frames).gitattributes+1
.gitignore+2
examples/voice_agent_webrtc_langgraph/start.sh+13
tests/__init__.pyDeleted-4
tests/perf/README.mdDeleted-101
tests/perf/file_input_client.pyDeleted-581
tests/perf/run_multi_client_benchmark.shDeleted-414
tests/perf/ttfb_analyzer.pyDeleted-253
tests/unit/__init__.pyDeleted-4
tests/unit/configs/animation_config.yamlDeleted-346
tests/unit/configs/test_speech_planner_prompt.yamlDeleted-15
tests/unit/test_ace_websocket_serializer.pyDeleted-147
tests/unit/test_acknowledgment.pyDeleted-71
tests/unit/test_animation_graph_services.pyDeleted-668
tests/unit/test_audio2face_3d_service.pyDeleted-182
tests/unit/test_audio_util.pyDeleted-64
tests/unit/test_basic_pipelines.pyDeleted-130
tests/unit/test_blingfire_text_aggregator.pyDeleted-244
tests/unit/test_custom_view.pyDeleted-203
tests/unit/test_elevenlabs.pyDeleted-184
tests/unit/test_frame_creation.pyDeleted-148
tests/unit/test_gesture.pyDeleted-94
tests/unit/test_guardrail.pyDeleted-110
tests/unit/test_message_broker.pyDeleted-111
tests/unit/test_nvidia_aggregators.pyDeleted-396
tests/unit/test_nvidia_llm_service.pyDeleted-386
tests/unit/test_nvidia_rag_service.pyDeleted-261
tests/unit/test_nvidia_tts_response_cacher.pyDeleted-79
tests/unit/test_posture.pyDeleted-104
tests/unit/test_proactivity.pyDeleted-85
tests/unit/test_riva_asr_service.pyDeleted-523
tests/unit/test_riva_nmt_service.pyDeleted-197
tests/unit/test_riva_tts_service.pyDeleted-301
tests/unit/test_speech_planner.pyDeleted-546
tests/unit/test_traced_processor.pyDeleted-159
tests/unit/test_transcription_sync_processors.pyDeleted-262
tests/unit/test_user_presence.pyDeleted-157
tests/unit/test_utils.pyDeleted-95
tests/unit/utils.pyDeleted-428