File size: 11,372 Bytes
d692e6c
 
 
 
 
a5e020e
d692e6c
 
dc52501
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d692e6c
ec12f4a
 
 
eaddeff
 
ec12f4a
 
 
 
 
 
 
 
 
 
eaddeff
 
 
 
 
 
 
 
dc52501
 
 
 
 
 
 
 
 
 
d692e6c
dc52501
d692e6c
dc52501
eaddeff
 
 
 
 
 
 
 
dc52501
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eaddeff
dc52501
d692e6c
eaddeff
ec12f4a
 
 
dc52501
 
99b0206
 
dc52501
 
99b0206
dc52501
99b0206
 
 
 
 
 
253c5ce
 
 
dc52501
eaddeff
ec12f4a
253c5ce
eaddeff
253c5ce
 
eaddeff
dc52501
253c5ce
dc52501
d692e6c
dc52501
eaddeff
d692e6c
 
 
 
 
dc52501
a5e020e
dc52501
 
 
ec12f4a
eaddeff
 
 
 
 
 
 
a5e020e
eaddeff
 
8bb23b5
 
 
eaddeff
 
ec12f4a
dc52501
 
ec12f4a
dc52501
d692e6c
dc52501
 
d692e6c
dc52501
d692e6c
 
 
 
dc52501
a5e020e
 
8bb23b5
ec12f4a
d692e6c
 
 
 
 
ec12f4a
eaddeff
ec12f4a
eaddeff
 
ec12f4a
 
8bb23b5
eaddeff
8bb23b5
d692e6c
 
 
 
 
 
 
 
 
 
eaddeff
 
 
 
 
 
 
 
 
 
 
 
 
ec12f4a
 
dc52501
 
eaddeff
dc52501
 
 
 
 
 
 
 
d692e6c
 
 
eaddeff
 
 
 
 
 
 
 
 
 
 
dc52501
 
eaddeff
d692e6c
 
 
 
 
 
 
 
dc52501
d692e6c
 
 
 
dc52501
a5e020e
dc52501
eaddeff
 
d692e6c
ec12f4a
 
 
 
 
8bb23b5
 
 
eaddeff
d692e6c
 
 
 
 
 
ec12f4a
d692e6c
eaddeff
 
d692e6c
 
dc52501
275db1e
dc52501
 
 
275db1e
 
d692e6c
 
 
 
dc52501
d692e6c
 
 
 
 
eaddeff
d692e6c
 
eaddeff
dc52501
eaddeff
 
 
dc52501
d692e6c
eaddeff
 
 
d692e6c
 
dc52501
d692e6c
 
 
 
ec12f4a
eaddeff
 
