Skip to main content

objectiveai_api/vector/completions/cache/
client.rs

1//! Vote cache client implementation.
2
3use crate::{ctx, vector};
4use std::sync::Arc;
5
6/// Client for retrieving cached votes.
7#[derive(Debug, Clone)]
8pub struct Client<CTXEXT, FVVOTE, FCVOTE> {
9    /// Fetcher for votes from historical completions.
10    pub completion_votes_fetcher: Arc<FVVOTE>,
11    /// Fetcher for votes from the global cache.
12    pub cache_vote_fetcher: Arc<FCVOTE>,
13    _marker: std::marker::PhantomData<CTXEXT>,
14}
15
16impl<CTXEXT, FVVOTE, FCVOTE> Client<CTXEXT, FVVOTE, FCVOTE> {
17    /// Creates a new cache client.
18    pub fn new(
19        completion_votes_fetcher: Arc<FVVOTE>,
20        cache_vote_fetcher: Arc<FCVOTE>,
21    ) -> Self {
22        Self {
23            completion_votes_fetcher,
24            cache_vote_fetcher,
25            _marker: std::marker::PhantomData,
26        }
27    }
28}
29
30impl<CTXEXT, FVVOTE, FCVOTE> Client<CTXEXT, FVVOTE, FCVOTE>
31where
32    CTXEXT: Send + Sync + 'static,
33    FVVOTE: vector::completions::completion_votes_fetcher::Fetcher<CTXEXT>
34        + Send
35        + Sync
36        + 'static,
37    FCVOTE: vector::completions::cache_vote_fetcher::Fetcher<CTXEXT>
38        + Send
39        + Sync
40        + 'static,
41{
42    /// Retrieves all votes from a historical vector completion.
43    pub async fn fetch_completion_votes(
44        &self,
45        ctx: ctx::Context<CTXEXT>,
46        id: &str,
47    ) -> Result<
48        objectiveai::vector::completions::cache::response::CompletionVotes,
49        objectiveai::error::ResponseError,
50    > {
51        let data = self.completion_votes_fetcher.fetch(ctx, id).await?;
52        Ok(objectiveai::vector::completions::cache::response::CompletionVotes {
53            data,
54        })
55    }
56
57    /// Requests a vote from the global ObjectiveAI cache.
58    ///
59    /// Returns a cached vote if one exists for the given model, messages, tools, and responses.
60    pub async fn fetch_cache_vote(
61        &self,
62        ctx: ctx::Context<CTXEXT>,
63        model: &objectiveai::chat::completions::request::Model,
64        models: Option<&[objectiveai::chat::completions::request::Model]>,
65        messages: &[objectiveai::chat::completions::request::Message],
66        tools: Option<&[objectiveai::chat::completions::request::Tool]>,
67        responses: &[objectiveai::chat::completions::request::RichContent],
68    ) -> Result<
69        objectiveai::vector::completions::cache::response::CacheVote,
70        objectiveai::error::ResponseError,
71    > {
72        let vote = self
73            .cache_vote_fetcher
74            .fetch(ctx, model, models, messages, tools, responses)
75            .await?;
76        Ok(
77            objectiveai::vector::completions::cache::response::CacheVote {
78                vote,
79            },
80        )
81    }
82}