@@ -182,24 +182,7 @@ def __init__(
182
182
else :
183
183
self ._system_messages = [SystemMessage (content = system_message )]
184
184
self ._tools : List [Tool ] = []
185
- if tools is not None :
186
- if model_client .capabilities ["function_calling" ] is False :
187
- raise ValueError ("The model does not support function calling." )
188
- for tool in tools :
189
- if isinstance (tool , Tool ):
190
- self ._tools .append (tool )
191
- elif callable (tool ):
192
- if hasattr (tool , "__doc__" ) and tool .__doc__ is not None :
193
- description = tool .__doc__
194
- else :
195
- description = ""
196
- self ._tools .append (FunctionTool (tool , description = description ))
197
- else :
198
- raise ValueError (f"Unsupported tool type: { type (tool )} " )
199
- # Check if tool names are unique.
200
- tool_names = [tool .name for tool in self ._tools ]
201
- if len (tool_names ) != len (set (tool_names )):
202
- raise ValueError (f"Tool names must be unique: { tool_names } " )
185
+ self ._model_context : List [LLMMessage ] = []
203
186
# Handoff tools.
204
187
self ._handoff_tools : List [Tool ] = []
205
188
self ._handoffs : Dict [str , HandoffBase ] = {}
@@ -214,6 +197,27 @@ def __init__(
214
197
self ._handoffs [handoff .name ] = handoff
215
198
else :
216
199
raise ValueError (f"Unsupported handoff type: { type (handoff )} " )
200
+ if tools is not None :
201
+ self .add_tools (tools )
202
+
203
+ def add_tools (self , tools : List [Tool | Callable [..., Any ] | Callable [..., Awaitable [Any ]]]) -> None :
204
+ if self ._model_client .capabilities ["function_calling" ] is False :
205
+ raise ValueError ("The model does not support function calling." )
206
+ for tool in tools :
207
+ if isinstance (tool , Tool ):
208
+ self ._tools .append (tool )
209
+ elif callable (tool ):
210
+ if hasattr (tool , "__doc__" ) and tool .__doc__ is not None :
211
+ description = tool .__doc__
212
+ else :
213
+ description = ""
214
+ self ._tools .append (FunctionTool (tool , description = description ))
215
+ else :
216
+ raise ValueError (f"Unsupported tool type: { type (tool )} " )
217
+ # Check if tool names are unique.
218
+ tool_names = [tool .name for tool in self ._tools ]
219
+ if len (tool_names ) != len (set (tool_names )):
220
+ raise ValueError (f"Tool names must be unique: { tool_names } " )
217
221
# Check if handoff tool names are unique.
218
222
handoff_tool_names = [tool .name for tool in self ._handoff_tools ]
219
223
if len (handoff_tool_names ) != len (set (handoff_tool_names )):
@@ -223,7 +227,26 @@ def __init__(
223
227
raise ValueError (
224
228
f"Handoff names must be unique from tool names. Handoff names: { handoff_tool_names } ; tool names: { tool_names } "
225
229
)
226
- self ._model_context : List [LLMMessage ] = []
230
+
231
+ def remove_all_tools (self ) -> None :
232
+ """Remove all tools."""
233
+ self ._tools = []
234
+
235
+ def remove_tools (self , tool_names : List [str ]) -> None :
236
+ """Remove tools by name."""
237
+ for name in tool_names :
238
+ for tool in self ._tools :
239
+ if tool .name == name :
240
+ self ._tools .remove (tool )
241
+ break
242
+ for tool in self ._handoff_tools :
243
+ if tool .name == name :
244
+ self ._handoff_tools .remove (tool )
245
+ break
246
+ for handoff in self ._handoffs .values ():
247
+ if handoff .name == name :
248
+ self ._handoffs .pop (handoff .name )
249
+ break
227
250
228
251
@property
229
252
def produced_message_types (self ) -> List [type [ChatMessage ]]:
0 commit comments