d692e6c
 
 
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Humanlike Chat</title>
  <style>
    * { box-sizing: border-box; }
    body {
      margin: 0;
      color: #111827;
      background: #fff;
      font: 15px/1.5 Inter, ui-sans-serif, system-ui, sans-serif;
    }
    main {
      display: grid;
      grid-template-rows: auto 1fr auto auto;
      width: min(880px, 100%);
      height: 100dvh;
      margin: auto;
    }
    header { padding: 14px 16px; border-bottom: 1px solid #e5e7eb; }
    h1 { margin: 0; font-size: 15px; font-weight: 600; }
    header p { margin: 4px 0 0; color: #6b7280; font-size: 12px; }
    header a { color: #4f46e5; }
    code { color: #374151; overflow-wrap: anywhere; }
    details { margin-top: 10px; }
    summary { color: #6b7280; font-size: 12px; cursor: pointer; }
    summary:hover { color: #4f46e5; }
    .field { margin-top: 10px; }
    .field label { display: block; margin-bottom: 4px; color: #6b7280; font-size: 12px; }
    #system {
      width: 100%;
      min-height: 76px;
      padding: 8px 10px;
      border: 1px solid #d1d5db;
      border-radius: 8px;
      font: inherit;
      font-size: 13px;
      resize: vertical;
    }
    #reasoning {
      padding: 6px 8px;
      border: 1px solid #d1d5db;
      border-radius: 8px;
      background: #fff;
      font: inherit;
      font-size: 13px;
    }
    #chat { padding: 20px 16px; overflow-y: auto; }
    .message {
      max-width: 80%;
      margin: 0 0 14px;
      padding: 10px 14px;
      border-radius: 12px;
      line-height: 1.55;
      white-space: pre-wrap;
      overflow-wrap: anywhere;
    }
    .assistant { background: #f3f4f6; }
    .user { margin-left: auto; color: #fff; background: #4f46e5; }
    .error { color: #991b1b; background: #fee2e2; }
    .pending { color: #6b7280; font-style: italic; }
    .reasoning {
      margin-bottom: 8px;
      padding-left: 9px;
      border-left: 2px solid #d1d5db;
      color: #6b7280;
      font-size: 13px;
      font-style: italic;
    }
    form {
      display: grid;
      grid-template-columns: 1fr auto;
      gap: 10px;
      padding: 14px 16px;
      border-top: 1px solid #e5e7eb;
    }
    textarea {
      min-height: 60px;
      padding: 10px 12px;
      border: 1px solid #d1d5db;
      border-radius: 8px;
      font: inherit;
      resize: vertical;
    }
    button {
      padding: 10px 20px;
      color: #fff;
      background: #4f46e5;
      border: 0;
      border-radius: 8px;
      font: inherit;
      font-weight: 600;
      cursor: pointer;
      align-self: end;
    }
    button:disabled { opacity: 0.5; cursor: wait; }
    textarea:focus, button:focus-visible, select:focus-visible, summary:focus-visible {
      outline: 2px solid #6366f1;
      outline-offset: 2px;
    }
    #status { min-height: 32px; margin: 0; padding: 0 16px 12px; color: #6b7280; font-size: 12px; }
    #status.busy::before {
      content: "";
      display: inline-block;
      width: 10px;
      height: 10px;
      margin-right: 8px;
      vertical-align: -1px;
      border: 2px solid #d1d5db;
      border-top-color: #4f46e5;
      border-radius: 50%;
      animation: spin 0.8s linear infinite;
    }
    @keyframes spin { to { transform: rotate(360deg); } }
    @media (prefers-color-scheme: dark) {
      :root { color-scheme: dark; }
      body { color: #f3f4f6; background: #111827; }
      header, form { border-color: #374151; }
      header p, #status, .pending, summary, .field label, .reasoning { color: #9ca3af; }
      summary:hover { color: #818cf8; }
      code { color: #d1d5db; }
      #system, textarea, #reasoning { color: #f3f4f6; background: #1f2937; border-color: #4b5563; }
      .assistant { background: #1f2937; }
      .error { color: #fecaca; background: #450a0a; }
      .reasoning { border-color: #4b5563; }
      #status.busy::before { border-color: #4b5563; border-top-color: #818cf8; }
    }
    @media (max-width: 620px) {
      .message { max-width: 92%; }
      form { grid-template-columns: 1fr; }
      button { width: 100%; }
    }
  </style>
</head>
<body>
  <main>
    <header>
      <h1>Humanlike Chat</h1>
      <p>
        Live demo of the hosted endpoint <code>https://api.lessthanthreeai.com/v1</code>.
      </p>
      <details>
        <summary>Settings</summary>
        <div class="field">
          <label for="system">System prompt</label>
          <textarea id="system"
            placeholder="Optional. Sets identity, setting, or relationship for the conversation."></textarea>
        </div>
        <div class="field">
          <label for="reasoning">Thinking</label>
          <select id="reasoning">
            <option value="off">Off (fastest, the default)</option>
            <option value="low">Low</option>
            <option value="medium">Medium</option>
            <option value="high">High</option>
          </select>
        </div>
      </details>
    </header>

    <div id="chat" aria-live="polite"></div>

    <form id="form">
      <textarea id="prompt" required aria-label="Message" placeholder="Type a message"></textarea>
      <button id="send" type="submit">Send</button>
    </form>

    <p id="status" aria-live="polite"></p>
  </main>

  <script>
    const API = "https://api.lessthanthreeai.com/v1";
    const MODEL = "gemma-4-31b-humanlike-chat";
    const INTRO = "Say something. The first reply can take a few minutes while the inference worker starts.";
    const MAX_TOKENS = { off: 512, low: 4096, medium: 4096, high: 8192 };

    const chat = document.querySelector("#chat");
    const form = document.querySelector("#form");
    const prompt = document.querySelector("#prompt");
    const send = document.querySelector("#send");
    const status = document.querySelector("#status");
    const system = document.querySelector("#system");
    const reasoning = document.querySelector("#reasoning");

    // One conversation per page load. Reloading starts a fresh session.
    const messages = [];

    system.value = localStorage.getItem("systemPrompt") || "";
    reasoning.value = localStorage.getItem("thinkingLevel") in MAX_TOKENS ? localStorage.getItem("thinkingLevel") : "off";
    system.addEventListener("input", () => localStorage.setItem("systemPrompt", system.value));
    reasoning.addEventListener("change", () => localStorage.setItem("thinkingLevel", reasoning.value));

    function addMessage(role, text) {
      const element = document.createElement("div");
      element.className = `message ${role}`;
      element.textContent = text;
      chat.append(element);
      chat.scrollTop = chat.scrollHeight;
      return element;
    }

    function addAssistantMessage() {
      const element = document.createElement("div");
      element.className = "message assistant";
      const thoughts = document.createElement("div");
      thoughts.className = "reasoning";
      thoughts.hidden = true;
      const content = document.createElement("div");
      content.className = "pending";
      content.textContent = "waiting for the first token";
      element.append(thoughts, content);
      chat.append(element);
      chat.scrollTop = chat.scrollHeight;
      return { element, thoughts, content };
    }

    // Server-sent events arrive as "data: {...}" chunks. The endpoint may also send
    // ": status={...}" comment lines while a cold worker starts.
    function applyEvent(line, target) {
      if (line.startsWith(":")) {
        const payload = line.slice(1).trim().replace(/^status=/, "");
        try {
          const state = JSON.parse(payload);
          if (state.message) status.textContent = state.message;
        } catch {}
        return;
      }
      if (!line.startsWith("data:")) return;
      const payload = line.slice(5).trim();
      if (!payload || payload === "[DONE]") return;

      const delta = (JSON.parse(payload).choices || [{}])[0].delta || {};
      if (delta.reasoning) {
        target.thoughts.hidden = false;
        target.thoughts.textContent += delta.reasoning;
        chat.scrollTop = chat.scrollHeight;
      }
      if (delta.content) {
        if (target.content.classList.contains("pending")) {
          target.content.classList.remove("pending");
          target.content.textContent = "";
          status.textContent = "";
        }
        target.content.textContent += delta.content;
        chat.scrollTop = chat.scrollHeight;
      }
    }

    form.addEventListener("submit", async (event) => {
      event.preventDefault();
      const text = prompt.value.trim();
      if (!text) return;

      messages.push({ role: "user", content: text });
      addMessage("user", text);
      prompt.value = "";
      send.disabled = true;
      status.classList.add("busy");
      status.textContent = "Starting the model. A cold start can take a few minutes.";

      const target = addAssistantMessage();
      const effort = reasoning.value;

      const systemPrompt = system.value.trim();
      const request = systemPrompt
        ? [{ role: "system", content: systemPrompt }, ...messages]
        : messages;

      const chatTemplateKwargs = effort === "off"
        ? { enable_thinking: false, preserve_thinking: false }
        : { enable_thinking: true, reasoning_effort: effort, preserve_thinking: false };

      try {
        const response = await fetch(`${API}/chat/completions`, {
          method: "POST",
          headers: { "content-type": "application/json" },
          body: JSON.stringify({
            model: MODEL,
            messages: request,
            stream: true,
            max_tokens: MAX_TOKENS[effort],
            chat_template_kwargs: chatTemplateKwargs,
          }),
        });

        if (!response.ok) {
          const body = await response.text();
          let message = body;
          try { message = JSON.parse(body).error?.message || body; } catch {}
          throw new Error(message || `HTTP ${response.status}`);
        }

        const reader = response.body.getReader();
        const decoder = new TextDecoder();
        let buffer = "";

        while (true) {
          const { value, done } = await reader.read();
          buffer += decoder.decode(value || new Uint8Array(), { stream: !done });
          const lines = buffer.split("\n");
          buffer = lines.pop() || "";
          for (const line of lines) applyEvent(line.trim(), target);
          if (done) break;
        }
        if (buffer.trim()) applyEvent(buffer.trim(), target);

        target.content.classList.remove("pending");
        if (!target.content.textContent) target.content.textContent = "(no final answer returned)";
        messages.push({ role: "assistant", content: target.content.textContent });
        status.textContent = "Complete. Later replies are fast while the worker stays warm.";
      } catch (error) {
        target.content.classList.remove("pending");
        target.element.classList.add("error");
        target.content.textContent = `Request failed: ${error.message}`;
        status.textContent = "The inference service did not complete the request.";
      } finally {
        status.classList.remove("busy");
        send.disabled = false;
        prompt.focus();
      }
    });

    addMessage("assistant", INTRO);
    prompt.focus();
  </script>
</body>
</html>