Skip to content

Commit 8d012c2

Browse files
committed
chore: refactor examples
1 parent 951afc3 commit 8d012c2

File tree

5 files changed

+19
-38
lines changed

5 files changed

+19
-38
lines changed

Cargo.lock

Lines changed: 1 addition & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[workspace]
22
resolver = "2"
3-
members = ["llama-cpp-sys-2", "llama-cpp-2", "embeddings", "examples/usage", "examples/simple"]
3+
members = ["llama-cpp-sys-2", "llama-cpp-2", "embeddings", "examples/simple"]
44

55
[workspace.dependencies]
66
# core library deps
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
//! # Usage
2-
//!
2+
//!
33
//! This is just about the smallest possible way to do inference. To fetch a model from hugging face:
4-
//!
5-
//! ```bash
4+
//!
5+
//! ```console
66
//! git clone --recursive https://github.com/utilityai/llama-cpp-rs
77
//! cd llama-cpp-rs/examples/usage
88
//! wget https://huggingface.co/Qwen/Qwen2-1.5B-Instruct-GGUF/resolve/main/qwen2-1_5b-instruct-q4_0.gguf
9-
//! cargo run --bin usage -- qwen2-1_5b-instruct-q4_0.gguf
9+
//! cargo run --example usage -- qwen2-1_5b-instruct-q4_0.gguf
1010
//! ```
11-
use std::io::Write;
1211
use llama_cpp_2::context::params::LlamaContextParams;
1312
use llama_cpp_2::llama_backend::LlamaBackend;
1413
use llama_cpp_2::llama_batch::LlamaBatch;
1514
use llama_cpp_2::model::params::LlamaModelParams;
1615
use llama_cpp_2::model::LlamaModel;
1716
use llama_cpp_2::model::{AddBos, Special};
1817
use llama_cpp_2::token::data_array::LlamaTokenDataArray;
18+
use std::io::Write;
1919

2020
#[allow(clippy::cast_possible_wrap, clippy::cast_possible_truncation)]
2121
fn main() {
2222
let model_path = std::env::args().nth(1).expect("Please specify model path");
2323
let backend = LlamaBackend::init().unwrap();
2424
let params = LlamaModelParams::default();
2525

26-
let prompt = "<|im_start|>user\nHello! how are you?<|im_end|>\n<|im_start|>assistant\n".to_string();
26+
let prompt =
27+
"<|im_start|>user\nHello! how are you?<|im_end|>\n<|im_start|>assistant\n".to_string();
2728
LlamaContextParams::default();
2829
let model =
2930
LlamaModel::load_from_file(&backend, model_path, &params).expect("unable to load model");
@@ -48,14 +49,11 @@ fn main() {
4849
}
4950
ctx.decode(&mut batch).expect("llama_decode() failed");
5051

51-
5252
let mut n_cur = batch.n_tokens();
5353

54-
5554
// The `Decoder`
5655
let mut decoder = encoding_rs::UTF_8.new_decoder();
5756

58-
5957
while n_cur <= n_len {
6058
// sample the next token
6159
{
@@ -72,7 +70,9 @@ fn main() {
7270
break;
7371
}
7472

75-
let output_bytes = model.token_to_bytes(new_token_id, Special::Tokenize).unwrap();
73+
let output_bytes = model
74+
.token_to_bytes(new_token_id, Special::Tokenize)
75+
.unwrap();
7676
// use `Decoder.decode_to_string()` to avoid the intermediate buffer
7777
let mut output_string = String::with_capacity(32);
7878
let _decode_result = decoder.decode_to_string(&output_bytes, &mut output_string, false);

examples/usage/Cargo.toml

Lines changed: 0 additions & 19 deletions
This file was deleted.

llama-cpp-2/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ llama-cpp-sys-2 = { path = "../llama-cpp-sys-2", version = "0.1.69" }
1414
thiserror = { workspace = true }
1515
tracing = { workspace = true }
1616

17+
[dev-dependencies]
18+
encoding_rs = { workspace = true }
19+
1720
[features]
1821
cuda = ["llama-cpp-sys-2/cuda"]
1922
metal = ["llama-cpp-sys-2/metal"]
@@ -32,3 +35,7 @@ workspace = true
3235

3336
[package.metadata.docs.rs]
3437
features = ["sampler"]
38+
39+
[[example]]
40+
name = "usage"
41+
path = "../examples/usage.rs"

0 commit comments

Comments
 (0)