Datasets:
Download code/src/barbet_bgd/serializers.py from voidful/barbet-sft: direct link, hf CLI and curl.
- Browser
- Download file 1.84 kB
-
https://huggingface.co/datasets/voidful/barbet-sft/resolve/main/code/src/barbet_bgd/serializers.py
- Command line
-
hf download hf://datasets/voidful/barbet-sft/code/src/barbet_bgd/serializers.py
-
curl -L -o serializers.py https://huggingface.co/datasets/voidful/barbet-sft/resolve/main/code/src/barbet_bgd/serializers.py
1.84 kB
| """Controlled serializers derived from one canonical semantic target.""" | |
| from __future__ import annotations | |
| import json | |
| from typing import Any | |
| def _compact(value: Any) -> str: | |
| return json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":")) | |
| def serialize_target(target: dict[str, Any], serializer: str) -> str: | |
| """Serialize a canonical target without changing its semantics.""" | |
| if serializer == "JSON": | |
| return json.dumps(target, ensure_ascii=False, sort_keys=True, indent=2) | |
| fields = [(key, target[key]) for key in sorted(target)] | |
| if serializer == "FUNCTION_CALL": | |
| args = ", ".join(f"{key}={_compact(value)}" for key, value in fields) | |
| return f"BGD_RESULT({args})" | |
| if serializer == "COMPACT_TAGS": | |
| return "".join(f"<{key}>{_compact(value)}</{key}>" for key, value in fields) | |
| if serializer == "KEY_VALUE": | |
| return "\n".join(f"{key}: {_compact(value)}" for key, value in fields) | |
| raise ValueError(f"unknown serializer: {serializer}") | |
| def build_messages(record: dict[str, Any]) -> list[dict[str, str]]: | |
| """Render one canonical record into a chat-style SFT example.""" | |
| input_payload = { | |
| "task": record["task_spec"], | |
| "inputs": record["inputs"], | |
| } | |
| user = ( | |
| record["task_spec"]["instruction"] | |
| + "\n\n輸入:\n" | |
| + json.dumps(input_payload, ensure_ascii=False, sort_keys=True, indent=2) | |
| ) | |
| assistant = serialize_target(record["target"], record["presentation"]["serializer"]) | |
| return [ | |
| { | |
| "role": "system", | |
| "content": "你是 Barbet Grounded Decision Controller。只依據可引用證據輸出指定結構;證據不足時必須棄答。", | |
| }, | |
| {"role": "user", "content": user}, | |
| {"role": "assistant", "content": assistant}, | |
| ] | |