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

Generic multimodal BAML type #692

Merged
merged 33 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
6eb87e6
refactoring media type
anish-palakurthi Jun 14, 2024
e8b8164
image works for gpt, claude. validating gemini
anish-palakurthi Jun 17, 2024
39f86a8
all providers support media type'
anish-palakurthi Jun 17, 2024
1210260
audio request works, create baml type
anish-palakurthi Jun 17, 2024
eba57f2
merged media
anish-palakurthi Jun 17, 2024
b5e2151
audio end to end, add rendering next
anish-palakurthi Jun 18, 2024
c9496bf
audio rendering works in playground
anish-palakurthi Jun 18, 2024
f59d310
rebasing canary
anish-palakurthi Jun 19, 2024
ea34076
added ts integ test
anish-palakurthi Jun 19, 2024
eac42e7
syntax highlighting for audio
anish-palakurthi Jun 19, 2024
30ac92c
PR cleanup
anish-palakurthi Jun 19, 2024
d15d360
display for bmt
anish-palakurthi Jun 19, 2024
2e637e7
Revert "display for bmt"
anish-palakurthi Jun 19, 2024
612fc45
BamlMediaType display trait
anish-palakurthi Jun 19, 2024
0688ccc
lock file
anish-palakurthi Jun 19, 2024
61c4157
re-generated clients
anish-palakurthi Jun 19, 2024
bcbebc3
optionals
anish-palakurthi Jun 19, 2024
fe3ae38
explicit image and audio prefixes
anish-palakurthi Jun 19, 2024
cc547b8
separated b64
anish-palakurthi Jun 19, 2024
dfe054a
curl works
anish-palakurthi Jun 20, 2024
6c03c3b
added integs
anish-palakurthi Jun 20, 2024
3f3b238
media url param
anish-palakurthi Jun 20, 2024
5a5baa7
removed old curl code
anish-palakurthi Jun 20, 2024
f8113f7
removed async for build_request
anish-palakurthi Jun 20, 2024
f8a7341
removed async from google
anish-palakurthi Jun 20, 2024
f9d7774
Merge branch 'canary' into multimodal_generic
anish-palakurthi Jun 20, 2024
7f0af61
fixed client role resolution
anish-palakurthi Jun 20, 2024
c0e9530
claude urls work, datab64 urls escape
anish-palakurthi Jun 20, 2024
6f7de88
Merge branch 'canary' into multimodal_generic
anish-palakurthi Jun 20, 2024
b150672
logs
anish-palakurthi Jun 20, 2024
165dd0a
fixed typescript media tojson
anish-palakurthi Jun 20, 2024
67ab411
comments
anish-palakurthi Jun 20, 2024
ec5ebc9
build tests
anish-palakurthi Jun 20, 2024
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
19 changes: 18 additions & 1 deletion docs/docs/syntax/type.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ title: Supported Types

- **Syntax:** `null`

### ✅ Images
### ✅ Images

You can use an image like this:

