Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding better UX for playground #742

Merged
merged 8 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions engine/baml-lib/baml-core/src/ir/repr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ use internal_baml_parser_database::{
ClassWalker, ClientWalker, ConfigurationWalker, EnumValueWalker, EnumWalker, FieldWalker,
FunctionWalker, TemplateStringWalker, VariantWalker,
},
ParserDatabase, PromptAst, RetryPolicyStrategy, ToStringAttributes,
WithStaticRenames,
ParserDatabase, PromptAst, RetryPolicyStrategy, ToStringAttributes, WithStaticRenames,
};

use internal_baml_schema_ast::ast::{self, FieldArity, WithName, WithSpan};
Expand Down Expand Up @@ -1102,6 +1101,14 @@ pub struct TestCase {
}

impl WithRepr<TestCase> for ConfigurationWalker<'_> {
fn attributes(&self, _db: &ParserDatabase) -> NodeAttributes {
NodeAttributes {
meta: IndexMap::new(),
overrides: IndexMap::new(),
span: Some(self.span().clone()),
}
}

fn repr(&self, db: &ParserDatabase) -> Result<TestCase> {
Ok(TestCase {
name: self.name().to_string(),
Expand Down
4 changes: 4 additions & 0 deletions engine/baml-lib/baml-core/src/ir/walker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ impl<'a> Walker<'a, (&'a Function, &'a TestCase)> {
&self.item.1.elem
}

pub fn span(&self) -> Option<&crate::Span> {
self.item.1.attributes.span.as_ref()
}

pub fn test_case_params(
&self,
env_values: &HashMap<String, String>,
Expand Down
3 changes: 3 additions & 0 deletions engine/baml-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ pub use internal_baml_jinja::{ChatMessagePart, RenderedPrompt};
#[cfg(feature = "internal")]
pub use runtime_interface::InternalRuntimeInterface;

#[cfg(feature = "internal")]
pub use internal_baml_core as internal_core;

#[cfg(not(feature = "internal"))]
pub(crate) use internal_baml_jinja::{ChatMessagePart, RenderedPrompt};
#[cfg(not(feature = "internal"))]
Expand Down
61 changes: 51 additions & 10 deletions engine/baml-schema-wasm/src/runtime_wasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ pub struct WasmFunction {
pub test_cases: Vec<WasmTestCase>,
#[wasm_bindgen(readonly)]
pub test_snippet: String,
#[wasm_bindgen(readonly)]
pub signature: String,
}

#[wasm_bindgen(getter_with_clone, inspectable)]
Expand All @@ -268,6 +270,31 @@ pub struct WasmSpan {
pub start: usize,
#[wasm_bindgen(readonly)]
pub end: usize,
#[wasm_bindgen(readonly)]
pub start_line: usize,
}

impl From<&baml_runtime::internal_core::internal_baml_diagnostics::Span> for WasmSpan {
fn from(span: &baml_runtime::internal_core::internal_baml_diagnostics::Span) -> Self {
let (start, end) = span.line_and_column();
WasmSpan {
file_path: span.file.path().to_string(),
start: span.start,
end: span.end,
start_line: start.0,
}
}
}

impl Default for WasmSpan {
fn default() -> Self {
WasmSpan {
file_path: "".to_string(),
start: 0,
end: 0,
start_line: 0,
}
}
}

#[wasm_bindgen(getter_with_clone, inspectable)]
Expand All @@ -279,6 +306,8 @@ pub struct WasmTestCase {
pub inputs: Vec<WasmParam>,
#[wasm_bindgen(readonly)]
pub error: Option<String>,
#[wasm_bindgen(readonly)]
pub span: WasmSpan,
}

#[wasm_bindgen(getter_with_clone, inspectable)]
Expand Down Expand Up @@ -743,21 +772,27 @@ impl WasmRuntime {
);

let wasm_span = match f.span() {
Some(span) => WasmSpan {
file_path: span.file.path().to_string(),
start: span.start,
end: span.end,
},
None => WasmSpan {
file_path: "".to_string(),
start: 0,
end: 0,
},
Some(span) => span.into(),
None => WasmSpan::default(),
};

WasmFunction {
name: f.name().to_string(),
span: wasm_span,
signature: {
let inputs = f
.inputs()
.right()
.map(|func_params| {
func_params
.iter()
.map(|(k, t)| format!("{}: {}", k, t))
.collect::<Vec<_>>()
.join(", ")
})
.unwrap_or_default();
format!("({}) -> {}", inputs, f.output().to_string())
},
test_snippet: snippet,
test_cases: f
.walk_tests()
Expand Down Expand Up @@ -812,10 +847,16 @@ impl WasmRuntime {
}
});

let wasm_span = match tc.span() {
Some(span) => span.into(),
None => WasmSpan::default(),
};

WasmTestCase {
name: tc.test_case().name.clone(),
inputs: params,
error,
span: wasm_span,
}
})
.collect(),
Expand Down
2 changes: 1 addition & 1 deletion integ-tests/baml_src/fiddle-examples/images/image.baml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ function DescribeImage4(classWithImage: ClassWithImage, img2: image) -> string {
test TestName {
functions [DescribeImage]
args {
img { url "https://imgs.xkcd.com/comics/standards.png"}
img { url "http://imgs.xkcd.com/comics/standards.png" }
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
function AudioInput(aud: audio) -> string{
class Response {
type string
reason string
appropriate_for_video_games bool
score int @description(#"
1-5, 1 being best at video game background music and 5 being worst
"#)
}

function AudioInput(aud: audio) -> Response {
client Gemini
prompt #"
{{ _.role("user") }}
Describe the sound.

Does this sound like a roar? Yes or no? One word no other characters.
{{ ctx.output_format(prefix="Answer in this schema:\n") }}


{{ _.role("user") }}

{{ aud }}
"#
Expand Down
15 changes: 8 additions & 7 deletions integ-tests/baml_src/test-files/testing_pipeline/resume.baml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ class Resume {
name string
email string
phone string
experience Education[]
education string[]
experience string[]
education Education[]
skills string[]
}

Expand All @@ -12,7 +12,9 @@ class Education {
location string
degree string
major string[]
graduation_date string?
date string? @alias(start_date) @description(#"
YYYY-MM
"#)
}

template_string AddRole(foo: string) #"
Expand All @@ -29,7 +31,6 @@ function ExtractResume(resume: string, img: image?) -> Resume {

Extract data:


<<<<
{{ resume }}
<<<<
Expand All @@ -45,9 +46,9 @@ function ExtractResume(resume: string, img: image?) -> Resume {
test sam_resume {
functions [ExtractResume]
input {
img {
url "https://avatars.githubusercontent.com/u/1016595?v=4"
}
// img {
// url "https://avatars.githubusercontent.com/u/1016595?v=4"
// }
resume #"
Sam Lijin
he/him | [email protected] | sxlijin.github.io | 111-222-3333 | sxlijin | sxlijin
Expand Down
12 changes: 6 additions & 6 deletions integ-tests/python/baml_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ async def AudioInput(
self,
aud: baml_py.Audio,
baml_options: BamlCallOptions = {},
) -> str:
) -> types.Response:
__tb__ = baml_options.get("tb", None)
if __tb__ is not None:
tb = __tb__._tb
Expand All @@ -74,7 +74,7 @@ async def AudioInput(
self.__ctx_manager.get(),
tb,
)
mdl = create_model("AudioInputReturnType", inner=(str, ...))
mdl = create_model("AudioInputReturnType", inner=(types.Response, ...))
return coerce(mdl, raw.parsed())

async def ClassifyMessage(
Expand Down Expand Up @@ -1433,7 +1433,7 @@ def AudioInput(
self,
aud: baml_py.Audio,
baml_options: BamlCallOptions = {},
) -> baml_py.BamlStream[Optional[str], str]:
) -> baml_py.BamlStream[partial_types.Response, types.Response]:
__tb__ = baml_options.get("tb", None)
if __tb__ is not None:
tb = __tb__._tb
Expand All @@ -1450,10 +1450,10 @@ def AudioInput(
tb,
)

mdl = create_model("AudioInputReturnType", inner=(str, ...))
partial_mdl = create_model("AudioInputPartialReturnType", inner=(Optional[str], ...))
mdl = create_model("AudioInputReturnType", inner=(types.Response, ...))
partial_mdl = create_model("AudioInputPartialReturnType", inner=(partial_types.Response, ...))

return baml_py.BamlStream[Optional[str], str](
return baml_py.BamlStream[partial_types.Response, types.Response](
raw,
lambda x: coerce(partial_mdl, x),
lambda x: coerce(mdl, x),
Expand Down
Loading
Loading