veadk-python 0.2.12__py3-none-any.whl → 0.2.14__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/cli/cli.py CHANGED
@@ -18,6 +18,7 @@ import click
18
18
  from veadk.cli.cli_deploy import deploy
19
19
  from veadk.cli.cli_eval import eval
20
20
  from veadk.cli.cli_init import init
21
+ from veadk.cli.cli_create import create
21
22
  from veadk.cli.cli_kb import kb
22
23
  from veadk.cli.cli_pipeline import pipeline
23
24
  from veadk.cli.cli_prompt import prompt
@@ -37,6 +38,7 @@ def veadk():
37
38
 
38
39
  veadk.add_command(deploy)
39
40
  veadk.add_command(init)
41
+ veadk.add_command(create)
40
42
  veadk.add_command(prompt)
41
43
  veadk.add_command(web)
42
44
  veadk.add_command(pipeline)
@@ -0,0 +1,122 @@
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 click
16
+ import shutil
17
+ from pathlib import Path
18
+
19
+ _CONFIG_YAML_TEMPLATE = """\
20
+ model:
21
+ agent:
22
+ name: doubao-seed-1-6-251015
23
+ api_key: {ark_api_key}
24
+ video:
25
+ name: doubao-seedance-1-0-pro-250528
26
+ # if you want to use different api_key, just uncomment following line and complete api_key
27
+ # api_key:
28
+ image:
29
+ name: doubao-seedream-4-0-250828
30
+ # if you want to use different api_key, just uncomment following line and complete api_key
31
+ # api_key:
32
+
33
+ logging:
34
+ # ERROR
35
+ # WARNING
36
+ # INFO
37
+ # DEBUG
38
+ level: DEBUG
39
+ """
40
+
41
+ _INIT_PY_TEMPLATE = """\
42
+ from . import agent
43
+ """
44
+
45
+ _AGENT_PY_TEMPLATE = """\
46
+ from veadk import Agent
47
+
48
+ root_agent = Agent(
49
+ name="root_agent",
50
+ description="A helpful assistant for user questions.",
51
+ instruction="Answer user questions to the best of your knowledge",
52
+ )
53
+ """
54
+
55
+ _SUCCESS_MSG = """\
56
+ Agent '{agent_name}' created successfully at '{agent_folder}':
57
+ - config.yaml
58
+ - {agent_name}/__init__.py
59
+ - {agent_name}/agent.py
60
+
61
+ You can run the agent by executing: cd {agent_name} && veadk web
62
+ """
63
+
64
+
65
+ def _prompt_for_ark_api_key() -> str:
66
+ click.secho(
67
+ "An API key is required to run the agent. See https://www.volcengine.com/docs/82379/1541594 for details.",
68
+ fg="green",
69
+ )
70
+ click.echo("You have two options:")
71
+ click.echo(" 1. Enter the API key now.")
72
+ click.echo(" 2. Configure it later in the generated config.yaml file.")
73
+ choice = click.prompt("Please select an option", type=click.Choice(["1", "2"]))
74
+ if choice == "1":
75
+ return click.prompt("Please enter your ARK API key")
76
+ else:
77
+ click.secho(
78
+ "You can set the `api_key` in the config.yaml file later.", fg="yellow"
79
+ )
80
+ return ""
81
+
82
+
83
+ def _generate_files(agent_name: str, ark_api_key: str, target_dir_path: Path) -> None:
84
+ agent_dir_path = target_dir_path / agent_name
85
+ agent_dir_path.mkdir(parents=True, exist_ok=True)
86
+ config_yaml_path = target_dir_path / "config.yaml"
87
+ init_file_path = agent_dir_path / "__init__.py"
88
+ agent_file_path = agent_dir_path / "agent.py"
89
+
90
+ config_yaml_content = _CONFIG_YAML_TEMPLATE.format(ark_api_key=ark_api_key)
91
+ config_yaml_path.write_text(config_yaml_content)
92
+ init_file_path.write_text(_INIT_PY_TEMPLATE)
93
+ agent_file_path.write_text(_AGENT_PY_TEMPLATE)
94
+
95
+ click.secho(
96
+ _SUCCESS_MSG.format(agent_name=agent_name, agent_folder=target_dir_path),
97
+ fg="green",
98
+ )
99
+
100
+
101
+ @click.command()
102
+ @click.option("--agent-name", help="The name of the agent.")
103
+ @click.option("--ark-api-key", help="The ARK API key.")
104
+ def create(agent_name: str, ark_api_key: str) -> None:
105
+ """Creates a new agent in the current folder with prepopulated agent template."""
106
+ if not agent_name:
107
+ agent_name = click.prompt("Enter the agent name")
108
+ if not ark_api_key:
109
+ ark_api_key = _prompt_for_ark_api_key()
110
+
111
+ cwd = Path.cwd()
112
+ target_dir_path = cwd / agent_name
113
+
114
+ if target_dir_path.exists() and any(target_dir_path.iterdir()):
115
+ if not click.confirm(
116
+ f"Directory '{target_dir_path}' already exists and is not empty. Do you want to overwrite it?"
117
+ ):
118
+ click.secho("Operation cancelled.", fg="red")
119
+ return
120
+ shutil.rmtree(target_dir_path)
121
+
122
+ _generate_files(agent_name, ark_api_key, target_dir_path)
veadk/consts.py CHANGED
@@ -62,9 +62,6 @@ DEFAULT_TOS_BUCKET_NAME = "ark-tutorial"
62
62
 
