Skip to content

Commit

Permalink
Get typescript integ test working with constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
imalsogreg committed Oct 16, 2024
1 parent 982b2a5 commit 528057a
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 35 deletions.
37 changes: 37 additions & 0 deletions engine/baml-lib/baml-types/src/baml_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,3 +533,40 @@ fn add_checks<'a, S: SerializeMap>(
}
Ok(())
}

#[cfg(test)]
mod tests {
use super::*;
use serde_json;
use crate::JinjaExpression;

#[test]
fn test_baml_value_with_meta_serialization() {
let baml_value: BamlValueWithMeta<Vec<ResponseCheck>> =
BamlValueWithMeta::String("hi".to_string(), vec![]);
let baml_value_2: BamlValueWithMeta<Vec<ResponseCheck>> =
BamlValueWithMeta::Class(
"ContactInfo".to_string(),
vec![
("primary".to_string(), BamlValueWithMeta::Class(
"PhoneNumber".to_string(),
vec![
("value".to_string(), BamlValueWithMeta::String(
"123-456-7890".to_string(),
vec![
ResponseCheck {
name: "foo".to_string(),
expression: "foo".to_string(),
status: "succeeded".to_string(),
}
]
))
].into_iter().collect(),
vec![]
))
].into_iter().collect(),
vec![]);
assert!(serde_json::to_value(baml_value).is_ok());
assert!(serde_json::to_value(baml_value_2).is_ok());
}
}
33 changes: 25 additions & 8 deletions engine/baml-lib/baml-types/src/constraint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,39 @@ pub enum ConstraintLevel {
/// The user-visible schema for a failed check.
#[derive(Clone, Debug, serde::Serialize)]
pub struct ResponseCheck {
pub name: Option<String>,
pub name: String,
pub expression: String,
pub status: String,
}

impl ResponseCheck {
pub fn from_constraint_result((Constraint{ level, expression, label }, succeeded): (Constraint, bool)) -> Option<Self> {
match level {
ConstraintLevel::Check => {
let status = if succeeded { "succeeded".to_string() } else { "failed".to_string() };
Some( ResponseCheck {
/// Convert a Constraint and its status to a ResponseCheck.
/// Returns `None` if the Constraint is not a check (i.e.,
/// if it doesn't meet the invariants that level==Check and
/// label==Some).
pub fn from_check_result(
(
Constraint {
level,
expression,
label,
},
succeeded,
): (Constraint, bool),
) -> Option<Self> {
match (level, label) {
(ConstraintLevel::Check, Some(label)) => {
let status = if succeeded {
"succeeded".to_string()
} else {
"failed".to_string()
};
Some(ResponseCheck {
name: label,
expression: expression.0,
status
status,
})
},
}
_ => None,
}
}
Expand Down
2 changes: 1 addition & 1 deletion engine/baml-runtime/src/internal/llm_client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub fn parsed_value_to_response(baml_value: BamlValueWithFlags) -> Result<Respon
Some(err) => Err(anyhow::anyhow!("Failed assertion: {:?}", err)),
None => Ok(baml_value_with_meta.map_meta(|cs| {
cs.into_iter()
.filter_map(|res| ResponseCheck::from_constraint_result(res))
.filter_map(|res| ResponseCheck::from_check_result(res))
.collect()
})),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ export { b } from "./sync_client"
{% endif %}
export * from "./types"
export * from "./tracing"
export * from "./checked"
export { resetBamlEnvVars } from "./globals"
export { BamlValidationError } from "@boundaryml/baml"
export { BamlValidationError, Checked } from "@boundaryml/baml"
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ impl FunctionResult {
.parsed_content()
.map_err(|e| from_anyhow_error(e))?;

Ok(serde_json::json!(BamlValue::from(parsed)))
Ok(serde_json::to_value(parsed)?)
}
}
2 changes: 1 addition & 1 deletion integ-tests/python/baml_client/partial_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from . import types


from .types import Checks__too_big, Checks__valid_email, Checks__valid_phone_number, Checks__earth_aged__no_infants, Checks__unreasonably_certain, Checks__regex_bad__regex_good__trivial, Checks__young_enough
from .types import Checks__valid_phone_number, Checks__earth_aged__no_infants, Checks__unreasonably_certain, Checks__too_big, Checks__valid_email, Checks__young_enough, Checks__regex_bad__regex_good__trivial


###############################################################################
Expand Down
16 changes: 8 additions & 8 deletions integ-tests/python/baml_client/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,29 +119,29 @@ class TestEnum(str, Enum):
F = "F"
G = "G"

class Checks__too_big(BaseModel):
too_big: baml_py.Check

class Checks__earth_aged__no_infants(BaseModel):
earth_aged: baml_py.Check
no_infants: baml_py.Check

class Checks__young_enough(BaseModel):
young_enough: baml_py.Check

class Checks__valid_email(BaseModel):
valid_email: baml_py.Check
class Checks__valid_phone_number(BaseModel):
valid_phone_number: baml_py.Check

class Checks__unreasonably_certain(BaseModel):
unreasonably_certain: baml_py.Check

class Checks__regex_bad__regex_good__trivial(BaseModel):
regex_bad: baml_py.Check
regex_good: baml_py.Check
regex_bad: baml_py.Check
trivial: baml_py.Check

class Checks__valid_phone_number(BaseModel):
valid_phone_number: baml_py.Check
class Checks__too_big(BaseModel):
too_big: baml_py.Check

class Checks__valid_email(BaseModel):
valid_email: baml_py.Check

class BigNumbers(BaseModel):

Expand Down
3 changes: 1 addition & 2 deletions integ-tests/typescript/baml_client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@ export { b } from "./async_client"

export * from "./types"
export * from "./tracing"
export * from "./checked"
export { resetBamlEnvVars } from "./globals"
export { BamlValidationError } from "@boundaryml/baml"
export { BamlValidationError, Checked } from "@boundaryml/baml"
22 changes: 11 additions & 11 deletions integ-tests/typescript/baml_client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,23 @@ export interface Checks__valid_email {
valid_email: Check
}

export interface Checks__earth_aged__no_infants {
no_infants: Check
earth_aged: Check
export interface Checks__regex_bad__regex_good__trivial {
regex_bad: Check
trivial: Check
regex_good: Check
}

export interface Checks__young_enough {
young_enough: Check
}

export interface Checks__unreasonably_certain {
unreasonably_certain: Check
}

export interface Checks__regex_bad__regex_good__trivial {
trivial: Check
regex_bad: Check
regex_good: Check
export interface Checks__earth_aged__no_infants {
earth_aged: Check
no_infants: Check
}

export interface Checks__valid_phone_number {
Expand All @@ -143,10 +147,6 @@ export interface Checks__too_big {
too_big: Check
}

export interface Checks__young_enough {
young_enough: Check
}

export interface BigNumbers {
a: number
b: number
Expand Down
Loading

0 comments on commit 528057a

Please sign in to comment.