File size: 11,816 Bytes
d0bf6e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!doctype html>
<html lang="ja">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="data:image/svg+xml,%3Csvg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%2270 90 846 846%22%3E%3Crect x=%22332%22 y=%22256%22 width=%22512%22 height=%22511%22 rx=%2280%22 fill=%22%231868f0%22/%3E%3Cline x1=%22181%22 y1=%22360%22 x2=%22208%22 y2=%22360%22 stroke=%22%231868f0%22 stroke-width=%2238%22 stroke-linecap=%22round%22/%3E%3Cline x1=%22181%22 y1=%22664%22 x2=%22212%22 y2=%22664%22 stroke=%22%231868f0%22 stroke-width=%2238%22 stroke-linecap=%22round%22/%3E%3Cline x1=%22436%22 y1=%22212%22 x2=%22436%22 y2=%22300%22 stroke=%22%231868f0%22 stroke-width=%2238%22 stroke-linecap=%22round%22/%3E%3Cline x1=%22436%22 y1=%22815%22 x2=%22436%22 y2=%22720%22 stroke=%22%231868f0%22 stroke-width=%2238%22 stroke-linecap=%22round%22/%3E%3Cline x1=%22512%22 y1=%22212%22 x2=%22512%22 y2=%22300%22 stroke=%22%231868f0%22 stroke-width=%2238%22 stroke-linecap=%22round%22/%3E%3Cline x1=%22512%22 y1=%22815%22 x2=%22512%22 y2=%22720%22 stroke=%22%231868f0%22 stroke-width=%2238%22 stroke-linecap=%22round%22/%3E%3Cline x1=%22588%22 y1=%22212%22 x2=%22588%22 y2=%22300%22 stroke=%22%231868f0%22 stroke-width=%2238%22 stroke-linecap=%22round%22/%3E%3Cline x1=%22588%22 y1=%22815%22 x2=%22588%22 y2=%22720%22 stroke=%22%231868f0%22 stroke-width=%2238%22 stroke-linecap=%22round%22/%3E%3Cline x1=%22664%22 y1=%22212%22 x2=%22664%22 y2=%22300%22 stroke=%22%231868f0%22 stroke-width=%2238%22 stroke-linecap=%22round%22/%3E%3Cline x1=%22664%22 y1=%22815%22 x2=%22664%22 y2=%22720%22 stroke=%22%231868f0%22 stroke-width=%2238%22 stroke-linecap=%22round%22/%3E%3Cline x1=%22740%22 y1=%22212%22 x2=%22740%22 y2=%22300%22 stroke=%22%231868f0%22 stroke-width=%2238%22 stroke-linecap=%22round%22/%3E%3Cline x1=%22740%22 y1=%22815%22 x2=%22740%22 y2=%22720%22 stroke=%22%231868f0%22 stroke-width=%2238%22 stroke-linecap=%22round%22/%3E%3Cline x1=%22891%22 y1=%22360%22 x2=%22800%22 y2=%22360%22 stroke=%22%231868f0%22 stroke-width=%2238%22 stroke-linecap=%22round%22/%3E%3Cline x1=%22891%22 y1=%22436%22 x2=%22800%22 y2=%22436%22 stroke=%22%231868f0%22 stroke-width=%2238%22 stroke-linecap=%22round%22/%3E%3Cline x1=%22891%22 y1=%22512%22 x2=%22800%22 y2=%22512%22 stroke=%22%231868f0%22 stroke-width=%2238%22 stroke-linecap=%22round%22/%3E%3Cline x1=%22891%22 y1=%22588%22 x2=%22800%22 y2=%22588%22 stroke=%22%231868f0%22 stroke-width=%2238%22 stroke-linecap=%22round%22/%3E%3Cline x1=%22891%22 y1=%22664%22 x2=%22800%22 y2=%22664%22 stroke=%22%231868f0%22 stroke-width=%2238%22 stroke-linecap=%22round%22/%3E%3Cline x1=%22268%22 y1=%22360%22 x2=%22380%22 y2=%22360%22 stroke=%22%231868f0%22 stroke-width=%2238%22 stroke-linecap=%22round%22/%3E%3Cline x1=%22124%22 y1=%22436%22 x2=%22380%22 y2=%22436%22 stroke=%22%231868f0%22 stroke-width=%2238%22 stroke-linecap=%22round%22/%3E%3Cline x1=%22209%22 y1=%22512%22 x2=%22380%22 y2=%22512%22 stroke=%22%231868f0%22 stroke-width=%2238%22 stroke-linecap=%22round%22/%3E%3Cline x1=%2295%22 y1=%22588%22 x2=%22380%22 y2=%22588%22 stroke=%22%231868f0%22 stroke-width=%2238%22 stroke-linecap=%22round%22/%3E%3Cline x1=%22275%22 y1=%22664%22 x2=%22380%22 y2=%22664%22 stroke=%22%231868f0%22 stroke-width=%2238%22 stroke-linecap=%22round%22/%3E%3Cellipse cx=%22588%22 cy=%22512%22 rx=%22120%22 ry=%22117%22 fill=%22%23fff%22/%3E%3Cellipse cx=%22588%22 cy=%22512%22 rx=%2257%22 ry=%2267%22 fill=%22%231868f0%22/%3E%3C/svg%3E">
<title>onw AMD 検証ページ</title>
<script>/*i18n*/</script>
<style>
:root { --bg: #f6f7f9; --fg: #1d2330; --muted: #5d6675; --card: #fff; --line: #dfe3ea; --ok: #1f8a4c; --bad: #c0392b; --accent: #2f6fde; }
@media (prefers-color-scheme: dark) { :root { --bg: #14171d; --fg: #e5e9f0; --muted: #9aa3b2; --card: #1c2029; --line: #2b313d; --ok: #3fbf74; --bad: #e0685a; --accent: #6b9cf0; } }
* { box-sizing: border-box; }
body { margin: 0; font: 15px/1.6 system-ui, "Segoe UI", "Hiragino Sans", "Noto Sans JP", sans-serif; background: var(--bg); color: var(--fg); }
main { max-width: 920px; margin: 0 auto; padding: 24px 16px 48px; }
h1 { font-size: 22px; margin: 0 0 4px; }
.sub { color: var(--muted); margin: 0 0 18px; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 14px 16px; margin-bottom: 14px; }
code { background: rgba(127,127,127,.15); padding: 1px 5px; border-radius: 4px; }
button { font: inherit; padding: 8px 16px; border-radius: 8px; border: 1px solid var(--accent); background: var(--accent); color: #fff; cursor: pointer; }
button:disabled { opacity: .5; cursor: default; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 7px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { color: var(--muted); font-weight: 600; font-size: 13px; }
td.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.ok { color: var(--ok); } .bad { color: var(--bad); }
.ans { color: var(--muted); font-size: 13px; max-width: 420px; }
#info td:first-child { color: var(--muted); width: 180px; }
</style>
</head>
<body>
<main>
  <h1>onw AMD 検証ページ</h1>
  <p class="sub">このサーバー(OpenAI互換API)に実際のリクエストを送って、動作と速度を確かめます。</p>

  <div class="card">
    <table id="info"><tbody><tr><td>読み込み中…</td><td></td></tr></tbody></table>
  </div>

  <div class="card">
    <button id="run">検証を実行</button>
    <span id="status" class="sub" style="margin-left:10px"></span>
    <table style="margin-top:12px">
      <thead><tr><th>項目</th><th>結果</th><th>プロンプト</th><th>応答開始</th><th>生成速度</th><th>回答</th></tr></thead>
      <tbody id="rows"></tbody>
    </table>
  </div>

  <div class="card sub" data-t>他のアプリからは <code id="base"></code> を OpenAI の base URL に、モデル名は <code id="mid"></code> を指定してください(APIキー設定時は <code>Authorization: Bearer キー</code>)。</div>
</main>
<script>
translate(document.body);
const $ = id => document.getElementById(id);
let INFO = {}, KEY = new URLSearchParams(location.search).get("key") || "";
const TEXT = T("東京は日本の首都であり、政治・経済・文化の中心地である。江戸時代には徳川幕府が置かれ、1868年の明治維新で東京と改称された。現在は約1400万人が暮らす世界有数の大都市で、多くの企業や大学が集まっている。");

// streamed, so the first-token time and the generation speed are measured here (the same for every NPU engine)
async function api(messages, max_tokens) {
  const t0 = performance.now();
  const r = await fetch("/v1/chat/completions", {
    method: "POST",
    headers: Object.assign({ "Content-Type": "application/json" }, KEY ? { Authorization: "Bearer " + KEY } : {}),
    body: JSON.stringify({ model: INFO.model, messages, max_tokens, temperature: 0, stream: true, stream_options: { include_usage: true } })
  });
  if (!r.ok) throw new Error((await r.text()).slice(0, 200));
  const reader = r.body.getReader(), dec = new TextDecoder();
  let buf = "", text = "", usage = null, chunks = 0, first = 0, last = 0;
  for (;;) {
    const { value, done } = await reader.read(); if (done) break;
    buf += dec.decode(value, { stream: true });
    let i;
    while ((i = buf.indexOf("\n")) >= 0) {
      const line = buf.slice(0, i).trim(); buf = buf.slice(i + 1);
      if (!line.startsWith("data:")) continue;
      const data = line.slice(5).trim(); if (data === "[DONE]") continue;
      const j = JSON.parse(data);
      if (j.error) throw new Error(j.error.message);
      const d = j.choices?.[0]?.delta?.content || j.choices?.[0]?.delta?.reasoning_content;
      if (d) { if (!first) first = performance.now(); last = performance.now(); chunks++; text += j.choices[0].delta.content || ""; }
      if (j.usage) usage = j.usage;
    }
  }
  const n = usage ? usage.completion_tokens : chunks;
  return { choices: [{ message: { content: text } }], usage: usage || { prompt_tokens: "—", completion_tokens: chunks },
           ttft: ((first || performance.now()) - t0) / 1000, tps: n > 1 && last > first ? (n - 1) / ((last - first) / 1000) : 0 };
}

function row(name, j, err) {
  const tr = document.createElement("tr");
  if (err) {
    tr.innerHTML = `<td>${name}</td><td class="bad">${T("失敗")}</td><td colspan="4" class="ans">${err}</td>`;
  } else {
    const u = j.usage || {};
    const cached = u.prompt_tokens_details && u.prompt_tokens_details.cached_tokens ? T("(再利用 {0})", u.prompt_tokens_details.cached_tokens) : "";
    const ans = (j.choices[0].message.content || "").replace(/[&<>]/g, c => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;" }[c]));
    tr.innerHTML = `<td>${name}</td><td class="ok">OK</td><td class="num">${u.prompt_tokens}${cached}</td>` +
      `<td class="num">${T("{0}秒", j.ttft.toFixed(2))}</td>` +
      `<td class="num">${j.tps.toFixed(1)} tok/s</td><td class="ans">${ans}</td>`;
  }
  $("rows").appendChild(tr);
}

function testImage() {                      // drawn here, so no image file is needed
  const c = document.createElement("canvas"); c.width = c.height = 512;
  const g = c.getContext("2d");
  g.fillStyle = "#ffffff"; g.fillRect(0, 0, 512, 512);
  g.fillStyle = "#e53935"; g.beginPath(); g.arc(150, 170, 90, 0, Math.PI * 2); g.fill();
  g.fillStyle = "#1e88e5"; g.fillRect(290, 90, 160, 160);
  g.fillStyle = "#111111"; g.font = "bold 96px sans-serif"; g.fillText("NPU", 150, 430);
  return c.toDataURL("image/png");
}

async function run() {
  $("run").disabled = true; $("rows").innerHTML = "";
  const steps = [
    [T("テキスト生成"), () => api([{ role: "user", content: T("日本の四季について、それぞれ一文ずつ説明してください。") }], 160)],
    [T("要約"), () => api([{ role: "user", content: T("次の文章を、元の表現をなるべく使って2文に要約してください。") + "\n\n" + TEXT }], 120)],
  ];
  const t1 = [{ role: "user", content: T("日本の首都はどこですか?一文で。") }];
  steps.push([T("2ターン会話: 1ターン目"), async () => { const j = await api(t1, 60); t1.push({ role: "assistant", content: j.choices[0].message.content }); return j; }]);
  steps.push([T("2ターン会話: 2ターン目"), () => api(t1.concat([{ role: "user", content: T("その都市の人口は?一文で。") }]), 60)]);
  if (INFO.vision) steps.push([T("画像入力"), () => api([{ role: "user", content: [
      { type: "image_url", image_url: { url: testImage() } },
      { type: "text", text: T("この画像に描かれている図形と文字を、色も含めて一文で答えてください。") }] }], 80)]);
  for (const [name, f] of steps) {
    $("status").textContent = name + " …";
    try { row(name, await f()); } catch (e) { row(name, null, e.message); }
  }
  $("status").textContent = T("完了"); $("run").disabled = false;
}

fetch("/api/info").then(r => r.json()).then(i => {
  INFO = i;
  const rows = [[T("モデル名"), i.model], [T("デバイス"), i.npu || "NPU"],
    [T("NPU エンジン"), (i.engine_name || "") + " " + (i.engine_version || "")],
    [T("コンテキスト長"), T("{0} トークン", i.context || i.max_context)], [T("画像入力"), i.vision ? T("対応") : T("なし")],
    [T("思考モード"), i.think ? T("対応") : T("なし")]];
  $("info").innerHTML = rows.map(([k, v]) => `<tr><td>${k}</td><td>${v}</td></tr>`).join("");
  $("base").textContent = location.origin + "/v1"; $("mid").textContent = i.model;
}).catch(() => { $("info").innerHTML = `<tr><td class='bad'>${T("サーバーに接続できません")}</td></tr>`; });
$("run").onclick = run;
</script>
</body>
</html>