refactor: config creation prompt aware of env var (#722)

pull/723/head
sigoden 3 months ago committed by GitHub
parent 24347a8a66
commit e81b215696
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -552,7 +552,7 @@ pub fn create_config(prompts: &[PromptAction], client: &str) -> Result<(String,
"type": client, "type": client,
}); });
let mut model = client.to_string(); 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]); let clients = json!(vec![config]);
Ok((model, clients)) 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(); 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]); let clients = json!(vec![config]);
Ok(Some((model, clients))) Ok(Some((model, clients)))
} }
@ -698,22 +698,30 @@ pub fn maybe_catch_error(data: &Value) -> Result<()> {
Ok(()) Ok(())
} }
fn set_client_config_values( fn set_client_config(
list: &[PromptAction], list: &[PromptAction],
model: &mut String, model: &mut String,
client_config: &mut Value, client_config: &mut Value,
) -> Result<()> { ) -> Result<()> {
let env_prefix = model.clone();
for (path, desc, required, kind) in list { 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 { match kind {
PromptKind::String => { 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); set_client_config_value(client_config, path, kind, &value);
if *path == "name" { if *path == "name" {
*model = value; *model = value;
} }
} }
PromptKind::Integer => { 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); set_client_config_value(client_config, path, kind, &value);
} }
} }

@ -104,7 +104,7 @@ impl ErnieClient {
async fn prepare_access_token(&self) -> Result<()> { async fn prepare_access_token(&self) -> Result<()> {
let client_name = self.name(); let client_name = self.name();
if !is_valid_access_token(client_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 = self.config.api_key.clone();
let api_key = api_key let api_key = api_key
.or_else(|| env::var(format!("{env_prefix}_API_KEY")).ok()) .or_else(|| env::var(format!("{env_prefix}_API_KEY")).ok())

@ -36,11 +36,7 @@ pub fn now() -> String {
} }
pub fn get_env_name(key: &str) -> String { pub fn get_env_name(key: &str) -> String {
format!( format!("{}_{key}", env!("CARGO_CRATE_NAME"),).to_ascii_uppercase()
"{}_{}",
env!("CARGO_CRATE_NAME").to_uppercase(),
key.to_uppercase(),
)
} }
pub fn estimate_token_length(text: &str) -> usize { pub fn estimate_token_length(text: &str) -> usize {

Loading…
Cancel
Save