File size: 3,800 Bytes
2e7213e
 
6552cfd
 
2e7213e
 
 
 
6552cfd
2e7213e
 
 
6552cfd
 
 
 
 
 
 
 
 
 
 
2e7213e
 
 
6552cfd
2e7213e
6552cfd
 
2e7213e
6552cfd
2e7213e
6552cfd
 
2e7213e
 
6552cfd
 
 
2e7213e
6552cfd
 
2e7213e
 
 
 
6552cfd
2e7213e
69d08bd
6552cfd
 
2e7213e
 
 
6552cfd
 
 
 
 
 
 
2e7213e
 
 
6552cfd
 
 
 
 
 
 
 
 
2e7213e
 
 
6552cfd
 
 
 
 
 
6cb53f4
6552cfd
 
 
6cb53f4
6552cfd
 
 
2e7213e
 
 
6552cfd
 
2e7213e
6552cfd
 
 
 
2e7213e
 
6552cfd
 
 
 
 
 
 
 
2e7213e
 
6552cfd
2e7213e
6552cfd
 
2e7213e
6552cfd
2e7213e
 
6552cfd
 
 
2e7213e
6552cfd
2e7213e
6552cfd
 
 
2e7213e
6552cfd
 
2e7213e
 
 
6552cfd
 
2e7213e
 
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
import marimo

__generated_with = "0.9.14"
app = marimo.App(width="medium")


@app.cell
def __(mo):
    mo.md(r"""# Chat is an abstraction""")
    return


@app.cell
def __(
    add_generation_prompt,
    mo,
    model_id,
    multiselect,
    output2,
    system_message,
    user_message,
):
    mo.hstack([mo.vstack([model_id, system_message, user_message, add_generation_prompt, multiselect]), mo.vstack([mo.md("**What the LLM sees:**"), output2], align="start", justify="start")], gap=2, widths=[1,2])
    return


@app.cell
def __(mo):
    add_generation_prompt = mo.ui.checkbox(label="Add generation prompt:")
    return (add_generation_prompt,)

@app.cell
def __(mo):
    model_id = mo.ui.text_area("Qwen/Qwen2.5-0.5B-Instruct", label="Model", rows=1)
    return (model_id,)


@app.cell
def __(model_id):
    from transformers import AutoTokenizer

    tokenizer = AutoTokenizer.from_pretrained(model_id.value)
    return AutoTokenizer, tokenizer


@app.cell
def __():
    import marimo as mo

    system_message = mo.ui.text_area("You are a helpful assistant.", rows=1, debounce=False, label="System message:")
    user_message = mo.ui.text_area("", rows=2, debounce=False, label="User message:")
    return mo, system_message, user_message


@app.cell
def __(system_message, user_message):
    messages = []
    if system_message.value != "":
        messages.append({"role": "system", "content": system_message.value})
    if user_message.value != "":
        messages.append({"role": "user", "content": user_message.value})
    return (messages,)


@app.cell
def __(add_generation_prompt, messages, mo, tokenizer, tools):
    if messages != []:
        output = mo.md(repr(tokenizer.apply_chat_template(messages,
                                                          add_generation_prompt=add_generation_prompt.value,
                                                          tools=tools,
                                                          tokenize=False)))
    else:
        output = ""
    return (output,)


@app.cell
def __(add_generation_prompt, messages, mo, tokenizer, tools):
    if messages != []:
        if tools != []:
            output2 = mo.md(tokenizer.apply_chat_template(messages,
                                                      add_generation_prompt= add_generation_prompt.value,
                                                      tools=tools,
                                                      tokenize=False).replace("\n","\n\n").replace("#", "\#"))
        else:
            output2 = mo.md(tokenizer.apply_chat_template(messages,
                                                      add_generation_prompt= add_generation_prompt.value,
                                                      tokenize=False).replace("\n","\n\n").replace("#", "\#"))
    else:
        output2 = ""
    return (output2,)


@app.cell
def __():
    import datetime

    def current_time():
        """Get the current local time as a string."""
        return str(datetime.now())
    return current_time, datetime


@app.cell
def __(current_time, multiply, multiselect):
    tools = []
    if "current_time" in multiselect.value:
        tools.append(current_time)
    if "multiply" in multiselect.value:
        tools.append(multiply)
    return (tools,)


@app.cell
def __(mo):
    multiselect = mo.ui.multiselect(
        options=["current_time", "multiply"], label="Provide some tools:"
    )
    return (multiselect,)


@app.cell
def __():
    def multiply(a: float, b: float):
        """
        A function that multiplies two numbers

        Args:
            a: The first number to multiply
            b: The second number to multiply
        """
        return a * b
    return (multiply,)


@app.cell
def __():
    # tokenizer.chat_template
    return