Skip to content

Commit be771a8

Browse files
committed
feat: port rust model pull logic to use rust AST
Signed-off-by: Nick Mitchell <[email protected]>
1 parent d5caf35 commit be771a8

File tree

4 files changed

+41
-49
lines changed

4 files changed

+41
-49
lines changed
Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
use yaml_rust2::Yaml;
1+
use crate::pdl::ast::PdlBlock;
22

33
/// Extract models referenced by the programs
4-
pub fn extract_models(programs: Vec<Yaml>) -> Vec<String> {
5-
extract_values(programs, "model")
4+
pub fn extract_models(program: &PdlBlock) -> Vec<String> {
5+
extract_values(program, "model")
66
}
77

88
/// Take a list of Yaml fragments and produce a vector of the string-valued entries of the given field
9-
pub fn extract_values(programs: Vec<Yaml>, field: &str) -> Vec<String> {
10-
let mut values = programs
11-
.into_iter()
12-
.flat_map(|p| extract_one_values(p, field))
13-
.collect::<Vec<String>>();
9+
pub fn extract_values(program: &PdlBlock, field: &str) -> Vec<String> {
10+
let mut values = vec![];
11+
extract_values_iter(program, field, &mut values);
1412

1513
// A single program may specify the same model more than once. Dedup!
1614
values.sort();
@@ -20,38 +18,37 @@ pub fn extract_values(programs: Vec<Yaml>, field: &str) -> Vec<String> {
2018
}
2119

2220
/// Take one Yaml fragment and produce a vector of the string-valued entries of the given field
23-
fn extract_one_values(program: Yaml, field: &str) -> Vec<String> {
24-
let mut values: Vec<String> = Vec::new();
25-
21+
fn extract_values_iter(program: &PdlBlock, field: &str, values: &mut Vec<String>) {
2622
match program {
27-
Yaml::Hash(h) => {
28-
for (key, val) in h {
29-
match key {
30-
Yaml::String(f) if f == field => match &val {
31-
Yaml::String(m) => {
32-
values.push(m.to_string());
33-
}
34-
_ => {}
35-
},
36-
_ => {}
37-
}
38-
39-
for m in extract_one_values(val, field) {
40-
values.push(m)
41-
}
42-
}
23+
PdlBlock::Model(b) => values.push(b.model.clone()),
24+
PdlBlock::Repeat(b) => {
25+
extract_values_iter(&b.repeat, field, values);
4326
}
44-
45-
Yaml::Array(a) => {
46-
for val in a {
47-
for m in extract_one_values(val, field) {
48-
values.push(m)
49-
}
27+
PdlBlock::Message(b) => {
28+
extract_values_iter(&b.content, field, values);
29+
}
30+
PdlBlock::Array(b) => b
31+
.array
32+
.iter()
33+
.for_each(|p| extract_values_iter(p, field, values)),
34+
PdlBlock::Text(b) => b
35+
.text
36+
.iter()
37+
.for_each(|p| extract_values_iter(p, field, values)),
38+
PdlBlock::LastOf(b) => b
39+
.last_of
40+
.iter()
41+
.for_each(|p| extract_values_iter(p, field, values)),
42+
PdlBlock::If(b) => {
43+
extract_values_iter(&b.then, field, values);
44+
if let Some(else_) = &b.else_ {
45+
extract_values_iter(else_, field, values);
5046
}
5147
}
52-
48+
PdlBlock::Object(b) => b
49+
.object
50+
.values()
51+
.for_each(|p| extract_values_iter(p, field, values)),
5352
_ => {}
5453
}
55-
56-
values
5754
}

pdl-live-react/src-tauri/src/pdl/interpreter.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,7 @@ pub async fn run_file(source_file_path: &str, debug: bool) -> Interpretation {
933933
let cwd = path.parent().and_then(|cwd| Some(cwd.to_path_buf()));
934934
let program = parse_file(&path)?;
935935

936+
crate::pdl::pull::pull_if_needed(&program).await?;
936937
run(&program, cwd, debug).await
937938
}
938939

pdl-live-react/src-tauri/src/pdl/pull.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
1-
use ::std::io::{Error, ErrorKind};
1+
use ::std::io::Error;
22

33
use duct::cmd;
44
use rayon::prelude::*;
5-
use yaml_rust2::{Yaml, YamlLoader};
65

6+
use crate::pdl::ast::PdlBlock;
77
use crate::pdl::extract;
88

9-
/// Read the given filesystem path and produce a potentially multi-document Yaml
10-
fn from_path(path: &str) -> Result<Vec<Yaml>, Error> {
11-
let content = std::fs::read_to_string(path)?;
12-
YamlLoader::load_from_str(&content).map_err(|e| Error::new(ErrorKind::Other, e.to_string()))
13-
}
14-
159
/// Pull models (in parallel) from the PDL program in the given filepath.
16-
pub async fn pull_if_needed(path: &str) -> Result<(), Error> {
17-
extract::extract_models(from_path(path)?)
10+
pub async fn pull_if_needed(program: &PdlBlock) -> Result<(), Error> {
11+
extract::extract_models(program)
1812
.into_par_iter()
1913
.try_for_each(|model| match model {
2014
m if model.starts_with("ollama/") => ollama_pull_if_needed(&m[7..]),

pdl-live-react/src-tauri/src/pdl/run.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use duct::cmd;
33
use futures::executor::block_on;
44

55
use crate::pdl::pip::pip_install_if_needed;
6-
use crate::pdl::pull::pull_if_needed;
6+
// use crate::pdl::pull::pull_if_needed;
77
use crate::pdl::requirements::PDL_INTERPRETER;
88

99
#[cfg(desktop)]
@@ -19,11 +19,11 @@ pub fn run_pdl_program(
1919
);
2020

2121
// async the model pull and pip installs
22-
let pull_future = pull_if_needed(&source_file_path);
22+
// let pull_future = pull_if_needed(&source_file_path);
2323
let bin_path_future = pip_install_if_needed(&PDL_INTERPRETER);
2424

2525
// wait for any model pulls to finish
26-
block_on(pull_future)?;
26+
//block_on(pull_future)?;
2727

2828
// wait for any pip installs to finish
2929
let bin_path = block_on(bin_path_future)?;

0 commit comments

Comments
 (0)