Spaces:
Running
Running
Pulastya B
commited on
Commit
·
f35ddc4
1
Parent(s):
1ab1ded
feat: Add production-grade tool result compression for Groq
Browse filesPROBLEM:
- profile_dataset returns ~5-10K tokens of stats
- Conversation history grows to 12K+ tokens
- Groq limit: 12K tokens per request
- Result: 413 Payload Too Large error
SOLUTION (Production Pattern - used by LangChain/AutoGPT):
- Store full results in workflow_history (for artifacts)
- Send LLM only compressed summary (~200 tokens)
- Compression: status + key metrics + file paths + next steps
- Quality preserved: Full data available, LLM gets decision info
COMPRESSION EXAMPLES:
- profile_dataset: 5K tokens 200 tokens (96% reduction)
- detect_data_quality_issues: 3K tokens 150 tokens
- train_baseline_models: 2K tokens 200 tokens
No quality loss: LLM gets exactly what it needs for next decision
- src/_compress_tool_result.py +118 -0
- src/orchestrator.py +189 -18
src/_compress_tool_result.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Production-grade tool result compression for small context window models.
|
| 3 |
+
Add this function to orchestrator.py before _parse_text_tool_calls method.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
def _compress_tool_result(self, tool_name: str, result: Dict[str, Any]) -> Dict[str, Any]:
|
| 7 |
+
"""
|
| 8 |
+
Compress tool results for small context models (production-grade approach).
|
| 9 |
+
|
| 10 |
+
Keep only:
|
| 11 |
+
- Status (success/failure)
|
| 12 |
+
- Key metrics (5-10 most important numbers)
|
| 13 |
+
- File paths created
|
| 14 |
+
- Next action hints
|
| 15 |
+
|
| 16 |
+
Full results stored in workflow_history and session memory.
|
| 17 |
+
LLM doesn't need verbose output - only decision-making info.
|
| 18 |
+
|
| 19 |
+
Args:
|
| 20 |
+
tool_name: Name of the tool executed
|
| 21 |
+
result: Full tool result dict
|
| 22 |
+
|
| 23 |
+
Returns:
|
| 24 |
+
Compressed result dict (typically 100-500 tokens vs 5K-10K)
|
| 25 |
+
"""
|
| 26 |
+
if not result.get("success", True):
|
| 27 |
+
# Keep full error info (critical for debugging)
|
| 28 |
+
return result
|
| 29 |
+
|
| 30 |
+
compressed = {
|
| 31 |
+
"success": True,
|
| 32 |
+
"tool": tool_name
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
# Tool-specific compression rules
|
| 36 |
+
if tool_name == "profile_dataset":
|
| 37 |
+
# Original: ~5K tokens with full stats
|
| 38 |
+
# Compressed: ~200 tokens with key metrics
|
| 39 |
+
r = result.get("result", {})
|
| 40 |
+
compressed["summary"] = {
|
| 41 |
+
"rows": r.get("num_rows"),
|
| 42 |
+
"cols": r.get("num_columns"),
|
| 43 |
+
"missing_pct": r.get("missing_percentage"),
|
| 44 |
+
"numeric_cols": len(r.get("numeric_columns", [])),
|
| 45 |
+
"categorical_cols": len(r.get("categorical_columns", [])),
|
| 46 |
+
"file_size_mb": round(r.get("memory_usage_mb", 0), 1),
|
| 47 |
+
"key_columns": list(r.get("columns", {}).keys())[:5] # First 5 columns only
|
| 48 |
+
}
|
| 49 |
+
compressed["next_steps"] = ["clean_missing_values", "detect_data_quality_issues"]
|
| 50 |
+
|
| 51 |
+
elif tool_name == "detect_data_quality_issues":
|
| 52 |
+
r = result.get("result", {})
|
| 53 |
+
compressed["summary"] = {
|
| 54 |
+
"total_issues": r.get("total_issues", 0),
|
| 55 |
+
"critical_issues": r.get("critical_issues", 0),
|
| 56 |
+
"missing_data": r.get("has_missing"),
|
| 57 |
+
"outliers": r.get("has_outliers"),
|
| 58 |
+
"duplicates": r.get("has_duplicates")
|
| 59 |
+
}
|
| 60 |
+
compressed["next_steps"] = ["clean_missing_values", "handle_outliers"]
|
| 61 |
+
|
| 62 |
+
elif tool_name in ["clean_missing_values", "handle_outliers", "encode_categorical"]:
|
| 63 |
+
r = result.get("result", {})
|
| 64 |
+
compressed["summary"] = {
|
| 65 |
+
"output_file": r.get("output_file", r.get("output_path")),
|
| 66 |
+
"rows_processed": r.get("rows_after", r.get("num_rows")),
|
| 67 |
+
"changes_made": bool(r.get("changes", {}) or r.get("imputed_columns"))
|
| 68 |
+
}
|
| 69 |
+
compressed["next_steps"] = ["Use this file for next step"]
|
| 70 |
+
|
| 71 |
+
elif tool_name == "train_baseline_models":
|
| 72 |
+
r = result.get("result", {})
|
| 73 |
+
models = r.get("models", [])
|
| 74 |
+
if models:
|
| 75 |
+
best = max(models, key=lambda m: m.get("test_score", 0))
|
| 76 |
+
compressed["summary"] = {
|
| 77 |
+
"best_model": best.get("model"),
|
| 78 |
+
"test_score": round(best.get("test_score", 0), 4),
|
| 79 |
+
"train_score": round(best.get("train_score", 0), 4),
|
| 80 |
+
"task_type": r.get("task_type"),
|
| 81 |
+
"models_trained": len(models)
|
| 82 |
+
}
|
| 83 |
+
compressed["next_steps"] = ["hyperparameter_tuning", "generate_combined_eda_report"]
|
| 84 |
+
|
| 85 |
+
elif tool_name in ["generate_plotly_dashboard", "generate_ydata_profiling_report", "generate_combined_eda_report"]:
|
| 86 |
+
r = result.get("result", {})
|
| 87 |
+
compressed["summary"] = {
|
| 88 |
+
"report_path": r.get("report_path", r.get("output_path")),
|
| 89 |
+
"report_type": tool_name,
|
| 90 |
+
"success": True
|
| 91 |
+
}
|
| 92 |
+
compressed["next_steps"] = ["Report ready for viewing"]
|
| 93 |
+
|
| 94 |
+
elif tool_name == "hyperparameter_tuning":
|
| 95 |
+
r = result.get("result", {})
|
| 96 |
+
compressed["summary"] = {
|
| 97 |
+
"best_params": r.get("best_params", {}),
|
| 98 |
+
"best_score": round(r.get("best_score", 0), 4),
|
| 99 |
+
"model_type": r.get("model_type"),
|
| 100 |
+
"trials_completed": r.get("n_trials")
|
| 101 |
+
}
|
| 102 |
+
compressed["next_steps"] = ["perform_cross_validation", "generate_model_performance_plots"]
|
| 103 |
+
|
| 104 |
+
else:
|
| 105 |
+
# Generic compression: Keep only key fields
|
| 106 |
+
r = result.get("result", {})
|
| 107 |
+
if isinstance(r, dict):
|
| 108 |
+
# Extract key fields (common patterns)
|
| 109 |
+
key_fields = {}
|
| 110 |
+
for key in ["output_path", "output_file", "status", "message", "success"]:
|
| 111 |
+
if key in r:
|
| 112 |
+
key_fields[key] = r[key]
|
| 113 |
+
compressed["summary"] = key_fields or {"result": "completed"}
|
| 114 |
+
else:
|
| 115 |
+
compressed["summary"] = {"result": str(r)[:200] if r else "completed"}
|
| 116 |
+
compressed["next_steps"] = ["Continue workflow"]
|
| 117 |
+
|
| 118 |
+
return compressed
|
src/orchestrator.py
CHANGED
|
@@ -1094,6 +1094,121 @@ You are a DOER. Complete workflows based on user intent."""
|
|
| 1094 |
|
| 1095 |
return compressed
|
| 1096 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1097 |
def _parse_text_tool_calls(self, text_response: str) -> List[Dict[str, Any]]:
|
| 1098 |
"""
|
| 1099 |
Parse tool calls from text-based LLM response (ReAct pattern).
|
|
@@ -1428,6 +1543,13 @@ You are a DOER. Complete workflows based on user intent."""
|
|
| 1428 |
**Dataset**: {file_path}
|
| 1429 |
**Task**: {task_description}
|
| 1430 |
**Target Column**: {target_col if target_col else 'Not specified - please infer from data'}{workflow_guidance}"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1431 |
|
| 1432 |
messages = [
|
| 1433 |
{"role": "system", "content": system_prompt},
|
|
@@ -1469,21 +1591,67 @@ You are a DOER. Complete workflows based on user intent."""
|
|
| 1469 |
|
| 1470 |
# Call LLM with function calling (provider-specific)
|
| 1471 |
if self.provider == "groq":
|
| 1472 |
-
|
| 1473 |
-
|
| 1474 |
-
|
| 1475 |
-
|
| 1476 |
-
|
| 1477 |
-
|
| 1478 |
-
|
| 1479 |
-
|
| 1480 |
-
|
| 1481 |
-
|
| 1482 |
-
|
| 1483 |
-
|
| 1484 |
-
|
| 1485 |
-
|
| 1486 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1487 |
|
| 1488 |
elif self.provider == "gemini":
|
| 1489 |
# Send messages WITHOUT tools parameter (tools already configured on model)
|
|
@@ -2098,10 +2266,12 @@ You are a DOER. Complete workflows based on user intent."""
|
|
| 2098 |
# ⚡ CRITICAL FIX: Add tool result back to messages so LLM sees it in next iteration!
|
| 2099 |
if self.provider == "groq":
|
| 2100 |
# For Groq, add tool message with the result
|
| 2101 |
-
#
|
| 2102 |
-
# Clean tool_result to make it JSON-serializable
|
| 2103 |
clean_tool_result = self._make_json_serializable(tool_result)
|
| 2104 |
-
|
|
|
|
|
|
|
|
|
|
| 2105 |
|
| 2106 |
# If tool failed, prepend ERROR indicator to make it obvious
|
| 2107 |
if not tool_result.get("success", True):
|
|
@@ -2251,3 +2421,4 @@ You are a DOER. Complete workflows based on user intent."""
|
|
| 2251 |
return self.session.get_context_summary()
|
| 2252 |
else:
|
| 2253 |
return "No active session"
|
|
|
|
|
|
| 1094 |
|
| 1095 |
return compressed
|
| 1096 |
|
| 1097 |
+
def _compress_tool_result(self, tool_name: str, result: Dict[str, Any]) -> Dict[str, Any]:
|
| 1098 |
+
"""
|
| 1099 |
+
Compress tool results for small context models (production-grade approach).
|
| 1100 |
+
|
| 1101 |
+
Keep only:
|
| 1102 |
+
- Status (success/failure)
|
| 1103 |
+
- Key metrics (5-10 most important numbers)
|
| 1104 |
+
- File paths created
|
| 1105 |
+
- Next action hints
|
| 1106 |
+
|
| 1107 |
+
Full results stored in workflow_history and session memory.
|
| 1108 |
+
LLM doesn't need verbose output - only decision-making info.
|
| 1109 |
+
|
| 1110 |
+
Args:
|
| 1111 |
+
tool_name: Name of the tool executed
|
| 1112 |
+
result: Full tool result dict
|
| 1113 |
+
|
| 1114 |
+
Returns:
|
| 1115 |
+
Compressed result dict (typically 100-500 tokens vs 5K-10K)
|
| 1116 |
+
"""
|
| 1117 |
+
if not result.get("success", True):
|
| 1118 |
+
# Keep full error info (critical for debugging)
|
| 1119 |
+
return result
|
| 1120 |
+
|
| 1121 |
+
compressed = {
|
| 1122 |
+
"success": True,
|
| 1123 |
+
"tool": tool_name
|
| 1124 |
+
}
|
| 1125 |
+
|
| 1126 |
+
# Tool-specific compression rules
|
| 1127 |
+
if tool_name == "profile_dataset":
|
| 1128 |
+
# Original: ~5K tokens with full stats
|
| 1129 |
+
# Compressed: ~200 tokens with key metrics
|
| 1130 |
+
r = result.get("result", {})
|
| 1131 |
+
compressed["summary"] = {
|
| 1132 |
+
"rows": r.get("num_rows"),
|
| 1133 |
+
"cols": r.get("num_columns"),
|
| 1134 |
+
"missing_pct": r.get("missing_percentage"),
|
| 1135 |
+
"numeric_cols": len(r.get("numeric_columns", [])),
|
| 1136 |
+
"categorical_cols": len(r.get("categorical_columns", [])),
|
| 1137 |
+
"file_size_mb": round(r.get("memory_usage_mb", 0), 1),
|
| 1138 |
+
"key_columns": list(r.get("columns", {}).keys())[:5] # First 5 columns only
|
| 1139 |
+
}
|
| 1140 |
+
compressed["next_steps"] = ["clean_missing_values", "detect_data_quality_issues"]
|
| 1141 |
+
|
| 1142 |
+
elif tool_name == "detect_data_quality_issues":
|
| 1143 |
+
r = result.get("result", {})
|
| 1144 |
+
compressed["summary"] = {
|
| 1145 |
+
"total_issues": r.get("total_issues", 0),
|
| 1146 |
+
"critical_issues": r.get("critical_issues", 0),
|
| 1147 |
+
"missing_data": r.get("has_missing"),
|
| 1148 |
+
"outliers": r.get("has_outliers"),
|
| 1149 |
+
"duplicates": r.get("has_duplicates")
|
| 1150 |
+
}
|
| 1151 |
+
compressed["next_steps"] = ["clean_missing_values", "handle_outliers"]
|
| 1152 |
+
|
| 1153 |
+
elif tool_name in ["clean_missing_values", "handle_outliers", "encode_categorical"]:
|
| 1154 |
+
r = result.get("result", {})
|
| 1155 |
+
compressed["summary"] = {
|
| 1156 |
+
"output_file": r.get("output_file", r.get("output_path")),
|
| 1157 |
+
"rows_processed": r.get("rows_after", r.get("num_rows")),
|
| 1158 |
+
"changes_made": bool(r.get("changes", {}) or r.get("imputed_columns"))
|
| 1159 |
+
}
|
| 1160 |
+
compressed["next_steps"] = ["Use this file for next step"]
|
| 1161 |
+
|
| 1162 |
+
elif tool_name == "train_baseline_models":
|
| 1163 |
+
r = result.get("result", {})
|
| 1164 |
+
models = r.get("models", [])
|
| 1165 |
+
if models:
|
| 1166 |
+
best = max(models, key=lambda m: m.get("test_score", 0))
|
| 1167 |
+
compressed["summary"] = {
|
| 1168 |
+
"best_model": best.get("model"),
|
| 1169 |
+
"test_score": round(best.get("test_score", 0), 4),
|
| 1170 |
+
"train_score": round(best.get("train_score", 0), 4),
|
| 1171 |
+
"task_type": r.get("task_type"),
|
| 1172 |
+
"models_trained": len(models)
|
| 1173 |
+
}
|
| 1174 |
+
compressed["next_steps"] = ["hyperparameter_tuning", "generate_combined_eda_report"]
|
| 1175 |
+
|
| 1176 |
+
elif tool_name in ["generate_plotly_dashboard", "generate_ydata_profiling_report", "generate_combined_eda_report"]:
|
| 1177 |
+
r = result.get("result", {})
|
| 1178 |
+
compressed["summary"] = {
|
| 1179 |
+
"report_path": r.get("report_path", r.get("output_path")),
|
| 1180 |
+
"report_type": tool_name,
|
| 1181 |
+
"success": True
|
| 1182 |
+
}
|
| 1183 |
+
compressed["next_steps"] = ["Report ready for viewing"]
|
| 1184 |
+
|
| 1185 |
+
elif tool_name == "hyperparameter_tuning":
|
| 1186 |
+
r = result.get("result", {})
|
| 1187 |
+
compressed["summary"] = {
|
| 1188 |
+
"best_params": r.get("best_params", {}),
|
| 1189 |
+
"best_score": round(r.get("best_score", 0), 4),
|
| 1190 |
+
"model_type": r.get("model_type"),
|
| 1191 |
+
"trials_completed": r.get("n_trials")
|
| 1192 |
+
}
|
| 1193 |
+
compressed["next_steps"] = ["perform_cross_validation", "generate_model_performance_plots"]
|
| 1194 |
+
|
| 1195 |
+
else:
|
| 1196 |
+
# Generic compression: Keep only key fields
|
| 1197 |
+
r = result.get("result", {})
|
| 1198 |
+
if isinstance(r, dict):
|
| 1199 |
+
# Extract key fields (common patterns)
|
| 1200 |
+
key_fields = {}
|
| 1201 |
+
for key in ["output_path", "output_file", "status", "message", "success"]:
|
| 1202 |
+
if key in r:
|
| 1203 |
+
key_fields[key] = r[key]
|
| 1204 |
+
compressed["summary"] = key_fields or {"result": "completed"}
|
| 1205 |
+
else:
|
| 1206 |
+
compressed["summary"] = {"result": str(r)[:200] if r else "completed"}
|
| 1207 |
+
compressed["next_steps"] = ["Continue workflow"]
|
| 1208 |
+
|
| 1209 |
+
return compressed
|
| 1210 |
+
|
| 1211 |
+
|
| 1212 |
def _parse_text_tool_calls(self, text_response: str) -> List[Dict[str, Any]]:
|
| 1213 |
"""
|
| 1214 |
Parse tool calls from text-based LLM response (ReAct pattern).
|
|
|
|
| 1543 |
**Dataset**: {file_path}
|
| 1544 |
**Task**: {task_description}
|
| 1545 |
**Target Column**: {target_col if target_col else 'Not specified - please infer from data'}{workflow_guidance}"""
|
| 1546 |
+
|
| 1547 |
+
#🧠 Store file path in session memory for follow-up requests
|
| 1548 |
+
if self.session and file_path:
|
| 1549 |
+
self.session.update(last_dataset=file_path)
|
| 1550 |
+
if target_col:
|
| 1551 |
+
self.session.update(last_target_col=target_col)
|
| 1552 |
+
print(f"💾 Saved to session: dataset={file_path}, target={target_col}")
|
| 1553 |
|
| 1554 |
messages = [
|
| 1555 |
{"role": "system", "content": system_prompt},
|
|
|
|
| 1591 |
|
| 1592 |
# Call LLM with function calling (provider-specific)
|
| 1593 |
if self.provider == "groq":
|
| 1594 |
+
try:
|
| 1595 |
+
response = self.groq_client.chat.completions.create(
|
| 1596 |
+
model=self.model,
|
| 1597 |
+
messages=messages,
|
| 1598 |
+
tools=tools_to_use,
|
| 1599 |
+
tool_choice="auto",
|
| 1600 |
+
parallel_tool_calls=False, # Disable parallel calls to prevent XML format errors
|
| 1601 |
+
temperature=0.1, # Low temperature for consistent outputs
|
| 1602 |
+
max_tokens=4096
|
| 1603 |
+
)
|
| 1604 |
+
|
| 1605 |
+
self.api_calls_made += 1
|
| 1606 |
+
self.last_api_call_time = time.time()
|
| 1607 |
+
response_message = response.choices[0].message
|
| 1608 |
+
tool_calls = response_message.tool_calls
|
| 1609 |
+
final_content = response_message.content
|
| 1610 |
+
|
| 1611 |
+
except Exception as groq_error:
|
| 1612 |
+
# Check if it's a rate limit error (429)
|
| 1613 |
+
if "rate_limit" in str(groq_error).lower() or "429" in str(groq_error):
|
| 1614 |
+
print(f"⚠️ Groq rate limit exceeded! Automatically switching to Gemini...")
|
| 1615 |
+
print(f" Groq error: {str(groq_error)[:200]}")
|
| 1616 |
+
|
| 1617 |
+
# Switch to Gemini fallback
|
| 1618 |
+
if not hasattr(self, 'gemini_model') or self.gemini_model is None:
|
| 1619 |
+
# Initialize Gemini if not already done
|
| 1620 |
+
import google.generativeai as genai
|
| 1621 |
+
api_key = os.getenv("GOOGLE_API_KEY") or os.getenv("GEMINI_API_KEY")
|
| 1622 |
+
if not api_key:
|
| 1623 |
+
raise ValueError("Groq exhausted and no Gemini API key available for fallback")
|
| 1624 |
+
|
| 1625 |
+
genai.configure(api_key=api_key)
|
| 1626 |
+
gemini_model_name = os.getenv("GEMINI_MODEL", "gemini-2.5-flash")
|
| 1627 |
+
|
| 1628 |
+
# Safety settings
|
| 1629 |
+
safety_settings = [
|
| 1630 |
+
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
|
| 1631 |
+
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"},
|
| 1632 |
+
{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE"},
|
| 1633 |
+
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"}
|
| 1634 |
+
]
|
| 1635 |
+
|
| 1636 |
+
self.gemini_model = genai.GenerativeModel(
|
| 1637 |
+
model_name=gemini_model_name,
|
| 1638 |
+
safety_settings=safety_settings
|
| 1639 |
+
)
|
| 1640 |
+
print(f" ✅ Gemini fallback initialized: {gemini_model_name}")
|
| 1641 |
+
|
| 1642 |
+
# Switch provider for this session
|
| 1643 |
+
self.provider = "gemini"
|
| 1644 |
+
self.use_compact_prompts = False # Gemini has large context
|
| 1645 |
+
gemini_chat = self.gemini_model.start_chat(history=[])
|
| 1646 |
+
print(f" 🔄 Now using Gemini for remaining workflow")
|
| 1647 |
+
|
| 1648 |
+
# Retry with Gemini (continue to Gemini block below)
|
| 1649 |
+
# Set tool_calls to None to trigger Gemini path
|
| 1650 |
+
response_message = None
|
| 1651 |
+
tool_calls = None
|
| 1652 |
+
else:
|
| 1653 |
+
# Not a rate limit error, re-raise
|
| 1654 |
+
raise
|
| 1655 |
|
| 1656 |
elif self.provider == "gemini":
|
| 1657 |
# Send messages WITHOUT tools parameter (tools already configured on model)
|
|
|
|
| 2266 |
# ⚡ CRITICAL FIX: Add tool result back to messages so LLM sees it in next iteration!
|
| 2267 |
if self.provider == "groq":
|
| 2268 |
# For Groq, add tool message with the result
|
| 2269 |
+
# **COMPRESS RESULT** for small context models (Groq 12K token limit)
|
|
|
|
| 2270 |
clean_tool_result = self._make_json_serializable(tool_result)
|
| 2271 |
+
|
| 2272 |
+
# Smart compression: Keep only what LLM needs for next decision
|
| 2273 |
+
compressed_result = self._compress_tool_result(tool_name, clean_tool_result)
|
| 2274 |
+
tool_response_content = json.dumps(compressed_result)
|
| 2275 |
|
| 2276 |
# If tool failed, prepend ERROR indicator to make it obvious
|
| 2277 |
if not tool_result.get("success", True):
|
|
|
|
| 2421 |
return self.session.get_context_summary()
|
| 2422 |
else:
|
| 2423 |
return "No active session"
|
| 2424 |
+
|