veadk-python 0.2.6__py3-none-any.whl → 0.2.8__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/agent.py +11 -18
- veadk/agent_builder.py +94 -0
- veadk/{database/__init__.py → auth/base_auth.py} +7 -2
- veadk/auth/veauth/apmplus_veauth.py +65 -0
- veadk/auth/veauth/ark_veauth.py +77 -0
- veadk/auth/veauth/base_veauth.py +50 -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/vesearch_veauth.py +62 -0
- veadk/cli/cli.py +4 -0
- veadk/cli/cli_deploy.py +3 -2
- veadk/cli/cli_eval.py +160 -0
- veadk/cli/cli_init.py +1 -1
- veadk/cli/cli_pipeline.py +220 -0
- veadk/cli/cli_prompt.py +4 -4
- veadk/cli/cli_web.py +3 -1
- veadk/config.py +45 -81
- veadk/configs/database_configs.py +117 -0
- veadk/configs/model_configs.py +74 -0
- veadk/configs/tool_configs.py +42 -0
- veadk/configs/tracing_configs.py +110 -0
- veadk/consts.py +13 -1
- veadk/evaluation/base_evaluator.py +60 -44
- veadk/evaluation/deepeval_evaluator/deepeval_evaluator.py +18 -12
- veadk/evaluation/eval_set_recorder.py +2 -2
- 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/ve_cr.py +20 -5
- veadk/integrations/ve_faas/template/cookiecutter.json +1 -1
- veadk/integrations/ve_faas/template/{{cookiecutter.local_dir_name}}/deploy.py +2 -2
- veadk/integrations/ve_faas/template/{{cookiecutter.local_dir_name}}/src/agent.py +1 -1
- veadk/integrations/ve_faas/template/{{cookiecutter.local_dir_name}}/src/run.sh +1 -5
- veadk/integrations/ve_faas/ve_faas.py +351 -36
- veadk/integrations/ve_prompt_pilot/ve_prompt_pilot.py +6 -3
- veadk/integrations/ve_tls/__init__.py +13 -0
- veadk/integrations/ve_tls/utils.py +117 -0
- veadk/integrations/ve_tls/ve_tls.py +208 -0
- veadk/integrations/ve_tos/ve_tos.py +71 -75
- veadk/knowledgebase/backends/__init__.py +13 -0
- veadk/knowledgebase/backends/base_backend.py +59 -0
- veadk/knowledgebase/backends/in_memory_backend.py +82 -0
- veadk/knowledgebase/backends/opensearch_backend.py +136 -0
- veadk/knowledgebase/backends/redis_backend.py +144 -0
- veadk/knowledgebase/backends/utils.py +91 -0
- veadk/knowledgebase/backends/vikingdb_knowledge_backend.py +412 -0
- veadk/knowledgebase/knowledgebase.py +109 -55
- veadk/memory/__init__.py +22 -0
- veadk/memory/long_term_memory.py +120 -51
- veadk/memory/long_term_memory_backends/__init__.py +13 -0
- veadk/{database/base_database.py → memory/long_term_memory_backends/base_backend.py} +10 -22
- veadk/memory/long_term_memory_backends/in_memory_backend.py +65 -0
- veadk/memory/long_term_memory_backends/opensearch_backend.py +120 -0
- veadk/memory/long_term_memory_backends/redis_backend.py +127 -0
- veadk/memory/long_term_memory_backends/vikingdb_memory_backend.py +148 -0
- veadk/memory/short_term_memory.py +80 -72
- 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 +41 -0
- veadk/memory/short_term_memory_backends/postgresql_backend.py +41 -0
- veadk/memory/short_term_memory_backends/sqlite_backend.py +48 -0
- veadk/memory/short_term_memory_processor.py +9 -4
- veadk/runner.py +204 -247
- veadk/tools/builtin_tools/vesearch.py +2 -2
- veadk/tools/builtin_tools/video_generate.py +27 -20
- veadk/tools/builtin_tools/web_scraper.py +1 -1
- veadk/tools/builtin_tools/web_search.py +7 -7
- veadk/tools/load_knowledgebase_tool.py +1 -1
- veadk/tracing/telemetry/attributes/extractors/llm_attributes_extractors.py +20 -2
- veadk/tracing/telemetry/exporters/apmplus_exporter.py +178 -14
- veadk/tracing/telemetry/exporters/cozeloop_exporter.py +6 -9
- veadk/tracing/telemetry/exporters/inmemory_exporter.py +22 -8
- veadk/tracing/telemetry/exporters/tls_exporter.py +6 -10
- veadk/tracing/telemetry/opentelemetry_tracer.py +5 -8
- veadk/tracing/telemetry/telemetry.py +66 -60
- veadk/utils/logger.py +1 -1
- veadk/utils/misc.py +63 -0
- veadk/utils/volcengine_sign.py +6 -2
- veadk/version.py +1 -1
- {veadk_python-0.2.6.dist-info → veadk_python-0.2.8.dist-info}/METADATA +16 -3
- {veadk_python-0.2.6.dist-info → veadk_python-0.2.8.dist-info}/RECORD +93 -64
- veadk/database/database_adapter.py +0 -368
- veadk/database/database_factory.py +0 -80
- veadk/database/kv/redis_database.py +0 -159
- veadk/database/local_database.py +0 -61
- veadk/database/relational/mysql_database.py +0 -173
- veadk/database/vector/opensearch_vector_database.py +0 -263
- veadk/database/vector/type.py +0 -50
- veadk/database/viking/viking_database.py +0 -471
- veadk/database/viking/viking_memory_db.py +0 -525
- /veadk/{database/kv → auth}/__init__.py +0 -0
- /veadk/{database/relational → auth/veauth}/__init__.py +0 -0
- /veadk/{database/vector/__init__.py → auth/veauth/cozeloop_veauth.py} +0 -0
- /veadk/{database/viking → configs}/__init__.py +0 -0
- /veadk/integrations/ve_faas/template/{{cookiecutter.local_dir_name}}/src/{{{ cookiecutter.app_name|replace('-', '_') }} → {{ cookiecutter.app_name }}}/__init__.py +0 -0
- /veadk/integrations/ve_faas/template/{{cookiecutter.local_dir_name}}/src/{{{ cookiecutter.app_name|replace('-', '_') }} → {{ cookiecutter.app_name }}}/agent.py +0 -0
- {veadk_python-0.2.6.dist-info → veadk_python-0.2.8.dist-info}/WHEEL +0 -0
- {veadk_python-0.2.6.dist-info → veadk_python-0.2.8.dist-info}/entry_points.txt +0 -0
- {veadk_python-0.2.6.dist-info → veadk_python-0.2.8.dist-info}/licenses/LICENSE +0 -0
- {veadk_python-0.2.6.dist-info → veadk_python-0.2.8.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,148 @@
|
|
|
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 json
|
|
16
|
+
import re
|
|
17
|
+
import time
|
|
18
|
+
import uuid
|
|
19
|
+
from typing import Any
|
|
20
|
+
|
|
21
|
+
from pydantic import Field
|
|
22
|
+
from typing_extensions import override
|
|
23
|
+
|
|
24
|
+
import veadk.config # noqa E401
|
|
25
|
+
from veadk.config import getenv
|
|
26
|
+
from veadk.memory.long_term_memory_backends.base_backend import (
|
|
27
|
+
BaseLongTermMemoryBackend,
|
|
28
|
+
)
|
|
29
|
+
from veadk.utils.logger import get_logger
|
|
30
|
+
|
|
31
|
+
try:
|
|
32
|
+
from mcp_server_vikingdb_memory.common.memory_client import VikingDBMemoryService
|
|
33
|
+
except ImportError:
|
|
34
|
+
raise ImportError(
|
|
35
|
+
"Please install VeADK extensions\npip install veadk-python[extensions]"
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
logger = get_logger(__name__)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class VikingDBLTMBackend(BaseLongTermMemoryBackend):
|
|
42
|
+
volcengine_access_key: str = Field(
|
|
43
|
+
default_factory=lambda: getenv("VOLCENGINE_ACCESS_KEY")
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
volcengine_secret_key: str = Field(
|
|
47
|
+
default_factory=lambda: getenv("VOLCENGINE_SECRET_KEY")
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
region: str = "cn-beijing"
|
|
51
|
+
"""VikingDB memory region"""
|
|
52
|
+
|
|
53
|
+
def precheck_index_naming(self):
|
|
54
|
+
if not (
|
|
55
|
+
isinstance(self.index, str)
|
|
56
|
+
and 1 <= len(self.index) <= 128
|
|
57
|
+
and re.fullmatch(r"^[a-zA-Z][a-zA-Z0-9_]*$", self.index)
|
|
58
|
+
):
|
|
59
|
+
raise ValueError(
|
|
60
|
+
"The index name does not conform to the rules: it must start with an English letter, contain only letters, numbers, and underscores, and have a length of 1-128."
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
def model_post_init(self, __context: Any) -> None:
|
|
64
|
+
self._client = VikingDBMemoryService(
|
|
65
|
+
ak=self.volcengine_access_key,
|
|
66
|
+
sk=self.volcengine_secret_key,
|
|
67
|
+
region=self.region,
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
# check whether collection exist, if not, create it
|
|
71
|
+
if not self._collection_exist():
|
|
72
|
+
self._create_collection()
|
|
73
|
+
|
|
74
|
+
def _collection_exist(self) -> bool:
|
|
75
|
+
try:
|
|
76
|
+
self._client.get_collection(collection_name=self.index)
|
|
77
|
+
return True
|
|
78
|
+
except Exception:
|
|
79
|
+
return False
|
|
80
|
+
|
|
81
|
+
def _create_collection(self) -> None:
|
|
82
|
+
response = self._client.create_collection(
|
|
83
|
+
collection_name=self.index,
|
|
84
|
+
description="Created by Volcengine Agent Development Kit VeADK",
|
|
85
|
+
builtin_event_types=["sys_event_v1"],
|
|
86
|
+
)
|
|
87
|
+
return response
|
|
88
|
+
|
|
89
|
+
@override
|
|
90
|
+
def save_memory(self, event_strings: list[str], **kwargs) -> bool:
|
|
91
|
+
user_id = kwargs.get("user_id")
|
|
92
|
+
if user_id is None:
|
|
93
|
+
raise ValueError("user_id is required")
|
|
94
|
+
session_id = str(uuid.uuid1())
|
|
95
|
+
messages = []
|
|
96
|
+
for raw_events in event_strings:
|
|
97
|
+
event = json.loads(raw_events)
|
|
98
|
+
content = event["parts"][0]["text"]
|
|
99
|
+
role = (
|
|
100
|
+
"user" if event["role"] == "user" else "assistant"
|
|
101
|
+
) # field 'role': viking memory only allow 'assistant','system','user',
|
|
102
|
+
messages.append({"role": role, "content": content})
|
|
103
|
+
metadata = {
|
|
104
|
+
"default_user_id": user_id,
|
|
105
|
+
"default_assistant_id": "assistant",
|
|
106
|
+
"time": int(time.time() * 1000),
|
|
107
|
+
}
|
|
108
|
+
response = self._client.add_messages(
|
|
109
|
+
collection_name=self.index,
|
|
110
|
+
messages=messages,
|
|
111
|
+
metadata=metadata,
|
|
112
|
+
session_id=session_id,
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
if not response.get("code") == 0:
|
|
116
|
+
raise ValueError(f"Save VikingDB memory error: {response}")
|
|
117
|
+
|
|
118
|
+
return True
|
|
119
|
+
|
|
120
|
+
@override
|
|
121
|
+
def search_memory(self, query: str, top_k: int, **kwargs) -> list[str]:
|
|
122
|
+
user_id = kwargs.get("user_id")
|
|
123
|
+
if user_id is None:
|
|
124
|
+
raise ValueError("user_id is required")
|
|
125
|
+
filter = {
|
|
126
|
+
"user_id": user_id,
|
|
127
|
+
"memory_type": ["sys_event_v1"],
|
|
128
|
+
}
|
|
129
|
+
response = self._client.search_memory(
|
|
130
|
+
collection_name=self.index, query=query, filter=filter, limit=top_k
|
|
131
|
+
)
|
|
132
|
+
|
|
133
|
+
if not response.get("code") == 0:
|
|
134
|
+
raise ValueError(f"Search VikingDB memory error: {response}")
|
|
135
|
+
|
|
136
|
+
result = response.get("data", {}).get("result_list", [])
|
|
137
|
+
if result:
|
|
138
|
+
return [
|
|
139
|
+
json.dumps(
|
|
140
|
+
{
|
|
141
|
+
"role": "user",
|
|
142
|
+
"parts": [{"text": r.get("memory_info").get("summary")}],
|
|
143
|
+
},
|
|
144
|
+
ensure_ascii=False,
|
|
145
|
+
)
|
|
146
|
+
for r in result
|
|
147
|
+
]
|
|
148
|
+
return []
|
|
@@ -12,95 +12,103 @@
|
|
|
12
12
|
# See the License for the specific language governing permissions and
|
|
13
13
|
# limitations under the License.
|
|
14
14
|
|
|
15
|
-
import
|
|
16
|
-
from typing import Literal
|
|
15
|
+
from functools import wraps
|
|
16
|
+
from typing import Any, Callable, Literal
|
|
17
|
+
|
|
18
|
+
from google.adk.sessions import (
|
|
19
|
+
BaseSessionService,
|
|
20
|
+
DatabaseSessionService,
|
|
21
|
+
InMemorySessionService,
|
|
22
|
+
)
|
|
23
|
+
from pydantic import BaseModel, Field, PrivateAttr
|
|
24
|
+
|
|
25
|
+
from veadk.memory.short_term_memory_backends.mysql_backend import (
|
|
26
|
+
MysqlSTMBackend,
|
|
27
|
+
)
|
|
28
|
+
from veadk.memory.short_term_memory_backends.postgresql_backend import (
|
|
29
|
+
PostgreSqlSTMBackend,
|
|
30
|
+
)
|
|
31
|
+
from veadk.memory.short_term_memory_backends.sqlite_backend import (
|
|
32
|
+
SQLiteSTMBackend,
|
|
33
|
+
)
|
|
34
|
+
from veadk.utils.logger import get_logger
|
|
17
35
|
|
|
18
|
-
|
|
36
|
+
logger = get_logger(__name__)
|
|
19
37
|
|
|
20
|
-
from veadk.config import getenv
|
|
21
|
-
from veadk.utils.logger import get_logger
|
|
22
38
|
|
|
23
|
-
|
|
39
|
+
def wrap_get_session_with_callbacks(obj, callback_fn: Callable):
|
|
40
|
+
get_session_fn = getattr(obj, "get_session")
|
|
24
41
|
|
|
25
|
-
|
|
42
|
+
@wraps(get_session_fn)
|
|
43
|
+
def wrapper(*args, **kwargs):
|
|
44
|
+
result = get_session_fn(*args, **kwargs)
|
|
45
|
+
callback_fn(result, *args, **kwargs)
|
|
46
|
+
return result
|
|
26
47
|
|
|
27
|
-
|
|
48
|
+
setattr(obj, "get_session", wrapper)
|
|
28
49
|
|
|
29
50
|
|
|
30
|
-
class ShortTermMemory:
|
|
31
|
-
"""
|
|
32
|
-
Short term memory
|
|
51
|
+
class ShortTermMemory(BaseModel):
|
|
52
|
+
backend: Literal["local", "mysql", "sqlite", "postgresql", "database"] = "local"
|
|
53
|
+
"""Short term memory backend. `Local` for in-memory storage, `mysql` for mysql / PostgreSQL storage. `sqlite` for sqlite storage."""
|
|
33
54
|
|
|
34
|
-
|
|
35
|
-
"""
|
|
55
|
+
backend_configs: dict = Field(default_factory=dict)
|
|
56
|
+
"""Backend specific configurations."""
|
|
36
57
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
backend: Literal["local", "database", "mysql"] = "local",
|
|
40
|
-
db_url: str = "",
|
|
41
|
-
enable_memory_optimization: bool = False,
|
|
42
|
-
):
|
|
43
|
-
self.backend = backend
|
|
44
|
-
self.db_url = db_url
|
|
45
|
-
|
|
46
|
-
if self.backend == "mysql":
|
|
47
|
-
host = getenv("DATABASE_MYSQL_HOST")
|
|
48
|
-
user = getenv("DATABASE_MYSQL_USER")
|
|
49
|
-
password = getenv("DATABASE_MYSQL_PASSWORD")
|
|
50
|
-
database = getenv("DATABASE_MYSQL_DATABASE")
|
|
51
|
-
db_url = f"mysql+pymysql://{user}:{password}@{host}/{database}"
|
|
52
|
-
|
|
53
|
-
self.db_url = db_url
|
|
54
|
-
self.backend = "database"
|
|
55
|
-
|
|
56
|
-
if self.backend == "local":
|
|
57
|
-
logger.warning(
|
|
58
|
-
f"Short term memory backend: {self.backend}, the history will be lost after application shutdown."
|
|
59
|
-
)
|
|
60
|
-
self.session_service = InMemorySessionService()
|
|
61
|
-
elif self.backend == "database":
|
|
62
|
-
if self.db_url == "" or self.db_url is None:
|
|
63
|
-
logger.warning("The `db_url` is an empty or None string.")
|
|
64
|
-
self._use_default_database()
|
|
65
|
-
else:
|
|
66
|
-
try:
|
|
67
|
-
self.session_service = DatabaseSessionService(db_url=self.db_url)
|
|
68
|
-
logger.info("Connected to database with db_url.")
|
|
69
|
-
except Exception as e:
|
|
70
|
-
logger.error(f"Failed to connect to database, error: {e}.")
|
|
71
|
-
self._use_default_database()
|
|
72
|
-
else:
|
|
73
|
-
raise ValueError(f"Unknown short term memory backend: {self.backend}")
|
|
58
|
+
db_url: str = ""
|
|
59
|
+
"""Database connection URL, e.g. `sqlite:///./test.db`. Once set, it will override the `backend` parameter."""
|
|
74
60
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
)
|
|
61
|
+
local_database_path: str = "/tmp/veadk_local_database.db"
|
|
62
|
+
"""Local database path, only used when `backend` is `sqlite`. Default to `/tmp/veadk_local_database.db`."""
|
|
63
|
+
|
|
64
|
+
after_load_memory_callback: Callable | None = None
|
|
65
|
+
"""A callback to be called after loading memory from the backend. The callback function should accept `Session` as an input."""
|
|
81
66
|
|
|
82
|
-
|
|
83
|
-
self.db_url = DEFAULT_LOCAL_DATABASE_PATH
|
|
84
|
-
logger.info(f"Using default local database {self.db_url}")
|
|
85
|
-
if not os.path.exists(self.db_url):
|
|
86
|
-
self.create_local_sqlite3_db(self.db_url)
|
|
87
|
-
self.session_service = DatabaseSessionService(db_url="sqlite:///" + self.db_url)
|
|
67
|
+
_session_service: BaseSessionService = PrivateAttr()
|
|
88
68
|
|
|
89
|
-
def
|
|
90
|
-
|
|
69
|
+
def model_post_init(self, __context: Any) -> None:
|
|
70
|
+
if self.db_url:
|
|
71
|
+
logger.info("The `db_url` is set, ignore `backend` option.")
|
|
72
|
+
self._session_service = DatabaseSessionService(db_url=self.db_url)
|
|
73
|
+
else:
|
|
74
|
+
if self.backend == "database":
|
|
75
|
+
logger.warning(
|
|
76
|
+
"Backend `database` is deprecated, use `sqlite` to create short term memory."
|
|
77
|
+
)
|
|
78
|
+
self.backend = "sqlite"
|
|
79
|
+
match self.backend:
|
|
80
|
+
case "local":
|
|
81
|
+
self._session_service = InMemorySessionService()
|
|
82
|
+
case "mysql":
|
|
83
|
+
self._session_service = MysqlSTMBackend(
|
|
84
|
+
**self.backend_configs
|
|
85
|
+
).session_service
|
|
86
|
+
case "sqlite":
|
|
87
|
+
self._session_service = SQLiteSTMBackend(
|
|
88
|
+
local_path=self.local_database_path
|
|
89
|
+
).session_service
|
|
90
|
+
case "postgresql":
|
|
91
|
+
self._session_service = PostgreSqlSTMBackend(
|
|
92
|
+
**self.backend_configs
|
|
93
|
+
).session_service
|
|
94
|
+
|
|
95
|
+
if self.after_load_memory_callback:
|
|
96
|
+
wrap_get_session_with_callbacks(
|
|
97
|
+
self._session_service, self.after_load_memory_callback
|
|
98
|
+
)
|
|
91
99
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
100
|
+
@property
|
|
101
|
+
def session_service(self) -> BaseSessionService:
|
|
102
|
+
return self._session_service
|
|
95
103
|
|
|
96
104
|
async def create_session(
|
|
97
105
|
self,
|
|
98
106
|
app_name: str,
|
|
99
107
|
user_id: str,
|
|
100
108
|
session_id: str,
|
|
101
|
-
):
|
|
102
|
-
if isinstance(self.
|
|
103
|
-
list_sessions_response = await self.
|
|
109
|
+
) -> None:
|
|
110
|
+
if isinstance(self._session_service, DatabaseSessionService):
|
|
111
|
+
list_sessions_response = await self._session_service.list_sessions(
|
|
104
112
|
app_name=app_name, user_id=user_id
|
|
105
113
|
)
|
|
106
114
|
|
|
@@ -109,12 +117,12 @@ class ShortTermMemory:
|
|
|
109
117
|
)
|
|
110
118
|
|
|
111
119
|
if (
|
|
112
|
-
await self.
|
|
120
|
+
await self._session_service.get_session(
|
|
113
121
|
app_name=app_name, user_id=user_id, session_id=session_id
|
|
114
122
|
)
|
|
115
123
|
is None
|
|
116
124
|
):
|
|
117
125
|
# create a new session for this running
|
|
118
|
-
await self.
|
|
126
|
+
await self._session_service.create_session(
|
|
119
127
|
app_name=app_name, user_id=user_id, session_id=session_id
|
|
120
128
|
)
|
|
@@ -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,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
|
+
|
|
16
|
+
from abc import ABC, abstractmethod
|
|
17
|
+
from functools import cached_property
|
|
18
|
+
|
|
19
|
+
from google.adk.sessions import BaseSessionService
|
|
20
|
+
from pydantic import BaseModel
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class BaseShortTermMemoryBackend(ABC, BaseModel):
|
|
24
|
+
"""
|
|
25
|
+
Base class for short term memory backend.
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
@cached_property
|
|
29
|
+
@abstractmethod
|
|
30
|
+
def session_service(self) -> BaseSessionService:
|
|
31
|
+
"""Return the session service instance."""
|
|
@@ -0,0 +1,41 @@
|
|
|
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 functools import cached_property
|
|
16
|
+
from typing import Any
|
|
17
|
+
|
|
18
|
+
from google.adk.sessions import (
|
|
19
|
+
BaseSessionService,
|
|
20
|
+
DatabaseSessionService,
|
|
21
|
+
)
|
|
22
|
+
from pydantic import Field
|
|
23
|
+
from typing_extensions import override
|
|
24
|
+
|
|
25
|
+
import veadk.config # noqa E401
|
|
26
|
+
from veadk.configs.database_configs import MysqlConfig
|
|
27
|
+
from veadk.memory.short_term_memory_backends.base_backend import (
|
|
28
|
+
BaseShortTermMemoryBackend,
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class MysqlSTMBackend(BaseShortTermMemoryBackend):
|
|
33
|
+
mysql_config: MysqlConfig = Field(default_factory=MysqlConfig)
|
|
34
|
+
|
|
35
|
+
def model_post_init(self, context: Any) -> None:
|
|
36
|
+
self._db_url = f"mysql+pymysql://{self.mysql_config.user}:{self.mysql_config.password}@{self.mysql_config.host}/{self.mysql_config.database}"
|
|
37
|
+
|
|
38
|
+
@cached_property
|
|
39
|
+
@override
|
|
40
|
+
def session_service(self) -> BaseSessionService:
|
|
41
|
+
return DatabaseSessionService(db_url=self._db_url)
|
|
@@ -0,0 +1,41 @@
|
|
|
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 functools import cached_property
|
|
16
|
+
from typing import Any
|
|
17
|
+
|
|
18
|
+
from google.adk.sessions import (
|
|
19
|
+
BaseSessionService,
|
|
20
|
+
DatabaseSessionService,
|
|
21
|
+
)
|
|
22
|
+
from pydantic import Field
|
|
23
|
+
from typing_extensions import override
|
|
24
|
+
|
|
25
|
+
import veadk.config # noqa E401
|
|
26
|
+
from veadk.configs.database_configs import PostgreSqlConfig
|
|
27
|
+
from veadk.memory.short_term_memory_backends.base_backend import (
|
|
28
|
+
BaseShortTermMemoryBackend,
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class PostgreSqlSTMBackend(BaseShortTermMemoryBackend):
|
|
33
|
+
postgresql_config: PostgreSqlConfig = Field(default_factory=PostgreSqlConfig)
|
|
34
|
+
|
|
35
|
+
def model_post_init(self, context: Any) -> None:
|
|
36
|
+
self._db_url = f"postgresql+psycopg2://{self.postgresql_config.user}:{self.postgresql_config.password}@{self.postgresql_config.host}:{self.postgresql_config.port}/{self.postgresql_config.database}"
|
|
37
|
+
|
|
38
|
+
@cached_property
|
|
39
|
+
@override
|
|
40
|
+
def session_service(self) -> BaseSessionService:
|
|
41
|
+
return DatabaseSessionService(db_url=self._db_url)
|
|
@@ -0,0 +1,48 @@
|
|
|
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
|
+
import sqlite3
|
|
17
|
+
from functools import cached_property
|
|
18
|
+
from typing import Any
|
|
19
|
+
|
|
20
|
+
from google.adk.sessions import (
|
|
21
|
+
BaseSessionService,
|
|
22
|
+
DatabaseSessionService,
|
|
23
|
+
)
|
|
24
|
+
from typing_extensions import override
|
|
25
|
+
|
|
26
|
+
from veadk.memory.short_term_memory_backends.base_backend import (
|
|
27
|
+
BaseShortTermMemoryBackend,
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class SQLiteSTMBackend(BaseShortTermMemoryBackend):
|
|
32
|
+
local_path: str
|
|
33
|
+
|
|
34
|
+
def model_post_init(self, context: Any) -> None:
|
|
35
|
+
# if the DB file not exists, create it
|
|
36
|
+
if not self._db_exists():
|
|
37
|
+
conn = sqlite3.connect(self.local_path)
|
|
38
|
+
conn.close()
|
|
39
|
+
|
|
40
|
+
self._db_url = f"sqlite:///{self.local_path}"
|
|
41
|
+
|
|
42
|
+
@cached_property
|
|
43
|
+
@override
|
|
44
|
+
def session_service(self) -> BaseSessionService:
|
|
45
|
+
return DatabaseSessionService(db_url=self._db_url)
|
|
46
|
+
|
|
47
|
+
def _db_exists(self) -> bool:
|
|
48
|
+
return os.path.exists(self.local_path)
|
|
@@ -20,7 +20,12 @@ from google.adk.sessions import Session
|
|
|
20
20
|
from google.genai.types import Content, Part
|
|
21
21
|
from litellm import completion
|
|
22
22
|
|
|
23
|
-
from veadk.config import
|
|
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
|
+
)
|
|
24
29
|
from veadk.prompts.prompt_memory_processor import render_prompt
|
|
25
30
|
from veadk.utils.logger import get_logger
|
|
26
31
|
|
|
@@ -62,9 +67,9 @@ class ShortTermMemoryProcessor:
|
|
|
62
67
|
prompt = render_prompt(messages=messages)
|
|
63
68
|
|
|
64
69
|
res = completion(
|
|
65
|
-
model=
|
|
66
|
-
base_url=
|
|
67
|
-
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,
|
|
68
73
|
messages=[
|
|
69
74
|
{
|
|
70
75
|
"role": "user",
|