63
63
  DEFAULT_COZELOOP_SPACE_NAME = "VeADK Space"
64
64
 
65
- DEFAULT_TEXT_TO_IMAGE_MODEL_NAME = "doubao-seedream-3-0-t2i-250415"
66
- DEFAULT_TEXT_TO_IMAGE_MODEL_API_BASE = "https://ark.cn-beijing.volces.com/api/v3/"
67
-
68
65
  DEFAULT_IMAGE_EDIT_MODEL_NAME = "doubao-seededit-3-0-i2i-250628"
69
66
  DEFAULT_IMAGE_EDIT_MODEL_API_BASE = "https://ark.cn-beijing.volces.com/api/v3/"
70
67
 
@@ -36,11 +36,14 @@ class VeTOS:
36
36
  self,
37
37
  ak: str = "",
38
38
  sk: str = "",
39
+ session_token: str = "",
39
40
  region: str = "cn-beijing",
40
41
  bucket_name: str = DEFAULT_TOS_BUCKET_NAME,
41
42
  ) -> None:
42
43
  self.ak = ak if ak else os.getenv("VOLCENGINE_ACCESS_KEY", "")
43
44
  self.sk = sk if sk else os.getenv("VOLCENGINE_SECRET_KEY", "")
45
+ self.session_token = session_token
46
+
44
47
  # Add empty value validation
45
48
  if not self.ak or not self.sk:
