File size: 3,095 Bytes
7debf49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""benak_lang: make Beam *think* in the user's language.



Default (primer=False) = Benak condition C2: "reason in X" as a system prompt. Robust across tasks: English share of

Beam's reasoning 60% -> 42% on Belebele (18 languages) and 70% -> 19% (Indonesian) on code-verified math, accuracy unchanged.

primer=True = C3: additionally replays one earlier turn whose `reasoning_content` is written in X. Best on reading

comprehension (-> 34%), but its reading-item primer does NOT transfer to math (Sundanese 56% -> 68%, Thai 51% -> 67%),

so only use it when your task resembles the primer. Templates for 19 languages ship in prompt_templates.json.



    from openai import OpenAI

    from benak_lang import think_in

    client = OpenAI(base_url="https://api.reflection.ai/openai/v1", api_key=...)

    msgs = think_in([{"role": "user", "content": "Berapa 17 x 23?"}], "ind_Latn")

    client.chat.completions.create(model="Beam-1.0-501B-A23B", messages=msgs, extra_body={"reasoning_effort": "low"})

"""
import json, os

_T = None
_EX_Q = {"role": "user", "content": None}


def templates(path=None):
    global _T
    if _T is None:
        here = os.path.dirname(os.path.abspath(__file__))
        for p in (path, os.path.join(here, "prompt_templates.json"), os.path.join(here, "data", "atlas", "templates.json"),
                  os.path.join(here, "..", "data", "prompt_templates.json")):
            if p and os.path.exists(p):
                _T = json.load(open(p, encoding="utf-8"))
                break
        else:
            raise FileNotFoundError("prompt_templates.json not found")
    return _T


def think_in(messages, lang, primer=False):
    """Return a new message list that asks Beam to reason in `lang` (FLORES code, e.g. 'jav_Latn').

    primer=False -> C2 (system prompt only, robust default); True -> C3 (adds the replayed-reasoning primer turn)."""
    t = templates()[lang]
    sys_text = t["reason_instruction"]
    rest = [m for m in messages if m["role"] != "system"]
    sys_msgs = [m for m in messages if m["role"] == "system"]
    system = {"role": "system", "content": (sys_msgs[0]["content"] + "\n\n" if sys_msgs else "") + sys_text}
    if not primer:
        return [system, *rest]
    # exactly the Benak C3 primer: a Belebele item in X and Beam-style reasoning about it, written in X
    ex = [{"role": "user", "content": t["primer_user"]},
          {"role": "assistant", "content": t["primer_answer"], "reasoning_content": t["example_reasoning"]}]
    return [system, *ex, *rest]


if __name__ == "__main__":
    m = think_in([{"role": "system", "content": "Be brief."}, {"role": "user", "content": "Berapa 17 x 23?"}], "ind_Latn",
                 primer=True)
    assert m[0]["role"] == "system" and "Be brief." in m[0]["content"] and "bahasa Indonesia" in m[0]["content"]
    assert m[2]["reasoning_content"] and m[-1]["content"] == "Berapa 17 x 23?" and len(m) == 4
    assert len(think_in([{"role": "user", "content": "x"}], "jav_Latn")) == 2  # default = C2
    print("benak_lang self-check ok")