-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_llm_setup.py
More file actions
207 lines (178 loc) · 6.78 KB
/
Copy pathdebug_llm_setup.py
File metadata and controls
207 lines (178 loc) · 6.78 KB
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
#!/usr/bin/env python3
"""Debug script to trace LLM setup line-by-line.
Run with: python debug_llm_setup.py
Set breakpoints in your IDE to step through each line.
"""
import asyncio
import logging
import sys
from pathlib import Path
# Setup logging to see what's happening
logging.basicConfig(
level=logging.DEBUG, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
# Add project root
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root))
from config import get_config
from src.agents.context_builder import (
build_analysis_context,
build_messages,
estimate_context_tokens,
)
from src.agents.llm_client import LLMClientFactory, count_tokens
from src.agents.schemas import (
AgentState,
DetectionResult,
IncidentMetrics,
create_initial_state,
)
print("=" * 80)
print("STEP 1: Loading Configuration")
print("=" * 80)
# Breakpoint here: Step into get_config()
config = get_config()
print(f"✅ Config loaded:")
print(f" Environment: {config.env}")
print(f" LLM Provider: {config.llm.provider}")
print(f" Analysis Model: {config.llm.analysis_model}")
print(f" Max Tokens: {config.llm.max_tokens_per_analysis}")
print(f" API Key Set: {config.api_keys.groq_api_key is not None}")
print("\n" + "=" * 80)
print("STEP 2: Creating Test Incident State")
print("=" * 80)
# Breakpoint here: Step into create_initial_state()
metrics = [
IncidentMetrics(
metric_name="http.request.duration",
current_value=500.0, # High latency
baseline_value=120.0, # Normal latency
deviation_score=4.2, # 4.2 sigma deviation
labels={"service": "web", "host": "server-01"},
)
]
state = create_initial_state("debug-incident-001", metrics)
print(f"✅ State created:")
print(f" Incident ID: {state['incident_id']}")
print(f" Metrics: {len(state['metrics'])} metric(s)")
# Add detection result (simulating Detection Agent output)
state["detection_result"] = DetectionResult(
is_anomaly=True,
confidence=0.85,
detection_method="z_score",
threshold_used=3.0,
)
print(f" Detection Result: Anomaly={state['detection_result'].is_anomaly}")
print("\n" + "=" * 80)
print("STEP 3: Building Analysis Context")
print("=" * 80)
# Breakpoint here: Step into build_analysis_context()
try:
context = build_analysis_context(state)
print(f"✅ Context built:")
print(f" Metric: {context.metric_name}")
print(f" Current Value: {context.current_value}")
print(f" Baseline: {context.baseline_value}")
print(f" Deviation: {context.deviation_score}")
print(f" Detection Method: {context.detection_method}")
print(f" Confidence: {context.detection_confidence}")
except Exception as e:
print(f"❌ Context building failed: {e}")
sys.exit(1)
print("\n" + "=" * 80)
print("STEP 4: Building LLM Messages")
print("=" * 80)
# Breakpoint here: Step into build_messages()
messages = build_messages(context)
print(f"✅ Messages built:")
print(f" Number of messages: {len(messages)}")
print(f" System prompt length: {len(messages[0]['content'])} chars")
print(f" User prompt length: {len(messages[1]['content'])} chars")
# Show first 200 chars of each message
print(f"\n System prompt preview:")
print(f" {messages[0]['content'][:200]}...")
print(f"\n User prompt preview:")
print(f" {messages[1]['content'][:200]}...")
print("\n" + "=" * 80)
print("STEP 5: Token Counting")
print("=" * 80)
# Breakpoint here: Step into estimate_context_tokens()
tokens = estimate_context_tokens(messages)
print(f"✅ Token count:")
print(f" Estimated tokens: {tokens}")
print(f" Max allowed: {config.llm.max_context_tokens}")
print(f" Under budget: {tokens <= config.llm.max_context_tokens}")
# Test individual token counting
system_tokens = count_tokens(messages[0]["content"])
user_tokens = count_tokens(messages[1]["content"])
print(f" System prompt tokens: {system_tokens}")
print(f" User prompt tokens: {user_tokens}")
print("\n" + "=" * 80)
print("STEP 6: Creating LLM Client Factory")
print("=" * 80)
# Breakpoint here: Step into LLMClientFactory()
try:
factory = LLMClientFactory(config.api_keys, config.llm)
print(f"✅ Factory created")
# Breakpoint here: Step into get_provider()
provider = factory.get_provider()
print(f"✅ Provider obtained:")
print(f" Provider name: {provider.provider_name}")
print(f" Provider type: {type(provider).__name__}")
except ValueError as e:
print(f"❌ Provider creation failed: {e}")
print(f"\n💡 TIP: Set your API key:")
print(f" export SENTINEL_GROQ_API_KEY=your_key_here")
print(f"\n Or get a free key at: https://console.groq.com")
sys.exit(1)
print("\n" + "=" * 80)
print("STEP 7: Making LLM API Call (Optional - requires API key)")
print("=" * 80)
# Only proceed if API key is set
if config.api_keys.groq_api_key:
print("✅ API key found, making test call...")
async def test_llm_call():
# Breakpoint here: Step into complete_with_retry()
try:
response = await factory.complete_with_retry(
messages=messages,
model=config.llm.analysis_model,
max_tokens=500, # Small for testing
temperature=config.llm.temperature,
)
print(f"✅ LLM Response received:")
print(f" Content length: {len(response.content)} chars")
print(f" Input tokens: {response.input_tokens}")
print(f" Output tokens: {response.output_tokens}")
print(f" Total tokens: {response.total_tokens}")
print(f" Latency: {response.latency_seconds:.2f}s")
print(f" Cost: ${response.cost_usd:.6f}")
print(f"\n Response preview:")
print(f" {response.content[:300]}...")
return response
except Exception as e:
print(f"❌ LLM call failed: {e}")
print(f"\n💡 Debugging tips:")
print(f" 1. Check API key: echo $SENTINEL_GROQ_API_KEY")
print(f" 2. Check network connectivity")
print(f" 3. Check rate limits (Groq: ~6000 req/min)")
raise
# Run async test
try:
response = asyncio.run(test_llm_call())
except Exception as e:
print(f"\n⚠️ LLM call skipped due to error (this is OK for debugging)")
else:
print("⚠️ No API key set - skipping actual LLM call")
print(f"\n💡 To test LLM call:")
print(f" 1. Get free API key: https://console.groq.com")
print(f" 2. Set: export SENTINEL_GROQ_API_KEY=your_key")
print(f" 3. Re-run this script")
print("\n" + "=" * 80)
print("✅ Debug script completed!")
print("=" * 80)
print("\nNext steps:")
print("1. Set breakpoints in your IDE")
print("2. Run this script in debug mode")
print("3. Step through each function call")
print("4. Inspect variables at each step")