mirror of
https://github.com/deepset-ai/haystack.git
synced 2025-12-06 11:57:14 +00:00
chore: Rename deserialize_tools_inplace -> deserialize_tools_or_toolset_inplace (#9190)
* Rename deserialize_tools_inplace -> deserialize_tools_or_toolset_inplace * Add reno note
This commit is contained in:
parent
c539ffa4c3
commit
2665d048b8
@ -11,7 +11,7 @@ from haystack.components.tools import ToolInvoker
|
|||||||
from haystack.dataclasses import ChatMessage
|
from haystack.dataclasses import ChatMessage
|
||||||
from haystack.dataclasses.state import State, _schema_from_dict, _schema_to_dict, _validate_schema
|
from haystack.dataclasses.state import State, _schema_from_dict, _schema_to_dict, _validate_schema
|
||||||
from haystack.dataclasses.streaming_chunk import SyncStreamingCallbackT
|
from haystack.dataclasses.streaming_chunk import SyncStreamingCallbackT
|
||||||
from haystack.tools import Tool, deserialize_tools_inplace
|
from haystack.tools import Tool, deserialize_tools_or_toolset_inplace
|
||||||
from haystack.utils.callable_serialization import deserialize_callable, serialize_callable
|
from haystack.utils.callable_serialization import deserialize_callable, serialize_callable
|
||||||
from haystack.utils.deserialization import deserialize_chatgenerator_inplace
|
from haystack.utils.deserialization import deserialize_chatgenerator_inplace
|
||||||
|
|
||||||
@ -168,7 +168,7 @@ class Agent:
|
|||||||
if init_params.get("streaming_callback") is not None:
|
if init_params.get("streaming_callback") is not None:
|
||||||
init_params["streaming_callback"] = deserialize_callable(init_params["streaming_callback"])
|
init_params["streaming_callback"] = deserialize_callable(init_params["streaming_callback"])
|
||||||
|
|
||||||
deserialize_tools_inplace(init_params, key="tools")
|
deserialize_tools_or_toolset_inplace(init_params, key="tools")
|
||||||
|
|
||||||
return default_from_dict(cls, data)
|
return default_from_dict(cls, data)
|
||||||
|
|
||||||
|
|||||||
@ -14,7 +14,7 @@ from haystack.tools import (
|
|||||||
Tool,
|
Tool,
|
||||||
Toolset,
|
Toolset,
|
||||||
_check_duplicate_tool_names,
|
_check_duplicate_tool_names,
|
||||||
deserialize_tools_inplace,
|
deserialize_tools_or_toolset_inplace,
|
||||||
serialize_tools_or_toolset,
|
serialize_tools_or_toolset,
|
||||||
)
|
)
|
||||||
from haystack.utils import Secret, deserialize_callable, deserialize_secrets_inplace, serialize_callable
|
from haystack.utils import Secret, deserialize_callable, deserialize_secrets_inplace, serialize_callable
|
||||||
@ -218,7 +218,7 @@ class AzureOpenAIChatGenerator(OpenAIChatGenerator):
|
|||||||
The deserialized component instance.
|
The deserialized component instance.
|
||||||
"""
|
"""
|
||||||
deserialize_secrets_inplace(data["init_parameters"], keys=["api_key", "azure_ad_token"])
|
deserialize_secrets_inplace(data["init_parameters"], keys=["api_key", "azure_ad_token"])
|
||||||
deserialize_tools_inplace(data["init_parameters"], key="tools")
|
deserialize_tools_or_toolset_inplace(data["init_parameters"], key="tools")
|
||||||
init_params = data.get("init_parameters", {})
|
init_params = data.get("init_parameters", {})
|
||||||
serialized_callback_handler = init_params.get("streaming_callback")
|
serialized_callback_handler = init_params.get("streaming_callback")
|
||||||
if serialized_callback_handler:
|
if serialized_callback_handler:
|
||||||
|
|||||||
@ -13,7 +13,7 @@ from haystack.tools import (
|
|||||||
Tool,
|
Tool,
|
||||||
Toolset,
|
Toolset,
|
||||||
_check_duplicate_tool_names,
|
_check_duplicate_tool_names,
|
||||||
deserialize_tools_inplace,
|
deserialize_tools_or_toolset_inplace,
|
||||||
serialize_tools_or_toolset,
|
serialize_tools_or_toolset,
|
||||||
)
|
)
|
||||||
from haystack.utils import Secret, deserialize_callable, deserialize_secrets_inplace, serialize_callable
|
from haystack.utils import Secret, deserialize_callable, deserialize_secrets_inplace, serialize_callable
|
||||||
@ -213,7 +213,7 @@ class HuggingFaceAPIChatGenerator:
|
|||||||
Deserialize this component from a dictionary.
|
Deserialize this component from a dictionary.
|
||||||
"""
|
"""
|
||||||
deserialize_secrets_inplace(data["init_parameters"], keys=["token"])
|
deserialize_secrets_inplace(data["init_parameters"], keys=["token"])
|
||||||
deserialize_tools_inplace(data["init_parameters"], key="tools")
|
deserialize_tools_or_toolset_inplace(data["init_parameters"], key="tools")
|
||||||
init_params = data.get("init_parameters", {})
|
init_params = data.get("init_parameters", {})
|
||||||
serialized_callback_handler = init_params.get("streaming_callback")
|
serialized_callback_handler = init_params.get("streaming_callback")
|
||||||
if serialized_callback_handler:
|
if serialized_callback_handler:
|
||||||
|
|||||||
@ -16,7 +16,7 @@ from haystack.tools import (
|
|||||||
Tool,
|
Tool,
|
||||||
Toolset,
|
Toolset,
|
||||||
_check_duplicate_tool_names,
|
_check_duplicate_tool_names,
|
||||||
deserialize_tools_inplace,
|
deserialize_tools_or_toolset_inplace,
|
||||||
serialize_tools_or_toolset,
|
serialize_tools_or_toolset,
|
||||||
)
|
)
|
||||||
from haystack.utils import (
|
from haystack.utils import (
|
||||||
@ -309,7 +309,7 @@ class HuggingFaceLocalChatGenerator:
|
|||||||
"""
|
"""
|
||||||
torch_and_transformers_import.check() # leave this, cls method
|
torch_and_transformers_import.check() # leave this, cls method
|
||||||
deserialize_secrets_inplace(data["init_parameters"], keys=["token"])
|
deserialize_secrets_inplace(data["init_parameters"], keys=["token"])
|
||||||
deserialize_tools_inplace(data["init_parameters"], key="tools")
|
deserialize_tools_or_toolset_inplace(data["init_parameters"], key="tools")
|
||||||
init_params = data.get("init_parameters", {})
|
init_params = data.get("init_parameters", {})
|
||||||
serialized_callback_handler = init_params.get("streaming_callback")
|
serialized_callback_handler = init_params.get("streaming_callback")
|
||||||
if serialized_callback_handler:
|
if serialized_callback_handler:
|
||||||
|
|||||||
@ -26,7 +26,7 @@ from haystack.tools import (
|
|||||||
Tool,
|
Tool,
|
||||||
Toolset,
|
Toolset,
|
||||||
_check_duplicate_tool_names,
|
_check_duplicate_tool_names,
|
||||||
deserialize_tools_inplace,
|
deserialize_tools_or_toolset_inplace,
|
||||||
serialize_tools_or_toolset,
|
serialize_tools_or_toolset,
|
||||||
)
|
)
|
||||||
from haystack.utils import Secret, deserialize_callable, deserialize_secrets_inplace, serialize_callable
|
from haystack.utils import Secret, deserialize_callable, deserialize_secrets_inplace, serialize_callable
|
||||||
@ -207,7 +207,7 @@ class OpenAIChatGenerator:
|
|||||||
The deserialized component instance.
|
The deserialized component instance.
|
||||||
"""
|
"""
|
||||||
deserialize_secrets_inplace(data["init_parameters"], keys=["api_key"])
|
deserialize_secrets_inplace(data["init_parameters"], keys=["api_key"])
|
||||||
deserialize_tools_inplace(data["init_parameters"], key="tools")
|
deserialize_tools_or_toolset_inplace(data["init_parameters"], key="tools")
|
||||||
init_params = data.get("init_parameters", {})
|
init_params = data.get("init_parameters", {})
|
||||||
serialized_callback_handler = init_params.get("streaming_callback")
|
serialized_callback_handler = init_params.get("streaming_callback")
|
||||||
if serialized_callback_handler:
|
if serialized_callback_handler:
|
||||||
|
|||||||
@ -14,7 +14,7 @@ from haystack.tools import (
|
|||||||
Tool,
|
Tool,
|
||||||
Toolset,
|
Toolset,
|
||||||
_check_duplicate_tool_names,
|
_check_duplicate_tool_names,
|
||||||
deserialize_tools_inplace,
|
deserialize_tools_or_toolset_inplace,
|
||||||
serialize_tools_or_toolset,
|
serialize_tools_or_toolset,
|
||||||
)
|
)
|
||||||
from haystack.tools.errors import ToolInvocationError
|
from haystack.tools.errors import ToolInvocationError
|
||||||
@ -458,5 +458,5 @@ class ToolInvoker:
|
|||||||
:returns:
|
:returns:
|
||||||
The deserialized component.
|
The deserialized component.
|
||||||
"""
|
"""
|
||||||
deserialize_tools_inplace(data["init_parameters"], key="tools")
|
deserialize_tools_or_toolset_inplace(data["init_parameters"], key="tools")
|
||||||
return default_from_dict(cls, data)
|
return default_from_dict(cls, data)
|
||||||
|
|||||||
@ -8,14 +8,14 @@
|
|||||||
from .from_function import create_tool_from_function, tool
|
from .from_function import create_tool_from_function, tool
|
||||||
from .tool import Tool, _check_duplicate_tool_names
|
from .tool import Tool, _check_duplicate_tool_names
|
||||||
from .component_tool import ComponentTool
|
from .component_tool import ComponentTool
|
||||||
from .serde_utils import deserialize_tools_inplace, serialize_tools_or_toolset
|
from .serde_utils import deserialize_tools_or_toolset_inplace, serialize_tools_or_toolset
|
||||||
from .toolset import Toolset
|
from .toolset import Toolset
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"_check_duplicate_tool_names",
|
"_check_duplicate_tool_names",
|
||||||
"ComponentTool",
|
"ComponentTool",
|
||||||
"create_tool_from_function",
|
"create_tool_from_function",
|
||||||
"deserialize_tools_inplace",
|
"deserialize_tools_or_toolset_inplace",
|
||||||
"serialize_tools_or_toolset",
|
"serialize_tools_or_toolset",
|
||||||
"Tool",
|
"Tool",
|
||||||
"Toolset",
|
"Toolset",
|
||||||
|
|||||||
@ -26,7 +26,7 @@ def serialize_tools_or_toolset(
|
|||||||
return [tool.to_dict() for tool in tools]
|
return [tool.to_dict() for tool in tools]
|
||||||
|
|
||||||
|
|
||||||
def deserialize_tools_inplace(data: Dict[str, Any], key: str = "tools"):
|
def deserialize_tools_or_toolset_inplace(data: Dict[str, Any], key: str = "tools"):
|
||||||
"""
|
"""
|
||||||
Deserialize a list of Tools or a Toolset in a dictionary inplace.
|
Deserialize a list of Tools or a Toolset in a dictionary inplace.
|
||||||
|
|
||||||
|
|||||||
@ -0,0 +1,4 @@
|
|||||||
|
---
|
||||||
|
upgrade:
|
||||||
|
- |
|
||||||
|
Utility function `deserialize_tools_inplace` has been renamed to `deserialize_tools_or_toolset_inplace` to reflect that it can deserialize both `Toolset` and `Tool` objects.
|
||||||
@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from haystack.tools import Tool, deserialize_tools_inplace, Toolset, serialize_tools_or_toolset
|
from haystack.tools import Tool, deserialize_tools_or_toolset_inplace, Toolset, serialize_tools_or_toolset
|
||||||
|
|
||||||
|
|
||||||
def get_weather_report(city: str) -> str:
|
def get_weather_report(city: str) -> str:
|
||||||
@ -41,38 +41,38 @@ class TestToolSerdeUtils:
|
|||||||
)
|
)
|
||||||
|
|
||||||
data = {"tools": [tool.to_dict()]}
|
data = {"tools": [tool.to_dict()]}
|
||||||
deserialize_tools_inplace(data)
|
deserialize_tools_or_toolset_inplace(data)
|
||||||
assert data["tools"] == [tool]
|
assert data["tools"] == [tool]
|
||||||
|
|
||||||
data = {"mytools": [tool.to_dict()]}
|
data = {"mytools": [tool.to_dict()]}
|
||||||
deserialize_tools_inplace(data, key="mytools")
|
deserialize_tools_or_toolset_inplace(data, key="mytools")
|
||||||
assert data["mytools"] == [tool]
|
assert data["mytools"] == [tool]
|
||||||
|
|
||||||
data = {"no_tools": 123}
|
data = {"no_tools": 123}
|
||||||
deserialize_tools_inplace(data)
|
deserialize_tools_or_toolset_inplace(data)
|
||||||
assert data == {"no_tools": 123}
|
assert data == {"no_tools": 123}
|
||||||
|
|
||||||
def test_deserialize_tools_inplace_failures(self):
|
def test_deserialize_tools_inplace_failures(self):
|
||||||
data = {"key": "value"}
|
data = {"key": "value"}
|
||||||
deserialize_tools_inplace(data)
|
deserialize_tools_or_toolset_inplace(data)
|
||||||
assert data == {"key": "value"}
|
assert data == {"key": "value"}
|
||||||
|
|
||||||
data = {"tools": None}
|
data = {"tools": None}
|
||||||
deserialize_tools_inplace(data)
|
deserialize_tools_or_toolset_inplace(data)
|
||||||
assert data == {"tools": None}
|
assert data == {"tools": None}
|
||||||
|
|
||||||
data = {"tools": "not a list"}
|
data = {"tools": "not a list"}
|
||||||
with pytest.raises(TypeError):
|
with pytest.raises(TypeError):
|
||||||
deserialize_tools_inplace(data)
|
deserialize_tools_or_toolset_inplace(data)
|
||||||
|
|
||||||
data = {"tools": ["not a dictionary"]}
|
data = {"tools": ["not a dictionary"]}
|
||||||
with pytest.raises(TypeError):
|
with pytest.raises(TypeError):
|
||||||
deserialize_tools_inplace(data)
|
deserialize_tools_or_toolset_inplace(data)
|
||||||
|
|
||||||
# not a subclass of Tool
|
# not a subclass of Tool
|
||||||
data = {"tools": [{"type": "haystack.dataclasses.ChatMessage", "data": {"irrelevant": "irrelevant"}}]}
|
data = {"tools": [{"type": "haystack.dataclasses.ChatMessage", "data": {"irrelevant": "irrelevant"}}]}
|
||||||
with pytest.raises(TypeError):
|
with pytest.raises(TypeError):
|
||||||
deserialize_tools_inplace(data)
|
deserialize_tools_or_toolset_inplace(data)
|
||||||
|
|
||||||
def test_deserialize_toolset_inplace(self):
|
def test_deserialize_toolset_inplace(self):
|
||||||
tool = Tool(
|
tool = Tool(
|
||||||
@ -82,7 +82,7 @@ class TestToolSerdeUtils:
|
|||||||
|
|
||||||
data = {"tools": toolset.to_dict()}
|
data = {"tools": toolset.to_dict()}
|
||||||
|
|
||||||
deserialize_tools_inplace(data)
|
deserialize_tools_or_toolset_inplace(data)
|
||||||
|
|
||||||
assert data["tools"] == toolset
|
assert data["tools"] == toolset
|
||||||
assert isinstance(data["tools"], Toolset)
|
assert isinstance(data["tools"], Toolset)
|
||||||
@ -91,8 +91,8 @@ class TestToolSerdeUtils:
|
|||||||
def test_deserialize_toolset_inplace_failures(self):
|
def test_deserialize_toolset_inplace_failures(self):
|
||||||
data = {"tools": {"key": "value"}}
|
data = {"tools": {"key": "value"}}
|
||||||
with pytest.raises(TypeError):
|
with pytest.raises(TypeError):
|
||||||
deserialize_tools_inplace(data)
|
deserialize_tools_or_toolset_inplace(data)
|
||||||
|
|
||||||
data = {"tools": {"type": "haystack.tools.Tool", "data": "some_data"}}
|
data = {"tools": {"type": "haystack.tools.Tool", "data": "some_data"}}
|
||||||
with pytest.raises(TypeError):
|
with pytest.raises(TypeError):
|
||||||
deserialize_tools_inplace(data)
|
deserialize_tools_or_toolset_inplace(data)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user