22
33from haystack import component , default_from_dict , default_to_dict
44from 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
612from haystack .utils .callable_serialization import deserialize_callable , serialize_callable
713from pydantic .json_schema import JsonSchemaValue
814
@@ -151,7 +157,7 @@ def __init__(
151157 timeout : int = 120 ,
152158 keep_alive : Optional [Union [float , str ]] = None ,
153159 streaming_callback : Optional [Callable [[StreamingChunk ], None ]] = None ,
154- tools : Optional [List [Tool ]] = None ,
160+ tools : Optional [Union [ List [Tool ], Toolset ]] = None ,
155161 response_format : Optional [Union [None , Literal ["json" ], JsonSchemaValue ]] = None ,
156162 ):
157163 """
@@ -177,7 +183,8 @@ def __init__(
177183 A callback function that is called when a new token is received from the stream.
178184 The callback function accepts StreamingChunk as an argument.
179185 :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.
181188 Not all models support tools. For a list of models compatible with tools, see the
182189 [models page](https://ollama.com/search?c=tools).
183190 :param response_format:
@@ -207,7 +214,7 @@ def to_dict(self) -> Dict[str, Any]:
207214 Dictionary with serialized data.
208215 """
209216 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+
211218 return default_to_dict (
212219 self ,
213220 model = self .model ,
@@ -216,7 +223,7 @@ def to_dict(self) -> Dict[str, Any]:
216223 generation_kwargs = self .generation_kwargs ,
217224 timeout = self .timeout ,
218225 streaming_callback = callback_name ,
219- tools = serialized_tools ,
226+ tools = serialize_tools_or_toolset ( self . tools ) ,
220227 response_format = self .response_format ,
221228 )
222229
@@ -280,7 +287,7 @@ def run(
280287 self ,
281288 messages : List [ChatMessage ],
282289 generation_kwargs : Optional [Dict [str , Any ]] = None ,
283- tools : Optional [List [Tool ]] = None ,
290+ tools : Optional [Union [ List [Tool ], Toolset ]] = None ,
284291 * ,
285292 streaming_callback : Optional [Callable [[StreamingChunk ], None ]] = None ,
286293 ):
@@ -294,7 +301,8 @@ def run(
294301 top_p, etc. See the
295302 [Ollama docs](https://github.com/jmorganca/ollama/blob/main/docs/modelfile.md#valid-parameters-and-values).
296303 :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
298306 during component initialization.
299307 :param streaming_callback:
300308 A callback function that is called when a new token is received from the stream.
@@ -320,6 +328,10 @@ def run(
320328 msg = "Ollama does not support streaming and response_format at the same time. Please choose one."
321329 raise ValueError (msg )
322330
331+ # Convert toolset to list of tools if needed
332+ if isinstance (tools , Toolset ):
333+ tools = list (tools )
334+
323335 ollama_tools = [{"type" : "function" , "function" : {** t .tool_spec }} for t in tools ] if tools else None
324336
325337 ollama_messages = [_convert_chatmessage_to_ollama_format (msg ) for msg in messages ]
0 commit comments