Expand All @@ -47,6 +47,23 @@ function DescribeImage(myImg: image) -> string {
}
```


### ✅ Audio
We support audio for existing models that support it, such as Gemini Pro and Flash.
You can use audio like this:

```rust
function DescribeAudio(myAudio: audio) -> string {
client GPT4Turbo
prompt #"
{{ _.role("user")}}
Describe the tone of this song in four words:
{{ myAudio }}
"#
}
```


### ⚠️ bytes

- Not yet supported. Use a `string[]` or `int[]` instead.
Expand Down
27 changes: 27 additions & 0 deletions engine/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 43 additions & 6 deletions engine/baml-lib/baml-core/src/ir/ir_helpers/to_baml_arg.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use baml_types::{BamlMap, BamlValue, TypeValue};
use baml_types::{BamlMap, BamlMedia, BamlMediaType, BamlValue, TypeValue};

use crate::ir::{FieldType, IntermediateRepr};

Expand Down Expand Up @@ -57,18 +57,23 @@ pub fn validate_arg(
TypeValue::Bool if matches!(value, BamlValue::Bool(_)) => Some(value.clone()),
TypeValue::Null if matches!(value, BamlValue::Null) => Some(value.clone()),
TypeValue::Image => match value {
BamlValue::Image(v) => Some(BamlValue::Image(v.clone())),
BamlValue::Media(v) => Some(BamlValue::Media(v.clone())),
BamlValue::Map(kv) => {
if let Some(BamlValue::String(s)) = kv.get("url") {
Some(BamlValue::Image(baml_types::BamlImage::url(s.to_string())))
Some(BamlValue::Media(baml_types::BamlMedia::url(
BamlMediaType::Image,
s.to_string(),
None,
)))
} else if let (
Some(BamlValue::String(s)),
Some(BamlValue::String(media_type)),
Some(BamlValue::String(media_type_str)),
) = (kv.get("base64"), kv.get("media_type"))
{
Some(BamlValue::Image(baml_types::BamlImage::base64(
Some(BamlValue::Media(baml_types::BamlMedia::base64(
BamlMediaType::Image,
s.to_string(),
media_type.to_string(),
media_type_str.to_string(),
)))
} else {
scope.push_error(format!(
Expand All @@ -83,6 +88,38 @@ pub fn validate_arg(
None
}
},
TypeValue::Audio => match value {
BamlValue::Media(v) => Some(BamlValue::Media(v.clone())),
BamlValue::Map(kv) => {
if let Some(BamlValue::String(s)) = kv.get("url") {
Some(BamlValue::Media(baml_types::BamlMedia::url(
BamlMediaType::Audio,
s.to_string(),
None,
)))
} else if let (
Some(BamlValue::String(s)),
Some(BamlValue::String(media_type_str)),
) = (kv.get("base64"), kv.get("media_type"))
{
Some(BamlValue::Media(baml_types::BamlMedia::base64(
BamlMediaType::Audio,
s.to_string(),
media_type_str.to_string(),
)))
} else {
scope.push_error(format!(
"Invalid audio: expected `url` or (`base64` and `media_type`), got `{}`",
value
));
None
}
}
_ => {
scope.push_error(format!("Expected type {:?}, got `{}`", t, value));
None
}
},
_ => {
scope.push_error(format!("Expected type {:?}, got `{}`", t, value));
None
Expand Down
11 changes: 11 additions & 0 deletions engine/baml-lib/baml-core/src/ir/json_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,17 @@ impl<'db> WithJsonSchema for FieldType {
"required": ["url"],

}),
TypeValue::Audio => json!({
// anyOf either an object that has a uri, or it has a base64 string
"type": "object",
"properties": {
"url": {
"type": "string",
// "format": "uri",
}
},
"required": ["url"],
}),
},
FieldType::List(item) => json!({
"type": "array",
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 @@ -340,6 +340,10 @@ impl<'a> Walker<'a, &'a Client> {
pub fn span(&self) -> Option<&crate::Span> {
self.item.attributes.span.as_ref()
}

pub fn options(&self) -> &Vec<(String, Expression)> {
&self.elem().options
}
}

impl<'a> Walker<'a, &'a RetryPolicy> {
Expand Down
50 changes: 35 additions & 15 deletions engine/baml-lib/baml-types/src/baml_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use std::{collections::HashSet, fmt};

use serde::{de::Visitor, ser::SerializeStruct, Deserialize, Deserializer};

use crate::{BamlImage, BamlMap};
use crate::media::BamlMediaType;
use crate::{BamlMap, BamlMedia};

#[derive(Debug, PartialEq, Clone)]
pub enum BamlValue {
Expand All @@ -12,7 +13,7 @@ pub enum BamlValue {
Bool(bool),
Map(BamlMap<String, BamlValue>),
List(Vec<BamlValue>),
Image(BamlImage),
Media(BamlMedia),
Enum(String, String),
Class(String, BamlMap<String, BamlValue>),
Null,
Expand All @@ -27,19 +28,33 @@ impl serde::Serialize for BamlValue {
BamlValue::Bool(b) => serializer.serialize_bool(*b),
BamlValue::Map(m) => m.serialize(serializer),
BamlValue::List(l) => l.serialize(serializer),
BamlValue::Image(i) => {
let mut s = serializer.serialize_struct("BamlImage", 2)?;
match i {
BamlImage::Url(u) => {
s.serialize_field("url", &u.url)?;
}
BamlImage::Base64(b) => {
s.serialize_field("base64", &b.base64)?;
s.serialize_field("media_type", &b.media_type)?;
}
BamlValue::Media(i) => match i {
BamlMedia::Url(BamlMediaType::Image, u) => {
let mut s = serializer.serialize_struct("BamlImage", 2)?;
s.serialize_field("url", &u.url)?;
s.end()
}
s.end()
}
BamlMedia::Base64(BamlMediaType::Image, b) => {
let mut s = serializer.serialize_struct("BamlImage", 2)?;
s.serialize_field("base64", &b.base64)?;
s.serialize_field("media_type", &b.media_type)?;
s.end()
}
BamlMedia::Url(BamlMediaType::Audio, u) => {
let mut s = serializer.serialize_struct("BamlAudio", 2)?;

s.serialize_field("url", &u.url)?;
s.end()
}
BamlMedia::Base64(BamlMediaType::Audio, b) => {
let mut s = serializer.serialize_struct("BamlAudio", 2)?;

s.serialize_field("base64", &b.base64)?;
s.serialize_field("media_type", &b.media_type)?;
s.end()
}
},

BamlValue::Enum(_, v) => serializer.serialize_str(v),
BamlValue::Class(_, m) => m.serialize(serializer),
BamlValue::Null => serializer.serialize_none(),
Expand Down Expand Up @@ -82,7 +97,12 @@ impl BamlValue {
format!("list<{}>", value_type)
}
}
BamlValue::Image(_) => "image".into(),
BamlValue::Media(m) => match m {
BamlMedia::Url(BamlMediaType::Image, _) => "image".into(),
BamlMedia::Base64(BamlMediaType::Image, _) => "image".into(),
BamlMedia::Url(BamlMediaType::Audio, _) => "audio".into(),
BamlMedia::Base64(BamlMediaType::Audio, _) => "audio".into(),
},
BamlValue::Enum(e, _) => format!("enum {}", e),
BamlValue::Class(c, _) => format!("class {}", c),
BamlValue::Null => "null".into(),
Expand Down
2 changes: 2 additions & 0 deletions engine/baml-lib/baml-types/src/field_type/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub enum TypeValue {
// Char,
Null,
Image,
Audio,
}

impl std::fmt::Display for TypeValue {
Expand All @@ -20,6 +21,7 @@ impl std::fmt::Display for TypeValue {
TypeValue::Bool => write!(f, "bool"),
TypeValue::Null => write!(f, "null"),
TypeValue::Image => write!(f, "image"),
TypeValue::Audio => write!(f, "audio"),
}
}
}
Expand Down
41 changes: 0 additions & 41 deletions engine/baml-lib/baml-types/src/image.rs

This file was deleted.

4 changes: 2 additions & 2 deletions engine/baml-lib/baml-types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod image;
mod map;
mod media;
#[cfg(feature = "mini-jinja")]
mod minijinja;

Expand All @@ -8,5 +8,5 @@ mod field_type;

pub use baml_value::BamlValue;
pub use field_type::{FieldType, TypeValue};
pub use image::{BamlImage, ImageBase64, ImageUrl};
pub use map::Map as BamlMap;
pub use media::{BamlMedia, BamlMediaType, MediaBase64, MediaUrl};
Loading
Loading