跳到内容

rg.Response

用于与记录的 Argilla Response 交互的类。Response 是用户对问题的回答。因此,一个问题可以有多个 Response,每个回答该问题的用户对应一个。Response 通常由 UI 中的用户创建,或者从数据源作为标签使用,这与通常由模型预测创建的 Suggestion 不同。

使用示例

可以将 Response 直接添加到实例化的 Record 中,也可以作为字典添加。以下示例演示了如何将 Response 添加到记录对象以及如何从记录对象访问 Response

实例化 Record 和相关的 Response 对象

dataset.records.log(
    [
        rg.Record(
            fields={"text": "Hello World, how are you?"},
            responses=[rg.Response("label", "negative", user_id=user.id)],
            external_id=str(uuid.uuid4()),
        )
    ]
)

或者,从字典中添加 Response,其中键是问题名称,值是 Response

dataset.records.log(
    [
        {
            "text": "Hello World, how are you?",
            "label.response": "negative",
        },
    ]
)

可以从 Record 中通过问题名称作为记录的属性来访问 Response。因此,如果问题名为 label,则可以作为 record.label 访问 Response。以下示例演示了如何从记录对象访问 Response

# iterate over the records and responses

for record in dataset.records:
    for response in record.responses["label"]: # (1)
        print(response.value)
        print(response.user_id)

# validate that the record has a response

for record in dataset.records:
    if record.responses["label"]:
        for response in record.responses["label"]:
            print(response.value)
            print(response.user_id)
    else:
        record.responses.add(
            rg.Response("label", "positive", user_id=user.id)
        ) # (2)
1. 像包含 Response 对象列表的字典一样,访问每个记录名为 label 的问题的 Response。2. 如果记录尚无 Response,则向记录添加 Response。

每种 Question 类型的格式

根据 Question 类型,Response 可能需要以略微不同的方式格式化。

rg.Response(
    question_name="label",
    value="positive",
    user_id=user.id,
    status="draft"
)
rg.Response(
    question_name="multi-label",
    value=["positive", "negative"],
    user_id=user.id,
    status="draft"
)
rg.Response(
    question_name="rank",
    value=["1", "3", "2"],
    user_id=user.id,
    status="draft"
)
rg.Response(
    question_name="rating",
    value=4,
    user_id=user.id,
    status="draft"
)
rg.Response(
    question_name="span",
    value=[{"start": 0, "end": 9, "label": "MISC"}],
    user_id=user.id,
    status="draft"
)
rg.Response(
    question_name="text",
    value="value",
    user_id=user.id,
    status="draft"
)

Response

用于与记录的 Argilla Response 交互的类。Response 是用户对问题的回答。因此,一个记录问题可以有多个 Response,每个回答该问题的用户对应一个。Response 通常由 UI 中的用户创建,或者从数据源作为标签使用,这与通常由模型预测创建的 Suggestion 不同。

源代码在 src/argilla/responses.py
class Response:
    """Class for interacting with Argilla Responses of records. Responses are answers to questions by a user.
    Therefore, a record question can have multiple responses, one for each user that has answered the question.
    A `Response` is typically created by a user in the UI or consumed from a data source as a label,
    unlike a `Suggestion` which is typically created by a model prediction.

    """

    def __init__(
        self,
        question_name: str,
        value: Any,
        user_id: UUID,
        status: Optional[Union[ResponseStatus, str]] = None,
        _record: Optional["Record"] = None,
    ) -> None:
        """Initializes a `Response` for a `Record` with a user_id and value

        Attributes:
            question_name (str): The name of the question that the suggestion is for.
            value (str): The value of the response
            user_id (UUID): The id of the user that submits the response
            status (Union[ResponseStatus, str]): The status of the response as "draft", "submitted", "discarded".
        """

        if isinstance(status, str):
            status = ResponseStatus(status)

        if question_name is None:
            raise ValueError("question_name is required")
        if value is None and status == ResponseStatus.submitted:
            raise ValueError("value is required")
        if user_id is None:
            raise ValueError("user_id is required")

        self._record = _record
        self.question_name = question_name
        self.value = value
        self.user_id = user_id
        self.status = status

    @property
    def record(self) -> "Record":
        """Returns the record associated with the response"""
        return self._record

    @record.setter
    def record(self, record: "Record") -> None:
        """Sets the record associated with the response"""
        self._record = record

    def serialize(self) -> dict[str, Any]:
        """Serializes the Response to a dictionary. This is principally used for sending the response to the API, \
            but can be used for data wrangling or manual export.

        Returns:
            dict[str, Any]: The serialized response as a dictionary with keys `question_name`, `value`, and `user_id`.

        Examples:

        ```python
        response = rg.Response("label", "negative", user_id=user.id)
        response.serialize()
        ```
        """
        return {
            "question_name": self.question_name,
            "value": self.value,
            "user_id": self.user_id,
            "status": self.status,
        }

record: Record property writable

返回与 Response 关联的记录

__init__(question_name, value, user_id, status=None, _record=None)

Record 初始化一个 Response,其中包含 user_id 和 value

属性

名称 类型 描述
question_name str

建议针对的问题的名称。

value str

response 的值

user_id UUID

提交 response 的用户的 ID

status Union[ResponseStatus, str]

response 的状态,如“草稿”、“已提交”、“已放弃”。

源代码在 src/argilla/responses.py
def __init__(
    self,
    question_name: str,
    value: Any,
    user_id: UUID,
    status: Optional[Union[ResponseStatus, str]] = None,
    _record: Optional["Record"] = None,
) -> None:
    """Initializes a `Response` for a `Record` with a user_id and value

    Attributes:
        question_name (str): The name of the question that the suggestion is for.
        value (str): The value of the response
        user_id (UUID): The id of the user that submits the response
        status (Union[ResponseStatus, str]): The status of the response as "draft", "submitted", "discarded".
    """

    if isinstance(status, str):
        status = ResponseStatus(status)

    if question_name is None:
        raise ValueError("question_name is required")
    if value is None and status == ResponseStatus.submitted:
        raise ValueError("value is required")
    if user_id is None:
        raise ValueError("user_id is required")

    self._record = _record
    self.question_name = question_name
    self.value = value
    self.user_id = user_id
    self.status = status

serialize()

将 Response 序列化为字典。这主要用于将 response 发送到 API,但也可用作数据整理或手动导出。

返回值

类型 描述
dict[str, Any]

dict[str, Any]: 序列化的 response,以字典形式表示,键为 question_namevalueuser_id

示例

response = rg.Response("label", "negative", user_id=user.id)
response.serialize()
源代码在 src/argilla/responses.py
def serialize(self) -> dict[str, Any]:
    """Serializes the Response to a dictionary. This is principally used for sending the response to the API, \
        but can be used for data wrangling or manual export.

    Returns:
        dict[str, Any]: The serialized response as a dictionary with keys `question_name`, `value`, and `user_id`.

    Examples:

    ```python
    response = rg.Response("label", "negative", user_id=user.id)
    response.serialize()
    ```
    """
    return {
        "question_name": self.question_name,
        "value": self.value,
        "user_id": self.user_id,
        "status": self.status,
    }