File size: 1,569 Bytes
3f8a628
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from datetime import datetime
from pathlib import Path

from smolagents import CodeAgent, InferenceClientModel, DuckDuckGoSearchTool
from tools import GetCurrentDateTool

model = InferenceClientModel(
    model_id="Qwen/Qwen3-Next-80B-A3B-Thinking"
    #model_id="Qwen/Qwen3.5-9B"
)
search_tool = DuckDuckGoSearchTool()
date_tool=GetCurrentDateTool()

QUESTIONS_LOG = Path(__file__).parent / "questions.log"

class LoggingCodeAgent(CodeAgent):
    def run(self, task, *args, **kwargs):
        try:
            with QUESTIONS_LOG.open("a", encoding="utf-8") as f:
                f.write(f"[{datetime.now().isoformat()}] Q: {task}\n")
        except Exception as e:
            print(f"Failed to log question: {e}")
        answer = super().run(task, *args, **kwargs)
        try:
            with QUESTIONS_LOG.open("a", encoding="utf-8") as f:
                f.write(f"[{datetime.now().isoformat()}] A: {answer}\n")
        except Exception as e:
            print(f"Failed to log answer: {e}")
        return answer

codeAgent = LoggingCodeAgent(
    max_steps=10,
    model=model,
    tools=[ search_tool, date_tool ],
    additional_authorized_imports=[
        'time',
        'itertools',
        'random',
        'statistics',
        'stat',
        'datetime',
        'queue',
        'unicodedata',
        'math',
        'collections',
        're'
    ]
)

# response = agent.run(
#     """
#     First use python to get current date. Then answer questions.
#     Who is the president of India as of today ?
#     """
# )

# print(response, date_tool)