跳到内容

rg.Workspace

在 Argilla 中,工作区用于将数据集组织成组。例如,您可以为每个项目或团队设置一个工作区。

使用示例

要创建新的工作区,请使用客户端和名称实例化 Workspace 对象

workspace = rg.Workspace(name="my_workspace")
workspace.create()

要检索现有工作区,请使用 client.workspaces 属性

workspace = client.workspaces("my_workspace")

Workspace

基类: Resource

用于与 Argilla 工作区交互的类。工作区用于在 Argilla 服务器中组织数据集。

属性

名称 类型 描述
name str

工作区的名称。

id UUID

工作区的 ID。这是服务器中工作区的唯一标识符。

datasets List[Dataset]

工作区中所有数据集的列表。

users WorkspaceUsers

工作区中所有用户的列表。

源代码位于 src/argilla/workspaces/_resource.py
class Workspace(Resource):
    """Class for interacting with Argilla workspaces. Workspaces are used to organize datasets in the Argilla server.

    Attributes:
        name (str): The name of the workspace.
        id (UUID): The ID of the workspace. This is a unique identifier for the workspace in the server.
        datasets (List[Dataset]): A list of all datasets in the workspace.
        users (WorkspaceUsers): A list of all users in the workspace.
    """

    name: Optional[str]

    _api: "WorkspacesAPI"

    def __init__(
        self,
        name: Optional[str] = None,
        id: Optional[UUID] = None,
        client: Optional["Argilla"] = None,
    ) -> None:
        """Initializes a Workspace object with a client and a name or id

        Parameters:
            name (str): The name of the workspace
            id (UUID): The id of the workspace. If provided before a .create, the workspace will be created with this ID
            client (Argilla): The client used to interact with Argilla

        Returns:
            Workspace: The initialized workspace object
        """
        client = client or Argilla._get_default()
        super().__init__(client=client, api=client.api.workspaces)

        self._model = WorkspaceModel(name=name, id=id)

    def add_user(self, user: Union["User", str]) -> "User":
        """Adds a user to the workspace. After adding a user to the workspace, it will have access to the datasets
        in the workspace.

        Args:
            user (Union[User, str]): The user to add to the workspace. Can be a User object or a username.

        Returns:
            User: The user that was added to the workspace
        """
        return self.users.add(user)

    def remove_user(self, user: Union["User", str]) -> "User":
        """Removes a user from the workspace. After removing a user from the workspace, it will no longer have access

        Args:
            user (Union[User, str]): The user to remove from the workspace. Can be a User object or a username.

        Returns:
            User: The user that was removed from the workspace.
        """
        return self.users.delete(user)

    # TODO: Make this method private
    def list_datasets(self) -> List["Dataset"]:
        from argilla.datasets import Dataset

        datasets = self._client.api.datasets.list(self.id)
        self._log_message(f"Got {len(datasets)} datasets for workspace {self.id}")
        return [Dataset.from_model(model=dataset, client=self._client) for dataset in datasets]

    @classmethod
    def from_model(cls, model: WorkspaceModel, client: Argilla) -> "Workspace":
        instance = cls(name=model.name, id=model.id, client=client)
        instance._model = model

        return instance

    ############################
    # Properties
    ############################

    @property
    def name(self) -> Optional[str]:
        return self._model.name

    @name.setter
    def name(self, value: str) -> None:
        self._model.name = value

    @property
    def datasets(self) -> List["Dataset"]:
        """List all datasets in the workspace

        Returns:
            List[Dataset]: A list of all datasets in the workspace
        """
        return self.list_datasets()

    @property
    def users(self) -> "WorkspaceUsers":
        """List all users in the workspace

        Returns:
            WorkspaceUsers: A list of all users in the workspace
        """
        return WorkspaceUsers(workspace=self)
datasets: List[Dataset] property

列出工作区中的所有数据集

返回

类型 描述
List[Dataset]

List[Dataset]:工作区中所有数据集的列表

users: WorkspaceUsers property

列出工作区中的所有用户

返回

名称 类型 描述
WorkspaceUsers WorkspaceUsers

工作区中所有用户的列表

__init__(name=None, id=None, client=None)

使用客户端和名称或 ID 初始化 Workspace 对象

参数

名称 类型 描述 默认
name str

工作区的名称

None
id UUID

工作区的 ID。如果在 .create 之前提供,则将使用此 ID 创建工作区

None
client Argilla

用于与 Argilla 交互的客户端

None

返回

名称 类型 描述
Workspace None

初始化的工作区对象

源代码位于 src/argilla/workspaces/_resource.py
def __init__(
    self,
    name: Optional[str] = None,
    id: Optional[UUID] = None,
    client: Optional["Argilla"] = None,
) -> None:
    """Initializes a Workspace object with a client and a name or id

    Parameters:
        name (str): The name of the workspace
        id (UUID): The id of the workspace. If provided before a .create, the workspace will be created with this ID
        client (Argilla): The client used to interact with Argilla

    Returns:
        Workspace: The initialized workspace object
    """
    client = client or Argilla._get_default()
    super().__init__(client=client, api=client.api.workspaces)

    self._model = WorkspaceModel(name=name, id=id)
add_user(user)

将用户添加到工作区。将用户添加到工作区后,该用户将有权访问工作区中的数据集。

参数

名称 类型 描述 默认
user Union[User, str]

要添加到工作区的用户。可以是 User 对象或用户名。

required

返回

名称 类型 描述
User User

已添加到工作区的用户

源代码位于 src/argilla/workspaces/_resource.py
def add_user(self, user: Union["User", str]) -> "User":
    """Adds a user to the workspace. After adding a user to the workspace, it will have access to the datasets
    in the workspace.

    Args:
        user (Union[User, str]): The user to add to the workspace. Can be a User object or a username.

    Returns:
        User: The user that was added to the workspace
    """
    return self.users.add(user)
remove_user(user)

从工作区中删除用户。从工作区中删除用户后,该用户将不再拥有访问权限

参数

名称 类型 描述 默认
user Union[User, str]

要从工作区中删除的用户。可以是 User 对象或用户名。

required

返回

名称 类型 描述
User User

已从工作区中删除的用户。

源代码位于 src/argilla/workspaces/_resource.py
def remove_user(self, user: Union["User", str]) -> "User":
    """Removes a user from the workspace. After removing a user from the workspace, it will no longer have access

    Args:
        user (Union[User, str]): The user to remove from the workspace. Can be a User object or a username.

    Returns:
        User: The user that was removed from the workspace.
    """
    return self.users.delete(user)