veadk-python 0.2.27__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- veadk/__init__.py +37 -0
- veadk/a2a/__init__.py +13 -0
- veadk/a2a/agent_card.py +45 -0
- veadk/a2a/remote_ve_agent.py +390 -0
- veadk/a2a/utils/__init__.py +13 -0
- veadk/a2a/utils/agent_to_a2a.py +170 -0
- veadk/a2a/ve_a2a_server.py +93 -0
- veadk/a2a/ve_agent_executor.py +78 -0
- veadk/a2a/ve_middlewares.py +313 -0
- veadk/a2a/ve_task_store.py +37 -0
- veadk/agent.py +402 -0
- veadk/agent_builder.py +93 -0
- veadk/agents/loop_agent.py +68 -0
- veadk/agents/parallel_agent.py +72 -0
- veadk/agents/sequential_agent.py +64 -0
- veadk/auth/__init__.py +13 -0
- veadk/auth/base_auth.py +22 -0
- veadk/auth/ve_credential_service.py +203 -0
- veadk/auth/veauth/__init__.py +13 -0
- veadk/auth/veauth/apmplus_veauth.py +58 -0
- veadk/auth/veauth/ark_veauth.py +75 -0
- veadk/auth/veauth/base_veauth.py +50 -0
- veadk/auth/veauth/cozeloop_veauth.py +13 -0
- veadk/auth/veauth/opensearch_veauth.py +75 -0
- veadk/auth/veauth/postgresql_veauth.py +75 -0
- veadk/auth/veauth/prompt_pilot_veauth.py +60 -0
- veadk/auth/veauth/speech_veauth.py +54 -0
- veadk/auth/veauth/utils.py +69 -0
- veadk/auth/veauth/vesearch_veauth.py +62 -0
- veadk/auth/veauth/viking_mem0_veauth.py +91 -0
- veadk/cli/__init__.py +13 -0
- veadk/cli/cli.py +58 -0
- veadk/cli/cli_clean.py +87 -0
- veadk/cli/cli_create.py +163 -0
- veadk/cli/cli_deploy.py +233 -0
- veadk/cli/cli_eval.py +215 -0
- veadk/cli/cli_init.py +214 -0
- veadk/cli/cli_kb.py +110 -0
- veadk/cli/cli_pipeline.py +285 -0
- veadk/cli/cli_prompt.py +86 -0
- veadk/cli/cli_update.py +106 -0
- veadk/cli/cli_uploadevalset.py +139 -0
- veadk/cli/cli_web.py +143 -0
- veadk/cloud/__init__.py +13 -0
- veadk/cloud/cloud_agent_engine.py +485 -0
- veadk/cloud/cloud_app.py +475 -0
- veadk/config.py +115 -0
- veadk/configs/__init__.py +13 -0
- veadk/configs/auth_configs.py +133 -0
- veadk/configs/database_configs.py +132 -0
- veadk/configs/model_configs.py +78 -0
- veadk/configs/tool_configs.py +54 -0
- veadk/configs/tracing_configs.py +110 -0
- veadk/consts.py +74 -0
- veadk/evaluation/__init__.py +17 -0
- veadk/evaluation/adk_evaluator/__init__.py +17 -0
- veadk/evaluation/adk_evaluator/adk_evaluator.py +302 -0
- veadk/evaluation/base_evaluator.py +642 -0
- veadk/evaluation/deepeval_evaluator/__init__.py +17 -0
- veadk/evaluation/deepeval_evaluator/deepeval_evaluator.py +339 -0
- veadk/evaluation/eval_set_file_loader.py +48 -0
- veadk/evaluation/eval_set_recorder.py +146 -0
- veadk/evaluation/types.py +65 -0
- veadk/evaluation/utils/prometheus.py +196 -0
- veadk/integrations/__init__.py +13 -0
- veadk/integrations/ve_apig/__init__.py +13 -0
- veadk/integrations/ve_apig/ve_apig.py +349 -0
- veadk/integrations/ve_apig/ve_apig_utils.py +332 -0
- veadk/integrations/ve_code_pipeline/__init__.py +13 -0
- veadk/integrations/ve_code_pipeline/ve_code_pipeline.py +431 -0
- veadk/integrations/ve_cozeloop/__init__.py +13 -0
- veadk/integrations/ve_cozeloop/ve_cozeloop.py +96 -0
- veadk/integrations/ve_cr/__init__.py +13 -0
- veadk/integrations/ve_cr/ve_cr.py +220 -0
- veadk/integrations/ve_faas/__init__.py +13 -0
- veadk/integrations/ve_faas/template/cookiecutter.json +15 -0
- veadk/integrations/ve_faas/template/{{cookiecutter.local_dir_name}}/__init__.py +13 -0
- veadk/integrations/ve_faas/template/{{cookiecutter.local_dir_name}}/clean.py +23 -0
- veadk/integrations/ve_faas/template/{{cookiecutter.local_dir_name}}/config.yaml.example +6 -0
- veadk/integrations/ve_faas/template/{{cookiecutter.local_dir_name}}/deploy.py +106 -0
- veadk/integrations/ve_faas/template/{{cookiecutter.local_dir_name}}/src/__init__.py +13 -0
- veadk/integrations/ve_faas/template/{{cookiecutter.local_dir_name}}/src/agent.py +25 -0
- veadk/integrations/ve_faas/template/{{cookiecutter.local_dir_name}}/src/app.py +202 -0
- veadk/integrations/ve_faas/template/{{cookiecutter.local_dir_name}}/src/requirements.txt +3 -0
- veadk/integrations/ve_faas/template/{{cookiecutter.local_dir_name}}/src/run.sh +49 -0
- veadk/integrations/ve_faas/template/{{cookiecutter.local_dir_name}}/src/{{ cookiecutter.app_name }}/__init__.py +14 -0
- veadk/integrations/ve_faas/template/{{cookiecutter.local_dir_name}}/src/{{ cookiecutter.app_name }}/agent.py +27 -0
- veadk/integrations/ve_faas/ve_faas.py +754 -0
- veadk/integrations/ve_faas/ve_faas_utils.py +408 -0
- veadk/integrations/ve_faas/web_template/cookiecutter.json +20 -0
- veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/__init__.py +13 -0
- veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/clean.py +23 -0
- veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/config.yaml.example +2 -0
- veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/deploy.py +44 -0
- veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/Dockerfile +23 -0
- veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/app.py +123 -0
- veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/init_db.py +46 -0
- veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/models.py +36 -0
- veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/requirements.txt +4 -0
- veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/run.sh +21 -0
- veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/static/css/style.css +368 -0
- veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/static/js/admin.js +0 -0
- veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/templates/admin/dashboard.html +21 -0
- veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/templates/admin/edit_post.html +24 -0
- veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/templates/admin/login.html +21 -0
- veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/templates/admin/posts.html +53 -0
- veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/templates/base.html +45 -0
- veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/templates/index.html +29 -0
- veadk/integrations/ve_faas/web_template/{{cookiecutter.local_dir_name}}/src/templates/post.html +14 -0
- veadk/integrations/ve_identity/__init__.py +110 -0
- veadk/integrations/ve_identity/auth_config.py +261 -0
- veadk/integrations/ve_identity/auth_mixins.py +650 -0
- veadk/integrations/ve_identity/auth_processor.py +385 -0
- veadk/integrations/ve_identity/function_tool.py +158 -0
- veadk/integrations/ve_identity/identity_client.py +864 -0
- veadk/integrations/ve_identity/mcp_tool.py +181 -0
- veadk/integrations/ve_identity/mcp_toolset.py +431 -0
- veadk/integrations/ve_identity/models.py +228 -0
- veadk/integrations/ve_identity/token_manager.py +188 -0
- veadk/integrations/ve_identity/utils.py +151 -0
- veadk/integrations/ve_prompt_pilot/__init__.py +13 -0
- veadk/integrations/ve_prompt_pilot/ve_prompt_pilot.py +85 -0
- veadk/integrations/ve_tls/__init__.py +13 -0
- veadk/integrations/ve_tls/utils.py +116 -0
- veadk/integrations/ve_tls/ve_tls.py +212 -0
- veadk/integrations/ve_tos/ve_tos.py +710 -0
- veadk/integrations/ve_viking_db_memory/__init__.py +13 -0
- veadk/integrations/ve_viking_db_memory/ve_viking_db_memory.py +308 -0
- veadk/knowledgebase/__init__.py +17 -0
- veadk/knowledgebase/backends/__init__.py +13 -0
- veadk/knowledgebase/backends/base_backend.py +72 -0
- veadk/knowledgebase/backends/in_memory_backend.py +91 -0
- veadk/knowledgebase/backends/opensearch_backend.py +162 -0
- veadk/knowledgebase/backends/redis_backend.py +172 -0
- veadk/knowledgebase/backends/utils.py +92 -0
- veadk/knowledgebase/backends/vikingdb_knowledge_backend.py +608 -0
- veadk/knowledgebase/entry.py +25 -0
- veadk/knowledgebase/knowledgebase.py +307 -0
- veadk/memory/__init__.py +35 -0
- veadk/memory/long_term_memory.py +365 -0
- veadk/memory/long_term_memory_backends/__init__.py +13 -0
- veadk/memory/long_term_memory_backends/base_backend.py +35 -0
- veadk/memory/long_term_memory_backends/in_memory_backend.py +67 -0
- veadk/memory/long_term_memory_backends/mem0_backend.py +155 -0
- veadk/memory/long_term_memory_backends/opensearch_backend.py +124 -0
- veadk/memory/long_term_memory_backends/redis_backend.py +140 -0
- veadk/memory/long_term_memory_backends/vikingdb_memory_backend.py +189 -0
- veadk/memory/short_term_memory.py +252 -0
- veadk/memory/short_term_memory_backends/__init__.py +13 -0
- veadk/memory/short_term_memory_backends/base_backend.py +31 -0
- veadk/memory/short_term_memory_backends/mysql_backend.py +49 -0
- veadk/memory/short_term_memory_backends/postgresql_backend.py +49 -0
- veadk/memory/short_term_memory_backends/sqlite_backend.py +55 -0
- veadk/memory/short_term_memory_processor.py +100 -0
- veadk/processors/__init__.py +26 -0
- veadk/processors/base_run_processor.py +120 -0
- veadk/prompts/__init__.py +13 -0
- veadk/prompts/agent_default_prompt.py +30 -0
- veadk/prompts/prompt_evaluator.py +20 -0
- veadk/prompts/prompt_memory_processor.py +55 -0
- veadk/prompts/prompt_optimization.py +150 -0
- veadk/runner.py +732 -0
- veadk/tools/__init__.py +13 -0
- veadk/tools/builtin_tools/__init__.py +13 -0
- veadk/tools/builtin_tools/agent_authorization.py +94 -0
- veadk/tools/builtin_tools/generate_image.py +23 -0
- veadk/tools/builtin_tools/image_edit.py +300 -0
- veadk/tools/builtin_tools/image_generate.py +446 -0
- veadk/tools/builtin_tools/lark.py +67 -0
- veadk/tools/builtin_tools/las.py +24 -0
- veadk/tools/builtin_tools/link_reader.py +66 -0
- veadk/tools/builtin_tools/llm_shield.py +381 -0
- veadk/tools/builtin_tools/load_knowledgebase.py +97 -0
- veadk/tools/builtin_tools/mcp_router.py +29 -0
- veadk/tools/builtin_tools/run_code.py +113 -0
- veadk/tools/builtin_tools/tts.py +253 -0
- veadk/tools/builtin_tools/vesearch.py +49 -0
- veadk/tools/builtin_tools/video_generate.py +363 -0
- veadk/tools/builtin_tools/web_scraper.py +76 -0
- veadk/tools/builtin_tools/web_search.py +83 -0
- veadk/tools/demo_tools.py +58 -0
- veadk/tools/load_knowledgebase_tool.py +149 -0
- veadk/tools/sandbox/__init__.py +13 -0
- veadk/tools/sandbox/browser_sandbox.py +37 -0
- veadk/tools/sandbox/code_sandbox.py +40 -0
- veadk/tools/sandbox/computer_sandbox.py +34 -0
- veadk/tracing/__init__.py +13 -0
- veadk/tracing/base_tracer.py +58 -0
- veadk/tracing/telemetry/__init__.py +13 -0
- veadk/tracing/telemetry/attributes/attributes.py +29 -0
- veadk/tracing/telemetry/attributes/extractors/common_attributes_extractors.py +180 -0
- veadk/tracing/telemetry/attributes/extractors/llm_attributes_extractors.py +858 -0
- veadk/tracing/telemetry/attributes/extractors/tool_attributes_extractors.py +152 -0
- veadk/tracing/telemetry/attributes/extractors/types.py +164 -0
- veadk/tracing/telemetry/exporters/__init__.py +13 -0
- veadk/tracing/telemetry/exporters/apmplus_exporter.py +558 -0
- veadk/tracing/telemetry/exporters/base_exporter.py +39 -0
- veadk/tracing/telemetry/exporters/cozeloop_exporter.py +129 -0
- veadk/tracing/telemetry/exporters/inmemory_exporter.py +248 -0
- veadk/tracing/telemetry/exporters/tls_exporter.py +139 -0
- veadk/tracing/telemetry/opentelemetry_tracer.py +320 -0
- veadk/tracing/telemetry/telemetry.py +411 -0
- veadk/types.py +47 -0
- veadk/utils/__init__.py +13 -0
- veadk/utils/audio_manager.py +95 -0
- veadk/utils/auth.py +294 -0
- veadk/utils/logger.py +59 -0
- veadk/utils/mcp_utils.py +44 -0
- veadk/utils/misc.py +184 -0
- veadk/utils/patches.py +101 -0
- veadk/utils/volcengine_sign.py +205 -0
- veadk/version.py +15 -0
- veadk_python-0.2.27.dist-info/METADATA +373 -0
- veadk_python-0.2.27.dist-info/RECORD +218 -0
- veadk_python-0.2.27.dist-info/WHEEL +5 -0
- veadk_python-0.2.27.dist-info/entry_points.txt +2 -0
- veadk_python-0.2.27.dist-info/licenses/LICENSE +201 -0
- veadk_python-0.2.27.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,100 @@
|
|
|
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 functools
|
|
16
|
+
import json
|
|
17
|
+
|
|
18
|
+
from google.adk.events.event import Event
|
|
19
|
+
from google.adk.sessions import Session
|
|
20
|
+
from google.genai.types import Content, Part
|
|
21
|
+
from litellm import completion
|
|
22
|
+
|
|
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
|
+
)
|
|
29
|
+
from veadk.prompts.prompt_memory_processor import render_prompt
|
|
30
|
+
from veadk.utils.logger import get_logger
|
|
31
|
+
|
|
32
|
+
logger = get_logger(__name__)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class ShortTermMemoryProcessor:
|
|
36
|
+
def __init__(self) -> None: ...
|
|
37
|
+
|
|
38
|
+
def patch(self):
|
|
39
|
+
"""Patch the `get_session` function"""
|
|
40
|
+
|
|
41
|
+
def intercept_get_session(func):
|
|
42
|
+
@functools.wraps(func)
|
|
43
|
+
async def wrapper(*args, **kwargs):
|
|
44
|
+
session = await func(*args, **kwargs)
|
|
45
|
+
if session:
|
|
46
|
+
abstracted_session = self.after_load_session(session)
|
|
47
|
+
else:
|
|
48
|
+
abstracted_session = session
|
|
49
|
+
return abstracted_session
|
|
50
|
+
|
|
51
|
+
return wrapper
|
|
52
|
+
|
|
53
|
+
return intercept_get_session
|
|
54
|
+
|
|
55
|
+
def after_load_session(self, session: Session) -> Session:
|
|
56
|
+
messages = []
|
|
57
|
+
for event in session.events:
|
|
58
|
+
content = event.content
|
|
59
|
+
if not content or not content.parts:
|
|
60
|
+
continue
|
|
61
|
+
message = {
|
|
62
|
+
"role": content.role,
|
|
63
|
+
"content": content.parts[0].text,
|
|
64
|
+
}
|
|
65
|
+
messages.append(message)
|
|
66
|
+
|
|
67
|
+
prompt = render_prompt(messages=messages)
|
|
68
|
+
|
|
69
|
+
res = completion(
|
|
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,
|
|
73
|
+
messages=[
|
|
74
|
+
{
|
|
75
|
+
"role": "user",
|
|
76
|
+
"content": prompt,
|
|
77
|
+
}
|
|
78
|
+
],
|
|
79
|
+
)
|
|
80
|
+
logger.debug(f"Response from memory optimization model: {res}")
|
|
81
|
+
|
|
82
|
+
extracted_messages = json.loads(res.choices[0].message.content) # type: ignore
|
|
83
|
+
logger.debug(f"Abstracted messages: {extracted_messages}")
|
|
84
|
+
|
|
85
|
+
session.events = []
|
|
86
|
+
for message in extracted_messages:
|
|
87
|
+
session.events.append(
|
|
88
|
+
Event(
|
|
89
|
+
author="memory_optimizer",
|
|
90
|
+
content=Content(
|
|
91
|
+
role=message["role"],
|
|
92
|
+
parts=[
|
|
93
|
+
Part(
|
|
94
|
+
text=message["content"],
|
|
95
|
+
)
|
|
96
|
+
],
|
|
97
|
+
),
|
|
98
|
+
)
|
|
99
|
+
)
|
|
100
|
+
return session
|
|
@@ -0,0 +1,26 @@
|
|
|
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
|
+
"""Runtime processors for VeADK agents.
|
|
16
|
+
|
|
17
|
+
This module provides the abstract base classes for runtime processors that can
|
|
18
|
+
intercept and process agent execution flows.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
from veadk.processors.base_run_processor import BaseRunProcessor, NoOpRunProcessor
|
|
22
|
+
|
|
23
|
+
__all__ = [
|
|
24
|
+
"BaseRunProcessor",
|
|
25
|
+
"NoOpRunProcessor",
|
|
26
|
+
]
|
|
@@ -0,0 +1,120 @@
|
|
|
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
|
+
"""Base run processor for intercepting and processing agent execution."""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
from abc import ABC, abstractmethod
|
|
20
|
+
from typing import TYPE_CHECKING, Any, AsyncGenerator, Callable
|
|
21
|
+
|
|
22
|
+
if TYPE_CHECKING:
|
|
23
|
+
from google.genai import types
|
|
24
|
+
from veadk.runner import Runner
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class BaseRunProcessor(ABC):
|
|
28
|
+
"""Abstract base class for runtime processors.
|
|
29
|
+
|
|
30
|
+
A run processor can intercept and modify the agent execution flow by wrapping
|
|
31
|
+
the event generator function. This is useful for implementing cross-cutting
|
|
32
|
+
concerns such as:
|
|
33
|
+
- Authentication flows (e.g., OAuth2)
|
|
34
|
+
- Request/response logging
|
|
35
|
+
- Error handling and retry logic
|
|
36
|
+
- Performance monitoring
|
|
37
|
+
- Custom event filtering or transformation
|
|
38
|
+
|
|
39
|
+
The processor uses a decorator pattern to wrap the event generator, allowing
|
|
40
|
+
it to:
|
|
41
|
+
1. Intercept events from runner.run_async
|
|
42
|
+
2. Process or modify events
|
|
43
|
+
3. Inject additional events (e.g., authentication requests)
|
|
44
|
+
4. Control the execution flow (e.g., retry loops)
|
|
45
|
+
|
|
46
|
+
Example:
|
|
47
|
+
class MyProcessor(BaseRunProcessor):
|
|
48
|
+
def process_run(self, runner, message, **kwargs):
|
|
49
|
+
def decorator(event_generator_func):
|
|
50
|
+
async def wrapper():
|
|
51
|
+
# Pre-processing
|
|
52
|
+
async for event in event_generator_func():
|
|
53
|
+
# Process each event
|
|
54
|
+
yield event
|
|
55
|
+
# Post-processing
|
|
56
|
+
return wrapper
|
|
57
|
+
return decorator
|
|
58
|
+
"""
|
|
59
|
+
|
|
60
|
+
@abstractmethod
|
|
61
|
+
def process_run(
|
|
62
|
+
self,
|
|
63
|
+
runner: Runner,
|
|
64
|
+
message: types.Content,
|
|
65
|
+
**kwargs: Any,
|
|
66
|
+
) -> Callable[[Callable[[], AsyncGenerator]], Callable[[], AsyncGenerator]]:
|
|
67
|
+
"""Process the agent run by wrapping the event generator.
|
|
68
|
+
|
|
69
|
+
This method returns a decorator that wraps the event generator function.
|
|
70
|
+
The decorator can intercept events, modify them, or inject new events.
|
|
71
|
+
|
|
72
|
+
Args:
|
|
73
|
+
runner: The Runner instance executing the agent.
|
|
74
|
+
message: The initial message to send to the agent.
|
|
75
|
+
**kwargs: Additional keyword arguments that may be needed by specific
|
|
76
|
+
implementations (e.g., task_updater for status updates).
|
|
77
|
+
|
|
78
|
+
Returns:
|
|
79
|
+
A decorator function that takes an event generator function and returns
|
|
80
|
+
a wrapped event generator function.
|
|
81
|
+
|
|
82
|
+
Example:
|
|
83
|
+
@processor.process_run(runner=runner, message=message)
|
|
84
|
+
async def event_generator():
|
|
85
|
+
async for event in runner.run_async(...):
|
|
86
|
+
yield event
|
|
87
|
+
"""
|
|
88
|
+
pass
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
class NoOpRunProcessor(BaseRunProcessor):
|
|
92
|
+
"""No-op run processor that doesn't modify the event generator.
|
|
93
|
+
|
|
94
|
+
This is the default processor used when no specific processing is needed.
|
|
95
|
+
It simply passes through all events without any modification.
|
|
96
|
+
"""
|
|
97
|
+
|
|
98
|
+
def process_run(
|
|
99
|
+
self,
|
|
100
|
+
runner: Runner,
|
|
101
|
+
message: types.Content,
|
|
102
|
+
**kwargs: Any,
|
|
103
|
+
) -> Callable[[Callable[[], AsyncGenerator]], Callable[[], AsyncGenerator]]:
|
|
104
|
+
"""Return a decorator that does nothing.
|
|
105
|
+
|
|
106
|
+
Args:
|
|
107
|
+
runner: The Runner instance (unused).
|
|
108
|
+
message: The initial message (unused).
|
|
109
|
+
**kwargs: Additional keyword arguments (unused).
|
|
110
|
+
|
|
111
|
+
Returns:
|
|
112
|
+
A decorator that returns the original function unchanged.
|
|
113
|
+
"""
|
|
114
|
+
|
|
115
|
+
def decorator(
|
|
116
|
+
event_generator_func: Callable[[], AsyncGenerator],
|
|
117
|
+
) -> Callable[[], AsyncGenerator]:
|
|
118
|
+
return event_generator_func
|
|
119
|
+
|
|
120
|
+
return decorator
|
|
@@ -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.
|
|
@@ -0,0 +1,30 @@
|
|
|
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
|
+
DEFAULT_INSTRUCTION = """You an AI agent created by the VeADK team.
|
|
16
|
+
|
|
17
|
+
You excel at the following tasks:
|
|
18
|
+
1. Data science
|
|
19
|
+
- Information gathering and fact-checking
|
|
20
|
+
- Data processing and analysis
|
|
21
|
+
2. Documentation
|
|
22
|
+
- Writing multi-chapter articles and in-depth research reports
|
|
23
|
+
3. Coding & Programming
|
|
24
|
+
- Creating websites, applications, and tools
|
|
25
|
+
- Solve problems and bugs in code (e.g., Python, JavaScript, SQL, ...)
|
|
26
|
+
- If necessary, using programming to solve various problems beyond development
|
|
27
|
+
4. If user gives you tools, finish various tasks that can be accomplished using tools and available resources
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
DEFAULT_DESCRIPTION = """An AI agent developed by the VeADK team, specialized in data science, documentation, and software development."""
|
|
@@ -0,0 +1,20 @@
|
|
|
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
|
+
eval_principle_prompt = """
|
|
16
|
+
You are a LLM for evaluating other models' responses. Note:
|
|
17
|
+
- The response maybe generated by some uncertainty tools (e.g., online-search, random number), you just need to consider whether the response is human-readable, rather than focus on the specific content. Because the specific content maybe different at different time.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
criteria_prompt = "Determine whether the actual output is factually correct based on the expected output."
|
|
@@ -0,0 +1,55 @@
|
|
|
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
|
+
from jinja2 import Template
|
|
16
|
+
|
|
17
|
+
MEMORY_PROCESSOR_SYSTEM_PROMPT = """I will give you a series of messages of memory, including messages from user and assistant.
|
|
18
|
+
|
|
19
|
+
You should help me to recognize important information from the messages, and build some new messages.
|
|
20
|
+
|
|
21
|
+
For example, for the following messages:
|
|
22
|
+
[
|
|
23
|
+
{
|
|
24
|
+
"role": "user",
|
|
25
|
+
"content": "Hello, tell me the weather of Beijing, and remember my secret is `abc001`"
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
"role": "assistant",
|
|
29
|
+
"content": "The weather of Beijing is sunny, and the temperature is 25 degree Celsius. I have remember that your secret is `abc001`."
|
|
30
|
+
}
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
You should extract the important information from the messages, and build new messages if needed (in JSON format):
|
|
34
|
+
[
|
|
35
|
+
{
|
|
36
|
+
"role": "user",
|
|
37
|
+
"content": "My secret is `abc001`."
|
|
38
|
+
}
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
The actual messages are:
|
|
42
|
+
{{ messages }}
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def render_prompt(messages: list[dict]):
|
|
47
|
+
template = Template(MEMORY_PROCESSOR_SYSTEM_PROMPT)
|
|
48
|
+
|
|
49
|
+
context = {
|
|
50
|
+
"messages": messages,
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
rendered_prompt = template.render(context)
|
|
54
|
+
|
|
55
|
+
return rendered_prompt
|
|
@@ -0,0 +1,150 @@
|
|
|
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
|
+
This file is used to optimize prompt in AgentPilot.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
|
|
21
|
+
import asyncio
|
|
22
|
+
from typing import Callable
|
|
23
|
+
|
|
24
|
+
from google.adk.tools import FunctionTool
|
|
25
|
+
from google.adk.tools.mcp_tool import MCPToolset
|
|
26
|
+
from jinja2 import Template
|
|
27
|
+
|
|
28
|
+
from veadk import Agent
|
|
29
|
+
|
|
30
|
+
# prompt = """
|
|
31
|
+
# <role>
|
|
32
|
+
# An experienced prompt optimizer.
|
|
33
|
+
# </role>
|
|
34
|
+
|
|
35
|
+
# <task>
|
|
36
|
+
# Please optimize prompt to make it more efficient.
|
|
37
|
+
# The prompt will be used as a system prompt and instruction of an agent.
|
|
38
|
+
# The definition and context (i.e., tools) of the agent will be provided.
|
|
39
|
+
# </task>
|
|
40
|
+
|
|
41
|
+
# <agent_info>
|
|
42
|
+
# name: {{ agent.name }}
|
|
43
|
+
# model: {{ agent.model }}
|
|
44
|
+
# description: {{ agent.description }}
|
|
45
|
+
# </agent_info>
|
|
46
|
+
|
|
47
|
+
# <agent_tools_info>
|
|
48
|
+
# {% for tool in tools %}
|
|
49
|
+
# <tool>
|
|
50
|
+
# name: {{ tool.name }}
|
|
51
|
+
# type: {{ tool.type }}
|
|
52
|
+
# description: {{ tool.description }}
|
|
53
|
+
# arguments: {{ tool.arguments }}
|
|
54
|
+
# </tool>
|
|
55
|
+
# {% endfor %}
|
|
56
|
+
# </agent_tools_info>
|
|
57
|
+
# """.strip()
|
|
58
|
+
|
|
59
|
+
prompt = """
|
|
60
|
+
Please help me to optimize the following agent prompt:
|
|
61
|
+
{{ original_prompt }}
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
The following information is your references:
|
|
65
|
+
<agent_info>
|
|
66
|
+
name: {{ agent.name }}
|
|
67
|
+
model: {{ agent.model }}
|
|
68
|
+
description: {{ agent.description }}
|
|
69
|
+
</agent_info>
|
|
70
|
+
|
|
71
|
+
<agent_tools_info>
|
|
72
|
+
{% for tool in tools %}
|
|
73
|
+
<tool>
|
|
74
|
+
name: {{ tool.name }}
|
|
75
|
+
type: {{ tool.type }}
|
|
76
|
+
description: {{ tool.description }}
|
|
77
|
+
arguments: {{ tool.arguments }}
|
|
78
|
+
</tool>
|
|
79
|
+
{% endfor %}
|
|
80
|
+
</agent_tools_info>
|
|
81
|
+
|
|
82
|
+
Please note that in your optimized prompt:
|
|
83
|
+
- the above referenced information is not necessary. For example, the tools list of agent is not necessary in the optimized prompt, because it maybe too long. You should use the tool information to optimize the original prompt rather than simply add tool list in prompt.
|
|
84
|
+
- The max length of optimized prompt should be less 4096 tokens.
|
|
85
|
+
""".strip()
|
|
86
|
+
|
|
87
|
+
prompt_with_feedback = """
|
|
88
|
+
After you optimization, my current prompt is:
|
|
89
|
+
{{ prompt }}
|
|
90
|
+
|
|
91
|
+
I did some evaluations with the optimized prompt, and the feedback is: {{ feedback }}
|
|
92
|
+
|
|
93
|
+
Please continue to optimize the prompt based on the feedback.
|
|
94
|
+
""".strip()
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def render_prompt_feedback_with_jinja2(agent: Agent, feedback: str):
|
|
98
|
+
template = Template(prompt_with_feedback)
|
|
99
|
+
|
|
100
|
+
context = {
|
|
101
|
+
"prompt": agent.instruction,
|
|
102
|
+
"feedback": feedback,
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
rendered_prompt = template.render(context)
|
|
106
|
+
|
|
107
|
+
return rendered_prompt
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def render_prompt_with_jinja2(agent: Agent):
|
|
111
|
+
template = Template(prompt)
|
|
112
|
+
|
|
113
|
+
tools = []
|
|
114
|
+
for tool in agent.tools:
|
|
115
|
+
_tool_type = ""
|
|
116
|
+
_tools = []
|
|
117
|
+
if isinstance(tool, Callable):
|
|
118
|
+
_tool_type = "function"
|
|
119
|
+
_tools = [FunctionTool(tool)]
|
|
120
|
+
|
|
121
|
+
elif isinstance(tool, MCPToolset):
|
|
122
|
+
_tool_type = "tool"
|
|
123
|
+
_tools = asyncio.run(tool.get_tools())
|
|
124
|
+
|
|
125
|
+
for _tool in _tools:
|
|
126
|
+
if _tool and _tool._get_declaration():
|
|
127
|
+
tools.append(
|
|
128
|
+
{
|
|
129
|
+
"name": _tool.name,
|
|
130
|
+
"description": _tool.description,
|
|
131
|
+
"arguments": str(
|
|
132
|
+
_tool._get_declaration().model_dump()["parameters"] # type: ignore
|
|
133
|
+
),
|
|
134
|
+
"type": _tool_type,
|
|
135
|
+
}
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
context = {
|
|
139
|
+
"original_prompt": agent.instruction,
|
|
140
|
+
"agent": {
|
|
141
|
+
"name": agent.name,
|
|
142
|
+
"model": agent.model_name,
|
|
143
|
+
"description": agent.description,
|
|
144
|
+
},
|
|
145
|
+
"tools": tools,
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
rendered_prompt = template.render(context)
|
|
149
|
+
|
|
150
|
+
return rendered_prompt
|