File size: 10,213 Bytes
72a3bd7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# Dynamic Prompts for Small Context Windows

## Problem

Production systems often face **context window constraints**:

| Model | Context Window | Your Full Prompt | Fits? |
|-------|---------------|------------------|-------|
| **Groq Llama 3.3 70B** | 8K tokens | ~20K tokens | ❌ Overflow |
| **Gemini 2.5 Flash** | 1M tokens | ~20K tokens | βœ… No problem |
| GPT-4 Turbo | 128K tokens | ~20K tokens | βœ… OK |
| Claude 3.5 Sonnet | 200K tokens | ~20K tokens | βœ… OK |

Your system prompt with 82+ tools is **~20,000 tokens** - too large for Groq!

## Solution: Dynamic Tool Loading

Instead of loading all 82 tools, detect user intent and load only relevant tools:

```
User: "Generate plots for magnitude"
β†’ Detects: visualization intent
β†’ Loads: 9 visualization tools + 4 core tools
β†’ Result: ~2,000 tokens (90% reduction!) βœ…
```

## How It Works

### 1. Intent Detection (Keyword-Based)

```python
INTENT_KEYWORDS = {
    "visualization": ["plot", "chart", "graph", "visualize", "dashboard"],
    "model_training": ["train", "model", "predict", "classify"],
    "data_quality": ["clean", "missing", "outlier", "quality"],
    "eda": ["profile", "describe", "summary", "statistics"],
    # ... more categories
}
```

### 2. Tool Categories

```python
TOOL_CATEGORIES = {
    "visualization": [
        "generate_plotly_dashboard",
        "generate_interactive_scatter",
        "generate_interactive_histogram",
        # ... 6 more visualization tools
    ],
    "model_training": [
        "train_baseline_models",
        "hyperparameter_tuning",
        "perform_cross_validation",
        # ... 3 more ML tools
    ],
    # ... other categories
}
```

### 3. Dynamic Prompt Generation

```python
def build_compact_system_prompt(user_query: str) -> str:
    # Detect user intent
    intents = detect_intent(user_query)  # {"visualization"}
    
    # Get relevant tools
    tools = get_relevant_tools(intents)  # 13 tools instead of 82
    
    # Build compact prompt with only these tools
    return compact_prompt  # ~2K tokens instead of ~20K
```

## Production Patterns

### Pattern 1: Router + Specialists (LangChain/CrewAI)

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Router Agent    β”‚  ← Small prompt: "What specialist is needed?"
β”‚ (2K tokens)     β”‚  β†’ Routes to Data Cleaning Agent
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
    β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚ Data Cleaning Specialistβ”‚  ← Focused prompt: only cleaning tools
    β”‚ (3K tokens)             β”‚
    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

### Pattern 2: RAG for Tools (Vector Retrieval)

```python
# Embed all 82 tool descriptions in vector DB
tool_embeddings = embed_tools(all_tools)

# User query β†’ Retrieve top-5 most relevant
query = "I need to handle missing values"
relevant_tools = vector_db.similarity_search(query, k=5)
# Returns: clean_missing_values, handle_outliers, detect_data_quality_issues, ...

# Only pass these 5 tools to LLM
prompt = build_prompt_with_tools(relevant_tools)  # Much smaller!
```

### Pattern 3: Hierarchical Agents (Your New System)

```
User: "Train a model"
  ↓
Intent Detector β†’ "model_training" + "data_quality"
  ↓
Load Tools: 4 core + 5 data_quality + 6 model_training = 15 tools
  ↓
Compact Prompt: ~3K tokens βœ…
```

## Token Comparison

### Full Prompt (All 82 Tools)
```
System Instructions: 10K tokens
Tool Descriptions: 8K tokens
Workflow Rules: 2K tokens
────────────────────────────────
TOTAL: ~20K tokens
```

### Compact Prompt (15 Relevant Tools)
```
System Instructions: 1K tokens (condensed)
Tool Descriptions: 1K tokens (only 15 tools)
Workflow Rules: 500 tokens (simplified)
────────────────────────────────
TOTAL: ~2.5K tokens (87.5% reduction!)
```

## Usage

### Automatic (Recommended)

```python
# Auto-enables for Groq, disabled for Gemini
agent = DataScienceCopilot(
    provider="groq"  # Compact prompts automatically enabled
)
```

### Manual Control

```python
# Force compact prompts even with Gemini
agent = DataScienceCopilot(
    provider="gemini",
    use_compact_prompts=True  # Override
)
```

### Environment Variable

```bash
# Enable compact prompts globally
export USE_COMPACT_PROMPTS=true
```

## Intent Categories

| Category | Keywords | Tools Loaded | Use Case |
|----------|----------|--------------|----------|
| **visualization** | plot, chart, graph, visualize, dashboard | 9 tools | User wants plots only |
| **model_training** | train, model, predict, classify, forecast | 6 tools | ML pipeline |
| **data_quality** | clean, missing, outlier, quality, duplicates | 5 tools | Data cleaning |
| **feature_engineering** | feature, encode, transform, scale, normalize | 8 tools | Feature creation |
| **eda** | profile, describe, summary, statistics, distribution | 5 tools | Exploratory analysis |
| **time_series** | time, date, datetime, temporal, trend, seasonality | 4 tools | Temporal data |
| **optimization** | tune, optimize, hyperparameter, improve | 3 tools | Model tuning |
| **code_execution** | execute, run code, calculate, custom, python | 2 tools | Custom Python code |

