Skip to content

Commit

Permalink
Add basic grammar for a vs an articles (#1158)
Browse files Browse the repository at this point in the history
<!-- ELLIPSIS_HIDDEN -->



> [!IMPORTANT]
> Add function to determine 'a' or 'an' for indefinite articles and
integrate it into output format rendering logic.
> 
>   - **Behavior**:
> - Add `indefinite_article_a_or_an()` function in `types.rs` to
determine 'a' or 'an' based on the first letter of a word.
> - Update `OutputFormatContent::prefix()` to use
`indefinite_article_a_or_an()` for primitive types.
>   - **Documentation**:
> - Update `output-format.mdx` to reflect changes in default prefixes
for primitive types.
>   - **Tests**:
> - Add tests `render_int()` and `render_float()` in `types.rs` to
verify correct article usage.
> 
> <sup>This description was created by </sup>[<img alt="Ellipsis"
src="https://img.shields.io/badge/Ellipsis-blue?color=175173">](https://www.ellipsis.dev?ref=BoundaryML%2Fbaml&utm_source=github&utm_medium=referral)<sup>
for 7c26436. It will automatically
update as commits are pushed.</sup>

<!-- ELLIPSIS_HIDDEN -->
  • Loading branch information
antoniosarosi authored Nov 11, 2024
1 parent e7021c5 commit e084130
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 e084130

Please sign in to comment.