rig/providers/cohere/
client.rs

1use crate::{
2    Embed,
3    client::{
4        self, BearerAuth, Capabilities, Capable, DebugExt, Nothing, Provider, ProviderBuilder,
5        ProviderClient,
6    },
7    embeddings::EmbeddingsBuilder,
8    http_client::{self, HttpClientExt},
9    wasm_compat::*,
10};
11
12use super::{CompletionModel, EmbeddingModel};
13use serde::Deserialize;
14
15// ================================================================
16// Main Cohere Client
17// ================================================================
18
19#[derive(Debug, Default, Clone, Copy)]
20pub struct CohereExt;
21
22#[derive(Debug, Default, Clone, Copy)]
23pub struct CohereBuilder;
24
25type CohereApiKey = BearerAuth;
26
27pub type Client<H = reqwest::Client> = client::Client<CohereExt, H>;
28pub type ClientBuilder<H = reqwest::Client> = client::ClientBuilder<CohereBuilder, CohereApiKey, H>;
29
30impl Provider for CohereExt {
31    type Builder = CohereBuilder;
32
33    const VERIFY_PATH: &'static str = "/models";
34
35    fn build<H>(
36        _: &client::ClientBuilder<Self::Builder, CohereApiKey, H>,
37    ) -> http_client::Result<Self> {
38        Ok(Self)
39    }
40}
41
42impl<H> Capabilities<H> for CohereExt {
43    type Completion = Capable<CompletionModel<H>>;
44    type Embeddings = Capable<EmbeddingModel<H>>;
45    type Transcription = Nothing;
46    #[cfg(feature = "image")]
47    type ImageGeneration = Nothing;
48
49    #[cfg(feature = "audio")]
50    type AudioGeneration = Nothing;
51}
52
53impl DebugExt for CohereExt {}
54
55impl ProviderBuilder for CohereBuilder {
56    type Output = CohereExt;
57    type ApiKey = CohereApiKey;
58
59    const BASE_URL: &'static str = "https://api.cohere.ai";
60
61    fn finish<H>(
62        &self,
63        builder: client::ClientBuilder<Self, CohereApiKey, H>,
64    ) -> http_client::Result<client::ClientBuilder<Self, CohereApiKey, H>> {
65        Ok(builder)
66    }
67}
68
69impl ProviderClient for Client {
70    type Input = CohereApiKey;
71
72    fn from_env() -> Self
73    where
74        Self: Sized,
75    {
76        let key = std::env::var("COHERE_API_KEY").expect("COHERE_API_KEY not set");
77        Self::new(key).unwrap()
78    }
79
80    fn from_val(input: Self::Input) -> Self
81    where
82        Self: Sized,
83    {
84        Self::new(input).unwrap()
85    }
86}
87
88#[derive(Debug, Deserialize)]
89pub struct ApiErrorResponse {
90    pub message: String,
91}
92
93#[derive(Debug, Deserialize)]
94#[serde(untagged)]
95pub enum ApiResponse<T> {
96    Ok(T),
97    Err(ApiErrorResponse),
98}
99
100impl<T> Client<T>
101where
102    T: HttpClientExt + Clone + WasmCompatSend + WasmCompatSync + 'static,
103{
104    pub fn embeddings<D: Embed>(
105        &self,
106        model: impl Into<String>,
107        input_type: &str,
108    ) -> EmbeddingsBuilder<EmbeddingModel<T>, D> {
109        EmbeddingsBuilder::new(self.embedding_model(model, input_type))
110    }
111
112    /// Note: default embedding dimension of 0 will be used if model is not known.
113    /// If this is the case, it's better to use function `embedding_model_with_ndims`
114    pub fn embedding_model(&self, model: impl Into<String>, input_type: &str) -> EmbeddingModel<T> {
115        let model = model.into();
116        let ndims = super::model_dimensions_from_identifier(&model).unwrap_or_default();
117
118        EmbeddingModel::new(self.clone(), model, input_type, ndims)
119    }
120
121    /// Create an embedding model with the given name and the number of dimensions in the embedding generated by the model.
122    pub fn embedding_model_with_ndims(
123        &self,
124        model: impl Into<String>,
125        input_type: &str,
126        ndims: usize,
127    ) -> EmbeddingModel<T> {
128        EmbeddingModel::new(self.clone(), model, input_type, ndims)
129    }
130}