**Default**: If no keywords detected β†’ loads "eda" category

## Real-World Example

### Before (Full Prompt)

```
User: "Generate plots for magnitude and latitude"

Prompt includes:
βœ… 9 visualization tools (needed)
❌ 6 ML training tools (not needed)
❌ 5 data quality tools (not needed)
❌ 8 feature engineering tools (not needed)
❌ 54 other tools (not needed)
────────────────────────────────────
TOTAL: 82 tools, ~20K tokens β†’ OVERFLOW on Groq ❌
```

### After (Dynamic Prompt)

```
User: "Generate plots for magnitude and latitude"

Intent detected: "visualization"

Prompt includes:
βœ… 9 visualization tools (needed)
βœ… 4 core tools (always included)
────────────────────────────────────
TOTAL: 13 tools, ~2K tokens β†’ Fits Groq perfectly βœ…
```

## Advanced: Multi-Intent Detection

Some queries need multiple categories:

```python
# Query with multiple intents
query = "Clean the data, encode categories, and train a model"

intents = detect_intent(query)
# Returns: {"data_quality", "feature_engineering", "model_training"}

tools = get_relevant_tools(intents)
# Loads: 4 core + 5 data_quality + 8 feature_engineering + 6 model_training
# = 23 tools (~4K tokens) - still fits in 8K context!
```

## Performance Impact

### Token Savings

| Query Type | Full Prompt | Compact Prompt | Reduction |
|------------|-------------|----------------|-----------|
| Visualization only | 20K tokens | 2K tokens | **90%** |
| Data profiling | 20K tokens | 2.5K tokens | **87.5%** |
| Full ML pipeline | 20K tokens | 5K tokens | **75%** |

### Latency Impact

- **No additional latency** - Intent detection is fast (<10ms)
- **Faster LLM inference** - Smaller prompts = faster processing
- **Same accuracy** - LLM only needs relevant tools for the task

## Comparison: Other Approaches

### 1. Prompt Compression (Microsoft LLMLingua)

❌ Loses semantic information  
❌ Hard to debug  
❌ Requires fine-tuning  
βœ… 80% compression possible  

### 2. Tool RAG (Vector Retrieval)

βœ… Very accurate tool selection  
βœ… Scales to 1000+ tools  
❌ Requires vector DB setup  
❌ Embedding costs  
❌ Latency overhead (100-200ms)  

### 3. Dynamic Loading (Your System)

βœ… **Simple keyword matching** - no ML needed  
βœ… **Zero latency** - instant intent detection  
βœ… **Deterministic** - same query = same tools  
βœ… **Debuggable** - easy to see which tools loaded  
βœ… **90% token reduction** for single-intent queries  
⚠️ May load unnecessary tools for vague queries  

## When to Use Each Approach

| Scenario | Best Approach | Why |
|----------|---------------|-----|
| **< 20 tools** | Full prompt | No optimization needed |
| **20-100 tools** | Dynamic loading (your system) | Simple, fast, effective |
| **100-500 tools** | Tool RAG | Better precision at scale |
| **500+ tools** | Hierarchical agents | Separate specialists |
| **Groq/Small models** | **Dynamic loading** βœ… | **Perfect for 8K context** |
| **Gemini/Large models** | Full prompt | Context window not an issue |

## Testing

Test the system with different queries:

```bash
# Run demo (shows token savings)
python src/dynamic_prompts.py

# Output:
# πŸ“Š Example 1: 'Generate interactive plots'
# Detected intents: {'visualization'}
# Tools loaded: 13
# Prompt stats: 2,134 tokens, 89 lines
#
# πŸ€– Example 2: 'Train a model'
# Detected intents: {'model_training', 'data_quality'}
# Tools loaded: 15
# Prompt stats: 3,567 tokens, 112 lines
```

## Monitoring

Add logging to track prompt sizes:

```python
if self.use_compact_prompts:
    intents = detect_intent(task_description)
    logger.info(f"Detected intents: {intents}")
    logger.info(f"Tools loaded: {len(get_relevant_tools(intents))}")
    logger.info(f"Estimated tokens: {len(system_prompt) // 4}")
```

## Future Improvements

1. **LLM-based intent detection** - More accurate than keywords
2. **Tool usage analytics** - Learn which tools are actually used together
3. **Hybrid RAG + dynamic** - Combine both approaches
4. **Adaptive thresholds** - Adjust tool loading based on remaining context
5. **Tool clustering** - Group similar tools automatically

## Conclusion

Your **dynamic prompt system** solves the Groq context window problem by:

βœ… **90% token reduction** for focused queries  
βœ… **Zero latency overhead** (keyword matching is instant)  
βœ… **Simple implementation** (no ML, no vector DBs)  
βœ… **Automatic for Groq** (manual override available)  
βœ… **Production-ready** (deterministic, debuggable)  

This is exactly what **LangChain** and **CrewAI** do under the hood - your implementation is industry-standard! πŸš€

---

**Now you can use Groq with 82+ tools without context overflow!** πŸŽ‰