Update app.py
Browse files
app.py
CHANGED
|
@@ -17,7 +17,7 @@ logging.basicConfig(
|
|
| 17 |
logger = logging.getLogger(__name__) # Define logger here
|
| 18 |
|
| 19 |
# Import necessary components from smolagents
|
| 20 |
-
#
|
| 21 |
try:
|
| 22 |
from smolagents import CodeAgent, Tool, OpenAIServerModel
|
| 23 |
SMOLAGENTS_IMPORTED_SUCCESSFULLY = True
|
|
@@ -244,26 +244,52 @@ class QuestionAnsweringAgent(CodeAgent):
|
|
| 244 |
api_key=openai_api_key
|
| 245 |
)
|
| 246 |
|
| 247 |
-
#
|
| 248 |
-
# This is the standard way to initialize CodeAgent.
|
| 249 |
-
# If this still causes a "MultiStepAgent... system_prompt" TypeError,
|
| 250 |
-
# it means the installed version of smolagents has an internal bug.
|
| 251 |
try:
|
| 252 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
model=effective_model,
|
| 254 |
tools=tools_list,
|
| 255 |
-
system_prompt=CONFIG.AGENT_SYSTEM_PROMPT,
|
| 256 |
-
additional_authorized_imports=["PIL", "pandas"],
|
| 257 |
max_steps=CONFIG.AGENT_MAX_STEPS,
|
| 258 |
-
|
| 259 |
)
|
| 260 |
-
logger.info(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
except TypeError as e:
|
| 262 |
-
logger.error(f"
|
| 263 |
-
raise e
|
| 264 |
-
except Exception as e_init:
|
| 265 |
-
logger.error(f"Unexpected error during QuestionAnsweringAgent __init__: {e_init}", exc_info=True)
|
| 266 |
raise e_init
|
|
|
|
|
|
|
| 267 |
|
| 268 |
|
| 269 |
async def process_question(self, question_text: str) -> Tuple[str, str]:
|
|
@@ -543,7 +569,7 @@ with gr.Blocks() as demo:
|
|
| 543 |
* The agent uses `smolagents`, OpenAI (model: `{CONFIG.OPENAI_MODEL_ID}`), and Serper API for searches.
|
| 544 |
* Ensure `OPENAI_API_KEY` and `SERPER_API_KEY` are set as Space Secrets.
|
| 545 |
* `requirements.txt` should specify a compatible `gradio` version and `smolagents`.
|
| 546 |
-
*
|
| 547 |
"""
|
| 548 |
)
|
| 549 |
|
|
|
|
| 17 |
logger = logging.getLogger(__name__) # Define logger here
|
| 18 |
|
| 19 |
# Import necessary components from smolagents
|
| 20 |
+
# We are NOT importing CodeWriter explicitly, relying on CodeAgent's internal setup.
|
| 21 |
try:
|
| 22 |
from smolagents import CodeAgent, Tool, OpenAIServerModel
|
| 23 |
SMOLAGENTS_IMPORTED_SUCCESSFULLY = True
|
|
|
|
| 244 |
api_key=openai_api_key
|
| 245 |
)
|
| 246 |
|
| 247 |
+
# REFINED WORKAROUND for MultiStepAgent internal TypeError
|
|
|
|
|
|
|
|
|
|
| 248 |
try:
|
| 249 |
+
# Step 1: Initialize the parent of CodeAgent (MultiStepAgent)
|
| 250 |
+
# Pass arguments that MultiStepAgent or its parent (Agent) would expect.
|
| 251 |
+
# 'additional_authorized_imports' is handled by the base 'Agent' class.
|
| 252 |
+
# 'system_prompt' and 'verbosity_level' are specific to CodeAgent and should NOT be passed here
|
| 253 |
+
# if MultiStepAgent does not accept them (which the TypeError indicates).
|
| 254 |
+
super(CodeAgent, self).__init__(
|
| 255 |
model=effective_model,
|
| 256 |
tools=tools_list,
|
|
|
|
|
|
|
| 257 |
max_steps=CONFIG.AGENT_MAX_STEPS,
|
| 258 |
+
additional_authorized_imports=["PIL", "pandas"] # This is passed to Agent.__init__
|
| 259 |
)
|
| 260 |
+
logger.info("QuestionAnsweringAgent > MultiStepAgent part initialized (workaround) including additional_authorized_imports.")
|
| 261 |
+
|
| 262 |
+
# Step 2: Manually set attributes specific to CodeAgent on the instance.
|
| 263 |
+
# CodeAgent's own __init__ logic (which runs after its super() call to MultiStepAgent)
|
| 264 |
+
# should pick these up to configure itself, especially for its CodeWriter.
|
| 265 |
+
self.model = effective_model # Ensure it's set on self for CodeWriter if not already
|
| 266 |
+
self.tools = tools_list # Ensure it's set on self for CodeWriter if not already
|
| 267 |
+
self.system_prompt = CONFIG.AGENT_SYSTEM_PROMPT # CRITICAL
|
| 268 |
+
self.verbosity_level = CONFIG.AGENT_VERBOSITY_LEVEL # For CodeAgent's own logging
|
| 269 |
+
# self.additional_authorized_imports is set by the super() call.
|
| 270 |
+
|
| 271 |
+
# Step 3: Ensure CodeAgent's CodeWriter is properly initialized.
|
| 272 |
+
# CodeAgent's __init__ typically does:
|
| 273 |
+
# self.code_writer = code_writer_param or CodeWriter(model=self.model, tools=self.tools, system_prompt=self.system_prompt, ...)
|
| 274 |
+
# We are relying on the fact that CodeAgent's __init__ (the part that runs *after* its super() call to MultiStepAgent)
|
| 275 |
+
# will correctly instantiate or use its internal CodeWriter with the self.system_prompt we've just set.
|
| 276 |
+
# We cannot directly import and create CodeWriter if `from smolagents.code_writer import CodeWriter` fails.
|
| 277 |
+
if hasattr(self, 'code_writer') and self.code_writer is not None:
|
| 278 |
+
logger.info("CodeAgent's internal CodeWriter seems to be initialized.")
|
| 279 |
+
# If CodeWriter was created by CodeAgent's __init__ *before* its super() call (unlikely),
|
| 280 |
+
# it might have a default system_prompt. We hope it uses self.system_prompt dynamically or
|
| 281 |
+
# re-initializes/updates based on self.system_prompt.
|
| 282 |
+
else:
|
| 283 |
+
logger.warning("CodeAgent's internal CodeWriter might not be initialized or accessible for verification after workaround. Agent may not use custom system_prompt correctly.")
|
| 284 |
+
|
| 285 |
except TypeError as e:
|
| 286 |
+
logger.error(f"WORKAROUND FAILED during __init__ (TypeError): {e}. This suggests an issue with the minimal args for MultiStepAgent or how CodeAgent handles its attributes post-super-init.", exc_info=True)
|
| 287 |
+
raise e
|
| 288 |
+
except Exception as e_init:
|
| 289 |
+
logger.error(f"Unexpected error during QuestionAnsweringAgent __init__ (workaround): {e_init}", exc_info=True)
|
| 290 |
raise e_init
|
| 291 |
+
|
| 292 |
+
logger.info(f"QuestionAnsweringAgent initialized (with workaround) with model: {CONFIG.OPENAI_MODEL_ID}.")
|
| 293 |
|
| 294 |
|
| 295 |
async def process_question(self, question_text: str) -> Tuple[str, str]:
|
|
|
|
| 569 |
* The agent uses `smolagents`, OpenAI (model: `{CONFIG.OPENAI_MODEL_ID}`), and Serper API for searches.
|
| 570 |
* Ensure `OPENAI_API_KEY` and `SERPER_API_KEY` are set as Space Secrets.
|
| 571 |
* `requirements.txt` should specify a compatible `gradio` version and `smolagents`.
|
| 572 |
+
* A workaround for `smolagents.CodeAgent` initialization is currently active.
|
| 573 |
"""
|
| 574 |
)
|
| 575 |
|