Skip to content

Commit

Permalink
pass temperature in lark for all lexemes
Browse files Browse the repository at this point in the history
  • Loading branch information
mmoskal committed Jan 9, 2025
1 parent 84ea558 commit 9e65e50
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 deletions.
2 changes: 2 additions & 0 deletions parser/src/earley/from_guidance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ fn grammar_from_json(
lark_to_llguidance(input.lark_grammar.as_ref().unwrap())?
};

// println!("GRM: {}", serde_json::to_string(&new_grm).unwrap());

additional_grammars = new_grm.grammars.drain(1..).collect::<Vec<_>>();
for (off, g) in additional_grammars.iter().enumerate() {
let name = g
Expand Down
4 changes: 4 additions & 0 deletions parser/src/earley/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ impl SymbolProps {
outp.push_str(format!(" max_tokens={}", props.max_tokens).as_str());
}

if props.temperature != 0.0 {
outp.push_str(format!(" temp={:.2}", props.temperature).as_str());
}

outp
}
}
Expand Down
2 changes: 1 addition & 1 deletion parser/src/lark/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ impl Compiler {
return Ok(self.builder.add_node(Node::Lexeme {
rx: RegexSpec::RegexId(rx_id),
contextual: None,
temperature: None,
temperature: rule.temperature,
json_string: None,
json_raw: None,
json_allowed_escapes: None,
Expand Down
14 changes: 9 additions & 5 deletions sample_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,16 +248,16 @@ lazy_static! {
pub fn check_lark_grammar_prompt(lark: &str, prompt_str: &str, output: &[&str]) -> Constraint {
let grm = TopLevelGrammar::from_lark(lark.to_string());
println!("\nChecking grammar:\n{}\nagainst: {:?}", lark, output);
check_grammar(&PARSER_FACTORY, prompt_str, grm, output, 0.0)
let temp = find_temperature(lark);
check_grammar(&PARSER_FACTORY, prompt_str, grm, output, temp)
}

pub fn check_lark_grammar(lark: &str, output: &[&str]) -> Constraint {
check_lark_grammar_prompt(lark, "", output)
}

pub fn check_lark_grammar_nested(lark: &str, sub_lark: &str, output: &[&str]) -> Constraint {
let temp = lark
.find("temperature=")
fn find_temperature(lark: &str) -> f32 {
lark.find("temperature=")
.map(|i| {
let i = i + "temperature=".len();
let mut end = i;
Expand All @@ -268,7 +268,11 @@ pub fn check_lark_grammar_nested(lark: &str, sub_lark: &str, output: &[&str]) ->
}
lark[i..end].parse::<f32>().unwrap()
})
.unwrap_or(0.0);
.unwrap_or(0.0)
}

pub fn check_lark_grammar_nested(lark: &str, sub_lark: &str, output: &[&str]) -> Constraint {
let temp = find_temperature(lark);
let mut top_grm = TopLevelGrammar::from_lark(lark.to_string());
let mut sub_grm = GrammarWithLexer::from_lark(sub_lark.to_string());
sub_grm.name = Some("sub".to_string());
Expand Down
15 changes: 15 additions & 0 deletions sample_parser/tests/test_ll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,21 @@ fn test_ll_temperature() {
"#,
&["", "[‧]"],
);

check_lark_grammar(
r#"start: gen
gen[temperature=0.5, stop=""]: /.*/
"#,
&["", "foo‧≺EOS≻"],
);

check_lark_grammar(
r#"start: foo | bar
foo[temperature=0.5]: "foo"
bar[temperature=0.5]: "bar"
"#,
&["", "foo"],
);
}

#[test]
Expand Down

0 comments on commit 9e65e50

Please sign in to comment.