Skip to content

Commit

Permalink
added multiple returnable results
Browse files Browse the repository at this point in the history
  • Loading branch information
andthattoo committed Nov 5, 2024
1 parent 831a236 commit 24a7721
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 290 deletions.
19 changes: 0 additions & 19 deletions examples/execute_workflow.rs

This file was deleted.

29 changes: 29 additions & 0 deletions examples/import_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use dotenv::dotenv;
use env_logger::Env;
use ollama_workflows::{Executor, Model, ProgramMemory, Workflow};

fn main() {
// Initialize environment
dotenv().ok();
let env = Env::default().filter_or("LOG_LEVEL", "info");
env_logger::Builder::from_env(env).init();

// Create runtime for async execution
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
// Initialize the executor with desired model
let exe = Executor::new(Model::Phi3Medium);

// Load workflow from JSON file
let workflow = Workflow::new_from_json("./tests/test_workflows/simple.json").unwrap();

// Initialize program memory
let mut memory = ProgramMemory::new();

// Execute workflow and handle the result
match exe.execute(None, &workflow, &mut memory).await {
Ok(result) => println!("Generated poem:\n{}", result),
Err(err) => eprintln!("Error executing workflow: {:?}", err),
}
});
}
253 changes: 0 additions & 253 deletions examples/workflows/search.json

This file was deleted.

14 changes: 14 additions & 0 deletions src/memory/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub enum MemoryReturnType {
//EntryRef(Option<&'a Entry>),
Entry(Option<Entry>),
EntryVec(Option<Vec<Entry>>),
Multiple(Vec<MemoryReturnType>),
}

impl MemoryReturnType {
Expand All @@ -59,6 +60,7 @@ impl MemoryReturnType {
//MemoryReturnType::EntryRef(entry_ref) => entry_ref.is_none(),
MemoryReturnType::Entry(entry) => entry.is_none(),
MemoryReturnType::EntryVec(entry_vec) => entry_vec.is_none(),
MemoryReturnType::Multiple(returns) => returns.is_empty(),
}
}

Expand All @@ -76,6 +78,10 @@ impl MemoryReturnType {
Err(_) => None,
}
}
MemoryReturnType::Multiple(returns) => {
let values: Vec<String> = returns.iter().map(|ret| ret.to_string()).collect();
serde_json::to_string(&values).ok()
}
_ => None,
}
}
Expand All @@ -100,6 +106,14 @@ impl fmt::Display for MemoryReturnType {
}
write!(f, "{}", result)
}
MemoryReturnType::Multiple(returns) => {
let mut result = String::new();
for ret in returns {
result.push_str(&ret.to_string());
result.push_str(" \n");
}
write!(f, "{}", result)
}
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion src/program/atomics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,16 @@ pub struct Task {
pub outputs: Vec<Output>,
}

#[derive(Debug, Deserialize)]
#[serde(untagged)]
pub enum TaskOutputInput {
Single(InputValue),
Multiple(Vec<InputValue>),
}

#[derive(Debug, Deserialize)]
pub struct TaskOutput {
pub input: InputValue,
pub input: TaskOutputInput,
pub to_json: Option<bool>,
pub post_process: Option<Vec<TaskPostProcess>>,
}
Expand Down
13 changes: 12 additions & 1 deletion src/program/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,18 @@ impl Executor {
));
}
let rv = workflow.get_return_value();
let return_value = self.handle_input(&rv.input, memory).await;

//let return_value = self.handle_input(&rv.input, memory).await;
let return_value = match &rv.input {
TaskOutputInput::Single(input) => self.handle_input(input, memory).await,
TaskOutputInput::Multiple(inputs) => {
let mut results = Vec::new();
for input in inputs {
results.push(self.handle_input(input, memory).await);
}
MemoryReturnType::Multiple(results)
}
};
let mut return_string = return_value.to_string().clone();

if rv.to_json.is_some() && rv.to_json.unwrap() {
Expand Down
Loading

0 comments on commit 24a7721

Please sign in to comment.