Skip to content

Commit ca34a85

Browse files
committed
Update ui.mdx
1 parent db33b7e commit ca34a85

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

content/docs/extensions/ui.mdx

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ const Brand = {
3535
<button type="button" @click="$router.push('/')" class="...">
3636
🎄 {{ $state.title }} 🎄
3737
</button>
38-
</div>`
38+
</div>
39+
`,
3940
}
4041

4142
const Welcome = {
@@ -219,6 +220,48 @@ export default {
219220
}
220221
```
221222
223+
## 7. Programmatic Chat Completions
224+
225+
You can use the chat API within your components to generate text using the configured LLMs. This is useful for creating dynamic content like stories, summaries, or finding information.
226+
227+
### Example: Generating a Story
228+
229+
The `XmasPage` component uses `ctx.chat.completion` to generate a Christmas story on mount.
230+
231+
```js
232+
onMounted(async () => {
233+
// 1. Find an available model
234+
const freeStoryModelNames = ['Kimi K2 0905', 'Kimi K2 Instruct', 'Kimi K2 (free)', 'Kimi Dev 72b (free)', 'GPT OSS 120B']
235+
const availableStoryModel = freeStoryModelNames.map(name => ctx.chat.getModel(name)).find(x => !!x)
236+
237+
if (!availableStoryModel) {
238+
console.log('No story models available')
239+
return
240+
}
241+
242+
story.value = `Santa is busy writing a christmas story...`
243+
244+
// 2. Create the request
245+
const request = ctx.chat.createRequest({
246+
model: availableStoryModel,
247+
text: `Write a short, feel-good Christmas story set on a quiet winter evening. Focus on simple kindness, cozy details, and gentle magic—twinkling lights, warm drinks, falling snow, and a small act of generosity that brings people together. Keep the tone hopeful and comforting, with a soft, joyful ending that leaves the reader smiling.`,
248+
systemPrompt: santaSystemPrompt,
249+
})
250+
251+
// 3. Execute completion
252+
const api = await ctx.chat.completion({
253+
request
254+
})
255+
256+
// 4. Handle response
257+
if (api.response) {
258+
story.value = ctx.chat.getAnswer(api.response)
259+
} else if (api.error) {
260+
story.value = api.error.message
261+
}
262+
})
263+
```
264+
222265
## Full Example Component: Top Panel
223266
224267
This is how you define a complex UI component like a Top Panel. It's just a standard Vue 3 component.

0 commit comments

Comments
 (0)