File size: 6,918 Bytes
7644eac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
"""
Test script to validate LangSmith and W&B API keys and setup.
Run this before generating learning paths to ensure observability is working.
"""

import sys
import os
from pathlib import Path

# Add project root to path
sys.path.insert(0, str(Path(__file__).parent))

print("=" * 60)
print("πŸ” Testing Observability Setup")
print("=" * 60)

# Test 1: Check environment variables
print("\n1️⃣ Checking environment variables...")
from src.utils.config import (
    LANGCHAIN_TRACING_V2,
    LANGCHAIN_API_KEY,
    LANGCHAIN_PROJECT,
    WANDB_API_KEY,
    WANDB_PROJECT,
    WANDB_ENTITY,
    WANDB_MODE
)

print(f"   LANGCHAIN_TRACING_V2: {LANGCHAIN_TRACING_V2}")
print(f"   LANGCHAIN_API_KEY: {'βœ… Set' if LANGCHAIN_API_KEY else '❌ Missing'}")
print(f"   LANGCHAIN_PROJECT: {LANGCHAIN_PROJECT}")
print(f"   WANDB_API_KEY: {'βœ… Set' if WANDB_API_KEY else '❌ Missing'}")
print(f"   WANDB_PROJECT: {WANDB_PROJECT}")
print(f"   WANDB_ENTITY: {WANDB_ENTITY or 'Not set (will use default)'}")
print(f"   WANDB_MODE: {WANDB_MODE}")

if not LANGCHAIN_API_KEY:
    print("\n❌ LangSmith API key is missing!")
    print("   Add LANGCHAIN_API_KEY to your .env file")
    sys.exit(1)

if not WANDB_API_KEY:
    print("\n❌ W&B API key is missing!")
    print("   Add WANDB_API_KEY to your .env file")
    sys.exit(1)

# Test 2: Initialize observability manager
print("\n2️⃣ Initializing observability manager...")
try:
    from src.utils.observability import get_observability_manager
    
    obs_manager = get_observability_manager()
    
    print(f"   LangSmith enabled: {'βœ… Yes' if obs_manager.langsmith_enabled else '❌ No'}")
    print(f"   W&B enabled: {'βœ… Yes' if obs_manager.wandb_enabled else '❌ No'}")
    
    if not obs_manager.langsmith_enabled:
        print("\n⚠️  LangSmith initialization failed. Check your API key.")
    
    if not obs_manager.wandb_enabled:
        print("\n⚠️  W&B initialization failed. Check your API key.")
    
except Exception as e:
    print(f"\n❌ Failed to initialize observability manager: {e}")
    import traceback
    traceback.print_exc()
    sys.exit(1)

# Test 3: Test LangSmith connection
print("\n3️⃣ Testing LangSmith connection...")
try:
    if obs_manager.langsmith_enabled:
        # LangSmith is automatically configured via environment variables
        # Just verify the environment is set correctly
        import os
        if os.getenv("LANGCHAIN_TRACING_V2") == "true":
            print("   βœ… LangSmith environment configured correctly")
            print(f"   πŸ“Š Project: {LANGCHAIN_PROJECT}")
            print(f"   πŸ”— Dashboard: https://smith.langchain.com")
        else:
            print("   ⚠️  LANGCHAIN_TRACING_V2 not set to 'true'")
    else:
        print("   ⏭️  LangSmith disabled, skipping")
except Exception as e:
    print(f"   ⚠️  LangSmith test warning: {e}")

