Skip to content

Commit

Permalink
Add basic grammar for a vs an articles
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniosarosi committed Nov 11, 2024
1 parent 1c96c14 commit 7c26436
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
29 changes: 28 additions & 1 deletion engine/baml-lib/jinja-runtime/src/output_format/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,16 @@ impl<'s> std::fmt::Display for MapRender<'s> {
}
}

/// Basic grammar for "a" VS "an" indefinite articles.
///
/// It does NOT cover all rules & exceptions.
fn indefinite_article_a_or_an(word: &str) -> &str {
match word.chars().next() {
Some(c) if matches!(c.to_ascii_lowercase(), 'a' | 'e' | 'i' | 'o' | 'u') => "an",
_ => "a",
}
}

struct RenderState {
hoisted_enums: IndexSet<String>,
}
Expand All @@ -302,7 +312,10 @@ impl OutputFormatContent {
) -> Option<String> {
match ft {
FieldType::Primitive(TypeValue::String) => None,
FieldType::Primitive(_) => Some(String::from("Answer as a: ")),
FieldType::Primitive(p) => Some(format!(
"Answer as {article} ",
article = indefinite_article_a_or_an(&p.to_string())
)),
FieldType::Literal(_) => Some(String::from("Answer using this specific value:\n")),
FieldType::Enum(_) => Some(String::from("Answer with any of the categories:\n")),
FieldType::Class(cls) => {
Expand Down Expand Up @@ -659,6 +672,20 @@ mod tests {
assert_eq!(rendered, None);
}

#[test]
fn render_int() {
let content = OutputFormatContent::target(FieldType::int()).build();
let rendered = content.render(RenderOptions::default()).unwrap();
assert_eq!(rendered, Some("Answer as an int".into()));
}

#[test]
fn render_float() {
let content = OutputFormatContent::target(FieldType::float()).build();
let rendered = content.render(RenderOptions::default()).unwrap();
assert_eq!(rendered, Some("Answer as a float".into()));
}

#[test]
fn render_array() {
let content = OutputFormatContent::new_array();
Expand Down
3 changes: 2 additions & 1 deletion fern/03-reference/baml/prompt-syntax/output-format.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ BAML's default prefix varies based on the function's return type.
| Fuction return type | Default Prefix |
| --- | --- |
| Primitive (String) | |
| Primitive (Other) | `Answer as a: ` |
| Primitive (Int) | `Answer as an ` |
| Primitive (Other) | `Answer as a ` |
| Enum | `Answer with any of the categories:\n` |
| Class | `Answer in JSON using this schema:\n` |
| List | `Answer with a JSON Array using this schema:\n` |
Expand Down

0 comments on commit 7c26436

Please sign in to comment.