veadk-python 0.1.0__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.
Potentially problematic release.
This version of veadk-python might be problematic. Click here for more details.
- veadk/__init__.py +31 -0
- veadk/a2a/__init__.py +13 -0
- veadk/a2a/agent_card.py +45 -0
- veadk/a2a/remote_ve_agent.py +19 -0
- veadk/a2a/ve_a2a_server.py +77 -0
- veadk/a2a/ve_agent_executor.py +78 -0
- veadk/a2a/ve_task_store.py +37 -0
- veadk/agent.py +253 -0
- veadk/cli/__init__.py +13 -0
- veadk/cli/main.py +278 -0
- veadk/cli/services/agentpilot/__init__.py +17 -0
- veadk/cli/services/agentpilot/agentpilot.py +77 -0
- veadk/cli/services/veapig/__init__.py +17 -0
- veadk/cli/services/veapig/apig.py +224 -0
- veadk/cli/services/veapig/apig_utils.py +332 -0
- veadk/cli/services/vefaas/__init__.py +17 -0
- veadk/cli/services/vefaas/template/deploy.py +44 -0
- veadk/cli/services/vefaas/template/src/app.py +30 -0
- veadk/cli/services/vefaas/template/src/config.py +58 -0
- veadk/cli/services/vefaas/vefaas.py +346 -0
- veadk/cli/services/vefaas/vefaas_utils.py +408 -0
- veadk/cli/services/vetls/__init__.py +17 -0
- veadk/cli/services/vetls/vetls.py +87 -0
- veadk/cli/studio/__init__.py +13 -0
- veadk/cli/studio/agent_processor.py +247 -0
- veadk/cli/studio/fast_api.py +232 -0
- veadk/cli/studio/model.py +116 -0
- veadk/cloud/__init__.py +13 -0
- veadk/cloud/cloud_agent_engine.py +144 -0
- veadk/cloud/cloud_app.py +123 -0
- veadk/cloud/template/app.py +30 -0
- veadk/cloud/template/config.py +55 -0
- veadk/config.py +131 -0
- veadk/consts.py +17 -0
- veadk/database/__init__.py +17 -0
- veadk/database/base_database.py +45 -0
- veadk/database/database_factory.py +80 -0
- veadk/database/kv/__init__.py +13 -0
- veadk/database/kv/redis_database.py +109 -0
- veadk/database/local_database.py +43 -0
- veadk/database/relational/__init__.py +13 -0
- veadk/database/relational/mysql_database.py +114 -0
- veadk/database/vector/__init__.py +13 -0
- veadk/database/vector/opensearch_vector_database.py +205 -0
- veadk/database/vector/type.py +50 -0
- veadk/database/viking/__init__.py +13 -0
- veadk/database/viking/viking_database.py +378 -0
- veadk/database/viking/viking_memory_db.py +521 -0
- veadk/evaluation/__init__.py +17 -0
- veadk/evaluation/adk_evaluator/__init__.py +13 -0
- veadk/evaluation/adk_evaluator/adk_evaluator.py +291 -0
- veadk/evaluation/base_evaluator.py +242 -0
- veadk/evaluation/deepeval_evaluator/__init__.py +17 -0
- veadk/evaluation/deepeval_evaluator/deepeval_evaluator.py +223 -0
- veadk/evaluation/eval_set_file_loader.py +28 -0
- veadk/evaluation/eval_set_recorder.py +91 -0
- veadk/evaluation/utils/prometheus.py +142 -0
- veadk/knowledgebase/__init__.py +17 -0
- veadk/knowledgebase/knowledgebase.py +83 -0
- veadk/knowledgebase/knowledgebase_database_adapter.py +259 -0
- veadk/memory/__init__.py +13 -0
- veadk/memory/long_term_memory.py +119 -0
- veadk/memory/memory_database_adapter.py +235 -0
- veadk/memory/short_term_memory.py +124 -0
- veadk/memory/short_term_memory_processor.py +90 -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 +158 -0
- veadk/runner.py +252 -0
- veadk/tools/__init__.py +13 -0
- veadk/tools/builtin_tools/__init__.py +13 -0
- veadk/tools/builtin_tools/lark.py +67 -0
- veadk/tools/builtin_tools/las.py +23 -0
- veadk/tools/builtin_tools/vesearch.py +49 -0
- veadk/tools/builtin_tools/web_scraper.py +76 -0
- veadk/tools/builtin_tools/web_search.py +192 -0
- veadk/tools/demo_tools.py +58 -0
- veadk/tools/load_knowledgebase_tool.py +144 -0
- veadk/tools/sandbox/__init__.py +13 -0
- veadk/tools/sandbox/browser_sandbox.py +27 -0
- veadk/tools/sandbox/code_sandbox.py +30 -0
- veadk/tools/sandbox/computer_sandbox.py +27 -0
- veadk/tracing/__init__.py +13 -0
- veadk/tracing/base_tracer.py +172 -0
- veadk/tracing/telemetry/__init__.py +13 -0
- veadk/tracing/telemetry/exporters/__init__.py +13 -0
- veadk/tracing/telemetry/exporters/apiserver_exporter.py +60 -0
- veadk/tracing/telemetry/exporters/apmplus_exporter.py +101 -0
- veadk/tracing/telemetry/exporters/base_exporter.py +28 -0
- veadk/tracing/telemetry/exporters/cozeloop_exporter.py +69 -0
- veadk/tracing/telemetry/exporters/inmemory_exporter.py +88 -0
- veadk/tracing/telemetry/exporters/tls_exporter.py +78 -0
- veadk/tracing/telemetry/metrics/__init__.py +13 -0
- veadk/tracing/telemetry/metrics/opentelemetry_metrics.py +73 -0
- veadk/tracing/telemetry/opentelemetry_tracer.py +167 -0
- veadk/types.py +23 -0
- veadk/utils/__init__.py +13 -0
- veadk/utils/logger.py +59 -0
- veadk/utils/misc.py +33 -0
- veadk/utils/patches.py +85 -0
- veadk/utils/volcengine_sign.py +199 -0
- veadk/version.py +15 -0
- veadk_python-0.1.0.dist-info/METADATA +124 -0
- veadk_python-0.1.0.dist-info/RECORD +110 -0
- veadk_python-0.1.0.dist-info/WHEEL +5 -0
- veadk_python-0.1.0.dist-info/entry_points.txt +2 -0
- veadk_python-0.1.0.dist-info/licenses/LICENSE +201 -0
- veadk_python-0.1.0.dist-info/top_level.txt +1 -0
veadk/__init__.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
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 .version import VERSION
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
# Lazy loading for `Agent` class
|
|
19
|
+
def __getattr__(name):
|
|
20
|
+
if name == "Agent":
|
|
21
|
+
from .agent import Agent
|
|
22
|
+
|
|
23
|
+
return Agent
|
|
24
|
+
if name == "Runner":
|
|
25
|
+
from .runner import Runner
|
|
26
|
+
|
|
27
|
+
return Runner
|
|
28
|
+
raise AttributeError(f"module 'veadk' has no attribute '{name}'")
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
__all__ = ["Agent", "Runner", "VERSION"]
|
veadk/a2a/__init__.py
ADDED
|
@@ -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/a2a/agent_card.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
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 a2a.types import AgentCapabilities, AgentCard, AgentProvider, AgentSkill
|
|
16
|
+
|
|
17
|
+
from veadk import Agent
|
|
18
|
+
from veadk.version import VERSION
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def get_agent_card(
|
|
22
|
+
agent: Agent, url: str, version: str = VERSION, provider: str = "veadk"
|
|
23
|
+
) -> AgentCard:
|
|
24
|
+
agent_provider = AgentProvider(organization=provider, url="")
|
|
25
|
+
agent_capabilities = AgentCapabilities()
|
|
26
|
+
agent_skills = [
|
|
27
|
+
AgentSkill(
|
|
28
|
+
id="0",
|
|
29
|
+
name="chat",
|
|
30
|
+
description="Basically chat with user.",
|
|
31
|
+
tags=["chat", "talk"],
|
|
32
|
+
)
|
|
33
|
+
]
|
|
34
|
+
agent_card = AgentCard(
|
|
35
|
+
capabilities=agent_capabilities,
|
|
36
|
+
description=agent.description,
|
|
37
|
+
name=agent.name,
|
|
38
|
+
defaultInputModes=["text"],
|
|
39
|
+
defaultOutputModes=["text"],
|
|
40
|
+
provider=agent_provider,
|
|
41
|
+
skills=agent_skills,
|
|
42
|
+
url=url,
|
|
43
|
+
version=version,
|
|
44
|
+
)
|
|
45
|
+
return agent_card
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import json
|
|
2
|
+
|
|
3
|
+
import requests
|
|
4
|
+
from a2a.types import AgentCard
|
|
5
|
+
from google.adk.agents.remote_a2a_agent import RemoteA2aAgent
|
|
6
|
+
|
|
7
|
+
AGENT_CARD_WELL_KNOWN_PATH = "/.well-known/agent.json"
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class RemoteVeAgent(RemoteA2aAgent):
|
|
11
|
+
def __init__(self, name: str, url: str):
|
|
12
|
+
agent_card_dict = requests.get(url + AGENT_CARD_WELL_KNOWN_PATH).json()
|
|
13
|
+
agent_card_dict["url"] = url
|
|
14
|
+
|
|
15
|
+
agent_card_json_str = json.dumps(agent_card_dict, ensure_ascii=False, indent=2)
|
|
16
|
+
|
|
17
|
+
agent_card_object = AgentCard.model_validate_json(str(agent_card_json_str))
|
|
18
|
+
|
|
19
|
+
super().__init__(name=name, agent_card=agent_card_object)
|
|
@@ -0,0 +1,77 @@
|
|
|
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 a2a.server.apps.jsonrpc.fastapi_app import A2AFastAPIApplication
|
|
16
|
+
from a2a.server.request_handlers.default_request_handler import DefaultRequestHandler
|
|
17
|
+
from a2a.server.tasks.inmemory_task_store import InMemoryTaskStore
|
|
18
|
+
from fastapi import FastAPI
|
|
19
|
+
|
|
20
|
+
from veadk import Agent
|
|
21
|
+
from veadk.a2a.agent_card import get_agent_card
|
|
22
|
+
from veadk.a2a.ve_agent_executor import VeAgentExecutor
|
|
23
|
+
from veadk.memory.short_term_memory import ShortTermMemory
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class VeA2AServer:
|
|
27
|
+
def __init__(
|
|
28
|
+
self, agent: Agent, url: str, app_name: str, short_term_memory: ShortTermMemory
|
|
29
|
+
):
|
|
30
|
+
self.agent_card = get_agent_card(agent, url)
|
|
31
|
+
|
|
32
|
+
self.agent_executor = VeAgentExecutor(
|
|
33
|
+
app_name=app_name,
|
|
34
|
+
agent=agent,
|
|
35
|
+
short_term_memory=short_term_memory,
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
self.task_store = InMemoryTaskStore()
|
|
39
|
+
|
|
40
|
+
self.request_handler = DefaultRequestHandler(
|
|
41
|
+
agent_executor=self.agent_executor, task_store=self.task_store
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
def build(self) -> FastAPI:
|
|
45
|
+
app_application = A2AFastAPIApplication(
|
|
46
|
+
agent_card=self.agent_card,
|
|
47
|
+
http_handler=self.request_handler,
|
|
48
|
+
)
|
|
49
|
+
app = app_application.build() # build routes
|
|
50
|
+
|
|
51
|
+
# import uvicorn
|
|
52
|
+
# uvicorn.run(app, host="127.0.0.1", port=8000)
|
|
53
|
+
return app
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def init_app(
|
|
57
|
+
server_url: str, app_name: str, agent: Agent, short_term_memory: ShortTermMemory
|
|
58
|
+
) -> FastAPI:
|
|
59
|
+
"""Init the fastapi application in terms of VeADK agent.
|
|
60
|
+
|
|
61
|
+
Args:
|
|
62
|
+
server_url: str, the url of the server
|
|
63
|
+
app_name: str, the name of the app
|
|
64
|
+
agent: Agent, the agent of the app
|
|
65
|
+
short_term_memory: ShortTermMemory, the short term memory of the app
|
|
66
|
+
|
|
67
|
+
Returns:
|
|
68
|
+
FastAPI, the fastapi app
|
|
69
|
+
"""
|
|
70
|
+
|
|
71
|
+
server = VeA2AServer(
|
|
72
|
+
agent=agent,
|
|
73
|
+
url=server_url,
|
|
74
|
+
app_name=app_name,
|
|
75
|
+
short_term_memory=short_term_memory,
|
|
76
|
+
)
|
|
77
|
+
return server.build()
|
|
@@ -0,0 +1,78 @@
|
|
|
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 a2a.server.agent_execution.agent_executor import AgentExecutor
|
|
16
|
+
from a2a.server.agent_execution.context import RequestContext
|
|
17
|
+
from a2a.server.events.event_queue import EventQueue
|
|
18
|
+
from a2a.utils import new_agent_text_message
|
|
19
|
+
from typing_extensions import override
|
|
20
|
+
|
|
21
|
+
from veadk import Agent
|
|
22
|
+
from veadk.memory.short_term_memory import ShortTermMemory
|
|
23
|
+
from veadk.runner import Runner
|
|
24
|
+
from veadk.utils.logger import get_logger
|
|
25
|
+
|
|
26
|
+
logger = get_logger(__name__)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class VeAgentExecutor(AgentExecutor):
|
|
30
|
+
def __init__(self, app_name: str, agent: Agent, short_term_memory: ShortTermMemory):
|
|
31
|
+
super().__init__()
|
|
32
|
+
self.app_name = app_name
|
|
33
|
+
self.agent = agent
|
|
34
|
+
self.short_term_memory = short_term_memory
|
|
35
|
+
|
|
36
|
+
self.runner = Runner(
|
|
37
|
+
agent=self.agent,
|
|
38
|
+
short_term_memory=self.short_term_memory,
|
|
39
|
+
app_name=app_name,
|
|
40
|
+
user_id="", # waiting for `execute` change
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
@override
|
|
44
|
+
async def execute(self, context: RequestContext, event_queue: EventQueue) -> None:
|
|
45
|
+
# extract metadata
|
|
46
|
+
user_id = (
|
|
47
|
+
context.metadata["user_id"]
|
|
48
|
+
if "user_id" in context.metadata
|
|
49
|
+
else "unkonwn_user"
|
|
50
|
+
)
|
|
51
|
+
self.runner.user_id = user_id
|
|
52
|
+
|
|
53
|
+
session_id = (
|
|
54
|
+
context.metadata["session_id"]
|
|
55
|
+
if "session_id" in context.metadata
|
|
56
|
+
else "unkonwn_session"
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
# process user input
|
|
60
|
+
user_input = context.get_user_input()
|
|
61
|
+
|
|
62
|
+
logger.debug(
|
|
63
|
+
f"Request: user_id: {user_id}, session_id: {session_id}, user_input: {user_input}"
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
# running
|
|
67
|
+
final_output = await self.runner.run(
|
|
68
|
+
messages=user_input,
|
|
69
|
+
session_id=session_id,
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
logger.debug(f"Final output: {final_output}")
|
|
73
|
+
|
|
74
|
+
await event_queue.enqueue_event(new_agent_text_message(final_output))
|
|
75
|
+
|
|
76
|
+
@override
|
|
77
|
+
async def cancel(self, context: RequestContext, event_queue: EventQueue) -> None:
|
|
78
|
+
return None
|
|
@@ -0,0 +1,37 @@
|
|
|
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 a2a.server.tasks import TaskStore
|
|
16
|
+
from a2a.types import Task
|
|
17
|
+
from typing_extensions import override
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class VeTaskStore(TaskStore):
|
|
21
|
+
def __init__(self):
|
|
22
|
+
super().__init__()
|
|
23
|
+
|
|
24
|
+
@override
|
|
25
|
+
async def save(self, task: Task) -> None:
|
|
26
|
+
"""Saves or updates a task in the store."""
|
|
27
|
+
return None
|
|
28
|
+
|
|
29
|
+
@override
|
|
30
|
+
async def get(self, task_id: str) -> Task | None:
|
|
31
|
+
"""Retrieves a task from the store by ID."""
|
|
32
|
+
return None
|
|
33
|
+
|
|
34
|
+
@override
|
|
35
|
+
async def delete(self, task_id: str) -> None:
|
|
36
|
+
"""Deletes a task from the store by ID."""
|
|
37
|
+
return None
|
veadk/agent.py
ADDED
|
@@ -0,0 +1,253 @@
|
|
|
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 __future__ import annotations
|
|
16
|
+
|
|
17
|
+
from typing import Optional
|
|
18
|
+
|
|
19
|
+
from google.adk.agents import LlmAgent, RunConfig
|
|
20
|
+
from google.adk.agents.llm_agent import ToolUnion
|
|
21
|
+
from google.adk.agents.run_config import StreamingMode
|
|
22
|
+
from google.adk.models.lite_llm import LiteLlm
|
|
23
|
+
from google.adk.runners import Runner
|
|
24
|
+
from google.genai import types
|
|
25
|
+
from pydantic import ConfigDict, Field
|
|
26
|
+
from typing_extensions import Any
|
|
27
|
+
|
|
28
|
+
from veadk.config import getenv
|
|
29
|
+
from veadk.consts import (
|
|
30
|
+
DEFALUT_MODEL_AGENT_PROVIDER,
|
|
31
|
+
DEFAULT_MODEL_AGENT_API_BASE,
|
|
32
|
+
DEFAULT_MODEL_AGENT_NAME,
|
|
33
|
+
)
|
|
34
|
+
from veadk.evaluation import EvalSetRecorder
|
|
35
|
+
from veadk.knowledgebase import KnowledgeBase
|
|
36
|
+
from veadk.memory.long_term_memory import LongTermMemory
|
|
37
|
+
from veadk.memory.short_term_memory import ShortTermMemory
|
|
38
|
+
from veadk.prompts.agent_default_prompt import DEFAULT_DESCRIPTION, DEFAULT_INSTRUCTION
|
|
39
|
+
from veadk.tracing.base_tracer import BaseTracer
|
|
40
|
+
from veadk.utils.logger import get_logger
|
|
41
|
+
from veadk.utils.patches import patch_asyncio
|
|
42
|
+
|
|
43
|
+
patch_asyncio()
|
|
44
|
+
logger = get_logger(__name__)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class Agent(LlmAgent):
|
|
48
|
+
"""LLM-based Agent with Volcengine capabilities."""
|
|
49
|
+
|
|
50
|
+
model_config = ConfigDict(arbitrary_types_allowed=True, extra="allow")
|
|
51
|
+
"""The model config"""
|
|
52
|
+
|
|
53
|
+
name: str = "veAgent"
|
|
54
|
+
"""The name of the agent."""
|
|
55
|
+
|
|
56
|
+
description: str = DEFAULT_DESCRIPTION
|
|
57
|
+
"""The description of the agent. This will be helpful in A2A scenario."""
|
|
58
|
+
|
|
59
|
+
instruction: str = DEFAULT_INSTRUCTION
|
|
60
|
+
"""The instruction for the agent, such as principles of function calling."""
|
|
61
|
+
|
|
62
|
+
# factory
|
|
63
|
+
model_name: str = getenv("MODEL_AGENT_NAME", DEFAULT_MODEL_AGENT_NAME)
|
|
64
|
+
"""The name of the model for agent running."""
|
|
65
|
+
|
|
66
|
+
model_provider: str = getenv("MODEL_AGENT_PROVIDER", DEFALUT_MODEL_AGENT_PROVIDER)
|
|
67
|
+
"""The provider of the model for agent running."""
|
|
68
|
+
|
|
69
|
+
model_api_base: str = getenv("MODEL_AGENT_API_BASE", DEFAULT_MODEL_AGENT_API_BASE)
|
|
70
|
+
"""The api base of the model for agent running."""
|
|
71
|
+
|
|
72
|
+
model_api_key: str = Field(
|
|
73
|
+
..., default_factory=lambda: getenv("MODEL_AGENT_API_KEY")
|
|
74
|
+
)
|
|
75
|
+
"""The api key of the model for agent running."""
|
|
76
|
+
|
|
77
|
+
tools: list[ToolUnion] = []
|
|
78
|
+
"""The tools provided to agent."""
|
|
79
|
+
|
|
80
|
+
sub_agents: list[Agent] = Field(default_factory=list, exclude=True)
|
|
81
|
+
"""The sub agents provided to agent."""
|
|
82
|
+
|
|
83
|
+
knowledgebase: Optional[KnowledgeBase] = None
|
|
84
|
+
"""The knowledgebase provided to agent."""
|
|
85
|
+
|
|
86
|
+
long_term_memory: Optional[LongTermMemory] = None
|
|
87
|
+
"""The long term memory provided to agent.
|
|
88
|
+
|
|
89
|
+
In VeADK, the `long_term_memory` refers to cross-session memory under the same user.
|
|
90
|
+
"""
|
|
91
|
+
|
|
92
|
+
tracers: list[BaseTracer] = []
|
|
93
|
+
"""The tracers provided to agent."""
|
|
94
|
+
|
|
95
|
+
serve_url: str = ""
|
|
96
|
+
"""The url of agent serving host. Show in agent card."""
|
|
97
|
+
|
|
98
|
+
def model_post_init(self, __context: Any) -> None:
|
|
99
|
+
super().model_post_init(None) # for sub_agents init
|
|
100
|
+
self.model = LiteLlm(
|
|
101
|
+
model=f"{self.model_provider}/{self.model_name}",
|
|
102
|
+
api_key=self.model_api_key,
|
|
103
|
+
api_base=self.model_api_base,
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
if self.knowledgebase:
|
|
107
|
+
from veadk.tools import load_knowledgebase_tool
|
|
108
|
+
|
|
109
|
+
load_knowledgebase_tool.knowledgebase = self.knowledgebase
|
|
110
|
+
self.tools.append(load_knowledgebase_tool.load_knowledgebase_tool)
|
|
111
|
+
|
|
112
|
+
if self.long_term_memory is not None:
|
|
113
|
+
from google.adk.tools import load_memory
|
|
114
|
+
|
|
115
|
+
self.tools.append(load_memory)
|
|
116
|
+
|
|
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)
|
|
123
|
+
|
|
124
|
+
logger.info(f"Agent `{self.name}` init done.")
|
|
125
|
+
logger.debug(
|
|
126
|
+
f"Agent: {self.model_dump(include={'name', 'model_name', 'model_api_base', 'tools', 'serve_url'})}"
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
async def _run(
|
|
130
|
+
self,
|
|
131
|
+
runner,
|
|
132
|
+
user_id: str,
|
|
133
|
+
session_id: str,
|
|
134
|
+
message: types.Content,
|
|
135
|
+
stream: bool,
|
|
136
|
+
):
|
|
137
|
+
stream_mode = StreamingMode.SSE if stream else StreamingMode.NONE
|
|
138
|
+
|
|
139
|
+
async def event_generator():
|
|
140
|
+
async for event in runner.run_async(
|
|
141
|
+
user_id=user_id,
|
|
142
|
+
session_id=session_id,
|
|
143
|
+
new_message=message,
|
|
144
|
+
run_config=RunConfig(streaming_mode=stream_mode),
|
|
145
|
+
):
|
|
146
|
+
if event.get_function_calls():
|
|
147
|
+
for function_call in event.get_function_calls():
|
|
148
|
+
logger.debug(f"Function call: {function_call}")
|
|
149
|
+
elif (
|
|
150
|
+
event.content is not None
|
|
151
|
+
and event.content.parts[0].text is not None
|
|
152
|
+
and len(event.content.parts[0].text.strip()) > 0
|
|
153
|
+
):
|
|
154
|
+
yield event.content.parts[0].text
|
|
155
|
+
|
|
156
|
+
final_output = ""
|
|
157
|
+
async for chunk in event_generator():
|
|
158
|
+
if stream:
|
|
159
|
+
print(chunk, end="", flush=True)
|
|
160
|
+
final_output += chunk
|
|
161
|
+
if stream:
|
|
162
|
+
print() # end with a new line
|
|
163
|
+
|
|
164
|
+
return final_output
|
|
165
|
+
|
|
166
|
+
async def run(
|
|
167
|
+
self,
|
|
168
|
+
prompt: str | list[str],
|
|
169
|
+
stream: bool = False,
|
|
170
|
+
app_name: str = "veadk_app",
|
|
171
|
+
user_id: str = "veadk_user",
|
|
172
|
+
session_id="veadk_session",
|
|
173
|
+
load_history_sessions_from_db: bool = False,
|
|
174
|
+
db_url: str = "",
|
|
175
|
+
collect_runtime_data: bool = False,
|
|
176
|
+
eval_set_id: str = "",
|
|
177
|
+
save_session_to_memory: bool = False,
|
|
178
|
+
enable_memory_optimization: bool = False,
|
|
179
|
+
):
|
|
180
|
+
"""Running the agent. The runner and session service will be created automatically.
|
|
181
|
+
|
|
182
|
+
For production, consider using Google-ADK runner to run agent, rather than invoking this method.
|
|
183
|
+
|
|
184
|
+
Args:
|
|
185
|
+
prompt (str | list[str]): The prompt to run the agent.
|
|
186
|
+
stream (bool, optional): Whether to stream the output. Defaults to False.
|
|
187
|
+
app_name (str, optional): The name of the application. Defaults to "veadk_app".
|
|
188
|
+
user_id (str, optional): The id of the user. Defaults to "veadk_user".
|
|
189
|
+
session_id (str, optional): The id of the session. Defaults to "veadk_session".
|
|
190
|
+
load_history_sessions_from_db (bool, optional): Whether to load history sessions from database. Defaults to False.
|
|
191
|
+
db_url (str, optional): The url of the database. Defaults to "".
|
|
192
|
+
collect_runtime_data (bool, optional): Whether to collect runtime data. Defaults to False.
|
|
193
|
+
eval_set_id (str, optional): The id of the eval set. Defaults to "".
|
|
194
|
+
save_session_to_memory (bool, optional): Whether to save this turn session to memory. Defaults to False.
|
|
195
|
+
"""
|
|
196
|
+
|
|
197
|
+
logger.warning(
|
|
198
|
+
"Running agent in this function is only for development and testing, do not use this function in production. For production, consider using `Google ADK Runner` to run agent, rather than invoking this method."
|
|
199
|
+
)
|
|
200
|
+
logger.info(
|
|
201
|
+
f"Run agent {self.name}: app_name: {app_name}, user_id: {user_id}, session_id: {session_id}."
|
|
202
|
+
)
|
|
203
|
+
prompt = [prompt] if isinstance(prompt, str) else prompt
|
|
204
|
+
|
|
205
|
+
# memory service
|
|
206
|
+
short_term_memory = ShortTermMemory(
|
|
207
|
+
backend="database" if load_history_sessions_from_db else "local",
|
|
208
|
+
enable_memory_optimization=enable_memory_optimization,
|
|
209
|
+
db_url=db_url,
|
|
210
|
+
)
|
|
211
|
+
session_service = short_term_memory.session_service
|
|
212
|
+
await short_term_memory.create_session(
|
|
213
|
+
app_name=app_name, user_id=user_id, session_id=session_id
|
|
214
|
+
)
|
|
215
|
+
|
|
216
|
+
# runner
|
|
217
|
+
runner = Runner(
|
|
218
|
+
agent=self,
|
|
219
|
+
app_name=app_name,
|
|
220
|
+
session_service=session_service,
|
|
221
|
+
memory_service=self.long_term_memory,
|
|
222
|
+
)
|
|
223
|
+
|
|
224
|
+
logger.info(f"Begin to process prompt {prompt}")
|
|
225
|
+
# run
|
|
226
|
+
final_output = ""
|
|
227
|
+
for _prompt in prompt:
|
|
228
|
+
message = types.Content(role="user", parts=[types.Part(text=_prompt)])
|
|
229
|
+
final_output = await self._run(runner, user_id, session_id, message, stream)
|
|
230
|
+
|
|
231
|
+
# VeADK features
|
|
232
|
+
if save_session_to_memory:
|
|
233
|
+
assert self.long_term_memory is not None, (
|
|
234
|
+
"Long-term memory is not initialized in agent"
|
|
235
|
+
)
|
|
236
|
+
session = await session_service.get_session(
|
|
237
|
+
app_name=app_name,
|
|
238
|
+
user_id=user_id,
|
|
239
|
+
session_id=session_id,
|
|
240
|
+
)
|
|
241
|
+
await self.long_term_memory.add_session_to_memory(session)
|
|
242
|
+
logger.info(f"Add session `{session.id}` to your long-term memory.")
|
|
243
|
+
|
|
244
|
+
if collect_runtime_data:
|
|
245
|
+
eval_set_recorder = EvalSetRecorder(session_service, eval_set_id)
|
|
246
|
+
dump_path = await eval_set_recorder.dump(app_name, user_id, session_id)
|
|
247
|
+
self._dump_path = dump_path # just for test/debug/instrumentation
|
|
248
|
+
|
|
249
|
+
if self.tracers:
|
|
250
|
+
for tracer in self.tracers:
|
|
251
|
+
tracer.dump(user_id, session_id)
|
|
252
|
+
|
|
253
|
+
return final_output
|
veadk/cli/__init__.py
ADDED
|
@@ -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.
|