Skip to content

Commit 41f6429

Browse files
committed
refactor: extract a common extract_values() in the rust model pulling logic
this should let us have an `extract_requirements()` to help with managing venvs of code blocks Signed-off-by: Nick Mitchell <[email protected]>
1 parent b1d3a13 commit 41f6429

File tree

3 files changed

+61
-48
lines changed

3 files changed

+61
-48
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use yaml_rust2::Yaml;
2+
3+
/// Extract models referenced by the programs
4+
pub fn extract_models(programs: Vec<Yaml>) -> Vec<String> {
5+
extract_values(programs, "model")
6+
}
7+
8+
/// 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>>();
14+
15+
// A single program may specify the same model more than once. Dedup!
16+
values.sort();
17+
values.dedup();
18+
19+
values
20+
}
21+
22+
/// 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+
26+
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+
}
43+
}
44+
45+
Yaml::Array(a) => {
46+
for val in a {
47+
for m in extract_one_values(val, field) {
48+
values.push(m)
49+
}
50+
}
51+
}
52+
53+
_ => {}
54+
}
55+
56+
values
57+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub mod extract;
12
pub mod pip;
23
pub mod pull;
34
pub mod shasum;

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

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,62 +3,17 @@ use rayon::prelude::*;
33
use yaml_rust2::yaml::LoadError;
44
use yaml_rust2::{ScanError, Yaml, YamlLoader};
55

6+
use crate::interpreter::extract;
7+
68
/// Read the given filesystem path and produce a potentially multi-document Yaml
79
fn from_path(path: &String) -> Result<Vec<Yaml>, ScanError> {
810
let content = std::fs::read_to_string(path).unwrap();
911
YamlLoader::load_from_str(&content)
1012
}
1113

12-
/// Take one Yaml fragment and produce the a vector of the models that are used
13-
fn extract_models(program: Yaml) -> Vec<String> {
14-
let mut models: Vec<String> = Vec::new();
15-
16-
match program {
17-
Yaml::Hash(h) => {
18-
for (key, val) in h {
19-
match key.as_str() {
20-
Some("model") => match &val {
21-
Yaml::String(m) => {
22-
models.push(m.to_string());
23-
}
24-
_ => {}
25-
},
26-
_ => {}
27-
}
28-
29-
for m in extract_models(val) {
30-
models.push(m)
31-
}
32-
}
33-
}
34-
35-
Yaml::Array(a) => {
36-
for val in a {
37-
for m in extract_models(val) {
38-
models.push(m)
39-
}
40-
}
41-
}
42-
43-
_ => {}
44-
}
45-
46-
models
47-
}
48-
4914
/// Pull models (in parallel) from the PDL program in the given filepath.
5015
pub async fn pull_if_needed(path: &String) -> Result<(), LoadError> {
51-
let mut models = from_path(path)
52-
.unwrap()
53-
.into_iter()
54-
.flat_map(extract_models)
55-
.collect::<Vec<String>>();
56-
57-
// A single program may specify the same model more than once. Dedup!
58-
models.sort();
59-
models.dedup();
60-
61-
models
16+
extract::extract_models(from_path(path).unwrap())
6217
.into_par_iter()
6318
.try_for_each(|model| match model {
6419
m if model.starts_with("ollama/") => ollama_pull_if_needed(&m[7..]),

0 commit comments

Comments
 (0)