notedeck

One damus client to rule them all
git clone git://jb55.com/notedeck
Log | Files | Refs | README | LICENSE

config.rs (2627B)


      1 use async_openai::config::OpenAIConfig;
      2 
      3 #[derive(Debug)]
      4 pub struct ModelConfig {
      5     pub trial: bool,
      6     endpoint: Option<String>,
      7     model: String,
      8     api_key: Option<String>,
      9 }
     10 
     11 // short-term trial key for testing
     12 const DAVE_TRIAL: &str = unsafe {
     13     std::str::from_utf8_unchecked(&[
     14         0x73, 0x6b, 0x2d, 0x70, 0x72, 0x6f, 0x6a, 0x2d, 0x54, 0x6b, 0x61, 0x48, 0x46, 0x32, 0x73,
     15         0x72, 0x43, 0x59, 0x73, 0x5a, 0x62, 0x33, 0x6f, 0x6b, 0x43, 0x75, 0x61, 0x78, 0x39, 0x57,
     16         0x76, 0x72, 0x41, 0x46, 0x67, 0x5f, 0x39, 0x58, 0x78, 0x35, 0x65, 0x37, 0x4b, 0x53, 0x36,
     17         0x76, 0x32, 0x32, 0x51, 0x30, 0x67, 0x48, 0x61, 0x58, 0x6b, 0x67, 0x6e, 0x4e, 0x4d, 0x63,
     18         0x7a, 0x69, 0x72, 0x5f, 0x44, 0x57, 0x6e, 0x7a, 0x43, 0x77, 0x52, 0x50, 0x4e, 0x50, 0x39,
     19         0x6b, 0x5a, 0x79, 0x75, 0x57, 0x4c, 0x35, 0x54, 0x33, 0x42, 0x6c, 0x62, 0x6b, 0x46, 0x4a,
     20         0x72, 0x66, 0x49, 0x4b, 0x31, 0x77, 0x4f, 0x67, 0x31, 0x6a, 0x37, 0x54, 0x57, 0x42, 0x5a,
     21         0x67, 0x66, 0x49, 0x75, 0x30, 0x51, 0x48, 0x4e, 0x31, 0x70, 0x6a, 0x72, 0x37, 0x4b, 0x38,
     22         0x55, 0x54, 0x6d, 0x34, 0x50, 0x6f, 0x65, 0x47, 0x39, 0x61, 0x35, 0x79, 0x6c, 0x78, 0x45,
     23         0x4f, 0x6f, 0x74, 0x43, 0x47, 0x42, 0x36, 0x65, 0x7a, 0x59, 0x5a, 0x37, 0x70, 0x54, 0x38,
     24         0x63, 0x44, 0x75, 0x66, 0x75, 0x36, 0x52, 0x4d, 0x6b, 0x6c, 0x2d, 0x44, 0x51, 0x41,
     25     ])
     26 };
     27 
     28 impl Default for ModelConfig {
     29     fn default() -> Self {
     30         let api_key = std::env::var("DAVE_API_KEY")
     31             .ok()
     32             .or(std::env::var("OPENAI_API_KEY").ok());
     33 
     34         // trial mode?
     35         let trial = api_key.is_none();
     36         let api_key = api_key.or(Some(DAVE_TRIAL.to_string()));
     37 
     38         ModelConfig {
     39             trial,
     40             endpoint: std::env::var("DAVE_ENDPOINT").ok(),
     41             model: std::env::var("DAVE_MODEL")
     42                 .ok()
     43                 .unwrap_or("gpt-4o".to_string()),
     44             api_key,
     45         }
     46     }
     47 }
     48 
     49 impl ModelConfig {
     50     pub fn model(&self) -> &str {
     51         &self.model
     52     }
     53 
     54     pub fn ollama() -> Self {
     55         ModelConfig {
     56             trial: false,
     57             endpoint: std::env::var("OLLAMA_HOST").ok().map(|h| h + "/v1"),
     58             model: "hhao/qwen2.5-coder-tools:latest".to_string(),
     59             api_key: None,
     60         }
     61     }
     62 
     63     pub fn to_api(&self) -> OpenAIConfig {
     64         let mut cfg = OpenAIConfig::new();
     65         if let Some(endpoint) = &self.endpoint {
     66             cfg = cfg.with_api_base(endpoint.to_owned());
     67         }
     68 
     69         if let Some(api_key) = &self.api_key {
     70             cfg = cfg.with_api_key(api_key.to_owned());
     71         }
     72 
     73         cfg
     74     }
     75 }