veadk-python 0.2.10__py3-none-any.whl → 0.2.12__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 +7 -3
- veadk/auth/veauth/ark_veauth.py +43 -51
- veadk/auth/veauth/utils.py +57 -0
- veadk/cli/cli.py +2 -0
- veadk/cli/cli_uploadevalset.py +125 -0
- veadk/cli/cli_web.py +15 -2
- veadk/configs/model_configs.py +3 -3
- veadk/consts.py +9 -0
- veadk/knowledgebase/knowledgebase.py +19 -32
- veadk/memory/long_term_memory.py +39 -92
- veadk/memory/long_term_memory_backends/base_backend.py +4 -2
- veadk/memory/long_term_memory_backends/in_memory_backend.py +8 -6
- veadk/memory/long_term_memory_backends/mem0_backend.py +8 -8
- veadk/memory/long_term_memory_backends/opensearch_backend.py +40 -36
- veadk/memory/long_term_memory_backends/redis_backend.py +59 -46
- veadk/memory/long_term_memory_backends/vikingdb_memory_backend.py +54 -29
- veadk/memory/short_term_memory.py +9 -11
- veadk/runner.py +19 -11
- veadk/tools/builtin_tools/generate_image.py +230 -189
- veadk/tools/builtin_tools/image_edit.py +24 -5
- veadk/tools/builtin_tools/image_generate.py +24 -5
- veadk/tools/builtin_tools/load_knowledgebase.py +97 -0
- veadk/tools/builtin_tools/video_generate.py +38 -11
- veadk/utils/misc.py +6 -10
- veadk/utils/volcengine_sign.py +2 -0
- veadk/version.py +1 -1
- {veadk_python-0.2.10.dist-info → veadk_python-0.2.12.dist-info}/METADATA +2 -1
- {veadk_python-0.2.10.dist-info → veadk_python-0.2.12.dist-info}/RECORD +32 -29
- {veadk_python-0.2.10.dist-info → veadk_python-0.2.12.dist-info}/WHEEL +0 -0
- {veadk_python-0.2.10.dist-info → veadk_python-0.2.12.dist-info}/entry_points.txt +0 -0
- {veadk_python-0.2.10.dist-info → veadk_python-0.2.12.dist-info}/licenses/LICENSE +0 -0
- {veadk_python-0.2.10.dist-info → veadk_python-0.2.12.dist-info}/top_level.txt +0 -0
veadk/agent.py
CHANGED
|
@@ -133,10 +133,14 @@ class Agent(LlmAgent):
|
|
|
133
133
|
)
|
|
134
134
|
|
|
135
135
|
if self.knowledgebase:
|
|
136
|
-
from veadk.tools import
|
|
136
|
+
from veadk.tools.builtin_tools.load_knowledgebase import (
|
|
137
|
+
LoadKnowledgebaseTool,
|
|
138
|
+
)
|
|
137
139
|
|
|
138
|
-
load_knowledgebase_tool
|
|
139
|
-
|
|
140
|
+
load_knowledgebase_tool = LoadKnowledgebaseTool(
|
|
141
|
+
knowledgebase=self.knowledgebase
|
|
142
|
+
)
|
|
143
|
+
self.tools.append(load_knowledgebase_tool)
|
|
140
144
|
|
|
141
145
|
if self.long_term_memory is not None:
|
|
142
146
|
from google.adk.tools import load_memory
|
veadk/auth/veauth/ark_veauth.py
CHANGED
|
@@ -14,64 +14,56 @@
|
|
|
14
14
|
|
|
15
15
|
import os
|
|
16
16
|
|
|
17
|
-
from
|
|
18
|
-
|
|
19
|
-
from veadk.auth.veauth.base_veauth import BaseVeAuth
|
|
17
|
+
from veadk.auth.veauth.utils import get_credential_from_vefaas_iam
|
|
20
18
|
from veadk.utils.logger import get_logger
|
|
21
19
|
from veadk.utils.volcengine_sign import ve_request
|
|
22
20
|
|
|
23
21
|
logger = get_logger(__name__)
|
|
24
22
|
|
|
25
23
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
self,
|
|
29
|
-
access_key: str = os.getenv("VOLCENGINE_ACCESS_KEY", ""),
|
|
30
|
-
secret_key: str = os.getenv("VOLCENGINE_SECRET_KEY", ""),
|
|
31
|
-
) -> None:
|
|
32
|
-
super().__init__(access_key, secret_key)
|
|
24
|
+
def get_ark_token(region: str = "cn-beijing") -> str:
|
|
25
|
+
logger.info("Fetching ARK token...")
|
|
33
26
|
|
|
34
|
-
|
|
27
|
+
access_key = os.getenv("VOLCENGINE_ACCESS_KEY")
|
|
28
|
+
secret_key = os.getenv("VOLCENGINE_SECRET_KEY")
|
|
29
|
+
session_token = ""
|
|
35
30
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
request_body={"ProjectName": "default", "Filter": {}},
|
|
43
|
-
action="ListApiKeys",
|
|
44
|
-
ak=self.access_key,
|
|
45
|
-
sk=self.secret_key,
|
|
46
|
-
service="ark",
|
|
47
|
-
version="2024-01-01",
|
|
48
|
-
region="cn-beijing",
|
|
49
|
-
host="open.volcengineapi.com",
|
|
50
|
-
)
|
|
51
|
-
try:
|
|
52
|
-
first_api_key_id = res["Result"]["Items"][0]["Id"]
|
|
53
|
-
except KeyError:
|
|
54
|
-
raise ValueError(f"Failed to get ARK api key list: {res}")
|
|
31
|
+
if not (access_key and secret_key):
|
|
32
|
+
# try to get from vefaas iam
|
|
33
|
+
cred = get_credential_from_vefaas_iam()
|
|
34
|
+
access_key = cred.access_key_id
|
|
35
|
+
secret_key = cred.secret_access_key
|
|
36
|
+
session_token = cred.session_token
|
|
55
37
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
38
|
+
res = ve_request(
|
|
39
|
+
request_body={"ProjectName": "default", "Filter": {}},
|
|
40
|
+
header={"X-Security-Token": session_token},
|
|
41
|
+
action="ListApiKeys",
|
|
42
|
+
ak=access_key,
|
|
43
|
+
sk=secret_key,
|
|
44
|
+
service="ark",
|
|
45
|
+
version="2024-01-01",
|
|
46
|
+
region=region,
|
|
47
|
+
host="open.volcengineapi.com",
|
|
48
|
+
)
|
|
49
|
+
try:
|
|
50
|
+
first_api_key_id = res["Result"]["Items"][0]["Id"]
|
|
51
|
+
except KeyError:
|
|
52
|
+
raise ValueError(f"Failed to get ARK api key list: {res}")
|
|
71
53
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
54
|
+
# get raw api key
|
|
55
|
+
res = ve_request(
|
|
56
|
+
request_body={"Id": first_api_key_id},
|
|
57
|
+
header={"X-Security-Token": session_token},
|
|
58
|
+
action="GetRawApiKey",
|
|
59
|
+
ak=access_key,
|
|
60
|
+
sk=secret_key,
|
|
61
|
+
service="ark",
|
|
62
|
+
version="2024-01-01",
|
|
63
|
+
region=region,
|
|
64
|
+
host="open.volcengineapi.com",
|
|
65
|
+
)
|
|
66
|
+
try:
|
|
67
|
+
return res["Result"]["ApiKey"]
|
|
68
|
+
except KeyError:
|
|
69
|
+
raise ValueError(f"Failed to get ARK api key: {res}")
|
|
@@ -0,0 +1,57 @@
|
|
|
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
|
+
from pathlib import Path
|
|
17
|
+
|
|
18
|
+
from pydantic import BaseModel
|
|
19
|
+
|
|
20
|
+
from veadk.consts import VEFAAS_IAM_CRIDENTIAL_PATH
|
|
21
|
+
from veadk.utils.logger import get_logger
|
|
22
|
+
|
|
23
|
+
logger = get_logger(__name__)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class VeIAMCredential(BaseModel):
|
|
27
|
+
access_key_id: str
|
|
28
|
+
secret_access_key: str
|
|
29
|
+
session_token: str
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def get_credential_from_vefaas_iam() -> VeIAMCredential:
|
|
33
|
+
"""Get credential from VeFaaS IAM file"""
|
|
34
|
+
logger.info(
|
|
35
|
+
f"Get Volcegnine access key or secret key from environment variables failed, try to get from VeFaaS IAM file (path={VEFAAS_IAM_CRIDENTIAL_PATH})."
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
path = Path(VEFAAS_IAM_CRIDENTIAL_PATH)
|
|
39
|
+
|
|
40
|
+
if not path.exists():
|
|
41
|
+
logger.error(
|
|
42
|
+
f"Get Volcegnine access key or secret key from environment variables failed, and VeFaaS IAM file (path={VEFAAS_IAM_CRIDENTIAL_PATH}) not exists. Please check your configuration."
|
|
43
|
+
)
|
|
44
|
+
raise FileNotFoundError(
|
|
45
|
+
f"Get Volcegnine access key or secret key from environment variables failed, and VeFaaS IAM file (path={VEFAAS_IAM_CRIDENTIAL_PATH}) not exists. Please check your configuration."
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
with open(VEFAAS_IAM_CRIDENTIAL_PATH, "r") as f:
|
|
49
|
+
cred_dict = json.load(f)
|
|
50
|
+
access_key = cred_dict["access_key_id"]
|
|
51
|
+
secret_key = cred_dict["secret_access_key"]
|
|
52
|
+
session_token = cred_dict["session_token"]
|
|
53
|
+
return VeIAMCredential(
|
|
54
|
+
access_key_id=access_key,
|
|
55
|
+
secret_access_key=secret_key,
|
|
56
|
+
session_token=session_token,
|
|
57
|
+
)
|
veadk/cli/cli.py
CHANGED
|
@@ -22,6 +22,7 @@ from veadk.cli.cli_kb import kb
|
|
|
22
22
|
from veadk.cli.cli_pipeline import pipeline
|
|
23
23
|
from veadk.cli.cli_prompt import prompt
|
|
24
24
|
from veadk.cli.cli_web import web
|
|
25
|
+
from veadk.cli.cli_uploadevalset import uploadevalset
|
|
25
26
|
from veadk.version import VERSION
|
|
26
27
|
|
|
27
28
|
|
|
@@ -41,6 +42,7 @@ veadk.add_command(web)
|
|
|
41
42
|
veadk.add_command(pipeline)
|
|
42
43
|
veadk.add_command(eval)
|
|
43
44
|
veadk.add_command(kb)
|
|
45
|
+
veadk.add_command(uploadevalset)
|
|
44
46
|
|
|
45
47
|
if __name__ == "__main__":
|
|
46
48
|
veadk()
|
|
@@ -0,0 +1,125 @@
|
|
|
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 json
|
|
17
|
+
import requests
|
|
18
|
+
from veadk.utils.logger import get_logger
|
|
19
|
+
from veadk.config import getenv
|
|
20
|
+
from pathlib import Path
|
|
21
|
+
|
|
22
|
+
logger = get_logger(__name__)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@click.command()
|
|
26
|
+
@click.option("--file", required=True, help="JSON file path containing dataset items")
|
|
27
|
+
@click.option("--cozeloop-workspace-id", default=None, help="CozeLoop workspace ID")
|
|
28
|
+
@click.option("--cozeloop-evalset-id", default=None, help="CozeLoop evaluation set ID")
|
|
29
|
+
@click.option(
|
|
30
|
+
"--cozeloop-api-key",
|
|
31
|
+
default=None,
|
|
32
|
+
help="CozeLoop API key (or set COZELOOP_API_KEY env var)",
|
|
33
|
+
)
|
|
34
|
+
def uploadevalset(
|
|
35
|
+
file: str,
|
|
36
|
+
cozeloop_workspace_id: str,
|
|
37
|
+
cozeloop_evalset_id: str,
|
|
38
|
+
cozeloop_api_key: str,
|
|
39
|
+
) -> None:
|
|
40
|
+
"""Upload dataset items to CozeLoop evaluation set."""
|
|
41
|
+
|
|
42
|
+
if not cozeloop_workspace_id:
|
|
43
|
+
cozeloop_workspace_id = getenv(
|
|
44
|
+
"OBSERVABILITY_OPENTELEMETRY_COZELOOP_SERVICE_NAME"
|
|
45
|
+
)
|
|
46
|
+
if not cozeloop_evalset_id:
|
|
47
|
+
cozeloop_evalset_id = getenv("OBSERVABILITY_OPENTELEMETRY_COZELOOP_EVALSET_ID")
|
|
48
|
+
if not cozeloop_api_key:
|
|
49
|
+
cozeloop_api_key = getenv("OBSERVABILITY_OPENTELEMETRY_COZELOOP_API_KEY")
|
|
50
|
+
|
|
51
|
+
# Read JSON file
|
|
52
|
+
file_path = Path(file)
|
|
53
|
+
if not file_path.exists():
|
|
54
|
+
logger.error(f"File not found: {file}")
|
|
55
|
+
return
|
|
56
|
+
|
|
57
|
+
logger.info(f"Reading dataset from {file}")
|
|
58
|
+
with open(file_path, "r", encoding="utf-8") as f:
|
|
59
|
+
data = json.load(f)
|
|
60
|
+
|
|
61
|
+
# Prepare items
|
|
62
|
+
items = []
|
|
63
|
+
for case in data.get("eval_cases", []):
|
|
64
|
+
conversation = case.get("conversation", [])
|
|
65
|
+
for turn in conversation:
|
|
66
|
+
user_text = (
|
|
67
|
+
turn.get("user_content", {}).get("parts", [{}])[0].get("text", "")
|
|
68
|
+
)
|
|
69
|
+
output_text = (
|
|
70
|
+
turn.get("final_response", {}).get("parts", [{}])[0].get("text", "")
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
items.append(
|
|
74
|
+
{
|
|
75
|
+
"turns": [
|
|
76
|
+
{
|
|
77
|
+
"field_datas": [
|
|
78
|
+
{
|
|
79
|
+
"name": "input",
|
|
80
|
+
"content": {
|
|
81
|
+
"content_type": "Text",
|
|
82
|
+
"text": user_text,
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
"name": "output",
|
|
87
|
+
"content": {
|
|
88
|
+
"content_type": "Text",
|
|
89
|
+
"text": output_text,
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
]
|
|
93
|
+
}
|
|
94
|
+
]
|
|
95
|
+
}
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
# Upload to CozeLoop
|
|
99
|
+
url = f"https://api.coze.cn/v1/loop/evaluation/evaluation_sets/{cozeloop_evalset_id}/items"
|
|
100
|
+
logger.info(
|
|
101
|
+
f"Uploading {len(items)} items to workspace_id={cozeloop_workspace_id} evalset_id={cozeloop_evalset_id}"
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
response = requests.post(
|
|
105
|
+
url=url,
|
|
106
|
+
headers={
|
|
107
|
+
"Authorization": f"Bearer {cozeloop_api_key}",
|
|
108
|
+
"Content-Type": "application/json",
|
|
109
|
+
"X-TT-ENV": "ppe_eval_openapi",
|
|
110
|
+
"x-use-ppe": "1",
|
|
111
|
+
},
|
|
112
|
+
json={
|
|
113
|
+
"workspace_id": cozeloop_workspace_id,
|
|
114
|
+
"is_allow_partial_add": True,
|
|
115
|
+
"is_skip_invalid_items": True,
|
|
116
|
+
"items": items,
|
|
117
|
+
},
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
if response.status_code == 200:
|
|
121
|
+
logger.info(
|
|
122
|
+
f"Successfully uploaded dataset to CozeLoop evalset {cozeloop_evalset_id}"
|
|
123
|
+
)
|
|
124
|
+
else:
|
|
125
|
+
logger.error(f"Failed to upload dataset: {response.text}")
|
veadk/cli/cli_web.py
CHANGED
|
@@ -56,10 +56,14 @@ def _get_ltm_from_env() -> LongTermMemory | None:
|
|
|
56
56
|
logger = get_logger(__name__)
|
|
57
57
|
|
|
58
58
|
long_term_memory_backend = os.getenv("LONG_TERM_MEMORY_BACKEND")
|
|
59
|
+
app_name = os.getenv("VEADK_WEB_APP_NAME", "")
|
|
60
|
+
user_id = os.getenv("VEADK_WEB_USER_ID", "")
|
|
59
61
|
|
|
60
62
|
if long_term_memory_backend:
|
|
61
63
|
logger.info(f"Long term memory: backend={long_term_memory_backend}")
|
|
62
|
-
return LongTermMemory(
|
|
64
|
+
return LongTermMemory(
|
|
65
|
+
backend=long_term_memory_backend, app_name=app_name, user_id=user_id
|
|
66
|
+
) # type: ignore
|
|
63
67
|
else:
|
|
64
68
|
logger.warning("No long term memory backend settings detected.")
|
|
65
69
|
return None
|
|
@@ -131,7 +135,13 @@ def patch_adkwebserver_disable_openapi():
|
|
|
131
135
|
|
|
132
136
|
@click.command()
|
|
133
137
|
@click.option("--host", default="127.0.0.1", help="Host to run the web server on")
|
|
134
|
-
|
|
138
|
+
@click.option(
|
|
139
|
+
"--app_name", default="", help="The `app_name` for initializing long term memory"
|
|
140
|
+
)
|
|
141
|
+
@click.option(
|
|
142
|
+
"--user_id", default="", help="The `user_id` for initializing long term memory"
|
|
143
|
+
)
|
|
144
|
+
def web(host: str, app_name: str, user_id: str) -> None:
|
|
135
145
|
"""Launch web with long term and short term memory."""
|
|
136
146
|
import os
|
|
137
147
|
from typing import Any
|
|
@@ -175,6 +185,9 @@ def web(host: str) -> None:
|
|
|
175
185
|
self.session_service = short_term_memory.session_service
|
|
176
186
|
self.memory_service = long_term_memory
|
|
177
187
|
|
|
188
|
+
os.environ["VEADK_WEB_APP_NAME"] = app_name
|
|
189
|
+
os.environ["VEADK_WEB_USER_ID"] = user_id
|
|
190
|
+
|
|
178
191
|
import google.adk.cli.adk_web_server
|
|
179
192
|
|
|
180
193
|
google.adk.cli.adk_web_server.AdkWebServer.__init__ = init_for_veadk
|
veadk/configs/model_configs.py
CHANGED
|
@@ -17,7 +17,7 @@ from functools import cached_property
|
|
|
17
17
|
|
|
18
18
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
19
19
|
|
|
20
|
-
from veadk.auth.veauth.ark_veauth import
|
|
20
|
+
from veadk.auth.veauth.ark_veauth import get_ark_token
|
|
21
21
|
from veadk.consts import (
|
|
22
22
|
DEFAULT_MODEL_AGENT_API_BASE,
|
|
23
23
|
DEFAULT_MODEL_AGENT_NAME,
|
|
@@ -39,7 +39,7 @@ class ModelConfig(BaseSettings):
|
|
|
39
39
|
|
|
40
40
|
@cached_property
|
|
41
41
|
def api_key(self) -> str:
|
|
42
|
-
return os.getenv("MODEL_AGENT_API_KEY") or
|
|
42
|
+
return os.getenv("MODEL_AGENT_API_KEY") or get_ark_token()
|
|
43
43
|
|
|
44
44
|
|
|
45
45
|
class EmbeddingModelConfig(BaseSettings):
|
|
@@ -56,7 +56,7 @@ class EmbeddingModelConfig(BaseSettings):
|
|
|
56
56
|
|
|
57
57
|
@cached_property
|
|
58
58
|
def api_key(self) -> str:
|
|
59
|
-
return os.getenv("MODEL_EMBEDDING_API_KEY") or
|
|
59
|
+
return os.getenv("MODEL_EMBEDDING_API_KEY") or get_ark_token()
|
|
60
60
|
|
|
61
61
|
|
|
62
62
|
class NormalEmbeddingModelConfig(BaseSettings):
|
veadk/consts.py
CHANGED
|
@@ -63,6 +63,15 @@ DEFAULT_TOS_BUCKET_NAME = "ark-tutorial"
|
|
|
63
63
|
DEFAULT_COZELOOP_SPACE_NAME = "VeADK Space"
|
|
64
64
|
|
|
65
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
|
+
|
|
66
68
|
DEFAULT_IMAGE_EDIT_MODEL_NAME = "doubao-seededit-3-0-i2i-250628"
|
|
69
|
+
DEFAULT_IMAGE_EDIT_MODEL_API_BASE = "https://ark.cn-beijing.volces.com/api/v3/"
|
|
70
|
+
|
|
67
71
|
DEFAULT_VIDEO_MODEL_NAME = "doubao-seedance-1-0-pro-250528"
|
|
72
|
+
DEFAULT_VIDEO_MODEL_API_BASE = "https://ark.cn-beijing.volces.com/api/v3/"
|
|
73
|
+
|
|
68
74
|
DEFAULT_IMAGE_GENERATE_MODEL_NAME = "doubao-seedream-4-0-250828"
|
|
75
|
+
DEFAULT_IMAGE_GENERATE_MODEL_API_BASE = "https://ark.cn-beijing.volces.com/api/v3/"
|
|
76
|
+
|
|
77
|
+
VEFAAS_IAM_CRIDENTIAL_PATH = "/var/run/secrets/iam/credential"
|
|
@@ -12,10 +12,11 @@
|
|
|
12
12
|
# See the License for the specific language governing permissions and
|
|
13
13
|
# limitations under the License.
|
|
14
14
|
|
|
15
|
-
from
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
from typing import Any, Callable, Literal, Union
|
|
16
18
|
|
|
17
19
|
from pydantic import BaseModel, Field
|
|
18
|
-
from typing_extensions import Union
|
|
19
20
|
|
|
20
21
|
from veadk.knowledgebase.backends.base_backend import BaseKnowledgebaseBackend
|
|
21
22
|
from veadk.knowledgebase.entry import KnowledgebaseEntry
|
|
@@ -54,11 +55,11 @@ def _get_backend_cls(backend: str) -> type[BaseKnowledgebaseBackend]:
|
|
|
54
55
|
raise ValueError(f"Unsupported knowledgebase backend: {backend}")
|
|
55
56
|
|
|
56
57
|
|
|
57
|
-
|
|
58
|
-
|
|
58
|
+
class KnowledgeBase(BaseModel):
|
|
59
|
+
name: str = "user_knowledgebase"
|
|
59
60
|
|
|
61
|
+
description: str = "This knowledgebase stores some user-related information."
|
|
60
62
|
|
|
61
|
-
class KnowledgeBase(BaseModel):
|
|
62
63
|
backend: Union[
|
|
63
64
|
Literal["local", "opensearch", "viking", "redis"], BaseKnowledgebaseBackend
|
|
64
65
|
] = "local"
|
|
@@ -73,9 +74,7 @@ class KnowledgeBase(BaseModel):
|
|
|
73
74
|
"""Configuration for the backend"""
|
|
74
75
|
|
|
75
76
|
top_k: int = 10
|
|
76
|
-
"""Number of top similar documents to retrieve during search
|
|
77
|
-
|
|
78
|
-
Default is 10."""
|
|
77
|
+
"""Number of top similar documents to retrieve during search"""
|
|
79
78
|
|
|
80
79
|
app_name: str = ""
|
|
81
80
|
|
|
@@ -85,38 +84,27 @@ class KnowledgeBase(BaseModel):
|
|
|
85
84
|
def model_post_init(self, __context: Any) -> None:
|
|
86
85
|
if isinstance(self.backend, BaseKnowledgebaseBackend):
|
|
87
86
|
self._backend = self.backend
|
|
87
|
+
self.index = self._backend.index
|
|
88
88
|
logger.info(
|
|
89
89
|
f"Initialized knowledgebase with provided backend instance {self._backend.__class__.__name__}"
|
|
90
90
|
)
|
|
91
91
|
return
|
|
92
92
|
|
|
93
|
-
#
|
|
94
|
-
if
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
)
|
|
98
|
-
|
|
99
|
-
# priority use index
|
|
100
|
-
if self.app_name and self.index:
|
|
101
|
-
logger.warning(
|
|
102
|
-
"`app_name` and `index` are both provided, using `index` as the knowledgebase index name."
|
|
103
|
-
)
|
|
93
|
+
# Once user define backend config, use it directly
|
|
94
|
+
if self.backend_config:
|
|
95
|
+
self._backend = _get_backend_cls(self.backend)(**self.backend_config)
|
|
96
|
+
return
|
|
104
97
|
|
|
105
|
-
|
|
106
|
-
if
|
|
107
|
-
|
|
108
|
-
logger.info(
|
|
109
|
-
f"Knowledgebase index is set to {self.index} (generated by the app_name: {self.app_name})."
|
|
110
|
-
)
|
|
98
|
+
self.index = self.index or self.app_name
|
|
99
|
+
if not self.index:
|
|
100
|
+
raise ValueError("Either `index` or `app_name` must be provided.")
|
|
111
101
|
|
|
112
102
|
logger.info(
|
|
113
|
-
f"Initializing knowledgebase: backend={self.backend} top_k={self.top_k}"
|
|
114
|
-
)
|
|
115
|
-
self._backend = _get_backend_cls(self.backend)(
|
|
116
|
-
index=self.index, **self.backend_config if self.backend_config else {}
|
|
103
|
+
f"Initializing knowledgebase: backend={self.backend} index={self.index} top_k={self.top_k}"
|
|
117
104
|
)
|
|
105
|
+
self._backend = _get_backend_cls(self.backend)(index=self.index)
|
|
118
106
|
logger.info(
|
|
119
|
-
f"Initialized knowledgebase with backend {self.
|
|
107
|
+
f"Initialized knowledgebase with backend {self.backend.__class__.__name__}"
|
|
120
108
|
)
|
|
121
109
|
|
|
122
110
|
def add_from_directory(self, directory: str, **kwargs) -> bool:
|
|
@@ -133,8 +121,7 @@ class KnowledgeBase(BaseModel):
|
|
|
133
121
|
|
|
134
122
|
def search(self, query: str, top_k: int = 0, **kwargs) -> list[KnowledgebaseEntry]:
|
|
135
123
|
"""Search knowledge from knowledgebase"""
|
|
136
|
-
if top_k
|
|
137
|
-
top_k = self.top_k
|
|
124
|
+
top_k = top_k if top_k != 0 else self.top_k
|
|
138
125
|
|
|
139
126
|
_entries = self._backend.search(query=query, top_k=top_k, **kwargs)
|
|
140
127
|
|