Skip to content

Commit b670af5

Browse files
committed
move grammar
1 parent 270e589 commit b670af5

File tree

8 files changed

+126
-154
lines changed

8 files changed

+126
-154
lines changed

Cargo.lock

+3-19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ hypr-db-user = { path = "crates/db-user", package = "db-user" }
2727
hypr-detect = { path = "crates/detect", package = "detect" }
2828
hypr-diart = { path = "crates/diart", package = "diart" }
2929
hypr-file = { path = "crates/file", package = "file" }
30+
hypr-gbnf = { path = "crates/gbnf", package = "gbnf" }
3031
hypr-gguf = { path = "crates/gguf", package = "gguf" }
3132
hypr-host = { path = "crates/host", package = "host" }
3233
hypr-llama = { path = "crates/llama", package = "llama" }

crates/gbnf/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ minijinja = { workspace = true }
88

99
[dev-dependencies]
1010
gbnf-validator = { workspace = true }
11+
12+
colored = "3"
13+
indoc = "2"

crates/gbnf/src/lib.rs

+106-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,106 @@
1-
pub const GBNF_GRAMMAR: &str = "TODO";
1+
pub const ENHANCE_AUTO: &str = include_str!("../assets/enhance-auto.gbnf");
2+
3+
#[cfg(test)]
4+
mod tests {
5+
use super::*;
6+
use indoc::indoc;
7+
8+
#[test]
9+
fn test_1() {
10+
let gbnf = gbnf_validator::Validator::new().unwrap();
11+
let input_1 = "<headers>\n- Objective\n- Key Takeaways\n- Importance of Complementary Skills\n- Benefits of Using Online Resources\n- Advice for Undergrad Students\n</headers># Objective\n\n- **Search is the Best Way to Find Answers**: The speaker emphasizes the importance of utilizing online resources like Google to find answers to questions.\n- **Value in Complementary Skills**: The speaker highlights the need to acquire complementary skills to traditional research methods.\n\n# Key Takeaways\n\n- **Complementary skills include both traditional research and online resource utilization**: The speaker suggests that skills like using a blank sheet of paper with no Internet and effective Google searching are essential.\n- **Online resources can help find pre-solved problems**: The speaker advises investing time in finding existing resources and communities that have already solved problems.\n\n# Importance of Complementary Skills\n\n- **Traditional research is just the starting point**: The speaker suggests that traditional research methods are just the beginning and should be complemented with other skills.\n- **Effective use of online resources can save time and effort**: The speaker highlights the benefits of utilizing online resources in research and problem-solving.\n\n# Benefits of Using Online Resources\n\n- **Access to knowledge from experts and communities**: The speaker suggests that online resources provide access to knowledge and expertise from experienced individuals.\n- **Time-saving and efficient**: The speaker emphasizes the benefits of finding pre-solved problems through online resources.\n\n# Advice for Undergrad Students\n\n- **Start by searching online**: The speaker advises undergrad students to start by searching online for answers to questions and exploring different resources.\n- **Be open to finding existing solutions**: The speaker emphasizes the importance of being open to finding pre-solved problems and leveraging existing resources.\n\n";
12+
let input_2 = indoc! {"
13+
<headers>
14+
- Objective
15+
- Key Takeaways
16+
- Importance of Complementary Skills
17+
- Benefits of Using Online Resources
18+
- Advice for Undergrad Students
19+
</headers># Objective
20+
21+
- **Search is the Best Way to Find Answers**: The speaker emphasizes the importance of utilizing online resources like Google to find answers to questions.
22+
- **Value in Complementary Skills**: The speaker highlights the need to acquire complementary skills to traditional research methods.
23+
24+
# Key Takeaways
25+
26+
- **Complementary skills include both traditional research and online resource utilization**: The speaker suggests that skills like using a blank sheet of paper with no Internet and effective Google searching are essential.
27+
- **Online resources can help find pre-solved problems**: The speaker advises investing time in finding existing resources and communities that have already solved problems.
28+
29+
# Importance of Complementary Skills
30+
31+
- **Traditional research is just the starting point**: The speaker suggests that traditional research methods are just the beginning and should be complemented with other skills.
32+
- **Effective use of online resources can save time and effort**: The speaker highlights the benefits of utilizing online resources in research and problem-solving.
33+
34+
# Benefits of Using Online Resources
35+
36+
- **Access to knowledge from experts and communities**: The speaker suggests that online resources provide access to knowledge and expertise from experienced individuals.
37+
- **Time-saving and efficient**: The speaker emphasizes the benefits of finding pre-solved problems through online resources.
38+
39+
# Advice for Undergrad Students
40+
41+
- **Start by searching online**: The speaker advises undergrad students to start by searching online for answers to questions and exploring different resources.
42+
- **Be open to finding existing solutions**: The speaker emphasizes the importance of being open to finding pre-solved problems and leveraging existing resources.
43+
44+
"};
45+
46+
assert_eq!(input_1, input_2);
47+
assert!(gbnf.validate(ENHANCE_AUTO, input_1).unwrap());
48+
}
49+
50+
#[test]
51+
fn test_2() {
52+
let gbnf = gbnf_validator::Validator::new().unwrap();
53+
let input = indoc! {"
54+
<headers>
55+
- Objective
56+
- Key Takeaways
57+
- Importance of Complementary Skills
58+
- Benefits of Using Online Resources
59+
- Advice for Undergrad Students
60+
</headers># Objective
61+
62+
- **Search is the Best Way to Find Answers**: The speaker emphasizes the importance of utilizing online resources like Google to find answers to questions.
63+
- **Value in Complementary Skills**: The speaker highlights the need to acquire complementary skills to traditional research methods.
64+
65+
# Key Takeaways
66+
67+
- **Complementary skills include both traditional research and online resource utilization**: The speaker suggests that skills like using a blank sheet of paper with no Internet and effective Google searching are essential.
68+
- **Online resources can help find pre-solved problems**: The speaker advises investing time in finding existing resources and communities that have already solved problems.
69+
70+
# Importance of Complementary Skills
71+
72+
- **Traditional research is just the starting point**: The speaker suggests that traditional research methods are just the beginning and should be complemented with other skills.
73+
- **Effective use of online resources can save time and effort**: The speaker highlights the benefits of utilizing online resources in research and problem-solving.
74+
75+
# Benefits of Using Online Resources
76+
77+
- **Access to knowledge from experts and communities**: The speaker suggests that online resources provide access to knowledge and expertise from experienced individuals.
78+
- **Time-saving and efficient**: The speaker emphasizes the benefits of finding pre-solved problems through online resources.
79+
80+
# Advice for Undergrad Students
81+
82+
- **Start by searching online**: The speaker advises undergrad students to start by searching online for answers to questions and exploring different resources.
83+
- **Be open to finding existing solutions**: The speaker emphasizes the importance of being open to finding pre-solved problems and leveraging existing resources.
84+
- **Follow on X**: [Thank you](https://x.com/yujonglee).
85+
86+
"};
87+
88+
assert!(gbnf.validate(ENHANCE_AUTO, input).unwrap());
89+
}
90+
91+
#[allow(dead_code)]
92+
fn debug_grammar_failure_point(gbnf: &gbnf_validator::Validator, grammar: &str, text: &str) {
93+
use colored::Colorize;
94+
95+
for length in 1..=text.len() {
96+
let substring = &text[0..length];
97+
let current_char = text.chars().nth(length - 1).unwrap();
98+
99+
match gbnf.validate(grammar, substring) {
100+
Ok(true) => print!("{}", current_char.to_string().green()),
101+
Ok(false) => print!("{}", current_char.to_string().red()),
102+
Err(_) => print!("{}", current_char.to_string().yellow()),
103+
}
104+
}
105+
}
106+
}

crates/llama/Cargo.toml

+12-15
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,9 @@ name = "llama"
33
version = "0.1.0"
44
edition = "2021"
55

6-
[dev-dependencies]
7-
hypr-buffer = { workspace = true }
8-
hypr-data = { workspace = true }
9-
hypr-listener-interface = { workspace = true }
10-
hypr-template = { workspace = true }
11-
hypr-timeline = { workspace = true }
12-
13-
colored = "3.0.0"
14-
dirs = { workspace = true }
15-
indoc = "2.0.6"
16-
minijinja = { workspace = true }
17-
rand = "0.9.0"
18-
serde_json = { workspace = true }
19-
206
[dependencies]
7+
hypr-gbnf = { workspace = true }
218
hypr-gguf = { workspace = true }
22-
include_url_macro = { workspace = true }
239

2410
encoding_rs = "0.8.35"
2511
gbnf-validator = { workspace = true }
@@ -37,3 +23,14 @@ llama-cpp-2 = { git = "https://github.com/utilityai/llama-cpp-rs", default-featu
3723

3824
[target.'cfg(target_os = "macos")'.dependencies]
3925
llama-cpp-2 = { git = "https://github.com/utilityai/llama-cpp-rs", features = ["openmp", "native", "metal"], branch = "update-llama-cpp-2025-04-06" }
26+
27+
[dev-dependencies]
28+
hypr-buffer = { workspace = true }
29+
hypr-data = { workspace = true }
30+
hypr-listener-interface = { workspace = true }
31+
hypr-template = { workspace = true }
32+
hypr-timeline = { workspace = true }
33+
34+
dirs = { workspace = true }
35+
rand = "0.9.0"
36+
serde_json = { workspace = true }

crates/llama/src/grammar/mod.rs

-117
This file was deleted.

crates/llama/src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use tokio_stream::wrappers::UnboundedReceiverStream;
1313
use hypr_gguf::GgufExt;
1414

1515
mod error;
16-
mod grammar;
1716
mod message;
1817
mod stream;
1918

@@ -101,7 +100,7 @@ impl Llama {
101100
let mut n_cur = batch.n_tokens();
102101
let mut decoder = encoding_rs::UTF_8.new_decoder();
103102
let mut sampler = LlamaSampler::chain_simple([
104-
LlamaSampler::grammar(&model, grammar::MARKDOWN_GRAMMAR, "root"),
103+
LlamaSampler::grammar(&model, hypr_gbnf::ENHANCE_AUTO, "root"),
105104
LlamaSampler::temp(0.8),
106105
LlamaSampler::penalties(0, 1.4, 0.1, 0.0),
107106
LlamaSampler::mirostat_v2(1234, 3.0, 0.2),

0 commit comments

Comments
 (0)