Skip to content

Commit 23eff68

Browse files
starpitclaudiosv
authored andcommitted
refactor: refactor rust ast to place metadata in common struct
And start populating the timing info (incomplete). Signed-off-by: Nick Mitchell <[email protected]> Signed-off-by: Claudio Spiess <[email protected]>
1 parent 71bd1da commit 23eff68

File tree

7 files changed

+449
-362
lines changed

7 files changed

+449
-362
lines changed

pdl-live-react/src-tauri/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pdl-live-react/src-tauri/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ rustpython-stdlib = { version = "0.4.0", features = ["zlib"] }
4747
schemars = "0.8.22"
4848
fs4 = "0.13.1"
4949
derive_builder = "0.20.2"
50+
iana-time-zone = "0.1.63"
5051

5152
[target.'cfg(not(any(target_os = "android", target_os = "ios")))'.dependencies]
5253
tauri-plugin-cli = "2"

pdl-live-react/src-tauri/src/compile/beeai.rs

Lines changed: 149 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use serde_json::{Map, Value, from_reader, json, to_string};
1212
use tempfile::Builder;
1313

1414
use crate::pdl::ast::{
15-
ArrayBlockBuilder, Block::*, CallBlock, EvalsTo, Expr, FunctionBlock, ListOrString,
15+
ArrayBlockBuilder, Block, Body::*, CallBlock, EvalsTo, Expr, FunctionBlock, ListOrString,
1616
MessageBlock, MetadataBuilder, ModelBlockBuilder, ObjectBlock, PdlBaseType, PdlBlock,
1717
PdlBlock::Advanced, PdlOptionalType, PdlParser, PdlType, PythonCodeBlock, RepeatBlock, Role,
1818
TextBlock, TextBlockBuilder,
@@ -191,51 +191,67 @@ fn with_tools(
191191
}
192192

193193
fn call_tools(model: &String, parameters: &HashMap<String, Value>) -> PdlBlock {
194-
let repeat = Advanced(Text(TextBlock {
194+
let repeat = Advanced(Block {
195195
metadata: Some(
196196
MetadataBuilder::default()
197197
.description("Calling tool ${ tool.function.name }".to_string())
198198
.build()
199199
.unwrap(),
200200
),
201-
role: None,
202-
parser: None,
203-
text: vec![Advanced(Model(
204-
ModelBlockBuilder::default()
205-
.model(model.as_str())
206-
.parameters(strip_nulls(parameters))
207-
.input(Advanced(Array(
208-
ArrayBlockBuilder::default()
209-
.array(vec![Advanced(Message(MessageBlock {
201+
202+
body: Text(TextBlock {
203+
role: None,
204+
parser: None,
205+
text: vec![Advanced(Block {
206+
metadata: None,
207+
body: Model(
208+
ModelBlockBuilder::default()
209+
.model(model.as_str())
210+
.parameters(strip_nulls(parameters))
211+
.input(Advanced(Block {
210212
metadata: None,
211-
role: Role::Tool,
212-
defsite: None,
213-
name: Some("${ tool.function.name }".to_string()),
214-
tool_call_id: Some("${ tool.id }".to_string()),
215-
content: Box::new(Advanced(Call(CallBlock {
216-
metadata: Some(
217-
MetadataBuilder::default()
218-
.defs(json_loads(
219-
&"args",
220-
&"pdl__args",
221-
&"${ tool.function.arguments }",
222-
))
223-
.build()
224-
.unwrap(),
225-
),
226-
call: EvalsTo::Jinja(
227-
"${ pdl__tools[tool.function.name] }".to_string(),
228-
), // look up tool in tool_declarations def (see below)
229-
args: Some("${ args }".into()), // invoke with arguments as specified by the model
230-
}))),
231-
}))])
213+
body: Array(
214+
ArrayBlockBuilder::default()
215+
.array(vec![Advanced(Block {
216+
metadata: None,
217+
body: Message(MessageBlock {
218+
role: Role::Tool,
219+
defsite: None,
220+
name: Some("${ tool.function.name }".to_string()),
221+
tool_call_id: Some("${ tool.id }".to_string()),
222+
content: Box::new(Advanced(Block {
223+
metadata: Some(
224+
MetadataBuilder::default()
225+
.defs(json_loads(
226+
&"args",
227+
&"pdl__args",
228+
&"${ tool.function.arguments }",
229+
))
230+
.build()
231+
.unwrap(),
232+
),
233+
234+
body: Call(CallBlock {
235+
call: EvalsTo::Jinja(
236+
"${ pdl__tools[tool.function.name] }"
237+
.to_string(),
238+
), // look up tool in tool_declarations def (see below)
239+
args: Some("${ args }".into()), // invoke with arguments as specified by the model
240+
pdl_trace: None,
241+
}),
242+
})),
243+
}),
244+
})])
245+
.build()
246+
.unwrap(),
247+
),
248+
}))
232249
.build()
233250
.unwrap(),
234-
)))
235-
.build()
236-
.unwrap(),
237-
))],
238-
}));
251+
),
252+
})],
253+
}),
254+
});
239255

240256
let mut for_ = HashMap::new();
241257
for_.insert(
@@ -249,11 +265,13 @@ fn call_tools(model: &String, parameters: &HashMap<String, Value>) -> PdlBlock {
249265
);
250266

251267
// response.choices[0].message.tool_calls
252-
Advanced(Repeat(RepeatBlock {
268+
Advanced(Block {
253269
metadata: None,
254-
for_: for_,
255-
repeat: Box::new(repeat),
256-
}))
270+
body: Repeat(RepeatBlock {
271+
for_: for_,
272+
repeat: Box::new(repeat),
273+
}),
274+
})
257275
}
258276

259277
fn json_loads(
@@ -264,22 +282,25 @@ fn json_loads(
264282
let mut m = indexmap::IndexMap::new();
265283
m.insert(
266284
outer_name.to_owned(),
267-
Advanced(Text(
268-
TextBlockBuilder::default()
269-
.text(vec![PdlBlock::String(format!(
270-
"{{\"{}\": {}}}",
271-
inner_name, value
272-
))])
273-
.metadata(
274-
MetadataBuilder::default()
275-
.description(format!("Parsing json for {}={}", inner_name, value))
276-
.build()
277-
.unwrap(),
278-
)
279-
.parser(PdlParser::Json)
280-
.build()
281-
.unwrap(),
282-
)),
285+
Advanced(Block {
286+
metadata: Some(
287+
MetadataBuilder::default()
288+
.description(format!("Parsing json for {}={}", inner_name, value))
289+
.build()
290+
.unwrap(),
291+
),
292+
293+
body: Text(
294+
TextBlockBuilder::default()
295+
.text(vec![PdlBlock::String(format!(
296+
"{{\"{}\": {}}}",
297+
inner_name, value
298+
))])
299+
.parser(PdlParser::Json)
300+
.build()
301+
.unwrap(),
302+
),
303+
}),
283304
);
284305
m
285306
}
@@ -428,12 +449,13 @@ pub fn compile(source_file_path: &str, debug: bool) -> Result<PdlBlock, Box<dyn
428449
tool_name.clone(),
429450
PdlBlock::Function(FunctionBlock {
430451
function: schema,
431-
return_: Box::new(Advanced(PythonCode(PythonCodeBlock {
432-
// tool function definition
452+
return_: Box::new(Advanced(Block {
433453
metadata: None,
434-
lang: "python".to_string(),
435-
code: format!(
436-
"
454+
body: PythonCode(PythonCodeBlock {
455+
// tool function definition
456+
lang: "python".to_string(),
457+
code: format!(
458+
"
437459
from {} import {}
438460
import asyncio
439461
async def invoke():
@@ -445,24 +467,25 @@ async def invoke():
445467
{}
446468
asyncio.run(invoke())
447469
",
448-
import_from,
449-
import_fn,
450-
if debug {
451-
format!("print('Invoking tool {}')", tool_name)
452-
} else {
453-
"".to_string()
454-
},
455-
import_fn,
456-
if debug {
457-
format!(
458-
"print(f'Response from tool {}: {{result}}')",
459-
tool_name
460-
)
461-
} else {
462-
"".to_string()
463-
}
464-
),
465-
}))),
470+
import_from,
471+
import_fn,
472+
if debug {
473+
format!("print('Invoking tool {}')", tool_name)
474+
} else {
475+
"".to_string()
476+
},
477+
import_fn,
478+
if debug {
479+
format!(
480+
"print(f'Response from tool {}: {{result}}')",
481+
tool_name
482+
)
483+
} else {
484+
"".to_string()
485+
}
486+
),
487+
}),
488+
})),
466489
}),
467490
)
468491
})
@@ -491,27 +514,23 @@ asyncio.run(invoke())
491514
let model = format!("{}/{}", provider, model);
492515

