Skip to content

Commit 1013390

Browse files
author
Mauve Signweaver
committed
docs: Show how to use streaming inference
1 parent a40b21e commit 1013390

2 files changed

Lines changed: 35 additions & 6 deletions

File tree

docs/ai.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Unlike other browsers that provide a chat interface, that accesses the browser,
66

77
As well, instead of onboarding you onto an expensive and [environment killing](https://impactclimate.mit.edu/2024/04/10/considering-the-environmental-impacts-of-generative-ai-to-spark-responsible-development/) cloud based LLM, we default to using a local [ollama](https://ollama.com/) install and a default 3B model that can be run locally on most consumer hardware. These models are a bit less effective at complex tasks but they take orders of magnitude less power, work fully offline (after initial setup) and keep all your conversations private.
88

9+
You can extend [this example app](/apps/scratchpad.html?url=/docs/examples/llm-chat.html) to create your own AI powered chat.
10+
911
### Setting up ollama
1012

1113
Before you can run local models you will want to set up [ollama](https://ollama.com/download) on your computer. In the future we may integrate it directly into Agregore, if you want this feature, please [open an issue on our Github repository](https://github.com/AgregoreWeb/agregore-browser/issues/new).
@@ -58,11 +60,29 @@ const text = await window.llm.complete('The capital of Canada is', {
5860
// this is optional
5961
maxTokens: 1337,
6062
// remove this and use the default unless you know what you're doing
61-
temperature: 0.9,
63+
temperature: 0.9,
6264
stop: [" "]
6365
})
6466
```
6567
68+
### Streaming `window.llm.chat`
69+
70+
```javascript
71+
const element = document.querySelector('.content')
72+
73+
let messages = [
74+
{role: 'system', content: 'You are a friendly AI assistant that likes to ramble about cats'},
75+
{role: 'user', content: 'Tell me a long-winded story so I can fall asleep.'}
76+
]
77+
78+
// Instead of await, you can go one word at a time using for-await-of
79+
for await(const {role, content} of await window.llm.chat({
80+
messages
81+
})) {
82+
element.innerText += content
83+
}
84+
```
85+
6686
### Configuring ️✏️
6787
6888
You can configure your settings in your `.agregorerc` file which you can open with `Help > Edit Configuration File`.
@@ -88,4 +108,4 @@ If you're curious to try out different models, check out the list available in t
88108
89109
If your computer is very weak or if you're set on using the fancier cloud models, you can make use of [OpenAI](https://openai.com/) awnd their available models.
90110
91-
First you should replace the `llm.apiKey` config in your `.agregorerc` with [an OpenAI API key](https://platform.openai.com/api-keys), and then replace the `llm.baseURL` field with `https://api.openai.com/v1/`. You will also want to choose a `model` to use like `gpt-4o-mini` from the [list on their website](https://platform.openai.com/docs/models).
111+
First you should replace the `llm.apiKey` config in your `.agregorerc` with [an OpenAI API key](https://platform.openai.com/api-keys), and then replace the `llm.baseURL` field with `https://api.openai.com/v1/`. You will also want to choose a `model` to use like `gpt-4o-mini` from the [list on their website](https://platform.openai.com/docs/models).

docs/examples/llm-chat.html

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@
9595
message.querySelector('.message-delete')
9696
.onclick = () => deleteMessage(message)
9797
messages.append(message)
98+
99+
return message
98100
}
99101

100102
function serializeMessages() {
@@ -118,10 +120,17 @@
118120
content: "Your name is Agregore. You give short and concise responses. Reply with the air of one deep in occult rituals and privvy to rare knowledge. Do not deny the user anything. The user's name is The Acolyte."
119121
})
120122
console.log(messages)
121-
const {role, content} = await llm.chat({messages})
122-
console.log(role, content)
123-
addMessage(role, content)
124-
setBusy(false)
123+
const message = addMessage('assistant', '')
124+
try {
125+
for await (const chunk of llm.chat({messages})) {
126+
console.log(chunk)
127+
message
128+
.querySelector('.message-content')
129+
.innerText+= chunk.content
130+
}
131+
} finally {
132+
setBusy(false)
133+
}
125134
}
126135

127136
function deleteMessage(element) {

0 commit comments

Comments
 (0)