07_inspect_history/
07-inspect-history.rs

1/*
2Script to inspect the history of an LM.
3
4Run with:
5```
6cargo run --example 07-inspect-history
7```
8*/
9
10use anyhow::Result;
11use bon::Builder;
12use dspy_rs::{
13    ChatAdapter, Example, LM, LMConfig, Module, Predict, Prediction, Predictor, configure, example,
14    get_lm, sign,
15};
16
17#[derive(Builder)]
18pub struct QARater {
19    #[builder(default = Predict::new(sign! { (question: String) -> answer: String }))]
20    pub answerer: Predict,
21}
22
23impl Module for QARater {
24    async fn forward(&self, inputs: Example) -> Result<Prediction> {
25        return self.answerer.forward(inputs.clone()).await;
26    }
27}
28
29#[tokio::main]
30async fn main() {
31    let lm = LM::builder()
32        .api_key(std::env::var("OPENAI_API_KEY").unwrap().into())
33        .config(LMConfig {
34            model: "gpt-4o-mini".to_string(),
35            ..LMConfig::default()
36        })
37        .build();
38    configure(lm, ChatAdapter);
39
40    let example = example! {
41        "question": "input" => "What is the capital of France?",
42    };
43
44    let qa_rater = QARater::builder().build();
45    let prediction = qa_rater.forward(example.clone()).await.unwrap();
46    println!("Prediction: {prediction:?}");
47
48    let history = get_lm().lock().await.inspect_history(1);
49    println!("History: {history:?}");
50}