493516
if let Some(instructions) = instructions {
494-
model_call.push(Advanced(Text(TextBlock {
495-
role: Some(Role::System),
496-
text: vec![PdlBlock::String(instructions)],
517+
model_call.push(Advanced(Block {
497518
metadata: Some(
498519
MetadataBuilder::default()
499520
.description("Model instructions".to_string())
500521
.build()
501522
.unwrap(),
502523
),
503-
parser: None,
504-
})));
524+
body: Text(TextBlock {
525+
role: Some(Role::System),
526+
text: vec![PdlBlock::String(instructions)],
527+
parser: None,
528+
}),
529+
}));
505530
}
506531

507532
let mut model_builder = ModelBlockBuilder::default();
508533
model_builder
509-
.metadata(
510-
MetadataBuilder::default()
511-
.description(description)
512-
.build()
513-
.unwrap(),
514-
)
515534
.model(model.clone())
516535
.parameters(with_tools(&tools, &parameters.state.dict));
517536

@@ -524,7 +543,15 @@ asyncio.run(invoke())
524543
}
525544
}
526545

527-
model_call.push(Advanced(Model(model_builder.build().unwrap())));
546+
model_call.push(Advanced(Block {
547+
metadata: Some(
548+
MetadataBuilder::default()
549+
.description(description)
550+
.build()
551+
.unwrap(),
552+
),
553+
body: Model(model_builder.build().unwrap()),
554+
}));
528555

