forked from volcengine/veadk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
140 lines (116 loc) · 5.01 KB
/
config.py
File metadata and controls
140 lines (116 loc) · 5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
from typing import Any
from dotenv import find_dotenv, load_dotenv, dotenv_values
from pydantic import BaseModel, Field
from veadk.configs.auth_configs import VeIdentityConfig
from veadk.configs.model_configs import RealtimeModelConfig
from veadk.configs.database_configs import (
MysqlConfig,
OpensearchConfig,
RedisConfig,
TOSConfig,
VikingKnowledgebaseConfig,
)
from veadk.configs.model_configs import ModelConfig
from veadk.configs.tool_configs import BuiltinToolConfigs, PromptPilotConfig
from veadk.configs.tracing_configs import (
APMPlusConfig,
CozeloopConfig,
PrometheusConfig,
TLSConfig,
)
from veadk.utils.logger import get_logger
from veadk.utils.misc import set_envs
logger = get_logger(__name__)
env_file_path = os.path.join(os.getcwd(), ".env")
if os.path.isfile(env_file_path):
load_dotenv(env_file_path)
env_from_dotenv = dotenv_values(env_file_path)
logger.info(f"Find `.env` file in {env_file_path}, load envs.")
else:
env_from_dotenv = {}
logger.info("No `.env` file found.")
provider = (os.getenv("CLOUD_PROVIDER") or "").lower()
if provider == "byteplus":
byteplus_access_key = os.getenv("BYTEPLUS_ACCESS_KEY")
if byteplus_access_key:
os.environ["VOLCENGINE_ACCESS_KEY"] = byteplus_access_key
byteplus_secret_key = os.getenv("BYTEPLUS_SECRET_KEY")
if byteplus_secret_key:
os.environ["VOLCENGINE_SECRET_KEY"] = byteplus_secret_key
class VeADKConfig(BaseModel):
model: ModelConfig = Field(default_factory=ModelConfig)
"""Config for agent reasoning model."""
tool: BuiltinToolConfigs = Field(default_factory=BuiltinToolConfigs)
prompt_pilot: PromptPilotConfig = Field(default_factory=PromptPilotConfig)
apmplus_config: APMPlusConfig = Field(default_factory=APMPlusConfig)
cozeloop_config: CozeloopConfig = Field(default_factory=CozeloopConfig)
tls_config: TLSConfig = Field(default_factory=TLSConfig)
prometheus_config: PrometheusConfig = Field(default_factory=PrometheusConfig)
tos: TOSConfig = Field(default_factory=TOSConfig)
opensearch: OpensearchConfig = Field(default_factory=OpensearchConfig)
mysql: MysqlConfig = Field(default_factory=MysqlConfig)
redis: RedisConfig = Field(default_factory=RedisConfig)
viking_knowledgebase: VikingKnowledgebaseConfig = Field(
default_factory=VikingKnowledgebaseConfig
)
veidentity: VeIdentityConfig = Field(default_factory=VeIdentityConfig)
realtime_model: RealtimeModelConfig = Field(default_factory=RealtimeModelConfig)
def getenv(
env_name: str, default_value: Any = "", allow_false_values: bool = False
) -> str:
"""
Get environment variable.
Args:
env_name (str): The name of the environment variable.
default_value (str): The default value of the environment variable.
allow_false_values (bool, optional): Whether to allow the environment variable to be None or false values. Defaults to False.
Returns:
str: The value of the environment variable.
"""
value = os.getenv(env_name, default_value)
if value == default_value or not value:
provider = (os.getenv("CLOUD_PROVIDER") or "").lower()
if provider == "byteplus":
if env_name == "VOLCENGINE_ACCESS_KEY":
byteplus_key = os.getenv("BYTEPLUS_ACCESS_KEY")
if byteplus_key:
os.environ["VOLCENGINE_ACCESS_KEY"] = byteplus_key
value = byteplus_key
elif env_name == "VOLCENGINE_SECRET_KEY":
byteplus_key = os.getenv("BYTEPLUS_SECRET_KEY")
if byteplus_key:
os.environ["VOLCENGINE_SECRET_KEY"] = byteplus_key
value = byteplus_key
if allow_false_values:
return value
if value:
return value
else:
raise ValueError(
f"The environment variable `{env_name}` not exists. Please set this in your environment variable or config.yaml."
)
config_yaml_path = find_dotenv(filename="config.yaml", usecwd=True)
veadk_environments = dict(env_from_dotenv)
if config_yaml_path:
logger.info(f"Find `config.yaml` file in {config_yaml_path}")
config_dict, _veadk_environments = set_envs(
config_yaml_path=config_yaml_path, env_from_dotenv=env_from_dotenv
)
veadk_environments.update(_veadk_environments)
else:
logger.warning("No `config.yaml` file found.")
settings = VeADKConfig()