xiaozhi-esp32-server/main/livekit-server/MUSIC_AND_STORY_FUNCTIONS.md
2025-09-16 21:08:31 +05:30

4.7 KiB

Music and Story Function Calling Implementation

Overview

The LiveKit agent now supports function calling for playing music and stories using semantic search and AWS CloudFront CDN streaming.

Features Added

1. Function Tools

  • play_music(song_name?, language?) - Play music (specific or random)
  • play_story(story_name?, category?) - Play stories (specific or random)
  • stop_audio() - Stop any currently playing audio
  • Semantic Search: Uses text similarity scoring for better matches
  • Alternative Names: Searches through alternative titles and romanized versions
  • Language/Category Filtering: Optional filtering by language or story category
  • Fallback to Random: If no specific match found, plays random content

3. Content Organization

  • Music: Organized by language folders (English, Hindi, Telugu, etc.)
  • Stories: Organized by category folders (Adventure, Bedtime, Educational, etc.)
  • Metadata Structure: JSON files with title, filename, romanized text, and alternatives

4. CDN Integration

  • AWS CloudFront: Fast global content delivery
  • S3 Fallback: Direct S3 access if CDN fails
  • URL Encoding: Proper handling of special characters in filenames

Usage Examples

Voice Commands That Work:

  • "Play music" / "Play a song" / "Sing something" → Random music
  • "Play Baby Shark" → Searches for specific song
  • "Play Hindi music" → Random Hindi song
  • "Play a story" / "Tell me a story" → Random story
  • "Play a bedtime story" → Random from Bedtime category
  • "Tell me about Bertie" → Searches for Bertie stories
  • "Stop the music" / "Stop audio" → Stops playback

Function Parameters:

# Play random music
await play_music(context)

# Play specific song
await play_music(context, song_name="baby shark")

# Play music in specific language
await play_music(context, language="Hindi")

# Play specific song in specific language
await play_music(context, song_name="twinkle star", language="English")

# Similar for stories
await play_story(context, story_name="bertie", category="Adventure")

Architecture

Services Structure:

src/services/
├── __init__.py
├── music_service.py      # Music search and URL generation
├── story_service.py      # Story search and URL generation
├── audio_player.py       # LiveKit audio streaming
└── semantic_search.py    # Smart search functionality

Data Flow:

  1. User Voice → LiveKit STT → LLM
  2. LLM → Calls function tool with parameters
  3. Service → Searches metadata using semantic search
  4. Audio Player → Downloads from CDN and streams to LiveKit
  5. LiveKit → Streams audio to user

Content Structure:

src/
├── music/
│   ├── English/metadata.json
│   ├── Hindi/metadata.json
│   └── Telugu/metadata.json
└── stories/
    ├── Adventure/metadata.json
    ├── Bedtime/metadata.json
    └── Educational/metadata.json

Configuration

Environment Variables:

CLOUDFRONT_DOMAIN=dbtnllz9fcr1z.cloudfront.net
S3_BASE_URL=https://cheeko-audio-files.s3.us-east-1.amazonaws.com
USE_CDN=true

Dependencies Added:

pydub                    # Audio processing
aiohttp                  # HTTP client for downloading
qdrant-client           # Optional: Enhanced semantic search
sentence-transformers   # Optional: Enhanced semantic search

Current Status

✅ Working Features:

  • Function calling for music and stories
  • Semantic text search with similarity scoring
  • Random content selection
  • Language/category filtering
  • CDN URL generation
  • LiveKit integration (main.py updated)

✅ Testing:

  • All services initialize correctly
  • Search functionality works with scoring
  • Random selection works
  • Function tools respond correctly
  • URL generation works for both music and stories

Next Steps (Optional Enhancements)

  1. Full Semantic Search: Install qdrant-client and sentence-transformers for vector-based search
  2. Audio Caching: Cache frequently played content locally
  3. Playlist Support: Queue multiple songs/stories
  4. User Preferences: Remember user's favorite languages/categories
  5. Analytics: Track most played content
  6. Voice Commands: Add pause/resume/skip functionality

Test Results

The test script shows everything working:

  • ✅ Music service loads 7 languages with semantic search
  • ✅ Story service loads 5 categories with semantic search
  • ✅ Random content selection works
  • ✅ Search finds relevant matches with scores
  • ✅ Function tools return proper responses
  • ✅ URLs are properly generated for CloudFront CDN

Ready for production use!