batch_mode_batch_triple/
batch_file_triple.rs

1// ---------------- [ File: src/batch_file_triple.rs ]
2crate::ix!();
3
4/// Represents the batch files associated with a specific index.
5#[derive(Getters,Clone)]
6#[getset(get="pub")]
7pub struct BatchFileTriple {
8    index:               BatchIndex,
9    input:               Option<PathBuf>,
10    output:              Option<PathBuf>,
11    error:               Option<PathBuf>,
12    associated_metadata: Option<PathBuf>,
13    workspace:           Arc<dyn BatchWorkspaceInterface>,
14}
15
16unsafe impl Send for BatchFileTriple {}
17unsafe impl Sync for BatchFileTriple {}
18
19impl Debug for BatchFileTriple {
20
21    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
22        f.debug_struct("BatchFileTriple")
23            .field("index",  &self.index)
24            .field("input",  &self.input)
25            .field("output", &self.output)
26            .field("error",  &self.error)
27            .field("associated_metadata", &self.associated_metadata)
28            .finish()
29    }
30}
31
32impl PartialOrd for BatchFileTriple {
33
34    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
35        self.index.partial_cmp(&other.index)
36    }
37}
38
39impl PartialEq for BatchFileTriple {
40
41    fn eq(&self, other: &BatchFileTriple) -> bool { 
42        self.index.eq(&other.index) 
43            &&
44        self.input.eq(&other.input) 
45            &&
46        self.output.eq(&other.output) 
47            &&
48        self.error.eq(&other.error) 
49            &&
50        self.associated_metadata.eq(&other.associated_metadata) 
51    }
52}
53
54impl Eq for BatchFileTriple {}
55
56impl Ord for BatchFileTriple {
57
58    fn cmp(&self, other: &Self) -> Ordering {
59        self.index.cmp(&other.index)
60    }
61}
62
63impl BatchFileTriple {
64
65    delegate!{
66        to self.workspace {
67            pub fn get_done_directory(&self) -> &PathBuf;
68        }
69    }
70
71    pub fn input_filename_which_maybe_does_not_yet_exist(&self) -> PathBuf {
72        self.workspace.input_filename(&self.index)
73    }
74
75    pub fn output_filename_which_maybe_does_not_yet_exist(&self) -> PathBuf {
76        self.workspace.output_filename(&self.index)
77    }
78
79    pub fn error_filename_which_maybe_does_not_yet_exist(&self) -> PathBuf {
80        self.workspace.error_filename(&self.index)
81    }
82
83    pub fn metadata_filename_which_maybe_does_not_yet_exist(&self) -> PathBuf {
84        self.workspace.metadata_filename(&self.index)
85    }
86
87    pub fn set_output_path(&mut self, path: Option<PathBuf>) {
88        self.output = path;
89    }
90
91    pub fn set_error_path(&mut self, path: Option<PathBuf>) {
92        self.error = path;
93    }
94
95    pub fn all_are_none(&self) -> bool {
96        self.input.is_none() && self.output.is_none() && self.error.is_none()
97    }
98
99    //--------------------------------------------
100    pub fn new_with_requests(requests: &[GptBatchAPIRequest], workspace: Arc<dyn BatchWorkspaceInterface>) 
101        -> Result<Self,BatchInputCreationError> 
102    {
103        let index = BatchIndex::new();
104
105        let batch_input_filename    = workspace.input_filename(&index);
106        let batch_output_filename   = workspace.output_filename(&index);
107        let batch_error_filename    = workspace.error_filename(&index);
108        let batch_metadata_filename = workspace.metadata_filename(&index);
109
110        info!("creating new batch at {:?} with {} requests", batch_input_filename, requests.len());
111
112        // Create input file
113        gpt_batch_scribe::create_batch_input_file(&requests,&batch_input_filename)?;
114
115        //we do these dev-only checks here just to be sure
116        assert!(batch_input_filename.exists());
117        assert!(!batch_output_filename.exists());
118        assert!(!batch_error_filename.exists());
119        assert!(!batch_metadata_filename.exists());
120
121        Ok(Self {
122            index,
123            input:               Some(batch_input_filename),
124            output:              None,
125            error:               None,
126            associated_metadata: None,
127            workspace,
128        })
129    }
130
131    pub fn new_direct(
132        index:               &BatchIndex, 
133        input:               Option<PathBuf>, 
134        output:              Option<PathBuf>, 
135        error:               Option<PathBuf>, 
136        associated_metadata: Option<PathBuf>, 
137        workspace:           Arc<dyn BatchWorkspaceInterface>
138
139    ) -> Self {
140
141        Self { 
142            index: index.clone(), 
143            input, 
144            output, 
145            error, 
146            associated_metadata, 
147            workspace 
148        }
149    }
150}