可以将 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 responsesforrecordindataset.records:forresponseinrecord.responses["label"]:# (1)print(response.value)print(response.user_id)# validate that the record has a responseforrecordindataset.records:ifrecord.responses["label"]:forresponseinrecord.responses["label"]:print(response.value)print(response.user_id)else:record.responses.add(rg.Response("label","positive",user_id=user.id))# (2)
classResponse:"""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". """ifisinstance(status,str):status=ResponseStatus(status)ifquestion_nameisNone:raiseValueError("question_name is required")ifvalueisNoneandstatus==ResponseStatus.submitted:raiseValueError("value is required")ifuser_idisNone:raiseValueError("user_id is required")self._record=_recordself.question_name=question_nameself.value=valueself.user_id=user_idself.status=status@propertydefrecord(self)->"Record":"""Returns the record associated with the response"""returnself._record@record.setterdefrecord(self,record:"Record")->None:"""Sets the record associated with the response"""self._record=recorddefserialize(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,}
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". """ifisinstance(status,str):status=ResponseStatus(status)ifquestion_nameisNone:raiseValueError("question_name is required")ifvalueisNoneandstatus==ResponseStatus.submitted:raiseValueError("value is required")ifuser_idisNone:raiseValueError("user_id is required")self._record=_recordself.question_name=question_nameself.value=valueself.user_id=user_idself.status=status
defserialize(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,}