batch_mode_batch_executor/
batch_execution_result.rs

1// ---------------- [ File: src/batch_execution_result.rs ]
2crate::ix!();
3
4#[derive(Debug,Getters)]
5#[getset(get="pub")]
6pub struct BatchExecutionResult {
7    outputs: Option<BatchOutputData>,
8    errors:  Option<BatchErrorData>,
9}
10
11impl BatchExecutionResult {
12
13    pub fn new(outputs: Option<BatchOutputData>, errors: Option<BatchErrorData>) -> Self {
14        Self { outputs, errors }
15    }
16}
17
18pub async fn fresh_execute_batch_processing(triple: &mut BatchFileTriple, client: &OpenAIClientHandle) 
19    -> Result<BatchExecutionResult, BatchProcessingError> 
20{
21    assert!(triple.input().is_some());
22    assert!(triple.output().is_none());
23    assert!(triple.error().is_none());
24    assert!(triple.associated_metadata().is_none());
25
26    info!("executing fresh batch processing for triple {:#?}", triple);
27
28    let input_filename    = triple.input_filename_which_maybe_does_not_yet_exist();
29    let output_filename   = triple.output_filename_which_maybe_does_not_yet_exist();
30    let error_filename    = triple.error_filename_which_maybe_does_not_yet_exist();
31    let metadata_filename = triple.metadata_filename_which_maybe_does_not_yet_exist();
32
33    info!("input_filename: {:?}",    input_filename);
34    info!("output_filename: {:?}",   output_filename);
35    info!("error_filename: {:?}",    error_filename);
36    info!("metadata_filename: {:?}", metadata_filename);
37
38    assert!(input_filename.exists());
39    assert!(!output_filename.exists());
40    assert!(!error_filename.exists());
41    assert!(!metadata_filename.exists());
42
43    // Upload file
44    let input_file = client.upload_batch_file(&input_filename).await?;
45
46    let input_file_id = input_file.id;
47
48    // Create batch
49    let batch    = client.create_batch(&input_file_id).await?;
50    let batch_id = batch.id.clone();
51
52    // ** Save batch_id to metadata file **
53    let mut metadata = BatchMetadata::with_input_id_and_batch_id(&input_file_id, &batch_id);
54    metadata.save_to_file(&metadata_filename).await?;
55
56    // Wait for completion
57    let completed_batch = client.wait_for_batch_completion(&batch_id).await?;
58
59    // Download output file
60    let outputs = if let Some(output_file_id) = completed_batch.output_file_id {
61        metadata.set_output_file_id(Some(output_file_id));
62        metadata.save_to_file(&metadata_filename).await?;
63        download_output_file(triple,client).await?;
64        let outputs = load_output_file(&output_filename).await?;
65        Some(outputs)
66    } else {
67        None
68    };
69
70    // Handle errors if any
71    let errors = if let Some(error_file_id) = completed_batch.error_file_id {
72        metadata.set_error_file_id(Some(error_file_id));
73        metadata.save_to_file(&metadata_filename).await?;
74        download_error_file(triple,client).await?;
75        let errors = load_error_file(&error_filename).await?;
76        Some(errors)
77    } else {
78        None
79    };
80
81    Ok(BatchExecutionResult::new(outputs,errors))
82}