11from copy import deepcopy
22from dataclasses import dataclass
33from textwrap import dedent
4- from typing import Any , Callable , Dict , List , Optional , cast
4+ from typing import Any , Callable , Dict , List , Optional
55
66from agno .memory .v2 .db .base import MemoryDb
77from agno .memory .v2 .db .schema import MemoryRow
@@ -44,30 +44,25 @@ def __init__(
4444 self .system_message = system_message
4545 self .memory_capture_instructions = memory_capture_instructions
4646 self .additional_instructions = additional_instructions
47-
48- def add_tools_to_model (self , model : Model , tools : List [Callable ]) -> None :
49- model = cast (Model , model )
50- model .reset_tools_and_functions ()
51-
52- _tools_for_model = []
53- _functions_for_model = {}
54-
55- for tool in tools :
56- try :
57- function_name = tool .__name__
58- if function_name not in _functions_for_model :
59- func = Function .from_callable (tool , strict = True ) # type: ignore
60- func .strict = True
61- _functions_for_model [func .name ] = func
62- _tools_for_model .append ({"type" : "function" , "function" : func .to_dict ()})
63- log_debug (f"Added function { func .name } " )
64- except Exception as e :
65- log_warning (f"Could not add function { tool } : { e } " )
66-
67- # Set tools on the model
68- model .set_tools (tools = _tools_for_model )
69- # Set functions on the model
70- model .set_functions (functions = _functions_for_model )
47+ self ._tools_for_model : Optional [List [Dict [str , Any ]]] = None
48+ self ._functions_for_model : Optional [Dict [str , Function ]] = None
49+
50+ def determine_tools_for_model (self , tools : List [Callable ]) -> None :
51+ if self ._tools_for_model is None :
52+ self ._tools_for_model = []
53+ self ._functions_for_model = {}
54+
55+ for tool in tools :
56+ try :
57+ function_name = tool .__name__
58+ if function_name not in self ._functions_for_model :
59+ func = Function .from_callable (tool , strict = True ) # type: ignore
60+ func .strict = True
61+ self ._functions_for_model [func .name ] = func
62+ self ._tools_for_model .append ({"type" : "function" , "function" : func .to_dict ()})
63+ log_debug (f"Added function { func .name } " )
64+ except Exception as e :
65+ log_warning (f"Could not add function { tool } : { e } " )
7166
7267 def get_system_message (
7368 self ,
@@ -166,8 +161,7 @@ def create_or_update_memories(
166161
167162 model_copy = deepcopy (self .model )
168163 # Update the Model (set defaults, add logit etc.)
169- self .add_tools_to_model (
170- model_copy ,
164+ self .determine_tools_for_model (
171165 self ._get_db_tools (
172166 user_id , db , input_string , enable_delete_memory = delete_memories , enable_clear_memory = clear_memories
173167 ),
@@ -184,7 +178,9 @@ def create_or_update_memories(
184178 ]
185179
186180 # Generate a response from the Model (includes running function calls)
187- response = model_copy .response (messages = messages_for_model )
181+ response = model_copy .response (
182+ messages = messages_for_model , tools = self ._tools_for_model , functions = self ._functions_for_model
183+ )
188184
189185 if response .tool_calls is not None and len (response .tool_calls ) > 0 :
190186 self .memories_updated = True
@@ -214,8 +210,7 @@ async def acreate_or_update_memories(
214210
215211 model_copy = deepcopy (self .model )
216212 # Update the Model (set defaults, add logit etc.)
217- self .add_tools_to_model (
218- model_copy ,
213+ self .determine_tools_for_model (
219214 self ._get_db_tools (
220215 user_id , db , input_string , enable_delete_memory = delete_memories , enable_clear_memory = clear_memories
221216 ),
@@ -232,7 +227,9 @@ async def acreate_or_update_memories(
232227 ]
233228
234229 # Generate a response from the Model (includes running function calls)
235- response = await model_copy .aresponse (messages = messages_for_model )
230+ response = await model_copy .aresponse (
231+ messages = messages_for_model , tools = self ._tools_for_model , functions = self ._functions_for_model
232+ )
236233
237234 if response .tool_calls is not None and len (response .tool_calls ) > 0 :
238235 self .memories_updated = True
@@ -257,8 +254,7 @@ def run_memory_task(
257254
258255 model_copy = deepcopy (self .model )
259256 # Update the Model (set defaults, add logit etc.)
260- self .add_tools_to_model (
261- model_copy ,
257+ self .determine_tools_for_model (
262258 self ._get_db_tools (
263259 user_id , db , task , enable_delete_memory = delete_memories , enable_clear_memory = clear_memories
264260 ),
@@ -274,7 +270,9 @@ def run_memory_task(
274270 ]
275271
276272 # Generate a response from the Model (includes running function calls)
277- response = model_copy .response (messages = messages_for_model )
273+ response = model_copy .response (
274+ messages = messages_for_model , tools = self ._tools_for_model , functions = self ._functions_for_model
275+ )
278276
279277 if response .tool_calls is not None and len (response .tool_calls ) > 0 :
280278 self .memories_updated = True
@@ -299,8 +297,7 @@ async def arun_memory_task(
299297
300298 model_copy = deepcopy (self .model )
301299 # Update the Model (set defaults, add logit etc.)
302- self .add_tools_to_model (
303- model_copy ,
300+ self .determine_tools_for_model (
304301 self ._get_db_tools (
305302 user_id , db , task , enable_delete_memory = delete_memories , enable_clear_memory = clear_memories
306303 ),
@@ -316,7 +313,9 @@ async def arun_memory_task(
316313 ]
317314
318315 # Generate a response from the Model (includes running function calls)
319- response = await model_copy .aresponse (messages = messages_for_model )
316+ response = await model_copy .aresponse (
317+ messages = messages_for_model , tools = self ._tools_for_model , functions = self ._functions_for_model
318+ )
320319
321320 if response .tool_calls is not None and len (response .tool_calls ) > 0 :
322321 self .memories_updated = True
0 commit comments