File size: 1,501 Bytes
2bb5577
 
 
d7590e5
2bb5577
 
 
 
d7590e5
 
 
2bb5577
a193c46
b08f9f2
 
 
 
 
2bb5577
 
 
 
 
 
 
 
a193c46
 
2bb5577
 
 
b08f9f2
2bb5577
 
 
 
 
d7590e5
2bb5577
 
 
 
4594618
2bb5577
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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

    @lru_cache(maxsize=1024)
    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