File size: 4,800 Bytes
6af4104
 
 
 
 
 
 
 
 
 
 
 
 
 
 
525c800
6af4104
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""chat_template.jinja render testi.

Şablonu OpenAI biçimindeki bir sohbet üzerinde çalıştırır ve modelin
göreceği ham metni ekrana basar. Model indirmeye gerek yoktur; yalnızca
jinja2 kullanılır.

Çalıştırma:  python test_render.py
"""

from pathlib import Path

from jinja2 import Environment, StrictUndefined

SABLON = Path(__file__).parent / "chat_template.jinja"

# --- Örnek araç şemaları (OpenAI biçiminde) --------------------------------
TOOLS = [
    {
        "type": "function",
        "function": {
            "name": "terim_ara",
            "description": "Biyoloji sözlüğünden bir terimin tanımını, kitap sayfasını ve kaynak adresini getirir.",
            "parameters": {
                "type": "object",
                "properties": {
                    "terim": {
                        "type": "string",
                        "description": "Aranacak biyoloji terimi",
                    }
                },
                "required": ["terim"],
            },
        },
    },
    {
        "type": "function",
        "function": {
            "name": "quiz_getir",
            "description": "Soru bankasından çoktan seçmeli soru getirir.",
            "parameters": {
                "type": "object",
                "properties": {
                    "konu": {"type": "string", "description": "Soru konusu"},
                    "adet": {"type": "integer", "description": "Kaç soru getirileceği"},
                    "zorluk": {
                        "type": "string",
                        "description": "Zorluk seviyesi",
                        "enum": ["kolay", "orta", "zor"],
                    },
                },
                "required": ["konu"],
            },
        },
    },
]

# --- Tam bir tool-calling turu ---------------------------------------------
MESSAGES = [
    {
        "role": "system",
        "content": "Sen bir biyoloji çalışma koçusun. Yanıtlarını YALNIZCA araçlardan dönen veriye dayandır.",
    },
    {"role": "user", "content": "Mayoz nedir?"},
    {
        "role": "assistant",
        "content": None,
        "tool_calls": [
            {
                "id": "call_1",
                "type": "function",
                "function": {"name": "terim_ara", "arguments": {"terim": "mayoz"}},
            }
        ],
    },
    {
        "role": "tool",
        "name": "terim_ara",
        "tool_call_id": "call_1",
        "content": {
            "tanim": "Üreme hücrelerinin oluşumunda kromozom sayısının yarıya indiği bölünme.",
            "kitap_sayfasi": 87,
            "bulundu": True,
        },
    },
    {
        "role": "assistant",
        "content": "Mayoz, üreme hücrelerinin oluşumunda kromozom sayısının yarıya indiği bölünmedir. (Kaynak: ders kitabı s.87)",
    },
    {"role": "user", "content": "Peki kuantum fotosentezi nedir?"},
]


def render(messages, tools=None, add_generation_prompt=True, **ekstra):
    env = Environment(trim_blocks=False, lstrip_blocks=False, undefined=StrictUndefined)
    env.policies["json.dumps_kwargs"] = {"ensure_ascii": False}
    sablon = env.from_string(SABLON.read_text(encoding="utf-8"))
    return sablon.render(
        messages=messages,
        tools=tools,
        add_generation_prompt=add_generation_prompt,
        bos_token="<bos>",
        **ekstra,
    )


def bolum(baslik):
    print("\n" + "=" * 70)
    print(baslik)
    print("=" * 70)


if __name__ == "__main__":
    bolum("TEST 1 — system + tools + tool_call + tool_response zinciri")
    print(render(MESSAGES, TOOLS))

    bolum("TEST 2 — system mesajı YOK, sadece tools (varsayılan talimat devreye girmeli)")
    print(render([{"role": "user", "content": "Mitoz nedir?"}], TOOLS))

    bolum("TEST 3 — araç yok, düz sohbet")
    print(
        render(
            [
                {"role": "user", "content": "Merhaba"},
                {"role": "assistant", "content": "Merhaba, hangi konuya çalışalım?"},
                {"role": "user", "content": "Mayoz"},
            ]
        )
    )

    bolum("TEST 4 — argümanlar JSON string olarak geldiğinde")
    print(
        render(
            [
                {"role": "user", "content": "Bana mayozdan 2 soru sor"},
                {
                    "role": "assistant",
                    "content": None,
                    "tool_calls": [
                        {
                            "function": {
                                "name": "quiz_getir",
                                "arguments": '{"konu": "mayoz", "adet": 2}',
                            }
                        }
                    ],
                },
            ],
            TOOLS,
            add_generation_prompt=False,
        )
    )