Skip to content
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

🐛: Add function to replace env vars when using multi-llm config #329

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion taskweaver/config/config_mgt.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import re
from dataclasses import dataclass
from typing import Any, Dict, List, Literal, NamedTuple, Optional
from ..utils import replace_env_vars

AppConfigSourceType = Literal["override", "env", "json", "app", "default"]
AppConfigValueType = Literal["str", "int", "float", "bool", "list", "enum", "path", "dict"]
Expand Down Expand Up @@ -97,7 +98,10 @@ def _get_config_value(
return val

if var_name in self.json_file_store.keys():
return self.json_file_store.get(var_name, default_value)
val = self.json_file_store.get(var_name, default_value)
# e.g., llm.api_base -> LLM_API_BASE
val = replace_env_vars(val)
return val

if default_value is not None:
return default_value
Expand Down
1 change: 1 addition & 0 deletions taskweaver/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from datetime import datetime
from hashlib import md5
from typing import Any, Dict, List, Union
from .json_replace import replace_env_vars


def create_id(length: int = 4) -> str:
Expand Down
12 changes: 12 additions & 0 deletions taskweaver/utils/json_replace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import os

# Function to replace env variables on nested JSON
def replace_env_vars(data, key=None):
if isinstance(data, list):
return data
if isinstance(data, dict):
return {k: replace_env_vars(v) for k, v in data.items()}
env_var_name = data.upper().replace(".", "_")
env_val = os.environ.get(env_var_name, None)
return env_val if env_val != None else data