Skip to content

Commit 2147f42

Browse files
chore(builder): support tools in yaml file (#160)
* chore(builder): support tools in yaml file * fix bugs * support auto fetch cozeloop space id
1 parent 3578650 commit 2147f42

File tree

6 files changed

+75
-49
lines changed

6 files changed

+75
-49
lines changed

docs/docs/agent.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@ root_agent:
129129
backend: local
130130
knowledgebase:
131131
backend: opensearch
132+
tools:
133+
- module: demo_tool # tool 所在的模块
134+
func: greeting # tool 的函数名称
135+
- module: tools.tool
136+
func: count
137+
sub_agents:
132138
sub_agents:
133139
- ${sub_agent_1}
134140

@@ -147,8 +153,7 @@ from veadk.agent_builder import AgentBuilder
147153
agent = AgentBuilder().build(path="./agent.yaml")
148154
```
149155

150-
函数`build`接收3个参数
156+
函数`build`接收 2 个参数
151157

152158
- `path`:配置文件路径
153159
- `root_agent_identifier`:配置文件中主 Agent 的名称,默认为`root_agent`
154-
- `tools`:主 agent 挂载的工具列表(子 Agent 工具列表暂未推出)

veadk/agent_builder.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import importlib
16+
1517
from google.adk.agents import BaseAgent
16-
from google.adk.agents.llm_agent import ToolUnion
1718
from omegaconf import OmegaConf
1819

1920
from veadk.a2a.remote_ve_agent import RemoteVeAgent
@@ -48,8 +49,20 @@ def _build(self, agent_config: dict) -> BaseAgent:
4849
sub_agents.append(agent)
4950
agent_config.pop("sub_agents")
5051

52+
tools = []
53+
if agent_config.get("tools", []):
54+
for tool in agent_config["tools"]:
55+
module_name = tool["module"]
56+
func_name = tool["func"]
57+
58+
module = importlib.import_module(module_name)
59+
func = getattr(module, func_name)
60+
61+
tools.append(func)
62+
agent_config.pop("tools")
63+
5164
agent_cls = AGENT_TYPES[agent_config["type"]]
52-
agent = agent_cls(**agent_config, sub_agents=sub_agents)
65+
agent = agent_cls(**agent_config, sub_agents=sub_agents, tools=tools)
5366

5467
logger.debug("Build agent done.")
5568

@@ -72,14 +85,10 @@ def build(
7285
self,
7386
path: str,
7487
root_agent_identifier: str = "root_agent",
75-
tools: list[ToolUnion] | None = None,
7688
) -> BaseAgent:
7789
config = self._read_config(path)
7890

7991
agent_config = config[root_agent_identifier]
8092
agent = self._build(agent_config)
8193

82-
if tools and isinstance(agent, Agent):
83-
agent.tools = tools
84-
8594
return agent

veadk/auth/veauth/vesearch_veauth.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ def _fetch_token(self):
4949
)
5050
try:
5151
self._token = res["Result"]["api_key_vos"][0]["api_key"]
52+
53+
logger.info("Fetching VeSearch token done.")
5254
except KeyError:
5355
raise ValueError(f"Failed to get VeSearch token: {res}")
5456

veadk/configs/tracing_configs.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@
2323
DEFAULT_APMPLUS_OTEL_EXPORTER_ENDPOINT,
2424
DEFAULT_APMPLUS_OTEL_EXPORTER_SERVICE_NAME,
2525
DEFAULT_COZELOOP_OTEL_EXPORTER_ENDPOINT,
26+
DEFAULT_COZELOOP_SPACE_NAME,
2627
DEFAULT_TLS_OTEL_EXPORTER_ENDPOINT,
2728
DEFAULT_TLS_OTEL_EXPORTER_REGION,
2829
)
30+
from veadk.integrations.ve_cozeloop.ve_cozeloop import VeCozeloop
2931
from veadk.integrations.ve_tls.ve_tls import VeTLS
3032

3133

@@ -58,27 +60,24 @@ class CozeloopConfig(BaseSettings):
5860
default="", alias="OBSERVABILITY_OPENTELEMETRY_COZELOOP_API_KEY"
5961
)
6062

61-
otel_exporter_space_id: str = Field(
62-
default="", alias="OBSERVABILITY_OPENTELEMETRY_COZELOOP_SERVICE_NAME"
63-
)
64-
6563
# TODO: auto fetching via AK/SK pair
6664
# @cached_property
6765
# def otel_exporter_api_key(self) -> str:
6866
# pass
6967

70-
# TODO: auto fetching workspace id
71-
# @cached_property
72-
# def otel_exporter_space_id(self) -> str:
73-
# workspace_id = os.getenv("OBSERVABILITY_OPENTELEMETRY_COZELOOP_SERVICE_NAME", "")
68+
@cached_property
69+
def otel_exporter_space_id(self) -> str:
70+
workspace_id = os.getenv(
71+
"OBSERVABILITY_OPENTELEMETRY_COZELOOP_SERVICE_NAME", ""
72+
)
7473

75-
# if not workspace_id:
76-
# # create a default one
77-
# workspace_id = VeCozeloop(self.otel_exporter_api_key).create_workspace(
78-
# workspace_name=DEFAULT_COZELOOP_SPACE_NAME
79-
# )
74+
if not workspace_id:
75+
# create a default one
76+
workspace_id = VeCozeloop(self.otel_exporter_api_key).create_workspace(
77+
workspace_name=DEFAULT_COZELOOP_SPACE_NAME
78+
)
8079

81-
# return workspace_id
80+
return workspace_id
8281

8382

8483
class TLSConfig(BaseSettings):

veadk/integrations/ve_cozeloop/ve_cozeloop.py

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,36 @@ def create_workspace(
3131
f"Automatically create Cozeloop workspace with name {workspace_name}"
3232
)
3333

34-
URL = "https://api.coze.cn/v1/workspaces"
34+
try:
35+
workspace_id = self.search_workspace_id(workspace_name=workspace_name)
36+
logger.info(f"Get existing Cozeloop workspace ID: {workspace_id}")
3537

36-
headers = {
37-
"Authorization": f"Bearer {self.api_key}",
38-
"Content-Type": "application/json",
39-
}
40-
41-
data = {
42-
"name": workspace_name,
43-
"description": "Created by Volcengine Agent Development Kit (VeADK)",
44-
}
45-
46-
response = requests.post(URL, headers=headers, json=data)
47-
48-
if response.json().get("code") == 0:
49-
workspace_id = response.json().get("data").get("id")
50-
logger.info(f"Cozeloop workspace ID: {workspace_id}")
5138
return workspace_id
52-
else:
53-
raise Exception(
54-
f"Failed to automatically create workspace: {response.json()}"
55-
)
56-
57-
def get_workspace_id(
39+
except Exception as _:
40+
URL = "https://api.coze.cn/v1/workspaces"
41+
42+
headers = {
43+
"Authorization": f"Bearer {self.api_key}",
44+
"Content-Type": "application/json",
45+
}
46+
47+
data = {
48+
"name": workspace_name,
49+
"description": "Created by Volcengine Agent Development Kit (VeADK)",
50+
}
51+
52+
response = requests.post(URL, headers=headers, json=data)
53+
54+
if response.json().get("code") == 0:
55+
workspace_id = response.json().get("data").get("id")
56+
logger.info(f"New created Cozeloop workspace ID: {workspace_id}")
57+
return workspace_id
58+
else:
59+
raise Exception(
60+
f"Failed to automatically create workspace: {response.json()}"
61+
)
62+
63+
def search_workspace_id(
5864
self, workspace_name: str = DEFAULT_COZELOOP_SPACE_NAME
5965
) -> str:
6066
logger.info(
@@ -73,7 +79,7 @@ def get_workspace_id(
7379
"page_size": 50,
7480
}
7581

76-
response = requests.post(URL, headers=headers, json=data)
82+
response = requests.get(URL, headers=headers, json=data)
7783

7884
if response.json().get("code") == 0:
7985
workspaces = response.json().get("data").get("workspaces", [])

veadk/memory/short_term_memory_processor.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@
2020
from google.genai.types import Content, Part
2121
from litellm import completion
2222

23-
from veadk.config import getenv
23+
from veadk.config import settings
24+
from veadk.consts import (
25+
DEFAULT_MODEL_AGENT_API_BASE,
26+
DEFAULT_MODEL_AGENT_NAME,
27+
DEFAULT_MODEL_AGENT_PROVIDER,
28+
)
2429
from veadk.prompts.prompt_memory_processor import render_prompt
2530
from veadk.utils.logger import get_logger
2631

@@ -62,9 +67,9 @@ def after_load_session(self, session: Session) -> Session:
6267
prompt = render_prompt(messages=messages)
6368

6469
res = completion(
65-
model=getenv("MODEL_AGENT_PROVIDER") + "/" + getenv("MODEL_AGENT_NAME"),
66-
base_url=getenv("MODEL_AGENT_API_BASE"),
67-
api_key=getenv("MODEL_AGENT_API_KEY"),
70+
model=DEFAULT_MODEL_AGENT_PROVIDER + "/" + DEFAULT_MODEL_AGENT_NAME,
71+
base_url=DEFAULT_MODEL_AGENT_API_BASE,
72+
api_key=settings.model.api_key,
6873
messages=[
6974
{
7075
"role": "user",

0 commit comments

Comments
 (0)