跳到内容

rg.User

在 Argilla 中,用户是使用 SDK 或 UI 的配置文件。他们的配置文件可以用来跟踪他们的反馈活动,并管理他们对 Argilla 服务器的访问权限。

使用示例

要创建新用户,请使用客户端和用户名实例化 User 对象

user = rg.User(username="my_username", password="my_password")
user.create()

现有用户可以通过其用户名检索

user = client.users("my_username")

可以使用 me 属性访问 rg.Argilla 客户端的当前用户

client.me

User

基类: Resource

用于与 Argilla 服务器中的 Argilla 用户交互的类。用户配置文件用于管理对 Argilla 服务器的访问并跟踪对记录的响应。

属性

名称 类型 描述
username str

用户的用户名。

first_name str

用户的名字。

last_name str

用户的姓氏。

role str

用户的角色,可以是 'annotator' 或 'admin'。

password str

用户的密码。

id UUID

用户的 ID。

源代码位于 src/argilla/users/_resource.py
class User(Resource):
    """Class for interacting with Argilla users in the Argilla server. User profiles \
        are used to manage access to the Argilla server and track responses to records.

    Attributes:
        username (str): The username of the user.
        first_name (str): The first name of the user.
        last_name (str): The last name of the user.
        role (str): The role of the user, either 'annotator' or 'admin'.
        password (str): The password of the user.
        id (UUID): The ID of the user.
    """

    _model: UserModel
    _api: UsersAPI

    def __init__(
        self,
        username: Optional[str] = None,
        first_name: Optional[str] = None,
        last_name: Optional[str] = None,
        role: Optional[str] = None,
        password: Optional[str] = None,
        id: Optional[UUID] = None,
        client: Optional["Argilla"] = None,
        _model: Optional[UserModel] = None,
    ) -> None:
        """Initializes a User object with a client and a username

        Parameters:
            username (str): The username of the user
            first_name (str): The first name of the user
            last_name (str): The last name of the user
            role (str): The role of the user, either 'annotator', admin, or 'owner'
            password (str): The password of the user
            id (UUID): The ID of the user. If provided before a .create, the will be created with this ID
            client (Argilla): The client used to interact with Argilla

        Returns:
            User: The initialized user object
        """
        client = client or Argilla._get_default()
        super().__init__(client=client, api=client.api.users)

        if _model is None:
            _model = UserModel(
                username=username,
                password=password,
                first_name=first_name or username,
                last_name=last_name,
                role=role or Role.annotator,
                id=id,
            )
            self._log_message(f"Initialized user with username {username}")
        self._model = _model

    def create(self) -> "User":
        """Creates the user in Argilla. After creating a user, it will be able to log in to the Argilla server.

        Returns:
            User: The user that was created in Argilla.
        """
        model_create = self.api_model()
        model = self._api.create(model_create)
        # The password is not returned in the response
        model.password = model_create.password
        self._model = model
        return self

    def delete(self) -> None:
        """Deletes the user from Argilla. After deleting a user, it will no longer be able to log in to the Argilla server."""
        super().delete()
        # exists relies on the id, so we need to set it to None
        self._model = UserModel(username=self.username)

    def add_to_workspace(self, workspace: "Workspace") -> "User":
        """Adds the user to a workspace. After adding a user to a workspace, it will have access to the datasets
        in the workspace.

        Args:
            workspace (Workspace): The workspace to add the user to.

        Returns:
            User: The user that was added to the workspace.
        """
        self._model = self._api.add_to_workspace(workspace.id, self.id)
        return self

    def remove_from_workspace(self, workspace: "Workspace") -> "User":
        """Removes the user from a workspace. After removing a user from a workspace, it will no longer have access to
        the datasets in the workspace.

        Args:
            workspace (Workspace): The workspace to remove the user from.

        Returns:
            User: The user that was removed from the workspace.

        """
        self._model = self._api.delete_from_workspace(workspace.id, self.id)
        return self

    ############################
    # Properties
    ############################
    @property
    def username(self) -> str:
        return self._model.username

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

    @property
    def password(self) -> str:
        return self._model.password

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

    @property
    def first_name(self) -> str:
        return self._model.first_name

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

    @property
    def last_name(self) -> str:
        return self._model.last_name

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

    @property
    def role(self) -> Role:
        return self._model.role

    @role.setter
    def role(self, value: Role) -> None:
        self._model.role = value

