Skip to content

Commit c896d01

Browse files
committed
fix(vefaas) change tracer callback
1 parent d0518f7 commit c896d01

File tree

14 files changed

+273
-134
lines changed

14 files changed

+273
-134
lines changed

veadk/agent.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -114,24 +114,18 @@ def model_post_init(self, __context: Any) -> None:
114114

115115
self.tools.append(load_memory)
116116

117-
self.before_model_callback = []
118-
self.after_model_callback = []
119-
self.update_tracers_callback()
117+
if self.tracers:
118+
self.before_model_callback = []
119+
self.after_model_callback = []
120+
for tracer in self.tracers:
121+
self.before_model_callback.append(tracer.llm_metrics_hook)
122+
self.after_model_callback.append(tracer.token_metrics_hook)
120123

121124
logger.info(f"Agent `{self.name}` init done.")
122125
logger.debug(
123126
f"Agent: {self.model_dump(include={'name', 'model_name', 'model_api_base', 'tools', 'serve_url'})}"
124127
)
125128

126-
def update_tracers_callback(self) -> None:
127-
"""Update tracer callbacks with tracers."""
128-
for tracer in self.tracers:
129-
# Add tracer callbacks if not already added
130-
if tracer.llm_metrics_hook not in self.before_model_callback:
131-
self.before_model_callback.append(tracer.llm_metrics_hook)
132-
if tracer.token_metrics_hook not in self.after_model_callback:
133-
self.after_model_callback.append(tracer.token_metrics_hook)
134-
135129
async def _run(
136130
self,
137131
runner,

veadk/cli/services/vefaas/template/README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ We implement an minimal agent to report weather in terms of the given city.
66

77
## Structure
88

9-
| File | Illustration |
10-
| - | - |
11-
| `src/app.py` | The entrypoint of VeFaaS server. |
12-
| `src/run.sh` | The launch script of VeFaaS server. |
9+
| File | Illustration |
10+
|------------------------| - |
11+
| `src/app.py` | The entrypoint of VeFaaS server. |
12+
| `src/run.sh` | The launch script of VeFaaS server. |
1313
| `src/requirements.txt` | Dependencies of your project. `VeADK`, `FastAPI`, and `uvicorn` must be included. |
14-
| `src/config.py` | The agent and memory definitions. **You may edit this file.** |
15-
| `config.yaml.example` | Envs for your project (e.g., `api_key`, `token`, ...). **You may edit this file.** |
16-
| `deploy.py` | Local script for deployment. |
14+
| `src/agent.py` | The agent and memory definitions. **You may edit this file.** |
15+
| `config.yaml.example` | Envs for your project (e.g., `api_key`, `token`, ...). **You may edit this file.** |
16+
| `deploy.py` | Local script for deployment. |
1717

18-
You must export your agent and short-term memory in `src/config.py`.
18+
You must export your agent and short-term memory in `src/agent.py`.
1919

2020
## Deploy
2121

veadk/cli/services/vefaas/template/config.yaml.example

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ model:
44
name: doubao-1-5-pro-256k-250115
55
api_base: https://ark.cn-beijing.volces.com/api/v3/
66
api_key:
7+
judge:
8+
name: doubao-1-5-pro-256k-250115
9+
api_base: https://ark.cn-beijing.volces.com/api/v3/
10+
api_key:
11+
12+
13+
agent_pilot:
14+
api_key:
15+
716

817
volcengine:
918
access_key:

veadk/cli/services/vefaas/template/deploy.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,32 @@
2020
SESSION_ID = "cloud_app_test_session"
2121
USER_ID = "cloud_app_test_user"
2222

23+
USE_STUDIO = False
24+
2325

2426
async def main():
2527
engine = CloudAgentEngine()
2628
cloud_app = engine.deploy(
2729
path=str(Path(__file__).parent / "src"),
28-
name="weather-reporter",
29-
# gateway_name="", # <--- your gateway instance name if you have
30+
name="weather-reporter", # <--- set your application name
31+
use_studio=USE_STUDIO,
32+
# gateway_name="", # <--- set your gateway instance name if you have one
3033
)
3134

32-
response_message = await cloud_app.message_send(
33-
"How is the weather like in Beijing?", SESSION_ID, USER_ID
34-
)
35+
if not USE_STUDIO:
36+
response_message = await cloud_app.message_send(
37+
"How is the weather like in Beijing?", SESSION_ID, USER_ID
38+
)
3539

36-
print(f"Message ID: {response_message.messageId}")
40+
print(f"Message ID: {response_message.messageId}")
3741

38-
print(f"Response from {cloud_app.endpoint}: {response_message.parts[0].root.text}")
42+
print(
43+
f"Response from {cloud_app.endpoint}: {response_message.parts[0].root.text}"
44+
)
3945

40-
print(f"App ID: {cloud_app.app_id}")
46+
print(f"App ID: {cloud_app.app_id}")
47+
else:
48+
print(f"VeADK Studio URL: {cloud_app.endpoint}")
4149

4250

4351
if __name__ == "__main__":

veadk/cli/services/vefaas/template/src/config.py renamed to veadk/cli/services/vefaas/template/src/agent.py

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

15-
import os
16-
1715
from veadk import Agent
1816
from veadk.memory.short_term_memory import ShortTermMemory
1917
from veadk.tools.demo_tools import get_city_weather
20-
from veadk.tracing.base_tracer import BaseTracer
21-
from veadk.tracing.telemetry.opentelemetry_tracer import OpentelemetryTracer
22-
23-
# =============
24-
# Generated by VeADK, do not edit!!!
25-
# =============
26-
TRACERS: list[BaseTracer] = []
27-
28-
exporters = []
29-
if os.getenv("VEADK_TRACER_APMPLUS", "").lower() == "true":
30-
from veadk.tracing.telemetry.exporters.apmplus_exporter import APMPlusExporter
31-
32-
exporters.append(APMPlusExporter())
33-
34-
if os.getenv("VEADK_TRACER_COZELOOP", "").lower() == "true":
35-
from veadk.tracing.telemetry.exporters.cozeloop_exporter import CozeloopExporter
3618

37-
exporters.append(CozeloopExporter())
38-
39-
if os.getenv("VEADK_TRACER_TLS", "").lower() == "true":
40-
from veadk.tracing.telemetry.exporters.tls_exporter import TLSExporter
41-
42-
exporters.append(TLSExporter())
43-
44-
TRACERS.append(OpentelemetryTracer(exporters=exporters))
45-
46-
# =============
47-
# Required [you can edit here]
48-
# =============
4919
APP_NAME: str = "weather-reporter" # <--- export your app name
5020
AGENT: Agent = Agent(tools=[get_city_weather]) # <--- export your agent
5121
SHORT_TERM_MEMORY: ShortTermMemory = (
5222
ShortTermMemory()
5323
) # <--- export your short term memory
54-
55-
# =============
56-
# Optional
57-
# =============
58-
# Other global variables

veadk/cli/services/vefaas/template/src/app.py

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,52 @@
1414

1515
import os
1616

17-
from config import AGENT, APP_NAME, SHORT_TERM_MEMORY, TRACERS
18-
17+
from agent import AGENT, APP_NAME, SHORT_TERM_MEMORY
1918
from veadk.a2a.ve_a2a_server import init_app
19+
from veadk.tracing.base_tracer import BaseTracer
20+
from veadk.tracing.telemetry.opentelemetry_tracer import OpentelemetryTracer
21+
22+
23+
# ==============================================================================
24+
# Tracer Config ================================================================
25+
26+
TRACERS: list[BaseTracer] = []
27+
28+
exporters = []
29+
if os.getenv("VEADK_TRACER_APMPLUS", "").lower() == "true":
30+
from veadk.tracing.telemetry.exporters.apmplus_exporter import APMPlusExporter
31+
32+
exporters.append(APMPlusExporter())
33+
34+
if os.getenv("VEADK_TRACER_COZELOOP", "").lower() == "true":
35+
from veadk.tracing.telemetry.exporters.cozeloop_exporter import CozeloopExporter
36+
37+
exporters.append(CozeloopExporter())
38+
39+
if os.getenv("VEADK_TRACER_TLS", "").lower() == "true":
40+
from veadk.tracing.telemetry.exporters.tls_exporter import TLSExporter
41+
42+
exporters.append(TLSExporter())
43+
44+
TRACERS.append(OpentelemetryTracer(exporters=exporters))
2045

21-
SERVER_HOST = os.getenv("SERVER_HOST")
2246

2347
AGENT.tracers.extend(TRACERS)
24-
AGENT.update_tracers_callback()
48+
if not getattr(AGENT, "before_model_callback", None):
49+
AGENT.before_model_callback = []
50+
if not getattr(AGENT, "after_model_callback", None):
51+
AGENT.after_model_callback = []
52+
for tracer in TRACERS:
53+
if tracer.llm_metrics_hook not in AGENT.before_model_callback:
54+
AGENT.before_model_callback.append(tracer.llm_metrics_hook)
55+
if tracer.token_metrics_hook not in AGENT.after_model_callback:
56+
AGENT.after_model_callback.append(tracer.token_metrics_hook)
57+
58+
# Tracer Config ================================================================
59+
# ==============================================================================
2560

2661
app = init_app(
27-
server_url=SERVER_HOST,
62+
server_url="0.0.0.0", # Automatic identification is not supported yet.
2863
app_name=APP_NAME,
2964
agent=AGENT,
3065
short_term_memory=SHORT_TERM_MEMORY,

veadk/cli/services/vefaas/template/src/run.sh

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,22 @@ python3 -m pip install uvicorn[standard]
3838

3939
python3 -m pip install fastapi
4040

41-
# running
42-
exec python3 -m uvicorn app:app --host $HOST --port $PORT --timeout-graceful-shutdown $TIMEOUT
41+
USE_STUDIO=${USE_STUDIO:-False}
42+
43+
if [ "$USE_STUDIO" = "True" ]; then
44+
echo "USE_STUDIO is True, running veadk studio"
45+
# running veadk studio
46+
exec python3 -m uvicorn studio_app:app --host $HOST --port $PORT --timeout-graceful-shutdown $TIMEOUT --loop asyncio
47+
elif [ "$USE_STUDIO" = "False" ]; then
48+
echo "USE_STUDIO is False, running a2a server"
49+
50+
# running a2a server
51+
exec python3 -m uvicorn app:app --host $HOST --port $PORT --timeout-graceful-shutdown $TIMEOUT --loop asyncio
52+
else
53+
echo "USE_STUDIO is an invalid value: $USE_STUDIO, running a2a server."
54+
55+
# running a2a server
56+
exec python3 -m uvicorn app:app --host $HOST --port $PORT --timeout-graceful-shutdown $TIMEOUT --loop asyncio
57+
fi
58+
59+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
from importlib.util import module_from_spec, spec_from_file_location
17+
from pathlib import Path
18+
19+
from veadk.cli.studio.fast_api import get_fast_api_app
20+
21+
path = Path(__file__).parent.resolve()
22+
23+
agent_py_path = os.path.join(path, "agent.py")
24+
if not os.path.exists(agent_py_path):
25+
raise FileNotFoundError(f"agent.py not found in {path}")
26+
27+
spec = spec_from_file_location("agent", agent_py_path)
28+
if spec is None:
29+
raise ImportError(f"Could not load spec for agent from {agent_py_path}")
30+
31+
module = module_from_spec(spec)
32+
33+
try:
34+
spec.loader.exec_module(module)
35+
except Exception as e:
36+
raise ImportError(f"Failed to execute agent.py: {e}")
37+
38+
agent = None
39+
short_term_memory = None
40+
try:
41+
agent = module.agent
42+
short_term_memory = module.short_term_memory
43+
except AttributeError as e:
44+
missing = str(e).split("'")[1] if "'" in str(e) else "unknown"
45+
raise AttributeError(f"agent.py is missing required variable: {missing}")
46+
47+
app = get_fast_api_app(agent, short_term_memory)

veadk/cloud/cloud_agent_engine.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ def _prepare(self, path: str, name: str):
5252
f"Local agent project path `{path}` is not a directory."
5353
)
5454

55-
assert os.path.exists(os.path.join(path, "config.py")), (
56-
f"Local agent project path `{path}` does not contain `config.py` file. Please prepare it according to veadk-python/cloud/template/config.py.example"
55+
assert os.path.exists(os.path.join(path, "agent.py")), (
56+
f"Local agent project path `{path}` does not contain `agent.py` file. Please prepare it according to veadk-python/cloud/template/agent.py.example"
5757
)
5858

5959
if os.path.exists(os.path.join(path, "app.py")):

veadk/cloud/template/agent.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
from veadk.agent import Agent
15+
from veadk.memory.short_term_memory import ShortTermMemory
16+
17+
AGENT: Agent = ...
18+
APP_NAME: str = ...
19+
SHORT_TERM_MEMORY: ShortTermMemory = ...

0 commit comments

Comments
 (0)