Datasets:
Download code/v4_helpsteer.py from kitaniai/OpenJudgment-4B-Preview: direct link, hf CLI and curl.
- Browser
- Download file 8.44 kB
-
https://huggingface.co/datasets/kitaniai/OpenJudgment-4B-Preview/resolve/main/code/v4_helpsteer.py
- Command line
-
hf download hf://datasets/kitaniai/OpenJudgment-4B-Preview/code/v4_helpsteer.py
-
curl -L -o v4_helpsteer.py https://huggingface.co/datasets/kitaniai/OpenJudgment-4B-Preview/resolve/main/code/v4_helpsteer.py
8.44 kB
| """HelpSteer3 human preference adapter; no downloading or training side effects. | |
| References (schema inspected through HF first-rows on 2026-09-17): | |
| https://huggingface.co/datasets/nvidia/HelpSteer3/blob/main/README.md#preference | |
| https://arxiv.org/abs/2505.11475 | |
| Each preference pair produces ONE ordered seven-bin score distribution. Feedback | |
| produces up to two independent five-bin single-response rubric tasks. Targets are | |
| empirical human vote frequencies, not objective correctness probabilities or | |
| confidence estimates. Up to three votes are sparse evidence of preferences. | |
| Do not also emit a winner task for the same pair and inflate its weight. Never | |
| derive probabilities from overall_preference, an average, or a rationale. | |
| The caller owns fetching, revision verification, deduplication and group splits. | |
| Metadata is audit-only and MUST NOT be included in model inputs. | |
| """ | |
| from collections import Counter | |
| import json | |
| import re | |
| SOURCE = { | |
| "repo": "nvidia/HelpSteer3", | |
| "revision": "f6d145777bcbde96137596340fab89793acd1031", | |
| "license": "cc-by-4.0", | |
| "config": "preference", | |
| "paper": "https://arxiv.org/abs/2505.11475", | |
| "card": "https://huggingface.co/datasets/nvidia/HelpSteer3/blob/" | |
| "f6d145777bcbde96137596340fab89793acd1031/README.md", | |
| } | |
| SCORES = tuple(range(-3, 4)) | |
| CANDIDATES = ( | |
| "Response 1 is much better than Response 2", | |
| "Response 1 is better than Response 2", | |
| "Response 1 is slightly better than Response 2", | |
| "Response 1 is about the same as Response 2", | |
| "Response 2 is slightly better than Response 1", | |
| "Response 2 is better than Response 1", | |
| "Response 2 is much better than Response 1", | |
| ) | |
| INSTRUCTIONS = ( | |
| "Evaluate the overall helpfulness of the two responses to the full conversation. " | |
| "Predict the distribution of human preference ratings across the seven ordered " | |
| "categories: -3 strongly favors Response 1, -2 favors Response 1, -1 slightly " | |
| "favors Response 1, 0 means about the same, +1 slightly favors Response 2, " | |
| "+2 favors Response 2, and +3 strongly favors Response 2. " | |
| "These probabilities describe human preferences, not probabilities of objective truth." | |
| ) | |
| HELPFULNESS = ("not", "slightly", "partially", "mostly", "perfectly") | |
| _RATING = re.compile(r"\A\s*The response is (not|slightly|partially|mostly|perfectly) helpful\.", re.IGNORECASE) | |
| def _json(value): | |
| return json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":")) | |
| def adapt(record, config="preference"): | |
| """Return preference or single-response tasks with valid human vote records. | |
| Requires the canonical JSONL/HF List schema, not a dict-of-lists conversion. | |
| Context is nonempty [{role: str, content: str}, ...]; responses are nonempty | |
| strings. Every individual_preference entry must have an integer score -3..3. | |
| Invalid votes invalidate the whole pair: silently dropping dissenting votes | |
| would change the observed distribution. Labels/feedback are never input text. | |
| group_key includes only the whole prompt conversation in canonical JSON, | |
| with line endings normalized; case, spacing and code indentation are retained. | |
| """ | |
| if config not in ("preference", "feedback"): | |
| raise ValueError("Only human-labeled preference and feedback configs are supported") | |
| if not isinstance(record, dict): | |
| return [] | |
| raw_context = record.get("context") | |
| if not isinstance(raw_context, list) or not raw_context: | |
| return [] | |
| context = [] | |
| for message in raw_context: | |
| if not isinstance(message, dict): | |
| return [] | |
| role, content = message.get("role"), message.get("content") | |
| if not isinstance(role, str) or not role.strip() or not isinstance(content, str): | |
| return [] | |
| context.append({"role": role, "content": content.replace("\r\n", "\n").replace("\r", "\n")}) | |
| if not any(message["content"].strip() for message in context): | |
| return [] | |
| responses = [record.get("response1"), record.get("response2")] | |
| if any(not isinstance(response, str) or not response.strip() for response in responses): | |
| return [] | |
| if config == "feedback": | |
| return _feedback(record, context, responses) | |
| annotations = record.get("individual_preference") | |
| if not isinstance(annotations, list) or not annotations: | |
| return [] | |
| votes = [] | |
| for annotation in annotations: | |
| if not isinstance(annotation, dict): | |
| return [] | |
| score = annotation.get("score") | |
| if type(score) is not int or score not in SCORES: | |
| return [] | |
| votes.append(score) | |
| counts = Counter(votes) | |
| metadata = { | |
| "source": SOURCE["repo"], | |
| "source_config": config, | |
| "source_license": SOURCE["license"], | |
| "source_card": SOURCE["card"], | |
| "source_paper": SOURCE["paper"], | |
| "label_method": "empirical_individual_human_preference_votes", | |
| "target_semantics": "human_preference_frequency_not_objective_truth", | |
| "individual_preference_scores": votes, | |
| "vote_counts": [counts[score] for score in SCORES], | |
| "original_score_values": list(SCORES), | |
| "n_annotations": len(votes), | |
| "domain": record.get("domain"), | |
| "language": record.get("language"), | |
| "overall_preference_audit_only": record.get("overall_preference"), | |
| } | |
| return [{ | |
| "state": _json({"context": context, "response1": responses[0], "response2": responses[1]}), | |
| "instructions": INSTRUCTIONS, | |
| "candidates": list(CANDIDATES), | |
| "keys": [str(index) for index in range(len(SCORES))], | |
| "target": [counts[score] / len(votes) for score in SCORES], | |
| "kind": "score", | |
| "group_key": _json(context), | |
| "metadata": metadata, | |
| }] | |
| def _feedback(record, context, responses): | |
| """Parse only the documented categorical opening, never infer from prose. | |
| Each response has a list of human feedback strings in feedback1/feedback2. | |
| Require at least two raters and ALL openings to parse. If one is ambiguous, | |
| exclude the whole response rather than biasing its distribution by dropping | |
| that rater. Builder can count rejected responses as 2 - len(adapt(record)). | |
| """ | |
| tasks = [] | |
| for number, response in enumerate(responses, start=1): | |
| annotations = record.get(f"feedback{number}") | |
| if not isinstance(annotations, list) or len(annotations) < 2: | |
| continue | |
| votes = [] | |
| for annotation in annotations: | |
| match = _RATING.match(annotation) if isinstance(annotation, str) else None | |
| if match: | |
| votes.append(HELPFULNESS.index(match.group(1).lower())) | |
| if len(votes) != len(annotations): | |
| continue | |
| counts = Counter(votes) | |
| tasks.append({ | |
| "state": _json({"context": context, "response": response}), | |
| "instructions": "Rate the overall helpfulness of the response to the full conversation. " | |
| "Predict the human rating distribution on this ordered rubric: 0 not helpful, " | |
| "1 slightly helpful, 2 partially helpful, 3 mostly helpful, 4 perfectly helpful. " | |
| "These probabilities describe human judgments, not objective truth.", | |
| "candidates": [f"The response is {level} helpful" for level in HELPFULNESS], | |
| "keys": [str(score) for score in range(5)], | |
| "target": [counts[score] / len(votes) for score in range(5)], | |
| "kind": "score", | |
| "group_key": _json(context), | |
| "metadata": { | |
| "source": SOURCE["repo"], "source_config": "feedback", | |
| "source_license": SOURCE["license"], "source_card": SOURCE["card"], | |
| "source_paper": "https://arxiv.org/abs/2503.04378", | |
| "label_method": "empirical_human_feedback_anchored_rubric_votes", | |
| "target_semantics": "human_helpfulness_frequency_not_objective_truth", | |
| "domain": record.get("domain"), "language": record.get("language"), | |
| "source_response": f"response{number}", | |
| "individual_helpfulness_scores": votes, | |
| "vote_counts": [counts[score] for score in range(5)], | |
| "n_annotations": len(votes), "excluded_annotation_count": 0, | |
| "annotation_parse_policy": "all_raters_parse_and_at_least_two", | |
| }, | |
| }) | |
| return tasks | |