529556
if let Some(tools) = tools {
530557
if tools.len() > 0 {
@@ -538,34 +565,38 @@ asyncio.run(invoke())
538565
closure_name.clone(),
539566
PdlBlock::Function(FunctionBlock {
540567
function: HashMap::new(),
541-
return_: Box::new(Advanced(Text(TextBlock {
568+
return_: Box::new(Advanced(Block {
542569
metadata: Some(
543570
MetadataBuilder::default()
544571
.description(format!("Model call {}", &model))
545572
.build()
546573
.unwrap(),
547574
),
548-
role: None,
549-
parser: None,
550-
text: model_call,
551-
}))),
575+
body: Text(TextBlock {
576+
role: None,
577+
parser: None,
578+
text: model_call,
579+
}),
580+
})),
552581
}),
553582
);
554-
Advanced(Text(TextBlock {
583+
Advanced(Block {
555584
metadata: Some(
556585
MetadataBuilder::default()
557586
.description("Model call wrapper".to_string())
558587
.defs(defs)
559588
.build()
560589
.unwrap(),
561590
),
562-
role: None,
563-
parser: None,
564-
text: vec![Advanced(Call(CallBlock::new(format!(
565-
"${{ {} }}",
566-
closure_name
567-
))))],
568-
}))
591+
body: Text(TextBlock {
592+
role: None,
593+
parser: None,
594+
text: vec![Advanced(Block {
595+
metadata: None,
596+
body: Call(CallBlock::new(format!("${{ {} }}", closure_name))),
597+
})],
598+
}),
599+
})
569600
},
570601
)
571602
.collect::<Vec<_>>();
@@ -580,19 +611,24 @@ asyncio.run(invoke())
580611
let mut defs = indexmap::IndexMap::new();
581612
defs.insert(
582613
"pdl__tools".to_string(),
583-
Advanced(Object(ObjectBlock {
584-
object: tool_declarations,
585-
})),
614+
Advanced(Block {
615+
metadata: None,
616+
body: Object(ObjectBlock {
617+
object: tool_declarations,
618+
}),
619+
}),
586620
);
587621
metadata.defs(defs);
588622
}
589623

590-
let pdl: PdlBlock = Advanced(Text(TextBlock {
624+
let pdl: PdlBlock = Advanced(Block {
591625
metadata: Some(metadata.build().unwrap()),
592-
role: None,
593-
parser: None,
594-
text: body,
595-
}));
626+
body: Text(TextBlock {
627+
role: None,
628+
parser: None,
629+
text: body,
630+
}),
631+
});
596632

597633
Ok(pdl)
598634
}

0 commit comments

Comments
 (0)