aha 0.2.6

aha model inference library, now supports Qwen(2.5VL/3/3VL/3.5/ASR/3Embedding/3Reranker), MiniCPM(4/5), VoxCPM(0.5B/1.5/2), DeepSeek-OCR/2, Hunyuan-OCR, PaddleOCR-VL/1.5, RMBG2.0, GLM(ASR-Nano-2512/OCR), Fun-ASR-Nano-2512, LFM(2/2.5/2VL/2.5VL)
Documentation
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
import { useState } from "react"
import { BookOpen, Copy, Check, Code, Terminal, FileType } from "lucide-react"
import { Card } from "@/components/ui/card"
import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"
import { Header } from "@/components/layout/header"
import { Main } from "@/components/layout/main"
import { ProfileDropdown } from "@/components/profile-dropdown"
import { ThemeSwitch } from "@/components/theme-switch"

interface CodeBlockProps {
  code: string
  language?: string
}

function CodeBlock({ code, language = "bash" }: CodeBlockProps) {
  const [copied, setCopied] = useState(false)

  const handleCopy = async () => {
    await navigator.clipboard.writeText(code)
    setCopied(true)
    setTimeout(() => setCopied(false), 2000)
  }

  const langIcons: Record<string, React.ReactNode> = {
    bash: <Terminal className="w-3.5 h-3.5" />,
    python: <FileType className="w-3.5 h-3.5" />,
    typescript: <Code className="w-3.5 h-3.5" />,
  }

  return (
    <div className="relative group">
      <div className="flex items-center justify-between px-4 py-1.5 bg-muted/50 border-b border-border/50 rounded-t-lg">
        <div className="flex items-center gap-1.5 text-xs text-muted-foreground">
          {langIcons[language] || null}
          {language}
        </div>
        <button
          onClick={handleCopy}
          className="flex items-center gap-1 text-xs text-muted-foreground hover:text-foreground transition-colors"
        >
          {copied ? (
            <>
              <Check className="w-3.5 h-3.5 text-green-500" />
              已复制
            </>
          ) : (
            <>
              <Copy className="w-3.5 h-3.5" />
              复制
            </>
          )}
        </button>
      </div>
      <pre className="bg-[#1e1e2e] text-gray-300 p-4 rounded-b-lg overflow-x-auto text-sm leading-relaxed">
        <code>{code}</code>
      </pre>
    </div>
  )
}

function ApiSection({
  title,
  method,
  path,
  description,
  children,
}: {
  title: string
  method: string
  path: string
  description: string
  children: React.ReactNode
}) {
  const methodColors: Record<string, string> = {
    GET: "bg-green-500/10 text-green-600 border-green-200 dark:border-green-800 dark:text-green-400",
    POST: "bg-blue-500/10 text-blue-600 border-blue-200 dark:border-blue-800 dark:text-blue-400",
  }

  return (
    <div className="space-y-3">
      <h3 className="text-base font-semibold">{title}</h3>
      <p className="text-sm text-muted-foreground">{description}</p>
      <div className="flex items-center gap-2 font-mono text-sm">
        <span className={`px-2 py-0.5 rounded border text-xs font-medium ${methodColors[method] || ""}`}>
          {method}
        </span>
        <span className="text-foreground">{path}</span>
      </div>
      {children}
    </div>
  )
}

