2
2
3
3
from haystack import component , default_from_dict , default_to_dict
4
4
from haystack .dataclasses import ChatMessage , StreamingChunk , ToolCall
5
- from haystack .tools import Tool , _check_duplicate_tool_names , deserialize_tools_or_toolset_inplace
5
+ from haystack .tools import (
6
+ Tool ,
7
+ _check_duplicate_tool_names ,
8
+ deserialize_tools_or_toolset_inplace ,
9
+ serialize_tools_or_toolset ,
10
+ )
11
+ from haystack .tools .toolset import Toolset
6
12
from haystack .utils .callable_serialization import deserialize_callable , serialize_callable
7
13
from pydantic .json_schema import JsonSchemaValue
8
14
@@ -151,7 +157,7 @@ def __init__(
151
157
timeout : int = 120 ,
152
158
keep_alive : Optional [Union [float , str ]] = None ,
153
159
streaming_callback : Optional [Callable [[StreamingChunk ], None ]] = None ,
154
- tools : Optional [List [Tool ]] = None ,
160
+ tools : Optional [Union [ List [Tool ], Toolset ]] = None ,
155
161
response_format : Optional [Union [None , Literal ["json" ], JsonSchemaValue ]] = None ,
156
162
):
157
163
"""
@@ -177,7 +183,8 @@ def __init__(
177
183
A callback function that is called when a new token is received from the stream.
178
184
The callback function accepts StreamingChunk as an argument.
179
185
:param tools:
180
- A list of tools for which the model can prepare calls.
186
+ A list of tools or a Toolset for which the model can prepare calls.
187
+ This parameter can accept either a list of `Tool` objects or a `Toolset` instance.
181
188
Not all models support tools. For a list of models compatible with tools, see the
182
189
[models page](https://ollama.com/search?c=tools).
183
190
:param response_format:
@@ -207,7 +214,7 @@ def to_dict(self) -> Dict[str, Any]:
207
214
Dictionary with serialized data.
208
215
"""
209
216
callback_name = serialize_callable (self .streaming_callback ) if self .streaming_callback else None
210
- serialized_tools = [ tool . to_dict () for tool in self . tools ] if self . tools else None
217
+
211
218
return default_to_dict (
212
219
self ,
213
220
model = self .model ,
@@ -216,7 +223,7 @@ def to_dict(self) -> Dict[str, Any]:
216
223
generation_kwargs = self .generation_kwargs ,
217
224
timeout = self .timeout ,
218
225
streaming_callback = callback_name ,
219
- tools = serialized_tools ,
226
+ tools = serialize_tools_or_toolset ( self . tools ) ,
220
227
response_format = self .response_format ,
221
228
)
222
229
@@ -280,7 +287,7 @@ def run(
280
287
self ,
281
288
messages : List [ChatMessage ],
282
289
generation_kwargs : Optional [Dict [str , Any ]] = None ,
283
- tools : Optional [List [Tool ]] = None ,
290
+ tools : Optional [Union [ List [Tool ], Toolset ]] = None ,
284
291
* ,
285
292
streaming_callback : Optional [Callable [[StreamingChunk ], None ]] = None ,
286
293
):
@@ -294,7 +301,8 @@ def run(
294
301
top_p, etc. See the
295
302
[Ollama docs](https://github.com/jmorganca/ollama/blob/main/docs/modelfile.md#valid-parameters-and-values).
296
303
:param tools:
297
- A list of tools for which the model can prepare calls. If set, it will override the `tools` parameter set
304
+ A list of tools or a Toolset for which the model can prepare calls. This parameter can accept either a
305
+ list of `Tool` objects or a `Toolset` instance. If set, it will override the `tools` parameter set
298
306
during component initialization.
299
307
:param streaming_callback:
300
308
A callback function that is called when a new token is received from the stream.
@@ -320,6 +328,10 @@ def run(
320
328
msg = "Ollama does not support streaming and response_format at the same time. Please choose one."
321
329
raise ValueError (msg )
322
330
331
+ # Convert toolset to list of tools if needed
332
+ if isinstance (tools , Toolset ):
333
+ tools = list (tools )
334
+
323
335
ollama_tools = [{"type" : "function" , "function" : {** t .tool_spec }} for t in tools ] if tools else None
324
336
325
337
ollama_messages = [_convert_chatmessage_to_ollama_format (msg ) for msg in messages ]
0 commit comments