vue-llm 1.2.0 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.es.md +401 -0
- package/README.ja.md +401 -0
- package/README.md +48 -14
- package/README.pt-BR.md +401 -0
- package/README.ru.md +401 -0
- package/README.zh-CN.md +401 -0
- package/dist/components/MessageParts.vue.d.ts +33 -0
- package/dist/components.cjs +5 -5
- package/dist/components.d.ts +1 -0
- package/dist/components.js +146 -75
- package/dist/composables/use-auto-scroll.d.ts +17 -0
- package/dist/composables/use-chat.d.ts +11 -5
- package/dist/composables/use-completion.d.ts +1 -1
- package/dist/composables/use-object.d.ts +1 -1
- package/dist/composables/use-prompt-input.d.ts +51 -0
- package/dist/core/devtools.d.ts +86 -0
- package/dist/core/partial-json.d.ts +6 -0
- package/dist/core/stream-processor.d.ts +4 -0
- package/dist/devtools-C4JE26vO.cjs +1 -0
- package/dist/devtools-DnDvylCG.js +87 -0
- package/dist/devtools.cjs +1 -0
- package/dist/devtools.d.ts +15 -0
- package/dist/devtools.js +210 -0
- package/dist/index.cjs +3 -3
- package/dist/index.d.ts +7 -1
- package/dist/index.js +576 -441
- package/dist/nuxt.cjs +2 -1
- package/dist/nuxt.d.ts +4 -0
- package/dist/nuxt.js +34 -11
- package/dist/providers/webllm.d.ts +87 -0
- package/dist/providers.cjs +5 -4
- package/dist/providers.d.ts +2 -0
- package/dist/providers.js +529 -275
- package/dist/server.d.ts +4 -0
- package/dist/types.d.ts +37 -1
- package/dist/utils/browser.d.ts +1 -0
- package/dist/vue-llm.css +1 -1
- package/package.json +44 -5
package/README.ja.md
ADDED
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
# vue-llm
|
|
2
|
+
|
|
3
|
+
[English](./README.md) · [Русский](./README.ru.md) · [简体中文](./README.zh-CN.md) · [Português](./README.pt-BR.md) · [Español](./README.es.md) · **日本語**
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/vue-llm)
|
|
6
|
+
[](https://www.npmjs.com/package/vue-llm)
|
|
7
|
+
[](https://github.com/dadashi44/vue-llm/actions/workflows/ci.yml)
|
|
8
|
+
[](./LICENSE)
|
|
9
|
+
|
|
10
|
+
**vue-llm** は Vue と Nuxt のための AI SDK です。ストリーミング対応の `useChat`、ツール呼び出し、Generative UI、ブラウザ内モデルを、OpenAI、Anthropic、Gemini、Ollama、WebGPU 向けに提供します。
|
|
11
|
+
|
|
12
|
+
ストリーミング、ツール呼び出し、マルチプロバイダー対応の `useChat()` を、わずか 10 行のコードで。
|
|
13
|
+
|
|
14
|
+
**ドキュメント:** https://vue-llm.js.org/ja/ · **[ブラウザーで試す](https://vue-llm.js.org/ja/playground)** — API キーは不要です
|
|
15
|
+
|
|
16
|
+
## 機能
|
|
17
|
+
|
|
18
|
+
- **`useChat()`** — ストリーミング、中断、再生成に対応したリアクティブなコンポーザブル
|
|
19
|
+
- **マルチプロバイダー** — OpenAI、Anthropic、Gemini、Ollama にすぐに対応
|
|
20
|
+
- **ブラウザ内モデル** — `createWebLLM` で Llama、Qwen、SmolLM を WebGPU 上で実行。サーバーも API キーも不要
|
|
21
|
+
- **Generative UI** — `<MessageParts>` でツール呼び出しを独自の Vue コンポーネントとして描画。引数のストリーミングと human-in-the-loop に対応
|
|
22
|
+
- **ツール呼び出し** — `execute` 付きの `defineTool`、`onToolCall`、`addToolResult`
|
|
23
|
+
- **添付ファイル** — 画像、PDF、音声、テキストファイルを、プロバイダーごとの形式に変換
|
|
24
|
+
- **永続化** — localStorage や任意のカスタムバックエンドからチャット履歴を復元
|
|
25
|
+
- **耐障害性** — 指数バックオフによるリトライ、クライアントとサーバーでのレート制限
|
|
26
|
+
- **プロンプトテンプレート** — 型推論付きの `{{variables}}`、チャットテンプレート、リアクティブな `usePrompt`
|
|
27
|
+
- **あらゆるバックエンドに対応** — `createLLMHandler` によるサーバーサイドのツール、独自の SSE API 向けの `createSSEProvider`
|
|
28
|
+
- **ストリーミング** — SSE パーサー、テキスト・推論・ツール呼び出しの差分
|
|
29
|
+
- **セキュリティ** — `dangerouslyAllowBrowser` ガードで API キーを保護
|
|
30
|
+
- **チャット UI** — 自分で所有できる完全なチャット:`npx shadcn-vue add https://vue-llm.js.org/r/chat.json`。ヘッドレスな `usePromptInput` と `useAutoScroll` も利用可能
|
|
31
|
+
- **Vue DevTools** — すべてのチャット、リクエストのタイムライン、最初のトークンまでの時間、ツール呼び出し、トークン使用量を表示する LLM タブ(`vue-llm/devtools`)
|
|
32
|
+
- **Nuxt モジュール** — 自動インポート、コンポーネント、開発時の DevTools、サーバーサイドの runtime config
|
|
33
|
+
- **ランタイム依存ゼロ** — peer dependency は `vue` のみ(Vue 3.3 以降。コンポーザブルは Vue 2.7 でも動作)
|
|
34
|
+
- **TypeScript** — 完全な型安全性、型もエクスポート
|
|
35
|
+
|
|
36
|
+
## クイックスタート
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
npm install vue-llm
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
```vue
|
|
43
|
+
<script setup>
|
|
44
|
+
import { useChat } from 'vue-llm'
|
|
45
|
+
import { createOpenAI } from 'vue-llm/providers'
|
|
46
|
+
|
|
47
|
+
const ai = createOpenAI({
|
|
48
|
+
apiKey: 'sk-...',
|
|
49
|
+
dangerouslyAllowBrowser: true, // only for prototyping
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
const { messages, input, handleSubmit, isLoading } = useChat({
|
|
53
|
+
provider: ai,
|
|
54
|
+
model: 'gpt-4o',
|
|
55
|
+
})
|
|
56
|
+
</script>
|
|
57
|
+
|
|
58
|
+
<template>
|
|
59
|
+
<div v-for="msg in messages" :key="msg.id">
|
|
60
|
+
<strong>{{ msg.role }}:</strong>
|
|
61
|
+
<span v-for="(part, i) in msg.parts" :key="i">
|
|
62
|
+
<template v-if="part.type === 'text'">{{ part.text }}</template>
|
|
63
|
+
</span>
|
|
64
|
+
</div>
|
|
65
|
+
|
|
66
|
+
<form @submit.prevent="handleSubmit">
|
|
67
|
+
<input v-model="input" placeholder="Ask something..." />
|
|
68
|
+
<button :disabled="isLoading">Send</button>
|
|
69
|
+
</form>
|
|
70
|
+
</template>
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## プロバイダー
|
|
74
|
+
|
|
75
|
+
### OpenAI
|
|
76
|
+
|
|
77
|
+
```ts
|
|
78
|
+
import { createOpenAI } from 'vue-llm/providers'
|
|
79
|
+
|
|
80
|
+
const ai = createOpenAI({
|
|
81
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
82
|
+
})
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### Anthropic
|
|
86
|
+
|
|
87
|
+
```ts
|
|
88
|
+
import { createAnthropic } from 'vue-llm/providers'
|
|
89
|
+
|
|
90
|
+
const ai = createAnthropic({
|
|
91
|
+
apiKey: process.env.ANTHROPIC_API_KEY,
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
const { messages, sendMessage } = useChat({
|
|
95
|
+
provider: ai,
|
|
96
|
+
model: 'claude-sonnet-4-5',
|
|
97
|
+
})
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Ollama(ローカル)
|
|
101
|
+
|
|
102
|
+
```ts
|
|
103
|
+
import { createOllama } from 'vue-llm/providers'
|
|
104
|
+
|
|
105
|
+
const ai = createOllama() // localhost:11434 by default
|
|
106
|
+
|
|
107
|
+
const { messages, sendMessage } = useChat({
|
|
108
|
+
provider: ai,
|
|
109
|
+
model: 'llama3',
|
|
110
|
+
})
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### ブラウザー内(WebGPU)
|
|
114
|
+
|
|
115
|
+
```ts
|
|
116
|
+
import { CreateMLCEngine } from '@mlc-ai/web-llm' // npm i @mlc-ai/web-llm
|
|
117
|
+
import { createWebLLM } from 'vue-llm/providers'
|
|
118
|
+
|
|
119
|
+
const ai = createWebLLM({
|
|
120
|
+
createEngine: CreateMLCEngine,
|
|
121
|
+
onProgress: ({ progress }) => console.log(`${Math.round(progress * 100)}%`),
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
const { messages, sendMessage } = useChat({
|
|
125
|
+
provider: ai,
|
|
126
|
+
model: 'Llama-3.2-1B-Instruct-q4f16_1-MLC',
|
|
127
|
+
})
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
モデルはユーザーの GPU 上で動作し、データがデバイスの外に出ることはありません。詳しくは [ブラウザ内モデル](https://vue-llm.js.org/ja/guide/in-browser) を参照してください。
|
|
131
|
+
|
|
132
|
+
## ツール呼び出し
|
|
133
|
+
|
|
134
|
+
```ts
|
|
135
|
+
const { messages, sendMessage } = useChat({
|
|
136
|
+
provider: ai,
|
|
137
|
+
model: 'gpt-4o',
|
|
138
|
+
tools: [
|
|
139
|
+
{
|
|
140
|
+
name: 'get_weather',
|
|
141
|
+
description: 'Get weather for a city',
|
|
142
|
+
parameters: {
|
|
143
|
+
type: 'object',
|
|
144
|
+
properties: {
|
|
145
|
+
city: { type: 'string', description: 'City name' },
|
|
146
|
+
},
|
|
147
|
+
required: ['city'],
|
|
148
|
+
},
|
|
149
|
+
},
|
|
150
|
+
],
|
|
151
|
+
onToolCall: async ({ toolName, args }) => {
|
|
152
|
+
if (toolName === 'get_weather') {
|
|
153
|
+
return { temperature: 22, condition: 'sunny' }
|
|
154
|
+
}
|
|
155
|
+
},
|
|
156
|
+
maxToolRoundtrips: 3,
|
|
157
|
+
})
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## 添付ファイル
|
|
161
|
+
|
|
162
|
+
`sendMessage` に `File`、`FileList`、`FilePart` オブジェクトを渡します。ファイルは data URL として読み込まれ、ユーザーメッセージの `file` パーツとして保存されます。
|
|
163
|
+
|
|
164
|
+
```vue
|
|
165
|
+
<script setup lang="ts">
|
|
166
|
+
const { messages, input, sendMessage } = useChat({ provider: ai, model: 'gpt-4o' })
|
|
167
|
+
const files = ref<FileList>()
|
|
168
|
+
|
|
169
|
+
async function submit() {
|
|
170
|
+
await sendMessage({ text: input.value, files: files.value })
|
|
171
|
+
input.value = ''
|
|
172
|
+
files.value = undefined
|
|
173
|
+
}
|
|
174
|
+
</script>
|
|
175
|
+
|
|
176
|
+
<template>
|
|
177
|
+
<input type="file" multiple @change="files = ($event.target as HTMLInputElement).files ?? undefined">
|
|
178
|
+
<input v-model="input" @keydown.enter="submit">
|
|
179
|
+
</template>
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
リモート URL も使えます:`sendMessage({ files: { type: 'file', mediaType: 'image/png', url: 'https://…' } })`。
|
|
183
|
+
|
|
184
|
+
| プロバイダー | 画像 | PDF | 音声 | テキスト | リモート URL |
|
|
185
|
+
|----------|--------|-----|-------|------|-------------|
|
|
186
|
+
| OpenAI | ✓ | ✓ | wav, mp3 | テキストとしてインライン展開 | 画像のみ |
|
|
187
|
+
| Anthropic | ✓ | ✓ | — | ✓(document) | 画像、PDF |
|
|
188
|
+
| Google | ✓ | ✓ | ✓ | ✓ | `fileData` URI |
|
|
189
|
+
| Ollama | ✓ | — | — | テキストとしてインライン展開 | — |
|
|
190
|
+
|
|
191
|
+
サポートされていない組み合わせの場合は、リクエストを送信する前にわかりやすいエラーがスローされます。
|
|
192
|
+
|
|
193
|
+
## 永続化
|
|
194
|
+
|
|
195
|
+
`storage` アダプターと固定の `id` を渡すと、履歴は初期化時に復元され、レスポンスが完了するたびに保存されます。
|
|
196
|
+
|
|
197
|
+
```ts
|
|
198
|
+
import { useChat, createWebStorage } from 'vue-llm'
|
|
199
|
+
|
|
200
|
+
const { messages, setMessages, isRestored } = useChat({
|
|
201
|
+
id: 'support-chat',
|
|
202
|
+
provider: ai,
|
|
203
|
+
model: 'gpt-4o',
|
|
204
|
+
storage: createWebStorage(), // localStorage, key `vue-llm:chat:support-chat`
|
|
205
|
+
})
|
|
206
|
+
|
|
207
|
+
setMessages([]) // clears history and removes the stored entry
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
`createWebStorage({ prefix, storage: sessionStorage })` でキーと保存先をカスタマイズできます。テストには `createMemoryStorage()` が便利です。`ChatStorage` を実装したオブジェクトであれば、同期・非同期を問わず使えます。
|
|
211
|
+
|
|
212
|
+
```ts
|
|
213
|
+
const storage: ChatStorage = {
|
|
214
|
+
load: id => fetch(`/api/chats/${id}`).then(r => r.json()),
|
|
215
|
+
save: (id, messages) => fetch(`/api/chats/${id}`, { method: 'PUT', body: JSON.stringify(messages) }).then(() => {}),
|
|
216
|
+
remove: id => fetch(`/api/chats/${id}`, { method: 'DELETE' }).then(() => {}),
|
|
217
|
+
}
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
非同期ストレージの場合、`load` が解決されるまで `isRestored` は `false` です。それまでに送信されたメッセージは保持され、保存済みの履歴で上書きされることはありません。ストレージのエラーは `onError` に渡されます。添付ファイルは data URL として保存されるため、大きなファイルは localStorage の上限(約 5 MB)を超えることがあります。
|
|
221
|
+
|
|
222
|
+
## リトライとレート制限
|
|
223
|
+
|
|
224
|
+
```ts
|
|
225
|
+
import { wrapProvider, retryMiddleware, rateLimitMiddleware } from 'vue-llm'
|
|
226
|
+
|
|
227
|
+
const ai = wrapProvider(createOpenAI({ apiKey }), [
|
|
228
|
+
retryMiddleware({ maxRetries: 3, initialDelay: 1000, maxDelay: 30_000 }),
|
|
229
|
+
rateLimitMiddleware({ maxRequests: 20, interval: 60_000, maxConcurrent: 2 }),
|
|
230
|
+
])
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
`retryMiddleware` は、408、409、429、5xx、ネットワークエラーの際に、指数バックオフ(`initialDelay * factor^n`、ジッター付き)でリクエストを再送します。`Retry-After` / `retry-after-ms` も尊重します。`shouldRetry(error, attempt)` でカスタマイズでき、`onRetry` で監視できます。リトライされるのは最初のリクエストのみで、ストリームが始まった後のエラーはリトライされません。
|
|
234
|
+
|
|
235
|
+
`rateLimitMiddleware` はクライアントサイドでリクエストをキューに入れます。`interval` あたり最大 `maxRequests` 件、同時に開いているストリームは最大 `maxConcurrent` 本です(スロットはストリームが終了するかキャンセルされると解放されます)。キューで待機中のリクエストを中断すると、すぐに reject されます。
|
|
236
|
+
|
|
237
|
+
プロバイダーは `status`、`provider`、`body`、`retryAfter`、`isRetryable` を持つ `LLMAPIError` をスローします。
|
|
238
|
+
|
|
239
|
+
```ts
|
|
240
|
+
import { LLMAPIError } from 'vue-llm'
|
|
241
|
+
|
|
242
|
+
onError(error) {
|
|
243
|
+
if (error instanceof LLMAPIError && error.status === 429) showToast('Slow down')
|
|
244
|
+
}
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
サーバーでは、`createLLMHandler` でクライアントごとにリクエストを制限でき(デフォルトのキーは `x-forwarded-for` / `x-real-ip`)、超過時は `429` + `Retry-After` を返します。
|
|
248
|
+
|
|
249
|
+
```ts
|
|
250
|
+
createLLMHandler({
|
|
251
|
+
provider,
|
|
252
|
+
rateLimit: { maxRequests: 10, interval: 60_000, key: req => req.headers.get('x-user-id') ?? 'anon' },
|
|
253
|
+
})
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
サーバーのリミッターはインスタンスごとのインメモリー実装です。複数インスタンスで運用する場合は、共有のリミッター(Redis ベースなど)をハンドラーの前段に置いてください。
|
|
257
|
+
|
|
258
|
+
## システムプロンプト
|
|
259
|
+
|
|
260
|
+
```ts
|
|
261
|
+
const { messages, sendMessage } = useChat({
|
|
262
|
+
provider: ai,
|
|
263
|
+
model: 'gpt-4o',
|
|
264
|
+
system: 'You are a helpful assistant that speaks like a pirate.',
|
|
265
|
+
})
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
`system` には ref やゲッターも渡せ(`useChat`、`useCompletion`、`useObject`)、リクエストのたびに読み直されます。
|
|
269
|
+
|
|
270
|
+
## プロンプトテンプレート
|
|
271
|
+
|
|
272
|
+
変数名はテンプレート文字列から推論されるため、変数の不足やスペルミスは型エラーになります。
|
|
273
|
+
|
|
274
|
+
```ts
|
|
275
|
+
import { createPrompt, createChatPrompt, usePrompt } from 'vue-llm'
|
|
276
|
+
|
|
277
|
+
const translate = createPrompt('Translate {{ text }} to {{lang}}.', { defaults: { lang: 'English' } })
|
|
278
|
+
|
|
279
|
+
translate.render({ text: 'Hola' }) // 'Translate Hola to English.'
|
|
280
|
+
translate.render({ txt: 'Hola' }) // ✗ type error
|
|
281
|
+
translate.variables // ['text', 'lang']
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
値が不足している場合は実行時にもエラーがスローされます。プレースホルダーはバックスラッシュでエスケープできます:`\{{literal}}`。
|
|
285
|
+
|
|
286
|
+
`usePrompt` によるリアクティブなプロンプト:
|
|
287
|
+
|
|
288
|
+
```ts
|
|
289
|
+
const lang = ref('French')
|
|
290
|
+
const system = usePrompt('Always reply in {{lang}}.', () => ({ lang: lang.value }))
|
|
291
|
+
|
|
292
|
+
const chat = useChat({ provider: ai, model: 'gpt-4o', system })
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
複数メッセージのテンプレート:
|
|
296
|
+
|
|
297
|
+
```ts
|
|
298
|
+
const tutor = createChatPrompt([
|
|
299
|
+
{ role: 'system', content: 'You are a {{level}} tutor.' },
|
|
300
|
+
{ role: 'user', content: 'Explain {{topic}}.' },
|
|
301
|
+
])
|
|
302
|
+
|
|
303
|
+
tutor.render({ level: 'patient', topic: 'recursion' }) // ChatMessage[] for provider.chat()
|
|
304
|
+
useChat({ provider: ai, model: 'gpt-4o', initialMessages: tutor.toUIMessages({ level: 'patient', topic: 'recursion' }) })
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
## API リファレンス
|
|
308
|
+
|
|
309
|
+
### `useChat(options)`
|
|
310
|
+
|
|
311
|
+
リアクティブなチャットインターフェースを返します。
|
|
312
|
+
|
|
313
|
+
**オプション:**
|
|
314
|
+
|
|
315
|
+
| オプション | 型 | 説明 |
|
|
316
|
+
|--------|------|-------------|
|
|
317
|
+
| `provider` | `LLMProvider` | プロバイダーのインスタンス(必須) |
|
|
318
|
+
| `model` | `string` | モデル ID(必須) |
|
|
319
|
+
| `system` | `MaybeRefOrGetter<string>` | システムプロンプト |
|
|
320
|
+
| `tools` | `ToolDefinition[]` | 利用可能なツール |
|
|
321
|
+
| `onToolCall` | `(call) => unknown` | ツール呼び出しのハンドラー |
|
|
322
|
+
| `onFinish` | `(msg, ctx) => void` | レスポンスの完了時に呼ばれる |
|
|
323
|
+
| `onError` | `(error) => void` | エラーハンドラー |
|
|
324
|
+
| `maxToolRoundtrips` | `number` | ツール呼び出しのラウンド数の上限(デフォルト: 5) |
|
|
325
|
+
| `storage` | `ChatStorage` | `id` ごとにメッセージを保存・復元する |
|
|
326
|
+
|
|
327
|
+
**戻り値:**
|
|
328
|
+
|
|
329
|
+
| プロパティ | 型 | 説明 |
|
|
330
|
+
|----------|------|-------------|
|
|
331
|
+
| `messages` | `Ref<UIMessage[]>` | チャットのメッセージ |
|
|
332
|
+
| `status` | `Ref<ChatStatus>` | `'ready' \| 'submitted' \| 'streaming' \| 'error'` |
|
|
333
|
+
| `error` | `Ref<Error \| undefined>` | 最後のエラー |
|
|
334
|
+
| `input` | `Ref<string>` | 入力テキスト(v-model) |
|
|
335
|
+
| `isLoading` | `Ref<boolean>` | リクエストが処理中かどうか |
|
|
336
|
+
| `sendMessage` | `(text? \| { text?, files? }) => Promise` | メッセージを送信する(添付ファイルも可) |
|
|
337
|
+
| `stop` | `() => void` | 現在のストリームを中断する |
|
|
338
|
+
| `regenerate` | `() => Promise` | 最後のアシスタントメッセージを削除して再生成します |
|
|
339
|
+
| `setMessages` | `(msgs) => void` | メッセージ履歴を置き換える |
|
|
340
|
+
| `handleSubmit` | `(event?: Event) => void` | フォーム送信ハンドラー(`input.value` を送信) |
|
|
341
|
+
| `clearError` | `() => void` | エラー状態をクリアする |
|
|
342
|
+
| `isRestored` | `Ref<boolean>` | 非同期ストレージの読み込み中は `false` |
|
|
343
|
+
|
|
344
|
+
## Nuxt モジュール
|
|
345
|
+
|
|
346
|
+
```ts
|
|
347
|
+
// nuxt.config.ts
|
|
348
|
+
export default defineNuxtConfig({
|
|
349
|
+
modules: ['vue-llm/nuxt'],
|
|
350
|
+
runtimeConfig: {
|
|
351
|
+
vueLlm: {
|
|
352
|
+
openaiApiKey: '',
|
|
353
|
+
anthropicApiKey: '',
|
|
354
|
+
},
|
|
355
|
+
},
|
|
356
|
+
})
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
コンポーザブルとプロバイダーのファクトリーは自動インポートされます。
|
|
360
|
+
|
|
361
|
+
## セキュリティ
|
|
362
|
+
|
|
363
|
+
明示的に許可しないままブラウザーで使用すると、プロバイダーはエラーをスローします。
|
|
364
|
+
|
|
365
|
+
```ts
|
|
366
|
+
// This will throw in browser:
|
|
367
|
+
const ai = createOpenAI({ apiKey: 'sk-...' })
|
|
368
|
+
|
|
369
|
+
// Opt-in for prototyping only:
|
|
370
|
+
const ai = createOpenAI({
|
|
371
|
+
apiKey: 'sk-...',
|
|
372
|
+
dangerouslyAllowBrowser: true,
|
|
373
|
+
})
|
|
374
|
+
```
|
|
375
|
+
|
|
376
|
+
本番環境では、API 呼び出しをサーバー経由でプロキシするか、`runtimeConfig` とともに Nuxt モジュールを使用してください。
|
|
377
|
+
|
|
378
|
+
## メッセージ形式
|
|
379
|
+
|
|
380
|
+
メッセージは、リッチなコンテンツを扱うためにパーツベースの形式を採用しています。
|
|
381
|
+
|
|
382
|
+
```ts
|
|
383
|
+
interface UIMessage {
|
|
384
|
+
id: string
|
|
385
|
+
role: 'system' | 'user' | 'assistant'
|
|
386
|
+
parts: UIMessagePart[]
|
|
387
|
+
metadata?: Record<string, unknown>
|
|
388
|
+
createdAt?: Date
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
type UIMessagePart =
|
|
392
|
+
| { type: 'text'; text: string; state?: 'streaming' | 'done' }
|
|
393
|
+
| { type: 'reasoning'; text: string; state?: 'streaming' | 'done' }
|
|
394
|
+
| { type: 'tool-call'; toolCallId: string; toolName: string; args: unknown; state: 'streaming' | 'pending' | 'running' | 'result' | 'error'; result?: unknown; error?: string }
|
|
395
|
+
| { type: 'file'; mediaType: string; url: string; filename?: string }
|
|
396
|
+
| { type: 'source'; url: string; title?: string }
|
|
397
|
+
```
|
|
398
|
+
|
|
399
|
+
## ライセンス
|
|
400
|
+
|
|
401
|
+
MIT
|
package/README.md
CHANGED
|
@@ -1,16 +1,25 @@
|
|
|
1
1
|
# vue-llm
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**English** · [Русский](./README.ru.md) · [简体中文](./README.zh-CN.md) · [Português](./README.pt-BR.md) · [Español](./README.es.md) · [日本語](./README.ja.md)
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/vue-llm)
|
|
6
|
+
[](https://www.npmjs.com/package/vue-llm)
|
|
7
|
+
[](https://github.com/dadashi44/vue-llm/actions/workflows/ci.yml)
|
|
8
|
+
[](./LICENSE)
|
|
9
|
+
|
|
10
|
+
**vue-llm** is the AI SDK for Vue and Nuxt: streaming `useChat`, tool calling, Generative UI and in-browser models for OpenAI, Anthropic, Gemini, Ollama and WebGPU.
|
|
4
11
|
|
|
5
12
|
`useChat()` with streaming, tool calling, multi-provider support — in 10 lines of code.
|
|
6
13
|
|
|
7
|
-
**Documentation:** https://
|
|
14
|
+
**Documentation:** https://vue-llm.js.org/ · **[Try it in your browser](https://vue-llm.js.org/playground)** — no API key needed
|
|
8
15
|
|
|
9
16
|
## Features
|
|
10
17
|
|
|
11
18
|
- **`useChat()`** — reactive composable with streaming, abort, regenerate
|
|
12
|
-
- **Multi-provider** — OpenAI, Anthropic, Ollama out of the box
|
|
13
|
-
- **
|
|
19
|
+
- **Multi-provider** — OpenAI, Anthropic, Gemini, Ollama out of the box
|
|
20
|
+
- **In-browser models** — run Llama, Qwen or SmolLM on WebGPU with `createWebLLM`, no server or API key
|
|
21
|
+
- **Generative UI** — render tool calls as your Vue components with `<MessageParts>`, streamed arguments and human-in-the-loop
|
|
22
|
+
- **Tool calling** — `defineTool` with `execute`, `onToolCall`, `addToolResult`
|
|
14
23
|
- **Attachments** — images, PDFs, audio and text files, mapped per provider
|
|
15
24
|
- **Persistence** — restore chat history from localStorage or any custom backend
|
|
16
25
|
- **Resilience** — retries with exponential backoff, client and server rate limiting
|
|
@@ -18,7 +27,9 @@ AI/LLM composables for Vue. The missing AI SDK for the Vue ecosystem.
|
|
|
18
27
|
- **Any backend** — server-side tools in `createLLMHandler`, `createSSEProvider` for your own SSE API
|
|
19
28
|
- **Streaming** — SSE parser, text/reasoning/tool-call deltas
|
|
20
29
|
- **Security** — `dangerouslyAllowBrowser` guard protects API keys
|
|
21
|
-
- **
|
|
30
|
+
- **Chat UI** — a complete chat you own: `npx shadcn-vue add https://vue-llm.js.org/r/chat.json`, plus headless `usePromptInput` and `useAutoScroll`
|
|
31
|
+
- **Vue DevTools** — an LLM tab with every chat, request timeline, time to first token, tool calls and token usage (`vue-llm/devtools`)
|
|
32
|
+
- **Nuxt module** — auto-imports, components, DevTools in development, server-side runtime config
|
|
22
33
|
- **Zero runtime deps** — only `vue` as peer dependency (Vue 3.3+, composables also on Vue 2.7)
|
|
23
34
|
- **TypeScript** — full type safety, exported types
|
|
24
35
|
|
|
@@ -82,7 +93,7 @@ const ai = createAnthropic({
|
|
|
82
93
|
|
|
83
94
|
const { messages, sendMessage } = useChat({
|
|
84
95
|
provider: ai,
|
|
85
|
-
model: 'claude-sonnet-4-
|
|
96
|
+
model: 'claude-sonnet-4-5',
|
|
86
97
|
})
|
|
87
98
|
```
|
|
88
99
|
|
|
@@ -99,6 +110,25 @@ const { messages, sendMessage } = useChat({
|
|
|
99
110
|
})
|
|
100
111
|
```
|
|
101
112
|
|
|
113
|
+
### In the browser (WebGPU)
|
|
114
|
+
|
|
115
|
+
```ts
|
|
116
|
+
import { CreateMLCEngine } from '@mlc-ai/web-llm' // npm i @mlc-ai/web-llm
|
|
117
|
+
import { createWebLLM } from 'vue-llm/providers'
|
|
118
|
+
|
|
119
|
+
const ai = createWebLLM({
|
|
120
|
+
createEngine: CreateMLCEngine,
|
|
121
|
+
onProgress: ({ progress }) => console.log(`${Math.round(progress * 100)}%`),
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
const { messages, sendMessage } = useChat({
|
|
125
|
+
provider: ai,
|
|
126
|
+
model: 'Llama-3.2-1B-Instruct-q4f16_1-MLC',
|
|
127
|
+
})
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
The model runs on the user's GPU, and nothing leaves the device. See [In-Browser Models](https://vue-llm.js.org/guide/in-browser).
|
|
131
|
+
|
|
102
132
|
## Tool Calling
|
|
103
133
|
|
|
104
134
|
```ts
|
|
@@ -286,7 +316,7 @@ Returns a reactive chat interface.
|
|
|
286
316
|
|--------|------|-------------|
|
|
287
317
|
| `provider` | `LLMProvider` | Provider instance (required) |
|
|
288
318
|
| `model` | `string` | Model ID (required) |
|
|
289
|
-
| `system` | `string
|
|
319
|
+
| `system` | `MaybeRefOrGetter<string>` | System prompt |
|
|
290
320
|
| `tools` | `ToolDefinition[]` | Available tools |
|
|
291
321
|
| `onToolCall` | `(call) => unknown` | Tool call handler |
|
|
292
322
|
| `onFinish` | `(msg, ctx) => void` | Called when response completes |
|
|
@@ -302,12 +332,12 @@ Returns a reactive chat interface.
|
|
|
302
332
|
| `status` | `Ref<ChatStatus>` | `'ready' \| 'submitted' \| 'streaming' \| 'error'` |
|
|
303
333
|
| `error` | `Ref<Error \| undefined>` | Last error |
|
|
304
334
|
| `input` | `Ref<string>` | Input text (v-model) |
|
|
305
|
-
| `isLoading` | `
|
|
335
|
+
| `isLoading` | `Ref<boolean>` | Whether a request is active |
|
|
306
336
|
| `sendMessage` | `(text? \| { text?, files? }) => Promise` | Send a message, optionally with attachments |
|
|
307
337
|
| `stop` | `() => void` | Abort current stream |
|
|
308
|
-
| `regenerate` | `() => Promise` |
|
|
338
|
+
| `regenerate` | `() => Promise` | Drop the last assistant message and generate it again |
|
|
309
339
|
| `setMessages` | `(msgs) => void` | Replace message history |
|
|
310
|
-
| `handleSubmit` | `() =>
|
|
340
|
+
| `handleSubmit` | `(event?: Event) => void` | Form submit handler (sends `input.value`) |
|
|
311
341
|
| `clearError` | `() => void` | Clear error state |
|
|
312
342
|
| `isRestored` | `Ref<boolean>` | `false` while async storage is loading |
|
|
313
343
|
|
|
@@ -352,14 +382,18 @@ Messages use a parts-based format for rich content:
|
|
|
352
382
|
```ts
|
|
353
383
|
interface UIMessage {
|
|
354
384
|
id: string
|
|
355
|
-
role: 'user' | 'assistant'
|
|
385
|
+
role: 'system' | 'user' | 'assistant'
|
|
356
386
|
parts: UIMessagePart[]
|
|
387
|
+
metadata?: Record<string, unknown>
|
|
388
|
+
createdAt?: Date
|
|
357
389
|
}
|
|
358
390
|
|
|
359
391
|
type UIMessagePart =
|
|
360
|
-
| { type: 'text'; text: string; state
|
|
361
|
-
| { type: 'reasoning'; text: string; state
|
|
362
|
-
| { type: 'tool-call'; toolCallId: string; toolName: string; args: unknown; state: string }
|
|
392
|
+
| { type: 'text'; text: string; state?: 'streaming' | 'done' }
|
|
393
|
+
| { type: 'reasoning'; text: string; state?: 'streaming' | 'done' }
|
|
394
|
+
| { type: 'tool-call'; toolCallId: string; toolName: string; args: unknown; state: 'streaming' | 'pending' | 'running' | 'result' | 'error'; result?: unknown; error?: string }
|
|
395
|
+
| { type: 'file'; mediaType: string; url: string; filename?: string }
|
|
396
|
+
| { type: 'source'; url: string; title?: string }
|
|
363
397
|
```
|
|
364
398
|
|
|
365
399
|
## License
|