File size: 5,110 Bytes
3962398
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
"""
Utilities to prepare ChessQA-Benchmark prompts for evaluation.

The dataset questions contain placeholder tokens:
  - ``CONTEXT_PLACEHOLDER``: optionally replace with descriptive context.
  - ``FORMAT_EXAMPLE_PLACEHOLDER``: optionally replace with a sample answer format.

The helpers below make it easy to fill those placeholders consistently before
calling an LLM.
"""

from __future__ import annotations

import json
import re
from dataclasses import dataclass
from typing import Any, Dict, Mapping, Optional, Sequence, Tuple

import chess


@dataclass
class PromptConfig:
    """Configuration for prompt formatting."""

    add_context: bool = True
    """Inject piece arrangement and legal moves into the prompt when true."""

    format_example_index: int = 0
    """Pick which entry from ``format_examples`` to include (0-based)."""


def _clean_fen(fen: str) -> str:
    """Strip move annotations that follow the FEN with a pipe separator."""
    return fen.split("|", 1)[0].strip()


def get_context(fen: str) -> str:
    """
    Build a textual description for the provided FEN.

    Returns piece arrangement and all legal moves. When the FEN is invalid the
    function returns an empty string instead of raising.
    """
    fen_clean = _clean_fen(fen)
    try:
        board = chess.Board(fen_clean)
    except Exception:
        try:
            board = chess.Board(" ".join(fen_clean.split()))
        except Exception:
            return ""

    piece_names = {
        chess.PAWN: "Pawn",
        chess.KNIGHT: "Knight",
        chess.BISHOP: "Bishop",
        chess.ROOK: "Rook",
        chess.QUEEN: "Queen",
        chess.KING: "King",
    }

    pieces: Dict[str, list[str]] = {}
    for square in chess.SQUARES:
        piece = board.piece_at(square)
        if not piece:
            continue
        color = "White" if piece.color == chess.WHITE else "Black"
        key = f"{color} {piece_names[piece.piece_type]}"
        pieces.setdefault(key, []).append(chess.square_name(square))

    arrangement_parts = [f"{k}: {sorted(v)}" for k, v in pieces.items()]
    arrangement = ", ".join(arrangement_parts)
    legal_moves = ", ".join(sorted(move.uci() for move in board.legal_moves))
    return f"Piece arrangement: {arrangement}\nLegal moves: {legal_moves}\n\n"


def _select_format_example(format_examples: Optional[Sequence[str]], index: int) -> str:
    if not format_examples:
        return ""
    if index < 0 or index >= len(format_examples):
        index = 0
    return format_examples[index]


def _ensure_question_fields(row: Mapping[str, Any]) -> Tuple[str, Sequence[str]]:
    question = row.get("question", "")
    if not isinstance(question, str):
        raise TypeError("Expected 'question' to be a string")

    raw_examples = row.get("format_examples", [])
    if isinstance(raw_examples, str):
        try:
            raw_examples = json.loads(raw_examples)
        except json.JSONDecodeError:
            raw_examples = [raw_examples]

    if not isinstance(raw_examples, Sequence):
        raise TypeError("Expected 'format_examples' to be a sequence")
    return question, raw_examples


def format_prompt(
    row: Mapping[str, Any],
    config: Optional[PromptConfig] = None,
) -> str:
    """
    Replace placeholders in the dataset question.

    Parameters
    ----------
    row:
        A single dataset row (e.g., from the Hugging Face ``datasets`` library).
    config:
        Prompt configuration. When omitted defaults to ``PromptConfig()``.
    """
    config = config or PromptConfig()
    question, format_examples = _ensure_question_fields(row)

    formatted = question
    if config.add_context:
        fen = row.get("input", "")
        if isinstance(fen, str):
            context = get_context(fen)
        else:
            context = ""
        formatted = formatted.replace("CONTEXT_PLACEHOLDER", context)
    else:
        formatted = formatted.replace("CONTEXT_PLACEHOLDER", "")

    example_text = _select_format_example(format_examples, config.format_example_index)
    formatted = formatted.replace("FORMAT_EXAMPLE_PLACEHOLDER", example_text)
    return formatted


FINAL_ANSWER_PATTERN = re.compile(r"FINAL ANSWER:\\s*(.+?)(?:\\n|$)", re.IGNORECASE | re.DOTALL)


def extract_final_answer(text: str) -> Tuple[str, bool]:
    """
    Extract the trailing ``FINAL ANSWER:`` segment from a model response.

    Returns the extracted answer and a flag that indicates if the marker was
    found. Convenience helper for downstream evaluation scripts.
    """
    matches = list(FINAL_ANSWER_PATTERN.finditer(text or ""))
    if not matches:
        boxed = re.findall(r"[Tt]he\\s+final\\s+answer\\s+is\\s+\\$?\\\\boxed\\{([^}]+)\\}\\$?", text or "")
        if boxed:
            return boxed[-1].strip(), True
        return "", False

    answer = matches[-1].group(1).strip()
    answer = re.sub(r"^FINAL ANSWER:\\s*", "", answer, flags=re.IGNORECASE).strip()
    answer = answer.strip("*").strip()
    return answer, True


__all__ = [
    "PromptConfig",
    "extract_final_answer",
    "format_prompt",
    "get_context",
]