From 2890b2c177bcfff9187d84e241300969f54bf886 Mon Sep 17 00:00:00 2001 From: hellovai Date: Thu, 13 Jun 2024 17:21:30 -0700 Subject: [PATCH] Add patch to support passthrough params for clients (#674) For example, safetey mode and response_type for gemini --- engine/baml-runtime/src/types/expression_helper.rs | 14 +++++++++++++- engine/baml-runtime/src/types/runtime_context.rs | 6 +++++- engine/baml-schema-wasm/src/runtime_wasm/mod.rs | 2 +- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/engine/baml-runtime/src/types/expression_helper.rs b/engine/baml-runtime/src/types/expression_helper.rs index ad2385273..dd05b724b 100644 --- a/engine/baml-runtime/src/types/expression_helper.rs +++ b/engine/baml-runtime/src/types/expression_helper.rs @@ -32,7 +32,19 @@ pub fn to_value(ctx: &RuntimeContext, expr: &Expression) -> Result s.clone(), - _ => anyhow::bail!("invalid key type"), + Expression::Identifier(internal_baml_core::ir::repr::Identifier::ENV( + key, + )) => match ctx.env.get(key) { + None => anyhow::bail!("unset env variable '{}'", key), + Some(val) => val.to_string(), + }, + Expression::Identifier( + internal_baml_core::ir::repr::Identifier::Local(key), + ) => key.clone(), + Expression::Identifier(internal_baml_core::ir::repr::Identifier::Ref( + key, + )) => key.join("."), + _ => anyhow::bail!("invalid key {:#?}", k), }; let v = to_value(ctx, v)?; Ok((k, v)) diff --git a/engine/baml-runtime/src/types/runtime_context.rs b/engine/baml-runtime/src/types/runtime_context.rs index 1ae1b8a62..7a645b415 100644 --- a/engine/baml-runtime/src/types/runtime_context.rs +++ b/engine/baml-runtime/src/types/runtime_context.rs @@ -44,7 +44,11 @@ impl RuntimeContext { &self, expr: &Expression, ) -> Result { - serde_json::from_value::(super::expression_helper::to_value(self, expr)?).map_err(|e| { + match super::expression_helper::to_value(self, expr) { + Ok(v) => serde_json::from_value(v).map_err(|e| e.into()), + Err(e) => Err(e), + } + .map_err(|e| { anyhow::anyhow!( "Failed to resolve expression {:?} with error: {:?}", expr, diff --git a/engine/baml-schema-wasm/src/runtime_wasm/mod.rs b/engine/baml-schema-wasm/src/runtime_wasm/mod.rs index 834afeb8b..c4e91212e 100644 --- a/engine/baml-schema-wasm/src/runtime_wasm/mod.rs +++ b/engine/baml-schema-wasm/src/runtime_wasm/mod.rs @@ -921,7 +921,7 @@ impl WasmFunction { .render_prompt(&self.name, &ctx, ¶ms, None) .as_ref() .map(|(p, scope)| (p, scope).into()) - .map_err(|e| wasm_bindgen::JsError::new(&e.to_string())) + .map_err(|e| wasm_bindgen::JsError::new(format!("{e:?}").as_str())) } #[wasm_bindgen]