Skip to content

Commit c5d5dc5

Browse files
feat(auth): add auto auth module (#133)
* feat(auth): add auto auth module * update auto auth * fix config path and envs * fix tls bugs * add some logs * fix envs
1 parent 93842a2 commit c5d5dc5

36 files changed

+1206
-414
lines changed

tests/test_agent.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -199,17 +199,6 @@ def test_agent_with_tracers():
199199
assert tracer2 in agent.tracers
200200

201201

202-
@patch.dict(
203-
"os.environ",
204-
{"MODEL_AGENT_NAME": "env_model_name", "MODEL_AGENT_API_KEY": "mock_api_key"},
205-
clear=True,
206-
)
207-
def test_agent_environment_variables():
208-
agent = Agent()
209-
print(agent)
210-
assert agent.model_name == "env_model_name"
211-
212-
213202
@patch.dict("os.environ", {"MODEL_AGENT_API_KEY": "mock_api_key"})
214203
def test_agent_custom_name_and_description():
215204
custom_name = "CustomAgent"

tests/test_tos.py

Lines changed: 0 additions & 169 deletions
This file was deleted.

veadk/agent.py

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,9 @@
2626
from pydantic import ConfigDict, Field
2727
from typing_extensions import Any
2828

29-
from veadk.config import getenv
29+
from veadk.config import settings
3030
from veadk.consts import (
3131
DEFAULT_AGENT_NAME,
32-
DEFAULT_MODEL_AGENT_API_BASE,
33-
DEFAULT_MODEL_AGENT_NAME,
34-
DEFAULT_MODEL_AGENT_PROVIDER,
3532
DEFAULT_MODEL_EXTRA_CONFIG,
3633
)
3734
from veadk.evaluation import EvalSetRecorder
@@ -63,26 +60,16 @@ class Agent(LlmAgent):
6360
instruction: str = DEFAULT_INSTRUCTION
6461
"""The instruction for the agent, such as principles of function calling."""
6562

66-
model_name: str = Field(
67-
default_factory=lambda: getenv("MODEL_AGENT_NAME", DEFAULT_MODEL_AGENT_NAME)
68-
)
63+
model_name: str = Field(default_factory=lambda: settings.model.name)
6964
"""The name of the model for agent running."""
7065

71-
model_provider: str = Field(
72-
default_factory=lambda: getenv(
73-
"MODEL_AGENT_PROVIDER", DEFAULT_MODEL_AGENT_PROVIDER
74-
)
75-
)
66+
model_provider: str = Field(default_factory=lambda: settings.model.provider)
7667
"""The provider of the model for agent running."""
7768

78-
model_api_base: str = Field(
79-
default_factory=lambda: getenv(
80-
"MODEL_AGENT_API_BASE", DEFAULT_MODEL_AGENT_API_BASE
81-
)
82-
)
69+
model_api_base: str = Field(default_factory=lambda: settings.model.api_base)
8370
"""The api base of the model for agent running."""
8471

85-
model_api_key: str = Field(default_factory=lambda: getenv("MODEL_AGENT_API_KEY"))
72+
model_api_key: str = Field(default_factory=lambda: settings.model.api_key)
8673
"""The api key of the model for agent running."""
8774

8875
model_extra_config: dict = Field(default_factory=dict)

veadk/auth/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.

veadk/auth/base_auth.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
class BaseAuth:
17+
def __init__(self) -> None: ...
18+
19+
def _fetch_token(self) -> str | dict: ...
20+
21+
@property
22+
def token(self) -> str | dict: ...

veadk/auth/veauth/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
17+
from typing_extensions import override
18+
19+
from veadk.auth.veauth.base_veauth import BaseVeAuth
20+
from veadk.utils.logger import get_logger
21+
from veadk.utils.volcengine_sign import ve_request
22+
23+
logger = get_logger(__name__)
24+
25+
26+
class APMPlusVeAuth(BaseVeAuth):
27+
def __init__(
28+
self,
29+
access_key: str = os.getenv("VOLCENGINE_ACCESS_KEY", ""),
30+
secret_key: str = os.getenv("VOLCENGINE_SECRET_KEY", ""),
31+
region: str = "cn-beijing",
32+
) -> None:
33+
super().__init__(access_key, secret_key)
34+
35+
self.region = region
36+
37+
self._token: str = ""
38+
39+
@override
40+
def _fetch_token(self) -> None:
41+
logger.info("Fetching APMPlus token...")
42+
43+
res = ve_request(
44+
request_body={},
45+
action="GetAppKey",
46+
ak=self.access_key,
47+
sk=self.secret_key,
48+
service="apmplus_server",
49+
version="2024-07-30",
50+
region=self.region,
51+
host="open.volcengineapi.com",
52+
# APMPlus frontend required
53+
header={"X-Apmplus-Region": self.region.replace("-", "_")},
54+
)
55+
try:
56+
self._token = res["data"]["app_key"]
57+
except KeyError:
58+
raise ValueError(f"Failed to get APMPlus token: {res}")
59+
60+
@property
61+
def token(self) -> str:
62+
if self._token:
63+
return self._token
64+
self._fetch_token()
65+
return self._token

0 commit comments

Comments
 (0)