diff options
| author | sigoden <sigoden@gmail.com> | 2024-07-17 12:30:35 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-17 12:30:35 +0800 |
| commit | e81b215696b8941acbe47a449c19f6967133e551 (patch) | |
| tree | 22f4796a09c41b26426bb132c634c6a89cb7668b /src | |
| parent | 24347a8a6634b18d3a7dbf7afb0df54fb4311826 (diff) | |
| download | aichat-e81b215696b8941acbe47a449c19f6967133e551.tar.gz | |
refactor: config creation prompt aware of env var (#722)
Diffstat (limited to 'src')
| -rw-r--r-- | src/client/common.rs | 18 | ||||
| -rw-r--r-- | src/client/ernie.rs | 2 | ||||
| -rw-r--r-- | src/utils/mod.rs | 6 |
3 files changed, 15 insertions, 11 deletions
diff --git a/src/client/common.rs b/src/client/common.rs index 725f715..dbae938 100644 --- a/src/client/common.rs +++ b/src/client/common.rs @@ -552,7 +552,7 @@ pub fn create_config(prompts: &[PromptAction], client: &str) -> Result<(String, "type": client, }); let mut model = client.to_string(); - set_client_config_values(prompts, &mut model, &mut config)?; + set_client_config(prompts, &mut model, &mut config)?; let clients = json!(vec![config]); Ok((model, clients)) } @@ -584,7 +584,7 @@ pub fn create_openai_compatible_client_config(client: &str) -> Result<Option<(St ] }; let mut model = client.to_string(); - set_client_config_values(&prompts, &mut model, &mut config)?; + set_client_config(&prompts, &mut model, &mut config)?; let clients = json!(vec![config]); Ok(Some((model, clients))) } @@ -698,22 +698,30 @@ pub fn maybe_catch_error(data: &Value) -> Result<()> { Ok(()) } -fn set_client_config_values( +fn set_client_config( list: &[PromptAction], model: &mut String, client_config: &mut Value, ) -> Result<()> { + let env_prefix = model.clone(); for (path, desc, required, kind) in list { + let mut required = *required; + if required { + let env_name = format!("{env_prefix}_{path}").to_ascii_uppercase(); + if std::env::var(&env_name).is_ok() { + required = false; + } + } match kind { PromptKind::String => { - let value = prompt_input_string(desc, *required)?; + let value = prompt_input_string(desc, required)?; set_client_config_value(client_config, path, kind, &value); if *path == "name" { *model = value; } } PromptKind::Integer => { - let value = prompt_input_integer(desc, *required)?; + let value = prompt_input_integer(desc, required)?; set_client_config_value(client_config, path, kind, &value); } } diff --git a/src/client/ernie.rs b/src/client/ernie.rs index 64c1820..fd5b907 100644 --- a/src/client/ernie.rs +++ b/src/client/ernie.rs @@ -104,7 +104,7 @@ impl ErnieClient { async fn prepare_access_token(&self) -> Result<()> { let client_name = self.name(); if !is_valid_access_token(client_name) { - let env_prefix = Self::name(&self.config).to_uppercase(); + let env_prefix = Self::name(&self.config).to_ascii_uppercase(); let api_key = self.config.api_key.clone(); let api_key = api_key .or_else(|| env::var(format!("{env_prefix}_API_KEY")).ok()) diff --git a/src/utils/mod.rs b/src/utils/mod.rs index aa63f3b..340359c 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -36,11 +36,7 @@ pub fn now() -> String { } pub fn get_env_name(key: &str) -> String { - format!( - "{}_{}", - env!("CARGO_CRATE_NAME").to_uppercase(), - key.to_uppercase(), - ) + format!("{}_{key}", env!("CARGO_CRATE_NAME"),).to_ascii_uppercase() } pub fn estimate_token_length(text: &str) -> usize { |
