use crate::client::env::{self, EnvError};
use crate::providers::openai::wire::{COHERE, Chat, OpenAIConfig};
use crate::wire::Secret;
use serde::{Deserialize, Serialize};
use super::NativeChat;
const BASE_URL: &str = "https://api.cohere.ai";
const COMPATIBILITY_PATH: &str = "/compatibility/v1";
const API_KEY_ENV: &str = "COHERE_API_KEY";
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CohereConfig {
pub api_key: Secret,
pub base_url: String,
}
impl CohereConfig {
pub fn new(api_key: impl Into<Secret>) -> Self {
Self {
api_key: api_key.into(),
base_url: BASE_URL.to_owned(),
}
}
pub fn from_env() -> Result<Self, EnvError> {
Ok(Self::new(env::required(API_KEY_ENV)?))
}
pub fn with_base_url(mut self, base_url: impl AsRef<str>) -> Self {
self.base_url = base_url.as_ref().trim_end_matches('/').to_owned();
self
}
pub fn completion(&self, model: impl Into<String>) -> Chat {
OpenAIConfig::with_key(&COHERE, self.api_key.clone())
.with_base_url(format!("{}{COMPATIBILITY_PATH}", self.base_url))
.chat(model)
}
pub fn native_completion(&self, model: impl Into<String>) -> NativeChat {
NativeChat::new(self.clone(), model)
}
}
#[cfg(test)]
mod tests;