Spaces:
Sleeping
Sleeping
simplified reasoning and prompts
Browse files
app.py
CHANGED
|
@@ -131,19 +131,15 @@ class SlpMultiAgent:
|
|
| 131 |
lambda: manager_agent.run(f"""
|
| 132 |
Question: {short_question}
|
| 133 |
|
| 134 |
-
|
| 135 |
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
</code>
|
|
|
|
| 139 |
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
<code>
|
| 143 |
-
final_answer("Your answer here")
|
| 144 |
-
</code>
|
| 145 |
-
|
| 146 |
-
Be precise and factual. Use search tools only when needed.
|
| 147 |
""")
|
| 148 |
)
|
| 149 |
break # Success, exit retry loop
|
|
@@ -165,49 +161,37 @@ class SlpMultiAgent:
|
|
| 165 |
return "I apologize, but I'm currently experiencing technical difficulties. Please try again later."
|
| 166 |
|
| 167 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
# Return the result from the agent
|
| 169 |
-
return result
|
| 170 |
|
| 171 |
def check_reasoning(final_answer, agent_memory):
|
| 172 |
try:
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
)
|
| 177 |
-
|
| 178 |
-
# More focused validation prompt
|
| 179 |
-
prompt = f"Rate answer quality 1-10: {final_answer[:200]}..."
|
| 180 |
-
|
| 181 |
-
messages = [
|
| 182 |
-
{
|
| 183 |
-
"role": "user",
|
| 184 |
-
"content": prompt
|
| 185 |
-
}
|
| 186 |
-
]
|
| 187 |
-
|
| 188 |
-
# Add retry mechanism for rate limits
|
| 189 |
-
max_retries = 2 # Reduced retries
|
| 190 |
-
for attempt in range(max_retries):
|
| 191 |
-
try:
|
| 192 |
-
output = multimodal_model(messages)
|
| 193 |
-
if hasattr(output, 'content'):
|
| 194 |
-
# Actually check the response instead of always returning True
|
| 195 |
-
response = output.content.lower()
|
| 196 |
-
# Look for quality indicators
|
| 197 |
-
if any(word in response for word in ['7', '8', '9', '10', 'good', 'correct']):
|
| 198 |
-
return True
|
| 199 |
-
elif any(word in response for word in ['1', '2', '3', '4', 'poor', 'wrong']):
|
| 200 |
-
return False
|
| 201 |
-
return True # Default to pass if unclear
|
| 202 |
-
break
|
| 203 |
-
except Exception as e:
|
| 204 |
-
if attempt < max_retries - 1:
|
| 205 |
-
print(f"Retry {attempt+1}/{max_retries} due to: {e}")
|
| 206 |
-
time.sleep(3) # Reduced wait time
|
| 207 |
-
else:
|
| 208 |
-
print(f"Final attempt failed: {e}")
|
| 209 |
|
| 210 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
except Exception as e:
|
| 212 |
print(f"Error in reasoning check: {e}")
|
| 213 |
return True # Default to passing on errors
|
|
|
|
| 131 |
lambda: manager_agent.run(f"""
|
| 132 |
Question: {short_question}
|
| 133 |
|
| 134 |
+
IMPORTANT: You must end with a final_answer() call containing ONLY the direct answer.
|
| 135 |
|
| 136 |
+
Steps:
|
| 137 |
+
1. Think through the problem
|
| 138 |
+
2. Use search if needed with: <code>search_result = reliable_search("query")</code>
|
| 139 |
+
3. End with: <code>final_answer("direct answer only")</code>
|
| 140 |
|
| 141 |
+
Do NOT include thoughts, explanations, or reasoning in the final_answer().
|
| 142 |
+
The final_answer() should contain only the specific answer requested.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
""")
|
| 144 |
)
|
| 145 |
break # Success, exit retry loop
|
|
|
|
| 161 |
return "I apologize, but I'm currently experiencing technical difficulties. Please try again later."
|
| 162 |
|
| 163 |
|
| 164 |
+
# Extract clean answer from result
|
| 165 |
+
if result and isinstance(result, str):
|
| 166 |
+
# Look for final_answer pattern
|
| 167 |
+
import re
|
| 168 |
+
final_answer_match = re.search(r'final_answer\(["\']([^"\']*)["\'\])', result)
|
| 169 |
+
if final_answer_match:
|
| 170 |
+
clean_answer = final_answer_match.group(1)
|
| 171 |
+
return clean_answer
|
| 172 |
+
|
| 173 |
+
# If no final_answer found, try to extract the last meaningful line
|
| 174 |
+
lines = result.strip().split('\n')
|
| 175 |
+
for line in reversed(lines):
|
| 176 |
+
line = line.strip()
|
| 177 |
+
if line and not line.startswith('#') and not line.startswith('###') and len(line) < 200:
|
| 178 |
+
return line
|
| 179 |
+
|
| 180 |
# Return the result from the agent
|
| 181 |
+
return result if result else "Unable to determine answer."
|
| 182 |
|
| 183 |
def check_reasoning(final_answer, agent_memory):
|
| 184 |
try:
|
| 185 |
+
# Simple validation - check if answer looks complete
|
| 186 |
+
if not final_answer or len(final_answer.strip()) < 2:
|
| 187 |
+
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
|
| 189 |
+
# Check if it's just thoughts/reasoning instead of an answer
|
| 190 |
+
bad_patterns = ['### Thought:', '### Code:', 'I will', 'Let me', 'First, I', 'Next, I']
|
| 191 |
+
if any(pattern in final_answer for pattern in bad_patterns):
|
| 192 |
+
return False
|
| 193 |
+
|
| 194 |
+
return True # Pass if it looks like a real answer
|
| 195 |
except Exception as e:
|
| 196 |
print(f"Error in reasoning check: {e}")
|
| 197 |
return True # Default to passing on errors
|