|
| 1 | +#![allow(dead_code)] |
| 2 | + |
| 3 | +use orch::alignment::AlignmentStrategyBuilder; |
| 4 | +use orch::execution::*; |
| 5 | +use orch::lm::*; |
| 6 | +use orch::response::*; |
| 7 | + |
| 8 | +#[derive(Variants, serde::Deserialize)] |
| 9 | +pub enum ResponseVariants { |
| 10 | + Answer(AnswerResponseVariant), |
| 11 | + Fail(FailResponseVariant), |
| 12 | +} |
| 13 | + |
| 14 | +#[derive(Variant, serde::Deserialize)] |
| 15 | +#[variant( |
| 16 | + variant = "Answer", |
| 17 | + scenario = "You know the answer", |
| 18 | + description = "Result of the calculation" |
| 19 | +)] |
| 20 | +pub struct AnswerResponseVariant { |
| 21 | + #[schema(description = "Result of the calculation", example = "42")] |
| 22 | + pub result: String, |
| 23 | +} |
| 24 | + |
| 25 | +#[derive(Variant, serde::Deserialize)] |
| 26 | +#[variant( |
| 27 | + variant = "Fail", |
| 28 | + scenario = "You don't know the answer", |
| 29 | + description = "Reason why the answer is not known" |
| 30 | +)] |
| 31 | +pub struct FailResponseVariant { |
| 32 | + #[schema( |
| 33 | + description = "Reason why the answer is not known", |
| 34 | + example = "The phrase is not a mathematical related expression" |
| 35 | + )] |
| 36 | + pub reason: String, |
| 37 | +} |
| 38 | + |
| 39 | +#[tokio::main] |
| 40 | +async fn main() { |
| 41 | + // We use a large foundational model for the main task. |
| 42 | + let ollama_large = OllamaBuilder::new() |
| 43 | + .with_model(ollama_model::LLAMA3_8B.to_string()) |
| 44 | + .try_build() |
| 45 | + .unwrap(); |
| 46 | + |
| 47 | + // We use a smaller model for the correction. |
| 48 | + let ollama_corrector = OllamaBuilder::new() |
| 49 | + .with_model(ollama_model::LLAMA3_8B.to_string()) |
| 50 | + .try_build() |
| 51 | + .unwrap(); |
| 52 | + |
| 53 | + // We define an alignment strategy that uses the correction model. |
| 54 | + let alignment_strategy = AlignmentStrategyBuilder::new() |
| 55 | + .with_retries(2) |
| 56 | + .with_lm(Box::new(ollama_corrector)) |
| 57 | + .try_build() |
| 58 | + .unwrap(); |
| 59 | + |
| 60 | + let executor = StructuredExecutorBuilder::new() |
| 61 | + .with_lm(&ollama_large) |
| 62 | + .with_preamble(" |
| 63 | + You are a mathematician who helps users understand the result of mathematical expressions. |
| 64 | + You will receive a mathematical expression, and you will need to provide the result of that expression. |
| 65 | + ") |
| 66 | + .with_options(Box::new(variants!(ResponseVariants))) |
| 67 | + .with_alignment(alignment_strategy) |
| 68 | + .try_build() |
| 69 | + .unwrap(); |
| 70 | + let response = executor.execute("2 + 2").await.expect("Execution failed"); |
| 71 | + |
| 72 | + match response.content { |
| 73 | + ResponseVariants::Answer(answer) => { |
| 74 | + println!("Result: {}", answer.result); |
| 75 | + } |
| 76 | + ResponseVariants::Fail(fail) => { |
| 77 | + println!("Model failed to generate a response: {}", fail.reason); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments