logfilesrefsstats
path: root/toucan.html
blob: bb1ce50519e15c1d3b19d8fd83594c532e203894 (plain) (blame)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>🦜 toucan</title>
</head>
<body>
<div id="content">
    <div id="nav">
        <h1>🦜 toucan</h1>
        <div>
        <label for="model">select model:</label>
        <select id="model"></select>
        </div>
        <div>
        <label for="ollama">ollama address:</label>
        <input id="ollama" type="text" placeholder="127.0.0.1"/>
        <button id="connect">connect</button>
        </div>
    </div>

    <div id="chat"></div>

    <div id="prompt">
        <input type="text" id="prompt-input" placeholder="enter a prompt"/>
        <button id="send">send</button>
    </div>
</div>
<style>
* {
    box-sizing: border-box;
}

html, body {
    width: 100%;
    height: 100%;
    margin: 0;
    background: #1a1a1a;
    color: white;
    font-family:sans-serif;
}

#content {
    height: 100dvh;
    display: flex;
    flex-direction: column;
}

#nav {
    height: 10vh;
    background: #2a2aa0;
    display: flex;
    align-items: center;
    justify-content: space-evenly;
    flex-shrink: 0;
}

#chat {
    flex: 1;
    min-height: 0;
    padding: 20px;
    overflow-y: auto;
    font-family:serif;
}

#prompt {
    height: 5vh;
    display: flex;
    align-items: center;
    padding: 10px;
    gap: 10px;
    flex-shrink: 0;
}

#prompt input {
    flex: 1;
    min-width: 0;
    height: 100%;
    padding: 12px 16px;
    background: #2a2a2a;
    color: white;
    border: 1px solid #1a1a1a;
    border-radius: 5px;
    outline: none;
}

#prompt input:focus {
    border-color: #2a2aa0;
}

#prompt button {
    width: 20%;
    height: 100%;
    background: #2a2aa0;
    color: white;
    border: none;
    border-radius: 5px;
    cursor: pointer;
}

#prompt button:hover {
    background: #3838bb;
}

.user {
    color:#ff6464;
    font-size:20px;
}
</style>
<script type="module">
import { Ollama } from 'https://esm.sh/ollama/browser';

const ollamalocation = document.getElementById("ollama");

let ollama;

const connectButton = document.getElementById("connect");

connectButton.addEventListener("click", async () => {
    ollama = new Ollama({
        host: 'http://' + ollamalocation.value + ":11434"
    });

    try {
        model.innerHTML = "";
        await loadModels();
        addMessage("assistant", "connected to ollama");
    } catch (error) {
        addMessage("assistant", "could not connect: " + error.message);
    }
});

const model = document.getElementById("model");
const chat = document.getElementById("chat");
const promptInput = document.getElementById("prompt-input");
const sendButton = document.getElementById("send");

let messages = [];

async function loadModels() {
    const result = await ollama.list();

    result.models.forEach(m => {
        const option = document.createElement("option");
        option.value = m.name;
        option.textContent = m.name;
        model.appendChild(option);
    });
}

function addMessage(role, content = "") {
    const message = document.createElement("div");
    message.className = role;
    message.textContent = content;

    const br = document.createElement("br");

    chat.appendChild(message);
    chat.appendChild(br);
    return message;
}

async function sendMessage() {
    const text = promptInput.value.trim();

    if (!text || !model.value || sendButton.disabled) return;

    promptInput.value = "";

    addMessage("user", text);

    messages.push({
        role: "user",
        content: text
    });

    const reply = addMessage("assistant");
    const assistantMessage = {
        role: "assistant",
        content: ""
    };

    sendButton.disabled = true;

    try {
        const response = await ollama.chat({
            model: model.value,
            messages: messages,
            stream: true
        });

        for await (const part of response) {
            const token = part.message.content;

            assistantMessage.content += token;
            reply.textContent += token;
        }

        messages.push(assistantMessage);

    } catch (error) {
        reply.textContent = `error: ${error.message}`;
    }

    sendButton.disabled = false;
    promptInput.focus();
}

sendButton.addEventListener("click", sendMessage);

promptInput.addEventListener("keydown", event => {
    if (event.key === "Enter") {
        sendMessage();
    }
});
</script>

</body>
</html>