Spaces:
Build error
Build error
| import asyncio | |
| from functools import lru_cache | |
| from typing import Final | |
| from venv import logger | |
| from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel | |
| DEFAULT_MODEL_ID: Final[str] = "Qwen/Qwen2.5-Coder-32B-Instruct" | |
| DEFAULT_TIMEOUT_SECS: Final[int] = 20 | |
| MAX_QUESTION_LEN: Final[int] = 5000 | |
| class BasicAgent: | |
| def __init__( | |
| self, | |
| model_id: str = DEFAULT_MODEL_ID, | |
| timeout_seconds: int = DEFAULT_TIMEOUT_SECS | |
| ): | |
| self.model_id = model_id | |
| self.model = InferenceClientModel(model_id=self.model_id) | |
| self.agent = CodeAgent(tools=[DuckDuckGoSearchTool()], model=self.model) | |
| self.timeout_seconds = timeout_seconds | |
| def _cached_run(self, question: str) -> str: | |
| return self.agent.run(question) | |
| def __call__(self, question: str) -> str: | |
| if not isinstance(question, str) or not question.strip(): | |
| raise ValueError("Empty Question") | |
| if len(question) > MAX_QUESTION_LEN: | |
| return "Question too long; please shorten." | |
| logger.info("Agent received question", extra={"prefix": question[:100]}) | |
| try: | |
| result = self._cached_run(question) | |
| except Exception as exc: | |
| logger.exception("Agent run failed") | |
| return "Sorry, I couldn't process that right now. Try again later." | |
| logger.info("Agent returning result", extra={"len": result, "type": str(type(result))}) | |
| return result | |