__init__(username=None, first_name=None, last_name=None, role=None, password=None, id=None, client=None, _model=None)

使用客户端和用户名初始化 User 对象

参数

名称 类型 描述 默认值
username str

用户的用户名

None
first_name str

用户的名字

None
last_name str

用户的姓氏

None
role str

用户的角色,可以是 'annotator'、admin 或 'owner'

None
password str

用户的密码

None
id UUID

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

None
client Argilla

用于与 Argilla 交互的客户端

None

返回

名称 类型 描述
User None

初始化后的用户对象

源代码位于 src/argilla/users/_resource.py
def __init__(
    self,
    username: Optional[str] = None,
    first_name: Optional[str] = None,
    last_name: Optional[str] = None,
    role: Optional[str] = None,
    password: Optional[str] = None,
    id: Optional[UUID] = None,
    client: Optional["Argilla"] = None,
    _model: Optional[UserModel] = None,
) -> None:
    """Initializes a User object with a client and a username

    Parameters:
        username (str): The username of the user
        first_name (str): The first name of the user
        last_name (str): The last name of the user
        role (str): The role of the user, either 'annotator', admin, or 'owner'
        password (str): The password of the user
        id (UUID): The ID of the user. If provided before a .create, the will be created with this ID
        client (Argilla): The client used to interact with Argilla

    Returns:
        User: The initialized user object
    """
    client = client or Argilla._get_default()
    super().__init__(client=client, api=client.api.users)

    if _model is None:
        _model = UserModel(
            username=username,
            password=password,
            first_name=first_name or username,
            last_name=last_name,
            role=role or Role.annotator,
            id=id,
        )
        self._log_message(f"Initialized user with username {username}")
    self._model = _model

create()

在 Argilla 中创建用户。创建用户后,它将能够登录到 Argilla 服务器。

返回

名称 类型 描述
User User

在 Argilla 中创建的用户。

源代码位于 src/argilla/users/_resource.py
def create(self) -> "User":
    """Creates the user in Argilla. After creating a user, it will be able to log in to the Argilla server.

    Returns:
        User: The user that was created in Argilla.
    """
    model_create = self.api_model()
    model = self._api.create(model_create)
    # The password is not returned in the response
    model.password = model_create.password
    self._model = model
    return self

delete()

从 Argilla 中删除用户。删除用户后,它将无法再登录到 Argilla 服务器。

源代码位于 src/argilla/users/_resource.py
def delete(self) -> None:
    """Deletes the user from Argilla. After deleting a user, it will no longer be able to log in to the Argilla server."""
    super().delete()
    # exists relies on the id, so we need to set it to None
    self._model = UserModel(username=self.username)

add_to_workspace(workspace)

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

参数

名称 类型 描述 默认值
workspace Workspace

要将用户添加到的工作区。

required

返回

名称 类型 描述
User User

已添加到工作区的用户。

源代码位于 src/argilla/users/_resource.py
def add_to_workspace(self, workspace: "Workspace") -> "User":
    """Adds the user to a workspace. After adding a user to a workspace, it will have access to the datasets
    in the workspace.

    Args:
        workspace (Workspace): The workspace to add the user to.

    Returns:
        User: The user that was added to the workspace.
    """
    self._model = self._api.add_to_workspace(workspace.id, self.id)
    return self

remove_from_workspace(workspace)

从工作区中移除用户。从工作区中移除用户后,它将不再有权访问工作区中的数据集。

参数

名称 类型 描述 默认值
workspace Workspace

要从中移除用户的工作区。

required

返回

名称 类型 描述
User User

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

源代码位于 src/argilla/users/_resource.py
def remove_from_workspace(self, workspace: "Workspace") -> "User":
    """Removes the user from a workspace. After removing a user from a workspace, it will no longer have access to
    the datasets in the workspace.

    Args:
        workspace (Workspace): The workspace to remove the user from.

    Returns:
        User: The user that was removed from the workspace.

    """
    self._model = self._api.delete_from_workspace(workspace.id, self.id)
    return self