Skip to main content

anylm/
options.rs

1use crate::{api::ApiKind, prelude::*};
2
3/// The AI options
4#[derive(Default, Clone, Debug, Serialize, Deserialize)]
5pub struct Options {
6    #[serde(rename = "type")]
7    pub kind: ApiKind,
8
9    #[serde(default)]
10    #[serde(skip_serializing_if = "Option::is_none")]
11    pub base_url: Option<String>,
12
13    #[serde(default)]
14    #[serde(skip_serializing_if = "Option::is_none")]
15    pub env_var: Option<String>,
16
17    #[serde(default)]
18    #[serde(skip_serializing_if = "Option::is_none")]
19    pub proxy: Option<String>,
20
21    pub model: String,
22
23    #[serde(default)]
24    #[serde(skip_serializing_if = "Option::is_none")]
25    pub max_tokens: Option<i32>,
26
27    #[serde(default)]
28    #[serde(skip_serializing_if = "Option::is_none")]
29    pub temperature: Option<f32>,
30}
31
32impl Options {
33    pub fn new(kind: ApiKind) -> Self {
34        Self {
35            kind,
36            ..Default::default()
37        }
38    }
39
40    pub fn openai() -> Self {
41        Self::new(ApiKind::OpenAi)
42    }
43
44    #[cfg(feature = "anthropic")]
45    pub fn anthropic() -> Self {
46        Self::new(ApiKind::Anthropic)
47    }
48
49    #[cfg(feature = "google")]
50    pub fn google() -> Self {
51        Self::new(ApiKind::Google)
52    }
53
54    pub fn kind(mut self, kind: ApiKind) -> Self {
55        self.kind = kind;
56        self
57    }
58
59    pub fn base_url(mut self, url: impl Into<String>) -> Self {
60        let val = url.into();
61        self.base_url = if val.is_empty() { None } else { Some(val) };
62        self
63    }
64
65    pub fn env_var(mut self, var: impl Into<String>) -> Self {
66        let val = var.into();
67        self.env_var = if val.is_empty() { None } else { Some(val) };
68        self
69    }
70
71    pub fn proxy(mut self, proxy: impl Into<String>) -> Self {
72        let val = proxy.into();
73        self.proxy = if val.is_empty() { None } else { Some(val) };
74        self
75    }
76
77    pub fn model(mut self, model: impl Into<String>) -> Self {
78        self.model = model.into();
79        self
80    }
81
82    pub fn max_tokens(mut self, tokens: i32) -> Self {
83        self.max_tokens = Some(tokens);
84        self
85    }
86
87    pub fn temperature(mut self, temp: f32) -> Self {
88        self.temperature = Some(temp);
89        self
90    }
91}