# Test 4: Test W&B connection
print("\n4️⃣ Testing W&B connection...")
try:
    if obs_manager.wandb_enabled:
        import wandb
        
        # Check if we can access the API
        api = wandb.Api()
        
        # Try to get user info
        try:
            # This will validate the API key
            print(f"   βœ… W&B API key is valid")
            print(f"   πŸ“Š Project: {WANDB_PROJECT}")
            if WANDB_ENTITY:
                print(f"   πŸ‘€ Entity: {WANDB_ENTITY}")
            print(f"   πŸ”— Dashboard: https://wandb.ai/{WANDB_ENTITY or 'your-username'}/{WANDB_PROJECT}")
        except Exception as e:
            print(f"   ⚠️  Could not validate W&B API key: {e}")
            print(f"   This might be okay - will test with actual logging")
    else:
        print("   ⏭️  W&B disabled, skipping")
except Exception as e:
    print(f"   ⚠️  W&B test warning: {e}")

# Test 5: Test logging functionality
print("\n5️⃣ Testing logging functionality...")
try:
    # Test metric logging
    obs_manager.log_metric("test_metric", 1.0, {"source": "validation_script"})
    print("   βœ… Metric logging works")
    
    # Test event logging
    obs_manager.log_event("test_event", {"status": "success", "test": True})
    print("   βœ… Event logging works")
    
    # Test LLM call logging
    obs_manager.log_llm_call(
        prompt="Test prompt",
        response="Test response",
        model="gpt-4o-mini",
        metadata={"test": True},
        latency_ms=100.0,
        token_count=50,
        cost=0.001
    )
    print("   βœ… LLM call logging works")
    
except Exception as e:
    print(f"   ⚠️  Logging test warning: {e}")
    import traceback
    traceback.print_exc()

# Test 6: Test cost estimation
print("\n6️⃣ Testing cost estimation...")
try:
    from src.utils.observability import estimate_cost
    
    cost = estimate_cost("gpt-4o-mini", input_tokens=1000, output_tokens=500)
    print(f"   βœ… Cost estimation works")
    print(f"   πŸ’° Example: 1000 input + 500 output tokens = ${cost:.4f}")
    
except Exception as e:
    print(f"   ❌ Cost estimation failed: {e}")

# Test 7: Test ModelOrchestrator integration
print("\n7️⃣ Testing ModelOrchestrator integration...")
try:
    from src.ml.model_orchestrator import ModelOrchestrator
    
    orchestrator = ModelOrchestrator()
    
    if hasattr(orchestrator, 'obs_manager'):
        print("   βœ… ModelOrchestrator has observability manager")
    else:
        print("   ⚠️  ModelOrchestrator missing observability manager")
    
except Exception as e:
    print(f"   ⚠️  ModelOrchestrator test warning: {e}")

# Final summary
print("\n" + "=" * 60)
print("πŸ“Š Summary")
print("=" * 60)

all_good = True

if not obs_manager.langsmith_enabled:
    print("⚠️  LangSmith: Not enabled or failed to initialize")
    all_good = False
else:
    print("βœ… LangSmith: Ready")

if not obs_manager.wandb_enabled:
    print("⚠️  W&B: Not enabled or failed to initialize")
    all_good = False
else:
    print("βœ… W&B: Ready")

print("\n" + "=" * 60)

if all_good:
    print("πŸŽ‰ All systems go! You're ready to generate learning paths.")
    print("\nNext steps:")
    print("1. Generate a learning path using your app")
    print("2. Check LangSmith dashboard: https://smith.langchain.com")
    print("3. Check W&B dashboard: https://wandb.ai")
    print("\nYou should see:")
    print("  β€’ Full LLM traces in LangSmith")
    print("  β€’ Metrics and costs in W&B")
else:
    print("⚠️  Some issues detected. Review the warnings above.")
    print("\nCommon fixes:")
    print("  β€’ Verify API keys are correct in .env")
    print("  β€’ Ensure LANGCHAIN_TRACING_V2=true (not 'True')")
    print("  β€’ Check internet connection")
    print("  β€’ Restart your application after changing .env")

print("=" * 60)

# Cleanup
if obs_manager.wandb_enabled:
    try:
        obs_manager.finish()
        print("\nβœ… W&B run finished cleanly")
    except:
        pass