You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This example allow you to have a llama.cpp http server to interact from a web page or consume the API.
4
4
5
-
## Table of Contents
5
+
Command line options:
6
+
7
+
-`--threads N`, `-t N`: use N threads.
8
+
-`-m FNAME`, `--model FNAME`: Specify the path to the LLaMA model file (e.g., `models/7B/ggml-model.bin`).
9
+
-`-c N`, `--ctx-size N`: Set the size of the prompt context. The default is 512, but LLaMA models were built with a context of 2048, which will provide better results for longer input/inference.
10
+
-`-ngl N`, `--n-gpu-layers N`: When compiled with appropriate support (currently CLBlast or cuBLAS), this option allows offloading some layers to the GPU for computation. Generally results in increased performance.
11
+
-`--embedding`: Enable the embedding mode. **Completion function doesn't work in this mode**.
12
+
-`--host`: Set the hostname or ip address to listen. Default `127.0.0.1`;
13
+
-`--port`: Set the port to listen. Default: `8080`.
6
14
7
-
1.[Quick Start](#quick-start)
8
-
2.[Node JS Test](#node-js-test)
9
-
3.[API Endpoints](#api-endpoints)
10
-
4.[More examples](#more-examples)
11
-
5.[Common Options](#common-options)
12
-
6.[Performance Tuning and Memory Options](#performance-tuning-and-memory-options)
13
15
14
16
## Quick Start
15
17
16
18
To get started right away, run the following command, making sure to use the correct path for the model you have:
That will start a server that by default listens on `127.0.0.1:8080`. You can consume the endpoints with Postman or NodeJS with axios library.
32
+
That will start a server that by default listens on `127.0.0.1:8080`.
33
+
You can consume the endpoints with Postman or NodeJS with axios library.
34
+
35
+
## Testing with CURL
36
+
37
+
Using [curl](https://curl.se/). On Windows `curl.exe` should be available in the base OS.
38
+
39
+
```sh
40
+
curl --request POST \
41
+
--url http://localhost:8080/completion \
42
+
--data '{"prompt": "Building a website can be done in 10 simple steps:","n_predict": 128}'
43
+
```
31
44
32
45
## Node JS Test
33
46
@@ -50,7 +63,6 @@ const prompt = `Building a website can be done in 10 simple steps:`;
50
63
asyncfunctionTest() {
51
64
let result =awaitaxios.post("http://127.0.0.1:8080/completion", {
52
65
prompt,
53
-
batch_size:128,
54
66
n_predict:512,
55
67
});
56
68
@@ -69,244 +81,45 @@ node .
69
81
70
82
## API Endpoints
71
83
72
-
You can interact with this API Endpoints. This implementations just support chat style interaction.
84
+
You can interact with this API Endpoints.
85
+
This implementations just support chat style interaction.
73
86
74
87
-**POST**`hostname:port/completion`: Setting up the Llama Context to begin the completions tasks.
75
88
76
-
*Options:*
77
-
78
-
`batch_size`: Set the batch size for prompt processing (default: 512).
79
-
80
-
`temperature`: Adjust the randomness of the generated text (default: 0.8).
81
-
82
-
`top_k`: Limit the next token selection to the K most probable tokens (default: 40).
83
-
84
-
`top_p`: Limit the next token selection to a subset of tokens with a cumulative probability above a threshold P (default: 0.9).
89
+
*Options:*
85
90
86
-
`n_predict`: Set the number of tokens to predict when generating text (default: 128, -1 = infinity).
91
+
`temperature`: Adjust the randomness of the generated text (default: 0.8).
87
92
88
-
`threads`: Set the number of threads to use during computation.
93
+
`top_k`: Limit the next token selection to the K most probable tokens (default: 40).
89
94
90
-
`n_keep`: Specify the number of tokens from the initial prompt to retain when the model resets its internal context. By default, this value is set to 0 (meaning no tokens are kept). Use `-1` to retain all tokens from the initial prompt.
95
+
`top_p`: Limit the next token selection to a subset of tokens with a cumulative probability above a threshold P (default: 0.9).
91
96
92
-
`as_loop`: It allows receiving each predicted token in real-time instead of waiting for the completion to finish. To enable this, set to `true`.
97
+
`n_predict`: Set the number of tokens to predict when generating text (default: 128, -1 = infinity).
93
98
94
-
`interactive`: It allows interacting with the completion, and the completion stops as soon as it encounters a `stop word`. To enable this, set to `true`.
99
+
`n_keep`: Specify the number of tokens from the initial prompt to retain when the model resets its internal context.
100
+
By default, this value is set to 0 (meaning no tokens are kept). Use `-1` to retain all tokens from the initial prompt.
95
101
96
-
`prompt`: Provide a prompt. Internally, the prompt is compared, and it detects if a part has already been evaluated, and the remaining part will be evaluate.
102
+
`stream`: It allows receiving each predicted token in real-time instead of waiting for the completion to finish. To enable this, set to `true`.
97
103
98
-
`stop`: Specify the words or characters that indicate a stop. These words will not be included in the completion, so make sure to add them to the prompt for the next iteration.
104
+
`prompt`: Provide a prompt. Internally, the prompt is compared, and it detects if a part has already been evaluated, and the remaining part will be evaluate.
99
105
100
-
`exclude`: Specify the words or characters you do not want to appear in the completion. These words will not be included in the completion, so make sure to add them to the prompt for the next iteration.
101
-
102
-
-**POST**`hostname:port/embedding`: Generate embedding of a given text
103
-
104
-
*Options:*
105
-
106
-
`content`: Set the text to get generate the embedding.
107
-
108
-
`threads`: Set the number of threads to use during computation.
109
-
110
-
To use this endpoint, you need to start the server with the `--embedding` option added.
106
+
`stop`: Specify the strings that indicate a stop.
107
+
These words will not be included in the completion, so make sure to add them to the prompt for the next iteration.
108
+
Default: `[]`
111
109
112
110
-**POST**`hostname:port/tokenize`: Tokenize a given text
113
111
114
-
*Options:*
115
-
116
-
`content`: Set the text to tokenize.
112
+
*Options:*
117
113
118
-
-**GET**`hostname:port/next-token`: Receive the next token predicted, execute this request in a loop. Make sure set `as_loop` as `true` in the completion request.
119
-
120
-
*Options:*
121
-
122
-
`stop`: Set `hostname:port/next-token?stop=true` to stop the token generation.
114
+
`content`: Set the text to tokenize.
123
115
124
116
## More examples
125
117
126
118
### Interactive mode
127
119
128
-
This mode allows interacting in a chat-like manner. It is recommended for models designed as assistants such as `Vicuna`, `WizardLM`, `Koala`, among others. Make sure to add the correct stop word for the corresponding model.
129
-
130
-
The prompt should be generated by you, according to the model's guidelines. You should keep adding the model's completions to the context as well.
131
-
132
-
This example works well for `Vicuna - version 1`.
133
-
134
-
```javascript
135
-
constaxios=require("axios");
136
-
137
-
let prompt =`A chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the human's questions.
138
-
### Human: Hello, Assistant.
139
-
### Assistant: Hello. How may I help you today?
140
-
### Human: Please tell me the largest city in Europe.
141
-
### Assistant: Sure. The largest city in Europe is Moscow, the capital of Russia.`;
142
-
143
-
asyncfunctionChatCompletion(answer) {
144
-
// the user's next question to the prompt
145
-
prompt +=`\n### Human: ${answer}\n`
146
-
147
-
result =awaitaxios.post("http://127.0.0.1:8080/completion", {
148
-
prompt,
149
-
batch_size:128,
150
-
temperature:0.2,
151
-
top_k:40,
152
-
top_p:0.9,
153
-
n_keep:-1,
154
-
n_predict:2048,
155
-
stop: ["\n### Human:"], // when detect this, stop completion
156
-
exclude: ["### Assistant:"], // no show in the completion
157
-
threads:8,
158
-
as_loop:true, // use this to request the completion token by token
159
-
interactive:true, // enable the detection of a stop word
160
-
});
161
-
162
-
// create a loop to receive every token predicted
163
-
// note: this operation is blocking, avoid use this in a ui thread
164
-
165
-
let message ="";
166
-
while (true) {
167
-
// you can stop the inference adding '?stop=true' like this http://127.0.0.1:8080/next-token?stop=true
168
-
result =awaitaxios.get("http://127.0.0.1:8080/next-token");
169
-
process.stdout.write(result.data.content);
170
-
message +=result.data.content;
171
-
172
-
// to avoid an infinite loop
173
-
if (result.data.stop) {
174
-
console.log("Completed");
175
-
// make sure to add the completion to the prompt.
176
-
prompt +=`### Assistant: ${message}`;
177
-
break;
178
-
}
179
-
}
180
-
}
181
-
182
-
// This function should be called every time a question to the model is needed.
183
-
asyncfunctionTest() {
184
-
// the server can't inference in paralell
185
-
awaitChatCompletion("Write a long story about a time magician in a fantasy world");
186
-
awaitChatCompletion("Summary the story");
187
-
}
188
-
189
-
Test();
190
-
```
191
-
192
-
### Alpaca example
193
-
194
-
**Temporaly note:** no tested, if you have the model, please test it and report me some issue
195
-
196
-
```javascript
197
-
constaxios=require("axios");
198
-
199
-
let prompt =`Below is an instruction that describes a task. Write a response that appropriately completes the request.
result =awaitaxios.post("http://127.0.0.1:8080/completion", {
205
-
prompt,
206
-
batch_size:128,
207
-
temperature:0.2,
208
-
top_k:40,
209
-
top_p:0.9,
210
-
n_keep:-1,
211
-
n_predict:2048,
212
-
stop: ["### Instruction:\n\n"], // when detect this, stop completion
213
-
exclude: [], // no show in the completion
214
-
threads:8,
215
-
as_loop:true, // use this to request the completion token by token
216
-
interactive:true, // enable the detection of a stop word
217
-
});
218
-
219
-
// create a loop to receive every token predicted
220
-
// note: this operation is blocking, avoid use this in a ui thread
221
-
222
-
let message ="";
223
-
while (true) {
224
-
result =awaitaxios.get("http://127.0.0.1:8080/next-token");
225
-
process.stdout.write(result.data.content);
226
-
message +=result.data.content;
227
-
228
-
// to avoid an infinite loop
229
-
if (result.data.stop) {
230
-
console.log("Completed");
231
-
// make sure to add the completion and the user's next question to the prompt.
232
-
prompt += message;
233
-
break;
234
-
}
235
-
}
236
-
}
237
-
238
-
// This function should be called every time a instruction to the model is needed.
239
-
DoInstruction("Destroy the world"); // as joke
240
-
```
120
+
Check the sample in [chat.mjs](chat.mjs).
121
+
Run with node:
241
122
242
-
### Embeddings
243
-
244
-
First, run the server with `--embedding` option:
245
-
246
-
```bash
247
-
server -m models/7B/ggml-model.bin --ctx_size 2048 --embedding
123
+
```sh
124
+
node chat.mjs
248
125
```
249
-
250
-
Run this code in NodeJS:
251
-
252
-
```javascript
253
-
constaxios=require('axios');
254
-
255
-
asyncfunctionTest() {
256
-
let result =awaitaxios.post("http://127.0.0.1:8080/embedding", {
257
-
content:`Hello`,
258
-
threads:5
259
-
});
260
-
// print the embedding array
261
-
console.log(result.data.embedding);
262
-
}
263
-
264
-
Test();
265
-
```
266
-
267
-
### Tokenize
268
-
269
-
Run this code in NodeJS:
270
-
271
-
```javascript
272
-
constaxios=require('axios');
273
-
274
-
asyncfunctionTest() {
275
-
let result =awaitaxios.post("http://127.0.0.1:8080/tokenize", {
276
-
content:`Hello`
277
-
});
278
-
// print the embedding array
279
-
console.log(result.data.tokens);
280
-
}
281
-
282
-
Test();
283
-
```
284
-
285
-
## Common Options
286
-
287
-
-`-m FNAME, --model FNAME`: Specify the path to the LLaMA model file (e.g., `models/7B/ggml-model.bin`).
288
-
-`-c N, --ctx-size N`: Set the size of the prompt context. The default is 512, but LLaMA models were built with a context of 2048, which will provide better results for longer input/inference.
289
-
-`-ngl N, --n-gpu-layers N`: When compiled with appropriate support (currently CLBlast or cuBLAS), this option allows offloading some layers to the GPU for computation. Generally results in increased performance.
290
-
-`--embedding`: Enable the embedding mode. **Completion function doesn't work in this mode**.
291
-
-`--host`: Set the hostname or ip address to listen. Default `127.0.0.1`;
292
-
-`--port`: Set the port to listen. Default: `8080`.
293
-
294
-
### RNG Seed
295
-
296
-
-`-s SEED, --seed SEED`: Set the random number generator (RNG) seed (default: -1, < 0 = random seed).
297
-
298
-
The RNG seed is used to initialize the random number generator that influences the text generation process. By setting a specific seed value, you can obtain consistent and reproducible results across multiple runs with the same input and settings. This can be helpful for testing, debugging, or comparing the effects of different options on the generated text to see when they diverge. If the seed is set to a value less than 0, a random seed will be used, which will result in different outputs on each run.
299
-
300
-
## Performance Tuning and Memory Options
301
-
302
-
### No Memory Mapping
303
-
304
-
-`--no-mmap`: Do not memory-map the model. By default, models are mapped into memory, which allows the system to load only the necessary parts of the model as needed. However, if the model is larger than your total amount of RAM or if your system is low on available memory, using mmap might increase the risk of pageouts, negatively impacting performance.
305
-
306
-
### Memory Float 32
307
-
308
-
-`--memory-f32`: Use 32-bit floats instead of 16-bit floats for memory key+value. This doubles the context memory requirement but does not appear to increase generation quality in a measurable way. Not recommended.
309
-
310
-
## Limitations:
311
-
312
-
- The actual implementation of llama.cpp need a `llama-state` for handle multiple contexts and clients, but this could require more powerful hardware.
0 commit comments