-
-
Notifications
You must be signed in to change notification settings - Fork 11.7k
(feat) - allow using os.environ/ vars for any value on config.yaml #6276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
fc0429a
add check for os.environ vars when readin config.yaml
ishaan-jaff 00163bc
use base class for reading from config.yaml
ishaan-jaff 33d25d1
fix import
ishaan-jaff 752c033
fix linting
ishaan-jaff e0ebf51
add unit tests for base config class
ishaan-jaff 5a431a4
fix order of reading elements from config.yaml
ishaan-jaff 37e455a
unit tests for reading configs from files
ishaan-jaff c7242d8
fix user_config_file_path
ishaan-jaff 2993fae
use simpler implementation
ishaan-jaff d2e6ea0
use helper to get_config
ishaan-jaff 64ebe61
working unit tests for reading configs
ishaan-jaff 070e2d8
Merge branch 'main' into litellm_update_proxy_config
ishaan-jaff File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,48 @@ | ||
| model_list: | ||
| - model_name: fake-openai-endpoint | ||
| ################################################################################ | ||
| # Azure | ||
| - model_name: gpt-4o-mini | ||
| litellm_params: | ||
| model: openai/fake | ||
| api_key: fake-key | ||
| api_base: https://exampleopenaiendpoint-production.up.railwaz.app/ | ||
| - model_name: db-openai-endpoint | ||
| model: azure/gpt-4o-mini | ||
| api_base: https://amazin-prod.openai.azure.com | ||
| api_key: "os.environ/AZURE_GPT_4O" | ||
| deployment_id: gpt-4o-mini | ||
| - model_name: gpt-4o | ||
| litellm_params: | ||
| model: openai/gpt-5 | ||
| api_key: fake-key | ||
| api_base: https://exampleopenaiendpoint-production.up.railwxaz.app/ | ||
| model: azure/gpt-4o | ||
| api_base: https://very-cool-prod.openai.azure.com | ||
| api_key: "os.environ/AZURE_GPT_4O" | ||
| deployment_id: gpt-4o | ||
|
|
||
| litellm_settings: | ||
| callbacks: ["arize"] | ||
| ################################################################################ | ||
| # Fireworks | ||
| - model_name: fireworks-llama-v3p1-405b-instruct | ||
| litellm_params: | ||
| model: fireworks_ai/accounts/fireworks/models/llama-v3p1-405b-instruct | ||
| api_key: "os.environ/FIREWORKS" | ||
| - model_name: fireworks-llama-v3p1-70b-instruct | ||
| litellm_params: | ||
| model: fireworks_ai/accounts/fireworks/models/llama-v3p1-70b-instruct | ||
| api_key: "os.environ/FIREWORKS" | ||
|
|
||
| general_settings: | ||
| alerting_threshold: 300 # sends alerts if requests hang for 5min+ and responses take 5min+ | ||
| litellm_settings: # module level litellm settings - https://github.com/BerriAI/litellm/blob/main/litellm/__init__.py | ||
| success_callback: ["prometheus"] | ||
| service_callback: ["prometheus_system"] | ||
| drop_params: False # Raise an exception if the openai param being passed in isn't supported. | ||
| cache: false | ||
| default_internal_user_params: | ||
| user_role: os.environ/DEFAULT_USER_ROLE | ||
|
|
||
| success_callback: ["s3"] | ||
| s3_callback_params: | ||
| s3_bucket_name: logs-bucket-litellm # AWS Bucket Name for S3 | ||
| s3_region_name: us-west-2 # AWS Region Name for S3 | ||
| s3_aws_access_key_id: os.environ/AWS_ACCESS_KEY_ID # us os.environ/<variable name> to pass environment variables. This is AWS Access Key ID for S3 | ||
| s3_aws_secret_access_key: os.environ/AWS_SECRET_ACCESS_KEY # AWS Secret Access Key for S3 | ||
| s3_path: my-test-path # [OPTIONAL] set path in bucket you want to write logs to | ||
| s3_endpoint_url: https://s3.amazonaws.com # [OPTIONAL] S3 endpoint URL, if you want to use Backblaze/cloudflare s3 buckets | ||
|
|
||
| router_settings: | ||
| routing_strategy: simple-shuffle # "simple-shuffle" shown to result in highest throughput. https://docs.litellm.ai/docs/proxy/configs#load-balancing |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
tests/local_testing/example_config_yaml/config_with_env_vars.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| model_list: | ||
| ################################################################################ | ||
| # Azure | ||
| - model_name: gpt-4o-mini | ||
| litellm_params: | ||
| model: azure/gpt-4o-mini | ||
| api_base: https://amazin-prod.openai.azure.com | ||
| api_key: "os.environ/AZURE_GPT_4O" | ||
| deployment_id: gpt-4o-mini | ||
| - model_name: gpt-4o | ||
| litellm_params: | ||
| model: azure/gpt-4o | ||
| api_base: https://very-cool-prod.openai.azure.com | ||
| api_key: "os.environ/AZURE_GPT_4O" | ||
| deployment_id: gpt-4o | ||
|
|
||
| ################################################################################ | ||
| # Fireworks | ||
| - model_name: fireworks-llama-v3p1-405b-instruct | ||
| litellm_params: | ||
| model: fireworks_ai/accounts/fireworks/models/llama-v3p1-405b-instruct | ||
| api_key: "os.environ/FIREWORKS" | ||
| - model_name: fireworks-llama-v3p1-70b-instruct | ||
| litellm_params: | ||
| model: fireworks_ai/accounts/fireworks/models/llama-v3p1-70b-instruct | ||
| api_key: "os.environ/FIREWORKS" | ||
|
|
||
| general_settings: | ||
| alerting_threshold: 300 # sends alerts if requests hang for 5min+ and responses take 5min+ | ||
| litellm_settings: # module level litellm settings - https://github.com/BerriAI/litellm/blob/main/litellm/__init__.py | ||
| success_callback: ["prometheus"] | ||
| service_callback: ["prometheus_system"] | ||
| drop_params: False # Raise an exception if the openai param being passed in isn't supported. | ||
| cache: false | ||
| default_internal_user_params: | ||
| user_role: os.environ/DEFAULT_USER_ROLE | ||
|
|
||
| success_callback: ["s3"] | ||
| s3_callback_params: | ||
| s3_bucket_name: logs-bucket-litellm # AWS Bucket Name for S3 | ||
| s3_region_name: us-west-2 # AWS Region Name for S3 | ||
| s3_aws_access_key_id: os.environ/AWS_ACCESS_KEY_ID # us os.environ/<variable name> to pass environment variables. This is AWS Access Key ID for S3 | ||
| s3_aws_secret_access_key: os.environ/AWS_SECRET_ACCESS_KEY # AWS Secret Access Key for S3 | ||
| s3_path: my-test-path # [OPTIONAL] set path in bucket you want to write logs to | ||
| s3_endpoint_url: https://s3.amazonaws.com # [OPTIONAL] S3 endpoint URL, if you want to use Backblaze/cloudflare s3 buckets | ||
|
|
||
| router_settings: | ||
| routing_strategy: simple-shuffle # "simple-shuffle" shown to result in highest throughput. https://docs.litellm.ai/docs/proxy/configs#load-balancing |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| import os | ||
| import sys | ||
| import traceback | ||
| from unittest import mock | ||
| import pytest | ||
|
|
||
| from dotenv import load_dotenv | ||
|
|
||
| import litellm.proxy | ||
| import litellm.proxy.proxy_server | ||
|
|
||
| load_dotenv() | ||
| import io | ||
| import os | ||
|
|
||
| # this file is to test litellm/proxy | ||
|
|
||
| sys.path.insert( | ||
| 0, os.path.abspath("../..") | ||
| ) # Adds the parent directory to the system path | ||
| import asyncio | ||
| import logging | ||
|
|
||
| from litellm.proxy.proxy_server import ProxyConfig | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_basic_reading_configs_from_files(): | ||
| """ | ||
| Test that the config is read correctly from the files in the example_config_yaml folder | ||
| """ | ||
| proxy_config_instance = ProxyConfig() | ||
| current_path = os.path.dirname(os.path.abspath(__file__)) | ||
| example_config_yaml_path = os.path.join(current_path, "example_config_yaml") | ||
|
|
||
| # get all the files from example_config_yaml | ||
| files = os.listdir(example_config_yaml_path) | ||
| print(files) | ||
|
|
||
| for file in files: | ||
| config_path = os.path.join(example_config_yaml_path, file) | ||
| config = await proxy_config_instance.get_config(config_file_path=config_path) | ||
| print(config) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_read_config_from_bad_file_path(): | ||
| """ | ||
| Raise an exception if the file path is not valid | ||
| """ | ||
| proxy_config_instance = ProxyConfig() | ||
| config_path = "non-existent-file.yaml" | ||
| with pytest.raises(Exception): | ||
| config = await proxy_config_instance.get_config(config_file_path=config_path) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_read_config_file_with_os_environ_vars(): | ||
| """ | ||
| Ensures os.environ variables are read correctly from config.yaml | ||
| Following vars are set as os.environ variables in the config.yaml file | ||
| - DEFAULT_USER_ROLE | ||
| - AWS_ACCESS_KEY_ID | ||
| - AWS_SECRET_ACCESS_KEY | ||
| - AZURE_GPT_4O | ||
| - FIREWORKS | ||
| """ | ||
|
|
||
| _env_vars_for_testing = { | ||
| "DEFAULT_USER_ROLE": "admin", | ||
| "AWS_ACCESS_KEY_ID": "1234567890", | ||
| "AWS_SECRET_ACCESS_KEY": "1234567890", | ||
| "AZURE_GPT_4O": "1234567890", | ||
| "FIREWORKS": "1234567890", | ||
| } | ||
|
|
||
| _old_env_vars = {} | ||
| for key, value in _env_vars_for_testing.items(): | ||
| if key in os.environ: | ||
| _old_env_vars[key] = os.environ.get(key) | ||
| os.environ[key] = value | ||
|
|
||
| # Read config | ||
| proxy_config_instance = ProxyConfig() | ||
| current_path = os.path.dirname(os.path.abspath(__file__)) | ||
| config_path = os.path.join( | ||
| current_path, "example_config_yaml", "config_with_env_vars.yaml" | ||
| ) | ||
| config = await proxy_config_instance.get_config(config_file_path=config_path) | ||
| print(config) | ||
|
|
||
| # Add assertions | ||
| assert ( | ||
| config["litellm_settings"]["default_internal_user_params"]["user_role"] | ||
| == "admin" | ||
| ) | ||
| assert ( | ||
| config["litellm_settings"]["s3_callback_params"]["s3_aws_access_key_id"] | ||
| == "1234567890" | ||
| ) | ||
| assert ( | ||
| config["litellm_settings"]["s3_callback_params"]["s3_aws_secret_access_key"] | ||
| == "1234567890" | ||
| ) | ||
|
|
||
| for model in config["model_list"]: | ||
| if "azure" in model["litellm_params"]["model"]: | ||
| assert model["litellm_params"]["api_key"] == "1234567890" | ||
| elif "fireworks" in model["litellm_params"]["model"]: | ||
| assert model["litellm_params"]["api_key"] == "1234567890" | ||
|
|
||
| # cleanup | ||
| for key, value in _env_vars_for_testing.items(): | ||
| if key in _old_env_vars: | ||
| os.environ[key] = _old_env_vars[key] | ||
| else: | ||
| del os.environ[key] |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this line is where i would just run a
get_env_var()function