46
49
  raise ValueError(
@@ -71,6 +74,7 @@ class VeTOS:
71
74
  self._client = self._tos_module.TosClientV2(
72
75
  ak=self.ak,
73
76
  sk=self.sk,
77
+ security_token=self.session_token,
74
78
  endpoint=f"tos-{self.region}.volces.com",
75
79
  region=self.region,
76
80
  )
@@ -85,6 +89,7 @@ class VeTOS:
85
89
  self._client = self._tos_module.TosClientV2(
86
90
  self.ak,
87
91
  self.sk,
92
+ security_token=self.session_token,
88
93
  endpoint=f"tos-{self.region}.volces.com",
89
94
  region=self.region,
90
95
  )
@@ -46,6 +46,7 @@ def build_vikingdb_knowledgebase_request(
46
46
  path: str,
47
47
  volcengine_access_key: str,
48
48
  volcengine_secret_key: str,
49
+ session_token: str = "",
49
50
  method: Literal["GET", "POST", "PUT", "DELETE"] = "POST",
50
51
  region: str = "cn-beijing",
51
52
  params=None,
@@ -85,7 +86,7 @@ def build_vikingdb_knowledgebase_request(
85
86
  r.set_body(json.dumps(data))
86
87
 
87
88
  credentials = Credentials(
88
- volcengine_access_key, volcengine_secret_key, "air", region
89
+ volcengine_access_key, volcengine_secret_key, "air", region, session_token
89
90
  )
90
91
  SignerV4.sign(r, credentials)
91
92
  return r
@@ -14,6 +14,7 @@
14
14
 
15
15
  import asyncio
16
16
  import json
17
+ import os
17
18
  import re
18
19
  from pathlib import Path
19
20
  from typing import Any, Literal
@@ -23,7 +24,7 @@ from pydantic import Field
23
24
  from typing_extensions import override
24
25
 
25
26
  import veadk.config # noqa E401
26
- from veadk.config import getenv
27
+ from veadk.auth.veauth.utils import get_credential_from_vefaas_iam
27
28
  from veadk.configs.database_configs import NormalTOSConfig, TOSConfig
28
29
  from veadk.knowledgebase.backends.base_backend import BaseKnowledgebaseBackend
29
30
  from veadk.knowledgebase.backends.utils import build_vikingdb_knowledgebase_request
@@ -58,14 +59,16 @@ def get_files_in_directory(directory: str):
58
59
 
59
60
 
60
61
  class VikingDBKnowledgeBackend(BaseKnowledgebaseBackend):
61
- volcengine_access_key: str = Field(
62
- default_factory=lambda: getenv("VOLCENGINE_ACCESS_KEY")
62
+ volcengine_access_key: str | None = Field(
63
+ default_factory=lambda: os.getenv("VOLCENGINE_ACCESS_KEY")
63
64
  )
64
65
 
65
- volcengine_secret_key: str = Field(
66
- default_factory=lambda: getenv("VOLCENGINE_SECRET_KEY")
66
+ volcengine_secret_key: str | None = Field(
67
+ default_factory=lambda: os.getenv("VOLCENGINE_SECRET_KEY")
67
68
  )
68
69
 
70
+ session_token: str = ""
71
+
69
72
  volcengine_project: str = "default"
70
73
  """VikingDB knowledgebase project in Volcengine console platform. Default by `default`"""
71
74
 
@@ -75,6 +78,15 @@ class VikingDBKnowledgeBackend(BaseKnowledgebaseBackend):
75
78
  tos_config: TOSConfig | NormalTOSConfig = Field(default_factory=TOSConfig)
76
79
  """TOS config, used to upload files to TOS"""
77
80
 
81
+ def model_post_init(self, __context: Any) -> None:
82
+ self.precheck_index_naming()
83
+
84
+ # check whether collection exist, if not, create it
85
+ if not self.collection_status()["existed"]:
86
+ logger.warning(
87
+ f"VikingDB knowledgebase collection {self.index} does not exist, please create it first..."
88
+ )
89
+
78
90
  def precheck_index_naming(self):
79
91
  if not (
80
92
  isinstance(self.index, str)
@@ -86,18 +98,21 @@ class VikingDBKnowledgeBackend(BaseKnowledgebaseBackend):
86
98
  "it must start with an English letter, contain only letters, numbers, and underscores, and have a length of 1-128."
87
99
  )
88
100
 
89
- def model_post_init(self, __context: Any) -> None:
90
- self.precheck_index_naming()
91
-
92
- # check whether collection exist, if not, create it
93
- if not self.collection_status()["existed"]:
94
- logger.warning(
95
- f"VikingDB knowledgebase collection {self.index} does not exist, please create it first..."
96
- )
97
-
98
- self._tos_client = VeTOS(
99
- ak=self.volcengine_access_key,
100
- sk=self.volcengine_secret_key,
101
+ def _get_tos_client(self) -> VeTOS:
102
+ volcengine_access_key = self.volcengine_access_key
103
+ volcengine_secret_key = self.volcengine_secret_key
104
+ session_token = self.session_token
105
+
106
+ if not (volcengine_access_key and volcengine_secret_key):
107
+ cred = get_credential_from_vefaas_iam()
108
+ volcengine_access_key = cred.access_key_id
109
+ volcengine_secret_key = cred.secret_access_key
110
+ session_token = cred.session_token
111
+
112
+ return VeTOS(
113
+ ak=volcengine_access_key,
114
+ sk=volcengine_secret_key,
115
+ session_token=session_token,
101
116
  region=self.tos_config.region,
102
117
  bucket_name=self.tos_config.bucket,
103
118
  )
@@ -404,6 +419,8 @@ class VikingDBKnowledgeBackend(BaseKnowledgebaseBackend):
404
419
  metadata: dict | None = None,
405
420
  ) -> str:
406
421
  # Here, we set the metadata via the TOS object, ref: https://www.volcengine.com/docs/84313/1254624
422
+ self._tos_client = self._get_tos_client()
423
+
407
424
  self._tos_client.bucket_name = tos_bucket_name
408
425
  coro = self._tos_client.upload(
409
426
  object_key=object_key,
@@ -504,10 +521,21 @@ class VikingDBKnowledgeBackend(BaseKnowledgebaseBackend):
504
521
  ) -> dict:
505
522
  VIKINGDB_KNOWLEDGEBASE_BASE_URL = "api-knowledgebase.mlp.cn-beijing.volces.com"
506
523
 
524
+ volcengine_access_key = self.volcengine_access_key
525
+ volcengine_secret_key = self.volcengine_secret_key
526
+ session_token = self.session_token
527
+
528
+ if not (volcengine_access_key and volcengine_secret_key):
529
+ cred = get_credential_from_vefaas_iam()
530
+ volcengine_access_key = cred.access_key_id
531
+ volcengine_secret_key = cred.secret_access_key
532
+ session_token = cred.session_token
533
+
507
534
  request = build_vikingdb_knowledgebase_request(
508
535
  path=path,
509
- volcengine_access_key=self.volcengine_access_key,
510
- volcengine_secret_key=self.volcengine_secret_key,
536
+ volcengine_access_key=volcengine_access_key,
537
+ volcengine_secret_key=volcengine_secret_key,
538
+ session_token=session_token,
511
539
  method=method,
512
540
  data=body,
513
541
  )
@@ -104,6 +104,15 @@ class LongTermMemory(BaseMemoryService, BaseModel):
104
104
 
105
105
  # Once user define backend config, use it directly
106
106
  if self.backend_config:
107
+ if "index" not in self.backend_config:
108
+ logger.warning(
109
+ "Attribute `index` not provided in backend_config, use `index` or `app_name` instead."
110
+ )
111
+ self.backend_config["index"] = self.index or self.app_name
112
+
113
+ logger.debug(
114
+ f"Init {self.backend}, Use provided backend config: {self.backend_config}"
115
+ )
107
116
  self._backend = _get_backend_cls(self.backend)(**self.backend_config)
108
117
  return
109
118
 
@@ -49,7 +49,23 @@ class VikingDBLTMBackend(BaseLongTermMemoryBackend):
49
49
  region: str = "cn-beijing"
50
50
  """VikingDB memory region"""
51
51
 
52
+ memory_type: list[str] = Field(default_factory=list)
53
+
52
54
  def model_post_init(self, __context: Any) -> None:
55
+ # We get memory type from:
56
+ # 1. user input
57
+ # 2. environment variable
58
+ # 3. default value
59
+ if not self.memory_type:
60
+ env_memory_type = os.getenv("DATABASE_VIKINGMEM_MEMORY_TYPE")
61
+ if env_memory_type:
62
+ # "event_1, event_2" -> ["event_1", "event_2"]
63
+ self.memory_type = [x.strip() for x in env_memory_type.split(",")]
64
+ else:
65
+ self.memory_type = ["sys_event_v1", "event_v1"]
66
+
67
+ logger.info(f"Using memory type: {self.memory_type}")
68
+
53
69
  # check whether collection exist, if not, create it
54
70
  if not self._collection_exist():
55
71
  self._create_collection()
@@ -68,17 +84,23 @@ class VikingDBLTMBackend(BaseLongTermMemoryBackend):
68
84
  try:
69
85
  client = self._get_client()
70
86
  client.get_collection(collection_name=self.index)
87
+ logger.info(f"Collection {self.index} exist.")
71
88
  return True
72
89
  except Exception:
90
+ logger.info(f"Collection {self.index} not exist.")
73
91
  return False
74
92
 
75
93
  def _create_collection(self) -> None:
94
+ logger.info(
95
+ f"Create collection with collection_name={self.index}, builtin_event_types={self.memory_type}"
96
+ )
76
97
  client = self._get_client()
77
98
  response = client.create_collection(
78
99
  collection_name=self.index,
79
100
  description="Created by Volcengine Agent Development Kit VeADK",
80
- builtin_event_types=["sys_event_v1"],
101
+ builtin_event_types=self.memory_type,
81
102
  )
103
+ logger.debug(f"Create collection with response {response}")
82
104
  return response
83
105
 
84
106
  def _get_client(self) -> VikingDBMemoryClient:
@@ -135,10 +157,7 @@ class VikingDBLTMBackend(BaseLongTermMemoryBackend):
135
157
  def search_memory(
136
158
  self, user_id: str, query: str, top_k: int, **kwargs
137
159
  ) -> list[str]:
138
- filter = {
139
- "user_id": user_id,
140
- "memory_type": ["sys_event_v1", "event_v1"],
141
- }
160
+ filter = {"user_id": user_id, "memory_type": self.memory_type}
142
161
 
143
162
  logger.debug(
144
163
  f"Request for search memory in VikingDB: filter={filter}, collection_name={self.index}, query={query}, limit={top_k}"