File size: 4,347 Bytes
66fef64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
"""
Test script for TMDB data loading and embedding generation
Run this to validate your setup before building the full index
"""
import os
import sys
import json
from settings import get_settings
from build_index import TMDBClient, create_composite_text, get_embeddings_batch
from openai import OpenAI

def test_tmdb_connection():
    """Test TMDB API connection"""
    print("πŸ” Testing TMDB API connection...")
    
    try:
        settings = get_settings()
        tmdb_client = TMDBClient(settings.tmdb_api_key)
        
        # Test getting popular movies (just first page)
        movie_ids = tmdb_client.get_popular_movies(max_pages=1)
        
        if movie_ids:
            print(f"βœ… Successfully fetched {len(movie_ids)} movie IDs from TMDB")
            
            # Test getting details for first movie
            movie_data = tmdb_client.get_movie_details(movie_ids[0])
            if movie_data:
                print(f"βœ… Successfully fetched details for movie: {movie_data.get('title', 'Unknown')}")
                
                # Test getting credits
                credits = tmdb_client.get_movie_credits(movie_ids[0])
                if credits:
                    print(f"βœ… Successfully fetched credits (cast: {len(credits.get('cast', []))}, crew: {len(credits.get('crew', []))})")
                else:
                    print("⚠️  Could not fetch credits")
                    
                return movie_data, credits
            else:
                print("❌ Could not fetch movie details")
        else:
            print("❌ Could not fetch movie IDs")
            
    except Exception as e:
        print(f"❌ TMDB API error: {e}")
        
    return None, None

def test_composite_text(movie_data, credits):
    """Test composite text creation"""
    print("\nπŸ“ Testing composite text creation...")
    
    if movie_data:
        # Add credits to movie data
        if credits:
            movie_data['credits'] = credits
            
        composite_text = create_composite_text(movie_data)
        print(f"βœ… Generated composite text ({len(composite_text)} chars)")
        print(f"Preview: {composite_text[:200]}...")
        return composite_text
    else:
        print("❌ No movie data to test")
        return None

def test_embeddings(composite_text):
    """Test embedding generation"""
    print("\nπŸ€– Testing embedding generation...")
    
    if composite_text:
        try:
            settings = get_settings()
            openai_client = OpenAI(api_key=settings.openai_api_key)
            
            embeddings = get_embeddings_batch([composite_text], openai_client)
            
            if embeddings:
                embedding = embeddings[0]
                print(f"βœ… Generated embedding (dimension: {len(embedding)})")
                print(f"Sample values: {embedding[:5]}...")
                return embedding
            else:
                print("❌ No embeddings generated")
                
        except Exception as e:
            print(f"❌ Embedding error: {e}")
    else:
        print("❌ No composite text to test")
        
    return None

def main():
    """Run all tests"""
    print("🎬 Karl Movie Vector Backend - Test Suite")
    print("=" * 50)
    
    # Test environment variables
    print("πŸ”§ Checking environment variables...")
    try:
        settings = get_settings()
        print(f"βœ… OpenAI API key: {'sk-...' + settings.openai_api_key[-10:] if settings.openai_api_key else 'Not set'}")
        print(f"βœ… TMDB API key: {'...' + settings.tmdb_api_key[-10:] if settings.tmdb_api_key else 'Not set'}")
    except Exception as e:
        print(f"❌ Settings error: {e}")
        print("Make sure you have a .env file with OPENAI_API_KEY and TMDB_API_KEY")
        return
    
    # Run tests
    movie_data, credits = test_tmdb_connection()
    composite_text = test_composite_text(movie_data, credits)
    embedding = test_embeddings(composite_text)
    
    print("\n" + "=" * 50)
    if movie_data and composite_text and embedding:
        print("πŸŽ‰ All tests passed! You can now run the full build:")
        print("   python app/build_index.py --max-pages 3")
    else:
        print("❌ Some tests failed. Check your API keys and internet connection.")

if __name__ == "__main__":
    main()