Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -235,12 +235,56 @@ def launch_interface():
|
|
| 235 |
custom_search_url=None
|
| 236 |
)
|
| 237 |
|
| 238 |
-
|
| 239 |
-
#
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
|
| 245 |
|
| 246 |
reasoning_buffer = []
|
|
|
|
| 235 |
custom_search_url=None
|
| 236 |
)
|
| 237 |
|
| 238 |
+
# Override the agent's system prompt for code generation
|
| 239 |
+
# Based on the error, smolagents uses prompt_templates for system prompts.
|
| 240 |
+
# The specific key might vary, but 'user_agent' or 'managed_agent' are common.
|
| 241 |
+
# Assuming the CodeAgent also has a 'system_prompt' in its prompt_templates
|
| 242 |
+
# or we can modify the one for its internal 'managed_agent' if applicable.
|
| 243 |
+
|
| 244 |
+
# A common pattern for smolagents' CodeAgent might be to modify the 'user_agent'
|
| 245 |
+
# or 'managed_agent' template that it uses internally.
|
| 246 |
+
# Let's try modifying the 'managed_agent' task and prepending the system prompt.
|
| 247 |
+
# If this doesn't fully capture the 'system' role, you might need to inspect
|
| 248 |
+
# the exact structure of `agent.prompt_templates` in a debugger.
|
| 249 |
+
|
| 250 |
+
# Safely try to modify the prompt template used by the CodeAgent.
|
| 251 |
+
# The exact key for the system prompt might vary (e.g., 'system', 'default', 'user_agent').
|
| 252 |
+
# A common structure is `agent.prompt_templates["user_agent"]["system_prompt"]`
|
| 253 |
+
# or it might be directly injected in the `task` for agents that don't have a separate system role.
|
| 254 |
+
|
| 255 |
+
# Let's assume the `CodeAgent` itself uses a "default" or similar template
|
| 256 |
+
# that allows injecting a system prompt, or that its primary instruction
|
| 257 |
+
# can be adjusted via its 'task' definition.
|
| 258 |
+
|
| 259 |
+
# A more direct way to ensure the system prompt is used by LiteLLMModel
|
| 260 |
+
# within smolagents is to pass it during agent creation, or if the agent
|
| 261 |
+
# has an exposed way to set a 'system' message.
|
| 262 |
+
|
| 263 |
+
# Given the error implies direct modification of `prompt_templates`,
|
| 264 |
+
# let's try to find a suitable place.
|
| 265 |
+
# Based on smolagents examples, `user_agent` or `managed_agent` templates are often customized.
|
| 266 |
+
# The `CodeAgent` itself doesn't typically expose a direct `system_prompt` property
|
| 267 |
+
# that it forwards to the LLM; instead, it uses the prompt templates.
|
| 268 |
+
|
| 269 |
+
# The error specifically points to `self.prompt_templates["system_prompt"]`.
|
| 270 |
+
# This implies there *is* a key "system_prompt" within prompt_templates.
|
| 271 |
+
# Let's apply SYSTEM_PROMPT_CODE_GEN to this specific key.
|
| 272 |
+
if hasattr(agent, 'prompt_templates') and "system_prompt" in agent.prompt_templates:
|
| 273 |
+
# If the agent explicitly exposes a "system_prompt" template key
|
| 274 |
+
agent.prompt_templates["system_prompt"] = SYSTEM_PROMPT_CODE_GEN
|
| 275 |
+
print("[DEBUG] Set agent.prompt_templates['system_prompt'] for code generation.")
|
| 276 |
+
elif hasattr(agent, 'prompt_templates') and 'user_agent' in agent.prompt_templates:
|
| 277 |
+
# If it's a ToolCallingAgent (which CodeAgent inherits from)
|
| 278 |
+
# and it has a 'user_agent' template, we can modify its system message.
|
| 279 |
+
# This is a common pattern for defining the agent's core persona.
|
| 280 |
+
agent.prompt_templates['user_agent']['system_message'] = SYSTEM_PROMPT_CODE_GEN
|
| 281 |
+
print("[DEBUG] Set agent.prompt_templates['user_agent']['system_message'] for code generation.")
|
| 282 |
+
else:
|
| 283 |
+
print("[WARNING] Could not set system prompt for CodeAgent using known patterns. "
|
| 284 |
+
"Agent might not follow code generation instructions optimally.")
|
| 285 |
+
# Fallback: Prepend to the question for basic instruction if no proper system prompt mechanism
|
| 286 |
+
# This is not ideal but ensures the instruction is conveyed.
|
| 287 |
+
query = SYSTEM_PROMPT_CODE_GEN + "\n\n" + query
|
| 288 |
|
| 289 |
|
| 290 |
reasoning_buffer = []
|