Skip to main content

rig_core/client/
cohere.rs

1//! The Cohere client: a [`CohereConfig`] on a transport, and the models it
2//! builds.
3
4use crate::client::macros::http_client;
5use crate::driver::Model;
6
7use crate::providers::cohere::{CohereChat, CohereConfig, Embeddings, ImageEmbeddings};
8
9http_client!(
10    /// Cohere: its [`CohereConfig`] on a transport. Every model it builds
11    /// sends through that transport.
12    Cohere,
13    CohereConfig
14);
15
16impl Cohere {
17    /// Cohere with `api_key` and default settings, on the shared reqwest
18    /// client.
19    #[cfg(feature = "reqwest")]
20    #[cfg_attr(docsrs, doc(cfg(feature = "reqwest")))]
21    pub fn new(api_key: impl Into<crate::wire::Secret>) -> Self {
22        CohereConfig::new(api_key).client()
23    }
24
25    /// Cohere from `COHERE_API_KEY`, on the shared reqwest client.
26    #[cfg(feature = "reqwest")]
27    #[cfg_attr(docsrs, doc(cfg(feature = "reqwest")))]
28    pub fn from_env() -> Result<Self, crate::client::env::EnvError> {
29        Ok(CohereConfig::from_env()?.client())
30    }
31
32    /// The chat model for `model`, on Cohere's OpenAI Compatibility API
33    /// until its wire opts in to the native chat API
34    /// ([`ChatRoute`](crate::providers::cohere::ChatRoute)).
35    pub fn completion(&self, model: impl Into<String>) -> Model<CohereChat> {
36        self.model(self.config.completion(model))
37    }
38
39    /// The text-embedding model for `model`. `ndims` is the width it
40    /// reports, defaulting to the model's known width.
41    pub fn embedding(&self, model: impl Into<String>, ndims: Option<usize>) -> Model<Embeddings> {
42        self.model(self.config.embedding(model, ndims))
43    }
44
45    /// The image-embedding model. Cohere embeds images with one fixed model,
46    /// so it names none.
47    pub fn image_embedding(&self) -> Model<ImageEmbeddings> {
48        self.model(self.config.image_embedding())
49    }
50}