Skip to content

Attachment

Defines the attachment schema.

Authors

Henry Wicaksono (henry.wicaksono@gdplabs.id)

References

NONE

Attachment

Bases: BaseModel

Defines a file attachment schema.

Attributes:

Name Type Description
data bytes

The content data of the file attachment.

filename str

The filename of the file attachment.

mime_type str

The mime type of the file attachment.

extension str

The extension of the file attachment.

url str | None

The URL of the file attachment. Defaults to None.

metadata dict[str, Any]

The metadata of the file attachment. Defaults to an empty dictionary.

__repr__()

Return string representation of the Attachment.

Returns:

Name Type Description
str str

The string representation of the Attachment.

__str__()

Return string representation of the Attachment.

Returns:

Name Type Description
str str

The string representation of the Attachment.

from_base64(base64_data, filename=None, metadata=None) classmethod

Creates an Attachment from a base64 string.

Parameters:

Name Type Description Default
base64_data str

The base64 string of the file.

required
filename str | None

The filename of the file. Defaults to None, in which case the filename will be derived from the mime type.

None
metadata dict[str, Any] | None

The metadata of the file attachment. Defaults to None, in which case an empty dictionary will be used.

None

Returns:

Name Type Description
Attachment Attachment

The instantiated Attachment.

from_bytes(bytes, filename=None, metadata=None) classmethod

Creates an Attachment from bytes.

Parameters:

Name Type Description Default
bytes bytes

The bytes of the file.

required
filename str | None

The filename of the file. Defaults to None, in which case the filename will be derived from the extension.

None
metadata dict[str, Any] | None

The metadata of the file attachment. Defaults to None, in which case an empty dictionary will be used.

None

Returns:

Name Type Description
Attachment Attachment

The instantiated Attachment.

from_data_url(data_url, filename=None, metadata=None) classmethod

Creates an Attachment from a data URL (data:[mime/type];base64,[bytes]).

Parameters:

Name Type Description Default
data_url str

The data URL of the file.

required
filename str | None

The filename of the file. Defaults to None, in which case the filename will be derived from the mime type.

None
metadata dict[str, Any] | None

The metadata of the file attachment. Defaults to None, in which case an empty dictionary will be used.

None

Returns:

Name Type Description
Attachment Attachment

The instantiated Attachment.

from_path(path, filename=None, metadata=None) classmethod

Creates an Attachment from a path.

Parameters:

Name Type Description Default
path str

The path to the file.

required
filename str | None

The filename of the file. Defaults to None, in which case the filename will be derived from the path.

None
metadata dict[str, Any] | None

The metadata of the file attachment. Defaults to None, in which case an empty dictionary will be used.

None

Returns:

Name Type Description
Attachment Attachment

The instantiated Attachment.

from_url(url, filename=None, metadata=None) classmethod

Creates an Attachment from a URL.

Parameters:

Name Type Description Default
url str

The URL of the file.

required
filename str | None

The filename of the file. Defaults to None, in which case the filename will be derived from the URL.

None
metadata dict[str, Any] | None

The metadata of the file attachment. Defaults to None, in which case an empty dictionary will be used.

None

Returns:

Name Type Description
Attachment Attachment

The instantiated Attachment.

write_to_file(path=None)

Writes the Attachment to a file.

Parameters:

Name Type Description Default
path str | None

The path to the file. Defaults to None, in which case the filename will be used as the path.

None

UploadedAttachment(id, provider, url=None, metadata=None, **kwargs)

Bases: Attachment

Defines an uploaded file attachment schema.

This class is used to represent a file attachment that has been uploaded to a certain LM provider's files management capabilities. It is recognized through the id attribute, which content depends on the nature of the LM provider's files management capabilities. It could be an ID, a URL, etc.

Attributes:

Name Type Description
id str

The identifier of the uploaded file.

provider str

The provider that stores and manages the uploaded file.

data bytes

The content data of the file attachment. Defaults to an empty bytes object.

filename str

The filename of the file attachment.

mime_type str

The mime type of the file attachment.

extension str

The extension of the file attachment.

url str | None

The URL of the file attachment. Defaults to None.

metadata dict[str, Any]

The metadata of the file attachment. Defaults to an empty dictionary.

Initializes an UploadedAttachment.

Parameters:

Name Type Description Default
id str

The ID of the uploaded file attachment.

required
provider str

The provider that stores and manages the uploaded file.

required
url str | None

The URL of the file attachment. Defaults to None.

None
metadata dict[str, Any] | None

The metadata of the file attachment. Defaults to None, in which case an empty dictionary will be used.

None
**kwargs Any

Additional keyword arguments passed to the parent class.

{}

__repr__()

Return string representation of the UploadedAttachment.

Returns:

Name Type Description
str str

The string representation of the UploadedAttachment.

__str__()

Return string representation of the UploadedAttachment.

Returns:

Name Type Description
str str

The string representation of the UploadedAttachment.