batch_mode_batch_client/
openai_client_handle.rs

1// ---------------- [ File: batch-mode-batch-client/src/openai_client_handle.rs ]
2crate::ix!();
3
4pub trait OpenAIConfigInterface = async_openai::config::Config;
5
6#[derive(Debug)]
7pub struct OpenAIClientHandle<E> 
8where
9    E: Debug + Send + Sync + From<OpenAIClientError>,
10{
11    client: async_openai::Client<OpenAIConfig>,
12    _marker: std::marker::PhantomData<E>,
13}
14
15#[async_trait]
16impl<E> LanguageModelClientInterface<E> for OpenAIClientHandle<E>
17where
18    // We unify each sub‐trait’s “type Error=E” with the needed bounds:
19    E: From<OpenAIClientError>
20     + From<std::io::Error>
21     + Debug
22     + Send
23     + Sync,
24{
25    // No additional methods to define here, because it's just the aggregator.
26    // The sub‐traits are already implemented above.
27}
28
29impl<E> OpenAIClientHandle<E> 
30where
31    E: Debug + Send + Sync + From<OpenAIClientError>, // so we can do `.map_err(E::from)?`
32{
33    pub fn new() -> Arc<Self> {
34
35        info!("creating new OpenAI Client Handle");
36
37        let openai_api_key 
38            = std::env::var("OPENAI_API_KEY")
39            .expect("OPENAI_API_KEY environment variable not set");
40
41        // Initialize OpenAI client with your API key
42        let config = OpenAIConfig::new().with_api_key(openai_api_key);
43
44        let client = async_openai::Client::with_config(config);
45
46        Arc::new(Self { 
47            client,
48            _marker: std::marker::PhantomData::<E>,
49        })
50    }
51
52    delegate!{
53        to self.client {
54            pub fn batches(&self) -> async_openai::Batches<OpenAIConfig>;
55            pub fn files(&self) -> async_openai::Files<OpenAIConfig>;
56        }
57    }
58}