feat(config): support environment variable references in configuration #1380
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Pull Request: Support environment variable references in config.yaml
Summary
This PR adds support for environment variable references in the configuration file using the
$ENV_VAR
or${ENV_VAR}
syntax. When a configuration field value starts with$
, it will be treated as an environment variable reference and resolved accordingly.Motivation
Although aichat by default reads the {ModelName}_API_KEY environment variable to get the api_key, sometimes I prefer to use environment variables with other names.
For example, when I use deepseek-chat, which is an OpenAI-compatible model, I have uniformly set it to OPENAI_API_KEY, but I have to configure a DEEPSEEK_API_KEY to make it work.
Moreover, when I first configured it, I didn't name the model "deepseek", so aichat kept prompting me with authentication errors until I read the source code.
Many other tools support configurations like ${MY_API_KEY}, and I think aichat should also support this. It would be more friendly to people who are new to aichat. At least when I was configuring it, I spent some time getting used to it.
Changes
Modified Files
src/client/macros.rs
: Updated theconfig_get_fn!
macro to support environment variable referencesImplementation Details
The
config_get_fn!
macro has been enhanced with the following priority logic:Environment Variable Reference (Highest Priority): If a configuration field value starts with
$
, it's treated as an environment variable reference$VAR
and${VAR}
syntax$
,{
, and}
charactersStandard Environment Variable: If no environment variable reference is found, falls back to the standard environment variable lookup using the pattern
{CLIENT_NAME}_{FIELD_NAME}
Configuration Value: If standard environment variable is not found, uses the original configuration value
Example Usage
Before (config.yaml)
After (config.yaml)