Skip to content

Commit

Permalink
fix single terminal grammars
Browse files Browse the repository at this point in the history
  • Loading branch information
mmoskal committed Jun 11, 2024
1 parent be3077c commit 5b6eb86
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
11 changes: 9 additions & 2 deletions controllers/ag2_ctrl/run_g.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,17 @@ def character_maker2(lm, id, description, valid_weapons):
grm = string("this is a test")

prompt = "How much is 2 + 2? "
grm = gen(name="test", max_tokens=30, regex=r"\d+")
grm = gen(name="test", max_tokens=30, regex=r"[0-9]+", stop=".")

prompt = "Three things about J. Random Hacker:\n"
grm = gen_json_object("hacker", max_tokens=150) + "\nScore (0-9): " + gen("score", regex=r"[0-9]")
grm = (
gen_json_object("hacker", max_tokens=150)
+ "\nScore (0-9): "
+ gen("score", regex=r"[0-9]")
)

prompt = "A number: "
grm = gen_grammar("", greedy_grammar(lexeme(r"[0-9]{3}")))

# grm = "Q: 7 * 8\nA: " + gen("text", regex="[0-9]+", max_tokens=20) + "\n"

Expand Down
7 changes: 7 additions & 0 deletions controllers/ag2_ctrl/src/earley/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,13 @@ impl Grammar {
}

let mut outp = Grammar::new();

let start_data = self.sym_data(self.start());
if start_data.is_terminal() {
let new_start = outp.fresh_symbol("_start_repl");
outp.add_rule(new_start, vec![SymIdx(1)]).unwrap();
}

for sym in &self.symbols {
if repl.contains_key(&sym.idx) {
continue;
Expand Down
2 changes: 1 addition & 1 deletion controllers/ag2_ctrl/src/earley/lexerspec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl Debug for LexemeSpec {
write!(f, " eos-only")?;
}
if self.contextual {
write!(f, " allow-others")?;
write!(f, " contextual")?;
}
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion py/guidance

0 comments on commit 5b6eb86

Please sign in to comment.