File size: 1,880 Bytes
b24b394
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
47
48
49
50
51
52
53
54
55
56
57
58
59
import os

from smolagents import CodeAgent, DuckDuckGoSearchTool, VisitWebpageTool, HfApiModel


class BasicAgent(CodeAgent):
    def __init__(self):
        print("Initializing BasicAgent...")

        model_name = os.getenv("MODEL_NAME")

        if not model_name:
            raise ValueError(
                "Environment variable 'MODEL_NAME' is required but not set.")

        print(f"LLM Config: Model='{model_name}'")

        try:
            llm = HfApiModel(
                model_id=model_name,
                temperature=0.1,
            )
        except Exception as e:
            print(f"Error initializing LLM: {e}")
            raise

        print("Initializing tools...")
        try:
            search_tool = DuckDuckGoSearchTool()
            visit_tool = VisitWebpageTool()
            tools = [search_tool, visit_tool]
            print(f"Tools initialized: {[tool.name for tool in tools]}")
        except Exception as e:
            print(f"Error initializing tools: {e}")
            raise

        try:
            super().__init__(
                model=llm,
                tools=tools,
            )
            print("BasicAgent (CodeAgent) initialized successfully.")
        except Exception as e:
            print(f"Error initializing CodeAgent: {e}")
            raise

    def __call__(self, question: str) -> str:
        print(
            f"BasicAgent received question (first 50 chars): {question[:50]}...")
        try:
            final_answer = self.run(question)
            print(
                f"BasicAgent returning answer (first 50 chars): {str(final_answer)[:50]}...")
            return str(final_answer)
        except Exception as e:
            print(
                f"Error during agent execution for question '{question[:50]}...': {e}")
            return f"AGENT ERROR: Failed to process the question due to: {e}"