export function UsagePage() {
  const [baseUrl, setBaseUrl] = useState("http://127.0.0.1:10100")

  return (
    <>
      <Header>
        <div className="flex items-center gap-2 ms-auto">
          <ThemeSwitch />
          <ProfileDropdown />
        </div>
      </Header>

      <Main>
        <div className="mb-6">
          <h1 className="text-2xl font-bold tracking-tight flex items-center gap-2">
            <BookOpen className="w-6 h-6" />
            API 使用指南
          </h1>
          <p className="text-muted-foreground text-sm mt-1">
            如何通过 HTTP 接口调用本地模型推理服务
          </p>
        </div>

        {/* 服务地址配置 */}
        <Card className="p-4 mb-6">
          <div className="flex items-end gap-4">
            <div className="flex-1 space-y-2">
              <Label htmlFor="base-url">服务地址</Label>
              <Input
                id="base-url"
                value={baseUrl}
                onChange={(e) => setBaseUrl(e.target.value)}
                placeholder="http://127.0.0.1:10100"
              />
            </div>
            <p className="text-xs text-muted-foreground pb-1">
              请确保服务已启动,地址与「启动服务」页面配置一致
            </p>
          </div>
        </Card>

        <div className="space-y-8">
          {/* 1. 查看可用模型 */}
          <Card className="p-5 space-y-4">
            <ApiSection
              title="查看可用模型"
              method="GET"
              path="/v1/models"
              description="获取当前服务中可用的模型列表"
            >
              <div className="space-y-3">
                <CodeBlock
                  language="bash"
                  code={`curl ${baseUrl}/v1/models`}
                />
                <CodeBlock
                  language="python"
                  code={`import requests

response = requests.get("${baseUrl}/v1/models")
models = response.json()
print(models)`}
                />
                <CodeBlock
                  language="typescript"
                  code={`const response = await fetch("${baseUrl}/v1/models")
const models = await response.json()
console.log(models)`}
                />
              </div>
            </ApiSection>
          </Card>

          {/* 2. Chat Completions */}
          <Card className="p-5 space-y-4">
            <ApiSection
              title="对话补全"
              method="POST"
              path="/v1/chat/completions"
              description="向模型发送对话消息,获取推理回复。兼容 OpenAI API 格式。"
            >
              <div className="space-y-3">
                <CodeBlock
                  language="bash"
                  code={`curl ${baseUrl}/v1/chat/completions \\
  -H "Content-Type: application/json" \\
  -d '{
    "model": "your-model-id",
    "messages": [
      {"role": "system", "content": "你是一个有用的助手"},
      {"role": "user", "content": "你好,请介绍一下你自己"}
    ],
    "temperature": 0.7,
    "max_tokens": 2048,
    "stream": false
  }'`}
                />
                <CodeBlock
                  language="python"
                  code={`import requests

response = requests.post(
    "${baseUrl}/v1/chat/completions",
    json={
        "model": "your-model-id",
        "messages": [
            {"role": "system", "content": "你是一个有用的助手"},
            {"role": "user", "content": "你好,请介绍一下你自己"}
        ],
        "temperature": 0.7,
        "max_tokens": 2048,
        "stream": False,
    },
)
result = response.json()
print(result["choices"][0]["message"]["content"])`}
                />
                <CodeBlock
                  language="typescript"
                  code={`const response = await fetch("${baseUrl}/v1/chat/completions", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({
    model: "your-model-id",
    messages: [
      { role: "system", content: "你是一个有用的助手" },
      { role: "user", content: "你好,请介绍一下你自己" }
    ],
    temperature: 0.7,
    max_tokens: 2048,
    stream: false,
  }),
})
const result = await response.json()
console.log(result.choices[0].message.content)`}
                />
              </div>

              <div className="mt-4 p-3 bg-muted/50 rounded-md text-sm space-y-2">
                <p className="font-medium">请求参数说明</p>
                <table className="w-full text-xs">
                  <thead>
                    <tr className="border-b">
                      <th className="text-left py-1 pr-2">参数</th>
                      <th className="text-left py-1 pr-2">类型</th>
                      <th className="text-left py-1">说明</th>
                    </tr>
                  </thead>
                  <tbody>
                    <tr className="border-b border-muted">
                      <td className="py-1 pr-2 font-mono">model</td>
                      <td className="py-1 pr-2">string</td>
                      <td className="py-1">模型 ID</td>
                    </tr>
                    <tr className="border-b border-muted">
                      <td className="py-1 pr-2 font-mono">messages</td>
                      <td className="py-1 pr-2">array</td>
                      <td className="py-1">对话消息列表</td>
                    </tr>
                    <tr className="border-b border-muted">
                      <td className="py-1 pr-2 font-mono">temperature</td>
                      <td className="py-1 pr-2">number</td>
                      <td className="py-1">采样温度 (0~2),默认 1.0</td>
                    </tr>
                    <tr className="border-b border-muted">
                      <td className="py-1 pr-2 font-mono">max_tokens</td>
                      <td className="py-1 pr-2">number</td>
                      <td className="py-1">最大生成 token 数</td>
                    </tr>
                    <tr>
                      <td className="py-1 pr-2 font-mono">stream</td>
                      <td className="py-1 pr-2">boolean</td>
                      <td className="py-1">是否流式输出,默认 false</td>
                    </tr>
                  </tbody>
                </table>
              </div>
            </ApiSection>
          </Card>

          {/* 3. 流式输出 */}
          <Card className="p-5 space-y-4">
            <ApiSection
              title="流式对话"
              method="POST"
              path="/v1/chat/completions (stream)"
              description="使用 SSE (Server-Sent Events) 实现流式输出,逐 token 返回推理结果。"
            >
              <div className="space-y-3">
                <CodeBlock
                  language="bash"
                  code={`curl ${baseUrl}/v1/chat/completions \\
  -H "Content-Type: application/json" \\
  -d '{
    "model": "your-model-id",
    "messages": [
      {"role": "user", "content": "用 Python 写一个递归遍历目录的例子"}
    ],
    "stream": true
  }'`}
                />
                <CodeBlock
                  language="python"
                  code={`import requests

response = requests.post(
    "${baseUrl}/v1/chat/completions",
    json={
        "model": "your-model-id",
        "messages": [
            {"role": "user", "content": "用 Python 写一个递归遍历目录的例子"}
        ],
        "stream": True,
    },
    stream=True,
)
for line in response.iter_lines():
    if line:
        text = line.decode("utf-8").removeprefix("data: ")
        if text != "[DONE]":
            import json
            chunk = json.loads(text)
            delta = chunk["choices"][0].get("delta", {}).get("content", "")
            print(delta, end="", flush=True)`}
                />
                <CodeBlock
                  language="typescript"
                  code={`const response = await fetch("${baseUrl}/v1/chat/completions", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({
    model: "your-model-id",
    messages: [
      { role: "user", content: "用 Python 写一个递归遍历目录的例子" }
    ],
    stream: true,
  }),
})

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

while (true) {
  const { done, value } = await reader.read()
  if (done) break

  buffer += decoder.decode(value, { stream: true })
  const lines = buffer.split("\\n")
  buffer = lines.pop() || ""

  for (const line of lines) {
    const text = line.replace(/^data: /, "").trim()
    if (!text || text === "[DONE]") continue

    const chunk = JSON.parse(text)
    const content = chunk.choices?.[0]?.delta?.content || ""
    process.stdout.write(content)
  }
}`}
                />
              </div>
            </ApiSection>
          </Card>

          {/* 4. 使用 OpenAI SDK */}
          <Card className="p-5 space-y-4">
            <h3 className="text-base font-semibold">使用 OpenAI SDK 调用</h3>
            <p className="text-sm text-muted-foreground">
              由于服务兼容 OpenAI API 格式,你可以直接使用 OpenAI 官方 SDK,只需修改 base URL 即可。
            </p>
            <div className="space-y-3">
              <CodeBlock
                language="python"
                code={`from openai import OpenAI

client = OpenAI(
    base_url="${baseUrl}/v1",
    api_key="not-needed",  # 本地服务不需要 API Key
)

response = client.chat.completions.create(
    model="your-model-id",
    messages=[
        {"role": "user", "content": "你好"}
    ],
    stream=True,
)

for chunk in response:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="")`}
              />
              <CodeBlock
                language="typescript"
                code={`import OpenAI from "openai"

const client = new OpenAI({
  baseURL: "${baseUrl}/v1",
  apiKey: "not-needed",
})

const stream = await client.chat.completions.create({
  model: "your-model-id",
  messages: [{ role: "user", content: "你好" }],
  stream: true,
})

for await (const chunk of stream) {
  const content = chunk.choices[0]?.delta?.content || ""
  process.stdout.write(content)
}`}
              />
            </div>
          </Card>

          {/* 5. 健康检查 */}
          <Card className="p-5 space-y-4">
            <ApiSection
              title="健康检查"
              method="GET"
              path="/health"
              description="检查服务运行状态"
            >
              <CodeBlock
                language="bash"
                code={`curl ${baseUrl}/health`}
              />
            </ApiSection>
          </Card>
        </div>
      </Main>
    </>
  )
}