File size: 4,449 Bytes
5a4ba7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import jinja2

def tojson_filter(val, indent=None):
    if indent is not None:
        return json.dumps(val, ensure_ascii=False, indent=indent)
    return json.dumps(val, ensure_ascii=False)

def render_with_jinja(template_path, messages, tools=None, add_generation_prompt=False):
    with open(template_path, "r", encoding="utf-8") as f:
        template_content = f.read()

    env = jinja2.Environment(trim_blocks=True, lstrip_blocks=True)
    env.filters["tojson"] = tojson_filter

    template = env.from_string(template_content)
    return template.render(
        messages=messages,
        tools=tools,
        add_generation_prompt=add_generation_prompt
    )

def render_with_huggingface(template_path, messages, tools=None, add_generation_prompt=False):
    try:
        from transformers import AutoTokenizer
        with open(template_path, "r", encoding="utf-8") as f:
            template_content = f.read()

        tokenizer = AutoTokenizer.from_pretrained("gpt2")
        tokenizer.chat_template = template_content
        return tokenizer.apply_chat_template(
            messages,
            tools=tools,
            add_generation_prompt=add_generation_prompt,
            tokenize=False
        )
    except Exception as e:
        return f"[HuggingFace Error / Not Available]: {e}"

if __name__ == "__main__":
    template_file = "chat_template.jinja"

    print("=" * 60)
    print(" 1. Standart Sohbet Senaryosu (PPSF v1.0)")
    print("=" * 60)
    messages_1 = [
        {
            "role": "system",
            "content": "Sen uzman bir eczacı yapay zekâsısın."
        },
        {
            "role": "user",
            "content": "Boğazım ağrıyor."
        }
    ]

    print("\n--- [Jinja2 Output] ---")
    print(render_with_jinja(template_file, messages_1, add_generation_prompt=True))

    print("--- [HuggingFace apply_chat_template Output] ---")
    print(render_with_huggingface(template_file, messages_1, add_generation_prompt=True))

    print("=" * 60)
    print(" 2. Tool Calling & Tool Response Senaryosu")
    print("=" * 60)
    messages_2 = [
        {
            "role": "system",
            "content": "Sen uzman bir eczacı yapay zekâsısın."
        },
        {
            "role": "user",
            "content": "Parol ne işe yarar?"
        },
        {
            "role": "assistant",
            "content": None,
            "tool_calls": [
                {
                    "function": {
                        "name": "search_drug",
                        "arguments": {"drug": "Parol"}
                    }
                }
            ]
        },
        {
            "role": "tool",
            "content": '{"drug": "Parol", "active_ingredient": "Parasetamol", "usage": "Ağrı kesici ve ateş düşürücü"}'
        }
    ]

    tools_2 = [
        {
            "name": "search_drug",
            "description": "Verilen ilaç hakkında detaylı bilgi arar.",
            "parameters": {
                "type": "object",
                "properties": {
                    "drug": {"type": "string", "description": "İlaç adı"}
                },
                "required": ["drug"]
            }
        }
    ]

    print("\n--- [Jinja2 Output] ---")
    print(render_with_jinja(template_file, messages_2, tools=tools_2, add_generation_prompt=True))

    print("=" * 60)
    print(" 3. PPSF v1.1 Gelecek Uyumlu Metadata (@THOUGHT & @REFERENCE)")
    print("=" * 60)
    messages_3 = [
        {
            "role": "system",
            "content": "Sen uzman bir eczacı yapay zekâsısın."
        },
        {
            "role": "user",
            "content": "Grip için hangi ilacı kullanmalıyım?",
            "metadata": {
                "reference": "[Kılavuz Doc #42]: Parasetamol 500mg hafif ağrı ve ateş durumlarında tercih edilir."
            }
        },
        {
            "role": "assistant",
            "content": "Ateş ve hafif ağrınız varsa doktor veya eczacınıza danışarak Parasetamol içerikli ilaçlar tercih edebilirsiniz.",
            "metadata": {
                "thought": "Kullanıcının semptomları grip kaynaklı ateş ve hafif ağrı belirtilerine uymaktadır. Referans doküman #42 incelenerek Parasetamol tavsiye edildi."
            }
        }
    ]

    print("\n--- [Jinja2 Output] ---")
    print(render_with_jinja(template_file, messages_3, add_generation_prompt=False))