Skip to main content

objectiveai_api/ensemble_llm/
client.rs

1//! Ensemble LLM client for listing, retrieving, and fetching Ensemble LLMs.
2
3use crate::ctx;
4use std::sync::Arc;
5
6/// Client for Ensemble LLM operations.
7///
8/// Combines a caching fetcher for Ensemble LLM definitions with a retrieval
9/// client for listing and usage statistics.
10pub struct Client<CTXEXT, FENSLLM, RTRVL> {
11    /// Caching fetcher for Ensemble LLM definitions.
12    pub ensemble_llm_fetcher: Arc<super::fetcher::CachingFetcher<CTXEXT, FENSLLM>>,
13    /// Client for listing Ensemble LLMs and getting usage.
14    pub retrieval_client: Arc<RTRVL>,
15    /// Phantom data for the context extension type.
16    pub _ctx_ext: std::marker::PhantomData<CTXEXT>,
17}
18
19impl<CTXEXT, FENSLLM, RTRVL> Client<CTXEXT, FENSLLM, RTRVL> {
20    /// Creates a new Ensemble LLM client.
21    pub fn new(
22        ensemble_llm_fetcher: Arc<
23            super::fetcher::CachingFetcher<
24                CTXEXT,
25                FENSLLM,
26            >,
27        >,
28        retrieval_client: Arc<RTRVL>,
29    ) -> Self {
30        Self {
31            ensemble_llm_fetcher,
32            retrieval_client,
33            _ctx_ext: std::marker::PhantomData,
34        }
35    }
36}
37
38impl<CTXEXT, FENSLLM, RTRVL> Client<CTXEXT, FENSLLM, RTRVL>
39where
40    CTXEXT: Send + Sync + 'static,
41    FENSLLM: super::fetcher::Fetcher<CTXEXT>
42        + Send
43        + Sync
44        + 'static,
45    RTRVL: super::retrieval_client::Client<CTXEXT> + Send + Sync + 'static,
46{
47    /// Lists all Ensemble LLMs.
48    pub async fn list(
49        &self,
50        ctx: ctx::Context<CTXEXT>,
51    ) -> Result<
52        objectiveai::ensemble_llm::response::ListEnsembleLlm,
53        objectiveai::error::ResponseError,
54    > {
55        self.retrieval_client.list(ctx).await
56    }
57
58    /// Retrieves an Ensemble LLM by its ID.
59    ///
60    /// Returns a 404 error if the Ensemble LLM is not found.
61    pub async fn get(
62        &self,
63        ctx: ctx::Context<CTXEXT>,
64        id: &str,
65    ) -> Result<
66        objectiveai::ensemble_llm::response::GetEnsembleLlm,
67        objectiveai::error::ResponseError,
68    > {
69        self.ensemble_llm_fetcher
70            .fetch(ctx, id)
71            .await?
72            .ok_or_else(|| objectiveai::error::ResponseError {
73                code: 404,
74                message: serde_json::json!({
75                    "kind": "ensemble_llm",
76                    "error": "Ensemble LLM not found"
77                }),
78            })
79            .map(|(inner, created)| {
80                objectiveai::ensemble_llm::response::GetEnsembleLlm {
81                    created,
82                    inner,
83                }
84            })
85    }
86
87    /// Retrieves usage statistics for an Ensemble LLM.
88    pub async fn get_usage(
89        &self,
90        ctx: ctx::Context<CTXEXT>,
91        id: &str,
92    ) -> Result<
93        objectiveai::ensemble_llm::response::UsageEnsembleLlm,
94        objectiveai::error::ResponseError,
95    > {
96        self.retrieval_client.get_usage(ctx, id).await
97    }
98}