baml-cc 0.208.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/exe/baml +1 -0
- data/exe/baml-cli +4 -0
- data/ext/ruby_ffi/Cargo.toml +52 -0
- data/ext/ruby_ffi/build.rs +21 -0
- data/ext/ruby_ffi/extconf.rb +17 -0
- data/ext/ruby_ffi/src/function_result.rs +75 -0
- data/ext/ruby_ffi/src/function_result_stream.rs +63 -0
- data/ext/ruby_ffi/src/lib.rs +388 -0
- data/ext/ruby_ffi/src/ruby_to_json.rs +651 -0
- data/ext/ruby_ffi/src/types/client_registry.rs +71 -0
- data/ext/ruby_ffi/src/types/lang_wrapper.rs +60 -0
- data/ext/ruby_ffi/src/types/log_collector.rs +607 -0
- data/ext/ruby_ffi/src/types/media.rs +142 -0
- data/ext/ruby_ffi/src/types/mod.rs +8 -0
- data/ext/ruby_ffi/src/types/request.rs +107 -0
- data/ext/ruby_ffi/src/types/response.rs +48 -0
- data/ext/ruby_ffi/src/types/runtime_ctx_manager.rs +18 -0
- data/ext/ruby_ffi/src/types/type_builder.rs +334 -0
- data/lib/baml.rb +63 -0
- data/lib/checked.rb +36 -0
- data/lib/stream.rb +87 -0
- data/lib/struct.rb +66 -0
- metadata +69 -0
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
use std::{cell::RefCell, str::FromStr};
|
|
2
|
+
|
|
3
|
+
use baml_runtime::client_registry;
|
|
4
|
+
use magnus::{
|
|
5
|
+
class, function, method, scan_args::scan_args, Error, Module, Object, RHash, Ruby, Value,
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
use crate::{ruby_to_json, Result};
|
|
9
|
+
|
|
10
|
+
#[magnus::wrap(class = "Baml::Ffi::ClientRegistry", free_immediately, size)]
|
|
11
|
+
pub(crate) struct ClientRegistry {
|
|
12
|
+
// This is the pattern suggeested in https://github.com/matsadler/magnus/blob/main/examples/mut_point.rs
|
|
13
|
+
pub(crate) inner: RefCell<client_registry::ClientRegistry>,
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
impl ClientRegistry {
|
|
17
|
+
pub fn new() -> Self {
|
|
18
|
+
Self {
|
|
19
|
+
inner: RefCell::new(client_registry::ClientRegistry::new()),
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
pub fn add_llm_client(ruby: &Ruby, rb_self: &Self, args: &[Value]) -> Result<()> {
|
|
24
|
+
let args = scan_args::<_, _, (), (), (), ()>(args)?;
|
|
25
|
+
let (name, provider, options): (String, String, RHash) = args.required;
|
|
26
|
+
let (retry_policy,): (Option<String>,) = args.optional;
|
|
27
|
+
|
|
28
|
+
let options = match ruby_to_json::RubyToJson::convert_hash_to_json(options) {
|
|
29
|
+
Ok(options) => options,
|
|
30
|
+
Err(e) => {
|
|
31
|
+
return Err(Error::new(
|
|
32
|
+
ruby.exception_syntax_error(),
|
|
33
|
+
format!("error while parsing call_function args:\n{e}"),
|
|
34
|
+
));
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
let provider = match client_registry::ClientProvider::from_str(&provider) {
|
|
39
|
+
Ok(provider) => provider,
|
|
40
|
+
Err(e) => {
|
|
41
|
+
return Err(Error::new(
|
|
42
|
+
ruby.exception_syntax_error(),
|
|
43
|
+
format!("Invalid provider: {e:?}"),
|
|
44
|
+
));
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
let client_property =
|
|
49
|
+
client_registry::ClientProperty::new(name, provider, retry_policy, options);
|
|
50
|
+
|
|
51
|
+
rb_self.inner.borrow_mut().add_client(client_property);
|
|
52
|
+
Ok(())
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
pub fn set_primary(&self, primary: String) {
|
|
56
|
+
self.inner.borrow_mut().set_primary(primary);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
pub fn define_in_ruby(module: &magnus::RModule) -> Result<()> {
|
|
60
|
+
let cls = module.define_class("ClientRegistry", class::object())?;
|
|
61
|
+
|
|
62
|
+
cls.define_singleton_method("new", function!(ClientRegistry::new, 0))?;
|
|
63
|
+
cls.define_method(
|
|
64
|
+
"add_llm_client",
|
|
65
|
+
method!(ClientRegistry::add_llm_client, -1),
|
|
66
|
+
)?;
|
|
67
|
+
cls.define_method("set_primary", method!(ClientRegistry::set_primary, 1))?;
|
|
68
|
+
|
|
69
|
+
Ok(())
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
#[macro_export]
|
|
2
|
+
macro_rules! lang_wrapper {
|
|
3
|
+
|
|
4
|
+
($name:ident, $wrap_name:expr, $type:ty, sync_thread_safe $(, $attr_name:ident : $attr_type:ty)*) => {
|
|
5
|
+
#[magnus::wrap(class = $wrap_name, free_immediately, size)]
|
|
6
|
+
pub struct $name {
|
|
7
|
+
pub(crate) inner: std::sync::Arc<std::sync::Mutex<$type>>,
|
|
8
|
+
$($attr_name: $attr_type),*
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
impl From<$type> for $name {
|
|
12
|
+
fn from(inner: $type) -> Self {
|
|
13
|
+
Self {
|
|
14
|
+
inner: std::sync::Arc::new(std::sync::Mutex::new(inner)),
|
|
15
|
+
$($attr_name: Default::default()),*
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
impl From<std::sync::Arc<std::sync::Mutex<$type>>> for $name {
|
|
22
|
+
fn from(inner: std::sync::Arc<std::sync::Mutex<$type>>) -> Self {
|
|
23
|
+
Self {
|
|
24
|
+
inner,
|
|
25
|
+
$($attr_name: Default::default()),*
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
($name:ident, $wrap_name:expr, $type:ty, clone_safe $(, $attr_name:ident : $attr_type:ty)*) => {
|
|
33
|
+
#[magnus::wrap(class = $wrap_name, free_immediately, size)]
|
|
34
|
+
pub struct $name {
|
|
35
|
+
pub(crate) inner: std::sync::Arc<$type>,
|
|
36
|
+
$($attr_name: $attr_type),*
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
impl From<$type> for $name {
|
|
40
|
+
fn from(inner: $type) -> Self {
|
|
41
|
+
Self {
|
|
42
|
+
inner: std::sync::Arc::new(inner),
|
|
43
|
+
$($attr_name: Default::default()),*
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
impl From<std::sync::Arc<$type>> for $name {
|
|
50
|
+
fn from(inner: std::sync::Arc<$type>) -> Self {
|
|
51
|
+
Self {
|
|
52
|
+
inner,
|
|
53
|
+
$($attr_name: Default::default()),*
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
}
|