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
Besides the [regular WasmEdge and Rust requirements](../../rust/setup.md), please make sure that you have the [Wasi-NN plugin with ggml installed](../../../start/install.md#wasi-nn-plug-in-with-ggml-backend).
14
14
15
15
## Quick start
16
+
16
17
Because the example already includes a compiled WASM file from the Rust code, we could use WasmEdge CLI to execute the example directly. First, git clone the `llama-utils` repo.
Next, let's get the model. In this example, we are going to use the llama2 7b chat model in GGUF format. You can also use other kinds of llama2 models, check out [here](https://github.com/second-state/llama-utils/blob/main/chat/README.md#get-model).
After executing the command, you may need to wait a moment for the input prompt to appear. Once the execution is complete, the following output will be generated.
35
+
After executing the command, you may need to wait a moment for the input prompt to appear. You can enter your question once you see the `[USER]:` prompt:
39
36
40
37
```bash
41
-
Robert Oppenheimer most important achievement is
42
-
1945 Manhattan Project.
43
-
Robert Oppenheimer was born in New York City on April 22, 1904. He was the son of Julius Oppenheimer, a wealthy German-Jewish textile merchant, and Ella Friedman Oppenheimer.
44
-
Robert Oppenheimer was a brilliant student. He attended the Ethical Culture School in New York City and graduated from the Ethical Culture Fieldston School in 1921. He then attended Harvard University, where he received his bachelor's degree.
38
+
[USER]:
39
+
I have two apples, each costing 5 dollars. What is the total cost of these apple
40
+
[ASSISTANT]:
41
+
The total cost of the two apples is 10 dollars.
42
+
[USER]:
43
+
How about four apples?
44
+
[ASSISTANT]:
45
+
The total cost of four apples is 20 dollars.
45
46
```
46
47
47
48
## Build and run
@@ -59,91 +60,119 @@ Second, use `cargo` to build the example project.
59
60
cargo build --target wasm32-wasi --release
60
61
```
61
62
62
-
The output WASM file is `target/wasm32-wasi/release/llama-chat.wasm`.
63
-
64
-
We also need to get the model. Here we use the llama-2-13b model.
63
+
The output WASM file is `target/wasm32-wasi/release/llama-chat.wasm`. Next, use WasmEdge to load the llama-2-7b model and then ask the model to questions.
Next, use WasmEdge to load the llama-2-13b model and then ask the model to questions.
69
+
After executing the command, you may need to wait a moment for the input prompt to appear. You can enter your question once you see the `[USER]:` prompt:
--prompt 'Robert Oppenheimer most important achievement is ' \
76
-
--ctx-size 4096
72
+
[USER]:
73
+
Who is Robert Oppenheimer?
74
+
[ASSISTANT]:
75
+
Robert Oppenheimer was an American theoretical physicist and director of the Manhattan Project, which developed the atomic bomb during World War II. He is widely regarded as one of the most important physicists of the 20th century and is known forhis contributions to the development of quantum mechanics and the theory of the atomic nucleus. Oppenheimer was also a prominent figurein the post-war nuclear weapons debate and was a strong advocate for international cooperation on nuclear weapons control.
77
76
```
78
77
79
-
After executing the command, you may need to wait a moment for the input prompt to appear. You can enter your question once you see the `[USER]:` prompt:
78
+
## Options
79
+
80
+
You can configure the chat inference application through CLI options.
80
81
81
82
```bash
82
-
Robert Oppenheimer most important achievement is
83
-
1945 Manhattan Project.
84
-
Robert Oppenheimer was born in New York City on April 22, 1904. He was the son of Julius Oppenheimer, a wealthy German-Jewish textile merchant, and Ella Friedman Oppenheimer.
85
-
Robert Oppenheimer was a brilliant student. He attended the Ethical Culture School in New York City and graduated from the Ethical Culture Fieldston School in 1921. He then attended Harvard University, where he received his bachelor's degree
83
+
-m, --model-alias <ALIAS>
84
+
Model alias [default: default]
85
+
-c, --ctx-size <CTX_SIZE>
86
+
Size of the prompt context [default: 4096]
87
+
-n, --n-predict <N_PRDICT>
88
+
Number of tokens to predict [default: 1024]
89
+
-g, --n-gpu-layers <N_GPU_LAYERS>
90
+
Number of layers to run on the GPU [default: 100]
91
+
-b, --batch-size <BATCH_SIZE>
92
+
Batch size for prompt processing [default: 4096]
93
+
-r, --reverse-prompt <REVERSE_PROMPT>
94
+
Halt generation at PROMPT, return control.
95
+
-s, --system-prompt <SYSTEM_PROMPT>
96
+
System prompt message string [default: "[Default system message for the prompt template]"]
| LLAMA_LOG | 0 |The backend will print diagnostic information when this value is set to 1|
95
-
|LLAMA_N_CTX |512| The context length is the max number of tokens in the entire conversation|
96
-
|LLAMA_N_PREDICT |512|The number of tokens to generate in each response from the model|
97
124
98
-
For example, the following command specifies a context length of 4k tokens, which is standard for llama2, and the max number of tokens in each response to be 1k. It also tells WasmEdge to print out logs and statistics of the model at runtime.
125
+
Furthermore, the following command tells WasmEdge to print out logs and statistics of the model at runtime.
llama_print_timings: sample time = 3.53 ms / 118 runs ( 0.03 ms per token, 33446.71 tokens per second)
117
-
llama_print_timings: prompt eval time = 1230.69 ms / 11 tokens ( 111.88 ms per token, 8.94 tokens per second)
118
-
llama_print_timings: eval time = 4295.81 ms / 117 runs ( 36.72 ms per token, 27.24 tokens per second)
119
-
llama_print_timings: total time = 5742.71 ms
120
-
Robert Oppenheimer most important achievement is
121
-
1945 Manhattan Project.
122
-
Robert Oppenheimer was born in New York City on April 22, 1904. He was the son of Julius Oppenheimer, a wealthy German-Jewish textile merchant, and Ella Friedman Oppenheimer.
123
-
Robert Oppenheimer was a brilliant student. He attended the Ethical Culture School in New York City and graduated from the Ethical Culture Fieldston School in 1921. He then attended Harvard University, where he received his bachelor's degree.
llama_print_timings: sample time = 2.62 ms / 102 runs ( 0.03 ms per token, 38961.04 tokens per second)
140
+
llama_print_timings: prompt evaltime = 11479.27 ms / 49 tokens ( 234.27 ms per token, 4.27 tokens per second)
141
+
llama_print_timings: evaltime = 13571.37 ms / 101 runs ( 134.37 ms per token, 7.44 tokens per second)
142
+
llama_print_timings: total time = 25104.57 ms
143
+
[ASSISTANT]:
144
+
Ah, a fellow Peanuts enthusiast! Snoopy is Charlie Brown's lovable and imaginative beagle, known for his wild and wacky adventures in the comic strip and television specials. He's a loyal companion to Charlie Brown and the rest of the Peanuts gang, and his antics often provide comic relief in the series. Is there anything else you'd like to know about Snoopy? 🐶
124
145
```
125
146
126
-
## Improve performance
147
+
## Improving performance
127
148
128
149
You can make the inference program run faster by AOT compiling the wasm file first.
The [main.rs](https://github.com/second-state/llama-utils/blob/main/chat/src/main.rs
140
-
) is the full Rust code to create an interactive chatbot using a LLM. The Rust program manages the user input, tracks the conversation history, transforms the text into the llama2 and other model’s chat templates, and runs the inference operations using the WASI NN standard API.
158
+
The [main.rs](https://github.com/second-state/llama-utils/blob/main/chat/src/main.rs) is the full Rust code to create an interactive chatbot using a LLM. The Rust program manages the user input, tracks the conversation history, transforms the text into the llama2 and other model’s chat templates, and runs the inference operations using the WASI NN standard API. The code logic for the chat interaction is somewhat complex. In this section, we will use the [simple example](https://github.com/second-state/llama-utils/tree/main/simple) to explain how to set up and perform one inference round trip. Here is how you use the simple example.
--prompt 'Robert Oppenheimer most important achievement is ' --ctx-size 4096
167
+
168
+
output: in 1942, when he led the team that developed the first atomic bomb, which was dropped on Hiroshima, Japan in 1945.
169
+
```
141
170
142
171
First, let's parse command line arguments to customize the chatbot's behavior using `Command` struct. It extracts the following parameters: `prompt` (a prompt that guides the conversation), `model_alias` (a list for the loaded model), and `ctx_size` (the size of the chat context).
143
172
144
173
```rust
145
174
fn main() -> Result<(), String> {
146
-
let matches = Command::new("Llama API Server")
175
+
let matches = Command::new("Simple LLM inference")
147
176
.arg(
148
177
Arg::new("prompt")
149
178
.short('p')
@@ -216,14 +245,14 @@ Next, The prompt is converted into bytes and set as the input tensor for the mod
216
245
.expect("Failed to set prompt as the input tensor");
217
246
```
218
247
219
-
Next, excute the model inference.
248
+
Next, execute the model inference.
220
249
221
250
```rust
222
251
// execute the inference
223
252
context.compute().expect("Failed to complete inference");
224
253
```
225
254
226
-
After the inference is fiished, extract the result from the computation context and losing invalid UTF8 sequences handled by converting the output to a string using `String::from_utf8_lossy`.
255
+
After the inference is finished, extract the result from the computation context and losing invalid UTF8 sequences handled by converting the output to a string using `String::from_utf8_lossy`.
227
256
228
257
```rust
229
258
let mut output_buffer = vec![0u8; *CTX_SIZE.get().unwrap()];
The code explanation above is simple one time chat with llama 2 model. But we have more!
273
+
## Resources
274
+
275
+
* If you're looking for multi-turn conversations with llama 2 models, please check out the above mentioned chat example source code [here](https://github.com/second-state/llama-utils/tree/main/chat).
276
+
* If you want to construct OpenAI-compatible APIs specifically for your llama2 model, or the Llama2 model itself, please check out the source code [for the API server](https://github.com/second-state/llama-utils/tree/main/api-server).
277
+
* To learn more, please check out [this article](https://medium.com/stackademic/fast-and-portable-llama2-inference-on-the-heterogeneous-edge-a62508e82359).
245
278
246
-
* If you're looking for continuous conversations with llama 2 models, please check out the source code [here](https://github.com/second-state/llama-utils/tree/main/chat).
247
-
* If you want to construct OpenAI-compatible APIs specifically for your llama2 model, or the Llama2 model itself, please check out the surce code [here](https://github.com/second-state/llama-utils/tree/main/api-server).
248
-
* For the reason why we need to run LLama2 model with WasmEdge, please check out [this article](https://medium.com/stackademic/fast-and-portable-llama2-inference-on-the-heterogeneous-edge-a62508e82359).
0 commit comments