File size: 5,979 Bytes
6b7b0c1
e1c1bb7
 
6b7b0c1
 
19b9935
 
 
1fa2efd
 
e631dac
adb4004
 
19b9935
 
 
adb4004
 
19b9935
 
adb4004
 
19b9935
 
adb4004
19b9935
 
 
 
 
 
 
 
1fa2efd
 
e631dac
adb4004
 
19b9935
 
 
adb4004
 
19b9935
 
adb4004
 
19b9935
 
adb4004
19b9935
 
 
 
 
 
 
 
1fa2efd
e631dac
adb4004
 
19b9935
 
 
adb4004
 
19b9935
 
adb4004
 
19b9935
 
adb4004
19b9935
 
 
 
 
 
 
 
1fa2efd
e631dac
adb4004
 
19b9935
 
 
adb4004
 
19b9935
 
adb4004
 
19b9935
 
adb4004
19b9935
 
 
 
 
 
 
 
 
73e8255
 
e631dac
adb4004
 
19b9935
 
 
adb4004
 
19b9935
 
adb4004
 
19b9935
 
adb4004
19b9935
 
 
73e8255
74cc1b7
e1c1bb7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74cc1b7
e1c1bb7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74cc1b7
 
 
 
 
e1c1bb7
74cc1b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1c1bb7
 
250ec74
e1c1bb7
250ec74
e1c1bb7
 
1fa2efd
19b9935
 
 
 
 
74cc1b7
 
 
1fa2efd
 
6b7b0c1
74cc1b7
 
 
6b7b0c1
1fa2efd
74cc1b7
 
 
 
 
 
 
1fa2efd
73e8255
6b7b0c1
73e8255
 
74cc1b7
 
 
 
1fa2efd
 
73e8255
57ee912
1fa2efd
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
from smolagents import HfApiModel, CodeAgent, DuckDuckGoSearchTool, WikipediaSearchTool, Tool, LiteLLMModel
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain_community.document_loaders import WikipediaLoader, ArxivLoader


class add(Tool):
    name = "add"
    description = """
        This tool adds two integers together and returns an integer.

        Args:
            a: first integer
            b: second integer
        """
    inputs = {
        "a":{
            "type":"integer",
            "description":"first integer"
        },
        "b":{
            "type":"integer",
            "description":"second integer"
        }
    }
    output_type = "integer"

    def forward(self, a: int, b: int):
        return a + b


class subtract(Tool):
    name = "subtract"
    description = """
        This tool subtracts two integers and returns an integer.

        Args:
            a: first integer
            b: second integer
        """
    inputs = {
        "a":{
            "type":"integer",
            "description":"first integer"
        },
        "b":{
            "type":"integer",
            "description":"second integer"
        }
    }
    output_type = "integer"

    def forward(self, a: int, b: int):
        return a - b

class multiply(Tool):
    name = "multiply"
    description = """
        This tool multiplies two integers and returns an integer.

        Args:
            a: first integer
            b: second integer
        """
    inputs = {
        "a":{
            "type":"integer",
            "description":"first integer"
        },
        "b":{
            "type":"integer",
            "description":"second integer"
        }
    }
    output_type = "integer"

    def forward(self, a: int, b: int):
        return a * b

class divide(Tool):
    name = "divide"
    description = """
        This tool divides two integers and returns an integer.

        Args:
            a: first integer
            b: second integer
        """
    inputs = {
        "a":{
            "type":"integer",
            "description":"first integer"
        },
        "b":{
            "type":"integer",
            "description":"second integer"
        }
    }
    output_type = "integer"

    def forward(self, a: int, b: int):
        if b == 0:
            raise ValueError("Cannot divide by zero.")
        return a / b

class modulo(Tool):
    name = "modulo"
    description = """
        This tool returns the modulus of two integers

        Args:
            a: first integer
            b: second integer
        """
    inputs = {
        "a":{
            "type":"integer",
            "description":"first integer"
        },
        "b":{
            "type":"integer",
            "description":"second integer"
        }
    }
    output_type = "integer"

    def forward(self, a: int, b: int):
        return a % b

class WikipediaSearchTool(Tool):
    name = "wikipedia_search_tool"
    
    description = """
        Search Wikipedia for a query and return top 2 results.
    
        Args:
            query: the search query.
    """
    
    inputs = {
        "query":{
            "type":"string",
            "description":"the search query"
        }
    }
    output_type = "string"

    def forward(self, query: str) -> str:
        documents = WikipediaLoader(query=query, load_max_docs=2).load()
        condensed_docs = "\n\n---\n\n".join(
        [
            f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
            for doc in documents
        ])
        return {"wikipedia_results": condensed_docs}

class TavilySearchTool(Tool):
    name = "tavily_search_tool"
    
    description = """
        Search the internet using the browser Tavily and return the top 3 results.
    
        Args:
            query: the search query.
    """
    
    inputs = {
        "query":{
            "type":"string",
            "description":"the search query"
        }
    }
    output_type = "string"

    def forward(self, query: str) -> str:
        documents = TavilySearchResults(max_results=3).invoke(query=query)
        condensed_docs = "\n\n---\n\n".join(
        [
            f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
            for doc in documents
        ])
        return {"web_search_results": condensed_docs}


class ArvixSearchTool(Tool):
    name = "arvix_search_tool"
    
    description = """
        Search arxiv for a query and return maximum 3 result.
    
        Args:
            query: The search query.
        """
    
    inputs = {
        "query":{
            "type":"string",
            "description":"the search query"
        }
    }
    output_type = "string"

    def forward(self, query: str) -> str:
        documents = ArxivLoader(query=query, load_max_docs=3).load()
        condensed_docs = "\n\n---\n\n".join(
        [
            f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content[:1000]}\n</Document>'
            for doc in documents
        ])
        return {"arvix_search_results": condensed_docs}


tools=[
    add(),
    subtract(),
    multiply(),
    divide(),
    modulo(),
    WikipediaSearchTool(),
    ArvixSearchTool(),
    TavilySearchTool()
]

model = LiteLLMModel(
    model_id='ollama_chat/gemma3:27b',
    api_base="http://127.0.0.1:11434",
    num_ctx=8192
)

def delay_execution_10(pagent, **kwargs) -> bool:
    """
    Delays the execution for 10 seconds.
    """
    time.sleep(10)
    return True

def create_agent():
    agent = CodeAgent(
        model = model,
        tools = tools,
        max_steps=10,
        verbosity_level=2,
        additional_authorized_imports=['*'],
        planning_interval=5,  
        step_callbacks=[delay_execution_10]
    )

    return agent