Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/docstrings dataclasses #7221

Merged
merged 6 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions haystack/dataclasses/byte_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def to_string(self, encoding: str = "utf-8") -> str:
Convert the ByteStream to a string, metadata will not be included.

:param encoding: The encoding used to convert the bytes to a string. Defaults to "utf-8".
:return: The string representation of the ByteStream.
:raises UnicodeDecodeError: If the ByteStream data cannot be decoded with the specified encoding.
:returns: The string representation of the ByteStream.
:raises: UnicodeDecodeError: If the ByteStream data cannot be decoded with the specified encoding.
davidsbatista marked this conversation as resolved.
Show resolved Hide resolved
"""
return self.data.decode(encoding)
10 changes: 5 additions & 5 deletions haystack/dataclasses/chat_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def is_from(self, role: ChatRole) -> bool:
Check if the message is from a specific role.

:param role: The role to check against.
:return: True if the message is from the specified role, False otherwise.
:returns: True if the message is from the specified role, False otherwise.
"""
return self.role == role

Expand All @@ -44,7 +44,7 @@ def from_assistant(cls, content: str, meta: Optional[Dict[str, Any]] = None) ->

:param content: The text content of the message.
:param meta: Additional metadata associated with the message.
:return: A new ChatMessage instance.
:returns: A new ChatMessage instance.
"""
return cls(content, ChatRole.ASSISTANT, None, meta or {})

Expand All @@ -54,7 +54,7 @@ def from_user(cls, content: str) -> "ChatMessage":
Create a message from the user.

:param content: The text content of the message.
:return: A new ChatMessage instance.
:returns: A new ChatMessage instance.
"""
return cls(content, ChatRole.USER, None)

Expand All @@ -64,7 +64,7 @@ def from_system(cls, content: str) -> "ChatMessage":
Create a message from the system.

:param content: The text content of the message.
:return: A new ChatMessage instance.
:returns: A new ChatMessage instance.
"""
return cls(content, ChatRole.SYSTEM, None)

Expand All @@ -75,6 +75,6 @@ def from_function(cls, content: str, name: str) -> "ChatMessage":

:param content: The text content of the message.
:param name: The name of the function being called.
:return: A new ChatMessage instance.
:returns: A new ChatMessage instance.
"""
return cls(content, ChatRole.FUNCTION, name)
21 changes: 14 additions & 7 deletions haystack/dataclasses/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ def __call__(cls, *args, **kwargs):
class Document(metaclass=_BackwardCompatible):
"""
Base data class containing some data to be queried.
Can contain text snippets, tables, and file paths to images or audios.
Documents can be sorted by score and saved to/from dictionary and JSON.

Can contain text snippets, tables, and file paths to images or audios. Documents can be sorted by score and saved
to/from dictionary and JSON.

:param id: Unique identifier for the document. When not set, it's generated based on the Document fields' values.
:param content: Text of the document, if the document contains text.
Expand Down Expand Up @@ -89,6 +90,7 @@ def __repr__(self):
def __eq__(self, other):
"""
Compares Documents for equality.

Two Documents are considered equals if their dictionary representation is identical.
"""
if type(self) != type(other):
Expand Down Expand Up @@ -118,9 +120,11 @@ def _create_id(self):
def to_dict(self, flatten=True) -> Dict[str, Any]:
"""
Converts Document into a dictionary.
`dataframe` and `blob` fields are converted to JSON-serializable types.

:param flatten: Whether to flatten `meta` field or not. Defaults to `True` to be backward-compatible with Haystack 1.x.
NOTE: `dataframe` and `blob` fields are converted to JSON-serializable types.
davidsbatista marked this conversation as resolved.
Show resolved Hide resolved

:param flatten:
Whether to flatten `meta` field or not. Defaults to `True` to be backward-compatible with Haystack 1.x.
"""
data = asdict(self)
if (dataframe := data.get("dataframe")) is not None:
Expand All @@ -138,7 +142,8 @@ def to_dict(self, flatten=True) -> Dict[str, Any]:
def from_dict(cls, data: Dict[str, Any]) -> "Document":
"""
Creates a new Document object from a dictionary.
`dataframe` and `blob` fields are converted to their original types.

NOTE: `dataframe` and `blob` fields are converted to their original types.
"""
if (dataframe := data.get("dataframe")) is not None:
data["dataframe"] = read_json(io.StringIO(dataframe))
Expand Down Expand Up @@ -172,9 +177,11 @@ def from_dict(cls, data: Dict[str, Any]) -> "Document":
def content_type(self):
"""
Returns the type of the content for the document.

This is necessary to keep backward compatibility with 1.x.
A ValueError will be raised if both `text` and `dataframe` fields are set
or both are missing.

:raises:
ValueError will be raised if both `text` and `dataframe` fields are set or both are missing.
davidsbatista marked this conversation as resolved.
Show resolved Hide resolved
"""
if self.content is not None and self.dataframe is not None:
raise ValueError("Both text and dataframe are set.")
Expand Down
6 changes: 3 additions & 3 deletions haystack/dataclasses/streaming_chunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
@dataclass
class StreamingChunk:
"""
The StreamingChunk class encapsulates a segment of streamed content along with
associated metadata. This structure facilitates the handling and processing of
streamed data in a systematic manner.
The StreamingChunk class encapsulates a segment of streamed content along with associated metadata.

This structure facilitates the handling and processing of streamed data in a systematic manner.

:param content: The content of the message chunk as a string.
:param meta: A dictionary containing metadata related to the message chunk.
Expand Down