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::wire::{Chat, 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`.
33    pub fn completion(&self, model: impl Into<String>) -> Model<Chat> {
34        self.model(self.config.completion(model))
35    }
36
37    /// The text-embedding model for `model`. `ndims` is the width it
38    /// reports, defaulting to the model's known width.
39    pub fn embedding(&self, model: impl Into<String>, ndims: Option<usize>) -> Model<Embeddings> {
40        self.model(self.config.embedding(model, ndims))
41    }
42
43    /// The image-embedding model. Cohere embeds images with one fixed model,
44    /// so it names none.
45    pub fn image_embedding(&self) -> Model<ImageEmbeddings> {
46        self.model(self.config.image_embedding())
47    }
48}