Spaces:
Sleeping
Sleeping
last try is a lie
Browse files
app.py
CHANGED
|
@@ -82,7 +82,25 @@ class GeminiModelAdapter:
|
|
| 82 |
self.model = model
|
| 83 |
def generate(self, *args, **kwargs):
|
| 84 |
kwargs.pop('stop_sequences', None) # Remove unsupported argument for Gemini
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
# --- Basic Agent Definition ---
|
| 88 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
|
@@ -106,6 +124,14 @@ class SlpMultiAgent:
|
|
| 106 |
# Patch: wrap Gemini model for smolagents compatibility
|
| 107 |
model = GeminiModelAdapter(genai.GenerativeModel('gemini-2.0-flash-exp'))
|
| 108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
# Create only essential agents with reduced complexity
|
| 110 |
research_agent = CodeAgent(
|
| 111 |
tools=[KnowledgeBaseTool(), WikipediaSearchTool(), DuckDuckGoSearchTool()],
|
|
@@ -114,7 +140,8 @@ class SlpMultiAgent:
|
|
| 114 |
max_steps=3, # Allow more reasoning steps
|
| 115 |
name="ResearchAgent",
|
| 116 |
verbosity_level=0,
|
| 117 |
-
description="Quick factual research and knowledge lookup."
|
|
|
|
| 118 |
)
|
| 119 |
|
| 120 |
solver_agent = CodeAgent(
|
|
@@ -124,7 +151,8 @@ class SlpMultiAgent:
|
|
| 124 |
max_steps=2, # Reduced steps
|
| 125 |
name="SolverAgent",
|
| 126 |
verbosity_level=0,
|
| 127 |
-
description="Problem solving, calculations, and logical reasoning."
|
|
|
|
| 128 |
)
|
| 129 |
|
| 130 |
manager_agent = CodeAgent(
|
|
@@ -146,7 +174,8 @@ class SlpMultiAgent:
|
|
| 146 |
planning_interval=1, # Faster planning
|
| 147 |
verbosity_level=0, # Reduce verbosity
|
| 148 |
max_steps=3, # Further reduced steps to avoid timeouts
|
| 149 |
-
final_answer_checks=[check_reasoning]
|
|
|
|
| 150 |
)
|
| 151 |
|
| 152 |
# Create a task for the agent run with retry mechanism for rate limits
|
|
@@ -160,19 +189,9 @@ class SlpMultiAgent:
|
|
| 160 |
None,
|
| 161 |
lambda: manager_agent.run(f"""
|
| 162 |
Question: {short_question}
|
| 163 |
-
|
| 164 |
-
You have knowledge_base() tool and two agents:
|
| 165 |
-
- ResearchAgent: For factual questions
|
| 166 |
-
- SolverAgent: For calculations and logic
|
| 167 |
-
|
| 168 |
-
IMPORTANT: Always end with exactly this format:
|
| 169 |
-
<code>
|
| 170 |
-
final_answer("your direct answer")
|
| 171 |
-
</code>
|
| 172 |
-
|
| 173 |
-
Be concise and direct.
|
| 174 |
-
""")
|
| 175 |
)
|
|
|
|
| 176 |
break # Success, exit retry loop
|
| 177 |
except Exception as e:
|
| 178 |
print(f"Attempt {attempt+1}/{max_retries} failed: {e}")
|
|
@@ -191,7 +210,6 @@ class SlpMultiAgent:
|
|
| 191 |
if result is None:
|
| 192 |
return "I apologize, but I'm currently experiencing technical difficulties. Please try again later."
|
| 193 |
|
| 194 |
-
|
| 195 |
# Extract clean answer from result
|
| 196 |
if result and isinstance(result, str):
|
| 197 |
import re
|
|
|
|
| 82 |
self.model = model
|
| 83 |
def generate(self, *args, **kwargs):
|
| 84 |
kwargs.pop('stop_sequences', None) # Remove unsupported argument for Gemini
|
| 85 |
+
result = self.model.generate_content(*args, **kwargs)
|
| 86 |
+
# Always return a string, not a ChatMessage or Gemini object
|
| 87 |
+
if hasattr(result, "text"):
|
| 88 |
+
print(f"[DEBUG] Gemini raw text output: {result.text}")
|
| 89 |
+
return result.text
|
| 90 |
+
elif isinstance(result, str):
|
| 91 |
+
print(f"[DEBUG] Gemini raw string output: {result}")
|
| 92 |
+
return result
|
| 93 |
+
elif hasattr(result, "candidates") and result.candidates:
|
| 94 |
+
# For some Gemini APIs, the text is in candidates[0].content.parts[0].text
|
| 95 |
+
try:
|
| 96 |
+
text = result.candidates[0].content.parts[0].text
|
| 97 |
+
print(f"[DEBUG] Gemini raw candidate output: {text}")
|
| 98 |
+
return text
|
| 99 |
+
except Exception:
|
| 100 |
+
pass
|
| 101 |
+
# Fallback: convert to string
|
| 102 |
+
print(f"[DEBUG] Gemini unknown response type: {type(result)}; value: {result}")
|
| 103 |
+
return str(result)
|
| 104 |
|
| 105 |
# --- Basic Agent Definition ---
|
| 106 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
|
|
|
| 124 |
# Patch: wrap Gemini model for smolagents compatibility
|
| 125 |
model = GeminiModelAdapter(genai.GenerativeModel('gemini-2.0-flash-exp'))
|
| 126 |
|
| 127 |
+
# Custom system prompt to force direct answer in code block
|
| 128 |
+
system_prompt = (
|
| 129 |
+
"You are a world expert at answering questions directly and concisely. "
|
| 130 |
+
"IMPORTANT: Only output a single code block in this format:\n"
|
| 131 |
+
"<code>\nfinal_answer(\"your direct, simple answer\")\n</code>\n"
|
| 132 |
+
"Do not include any other text, explanations, plans, or comments."
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
# Create only essential agents with reduced complexity
|
| 136 |
research_agent = CodeAgent(
|
| 137 |
tools=[KnowledgeBaseTool(), WikipediaSearchTool(), DuckDuckGoSearchTool()],
|
|
|
|
| 140 |
max_steps=3, # Allow more reasoning steps
|
| 141 |
name="ResearchAgent",
|
| 142 |
verbosity_level=0,
|
| 143 |
+
description="Quick factual research and knowledge lookup.",
|
| 144 |
+
system_prompt=system_prompt
|
| 145 |
)
|
| 146 |
|
| 147 |
solver_agent = CodeAgent(
|
|
|
|
| 151 |
max_steps=2, # Reduced steps
|
| 152 |
name="SolverAgent",
|
| 153 |
verbosity_level=0,
|
| 154 |
+
description="Problem solving, calculations, and logical reasoning.",
|
| 155 |
+
system_prompt=system_prompt
|
| 156 |
)
|
| 157 |
|
| 158 |
manager_agent = CodeAgent(
|
|
|
|
| 174 |
planning_interval=1, # Faster planning
|
| 175 |
verbosity_level=0, # Reduce verbosity
|
| 176 |
max_steps=3, # Further reduced steps to avoid timeouts
|
| 177 |
+
final_answer_checks=[check_reasoning],
|
| 178 |
+
system_prompt=system_prompt
|
| 179 |
)
|
| 180 |
|
| 181 |
# Create a task for the agent run with retry mechanism for rate limits
|
|
|
|
| 189 |
None,
|
| 190 |
lambda: manager_agent.run(f"""
|
| 191 |
Question: {short_question}
|
| 192 |
+
\nIMPORTANT: Only output a single code block in this format:\n<code>\nfinal_answer(\"your direct answer\")\n</code>\nBe concise and direct.\n""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
)
|
| 194 |
+
print(f"[DEBUG] Raw agent output: {result}")
|
| 195 |
break # Success, exit retry loop
|
| 196 |
except Exception as e:
|
| 197 |
print(f"Attempt {attempt+1}/{max_retries} failed: {e}")
|
|
|
|
| 210 |
if result is None:
|
| 211 |
return "I apologize, but I'm currently experiencing technical difficulties. Please try again later."
|
| 212 |
|
|
|
|
| 213 |
# Extract clean answer from result
|
| 214 |
if result and isinstance(result, str):
|
| 215 |
import re
|