Skip to content

Commit 92f08c0

Browse files
committed
feat: add vikingmem project and region env
1 parent 693cf04 commit 92f08c0

File tree

4 files changed

+68
-22
lines changed

4 files changed

+68
-22
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "veadk-python"
3-
version = "0.2.35"
3+
version = "0.2.36"
44
description = "Volcengine agent development kit, integrations with Volcengine cloud services."
55
readme = "README.md"
66
requires-python = ">=3.10"

veadk/integrations/ve_viking_db_memory/ve_viking_db_memory.py

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414

1515
import json
1616
import threading
17-
from veadk.utils.misc import getenv
17+
1818
from volcengine.ApiInfo import ApiInfo
1919
from volcengine.auth.SignerV4 import SignerV4
2020
from volcengine.base.Service import Service
2121
from volcengine.Credentials import Credentials
2222
from volcengine.ServiceInfo import ServiceInfo
2323

24+
from veadk.utils.misc import getenv
25+
2426

2527
class VikingDBMemoryException(Exception):
2628
def __init__(self, code, request_id, message=None):
@@ -56,7 +58,9 @@ def __init__(
5658
socket_timeout=30,
5759
):
5860
env_host = getenv(
59-
"DATABASE_VIKINGMEM_BASE_URL", default_value=None, allow_false_values=True
61+
"DATABASE_VIKINGMEM_BASE_URL",
62+
default_value=None,
63+
allow_false_values=True,
6064
)
6165
if env_host:
6266
if env_host.startswith("http://"):
@@ -85,7 +89,9 @@ def __init__(
8589
self.get_body("Ping", {}, json.dumps({}))
8690
except Exception as e:
8791
raise VikingDBMemoryException(
88-
1000028, "missed", "host or region is incorrect: {}".format(str(e))
92+
1000028,
93+
"missed",
94+
"host or region is incorrect: {}".format(str(e)),
8995
) from None
9096

9197
def setHeader(self, header):
@@ -118,49 +124,70 @@ def get_api_info():
118124
"/api/memory/collection/create",
119125
{},
120126
{},
121-
{"Accept": "application/json", "Content-Type": "application/json"},
127+
{
128+
"Accept": "application/json",
129+
"Content-Type": "application/json",
130+
},
122131
),
123132
"GetCollection": ApiInfo(
124133
"POST",
125134
"/api/memory/collection/info",
126135
{},
127136
{},
128-
{"Accept": "application/json", "Content-Type": "application/json"},
137+
{
138+
"Accept": "application/json",
139+
"Content-Type": "application/json",
140+
},
129141
),
130142
"DropCollection": ApiInfo(
131143
"POST",
132144
"/api/memory/collection/delete",
133145
{},
134146
{},
135-
{"Accept": "application/json", "Content-Type": "application/json"},
147+
{
148+
"Accept": "application/json",
149+
"Content-Type": "application/json",
150+
},
136151
),
137152
"UpdateCollection": ApiInfo(
138153
"POST",
139154
"/api/memory/collection/update",
140155
{},
141156
{},
142-
{"Accept": "application/json", "Content-Type": "application/json"},
157+
{
158+
"Accept": "application/json",
159+
"Content-Type": "application/json",
160+
},
143161
),
144162
"SearchMemory": ApiInfo(
145163
"POST",
146164
"/api/memory/search",
147165
{},
148166
{},
149-
{"Accept": "application/json", "Content-Type": "application/json"},
167+
{
168+
"Accept": "application/json",
169+
"Content-Type": "application/json",
170+
},
150171
),
151172
"AddMessages": ApiInfo(
152173
"POST",
153174
"/api/memory/messages/add",
154175
{},
155176
{},
156-
{"Accept": "application/json", "Content-Type": "application/json"},
177+
{
178+
"Accept": "application/json",
179+
"Content-Type": "application/json",
180+
},
157181
),
158182
"Ping": ApiInfo(
159183
"GET",
160184
"/api/memory/ping",
161185
{},
162186
{},
163-
{"Accept": "application/json", "Content-Type": "application/json"},
187+
{
188+
"Accept": "application/json",
189+
"Content-Type": "application/json",
190+
},
164191
),
165192
}
166193
return api_info
@@ -199,7 +226,9 @@ def get_body_exception(self, api, params, body):
199226
res_json = json.loads(e.args[0].decode("utf-8"))
200227
except Exception as e:
201228
raise VikingDBMemoryException(
202-
1000028, "missed", "json load res error, res:{}".format(str(e))
229+
1000028,
230+
"missed",
231+
"json load res error, res:{}".format(str(e)),
203232
) from None
204233
code = res_json.get("code", 1000028)
205234
request_id = res_json.get("request_id", 1000028)
@@ -223,7 +252,9 @@ def get_exception(self, api, params):
223252
res_json = json.loads(e.args[0].decode("utf-8"))
224253
except Exception as e:
225254
raise VikingDBMemoryException(
226-
1000028, "missed", "json load res error, res:{}".format(str(e))
255+
1000028,
256+
"missed",
257+
"json load res error, res:{}".format(str(e)),
227258
) from None
228259
code = res_json.get("code", 1000028)
229260
request_id = res_json.get("request_id", 1000028)
@@ -241,13 +272,15 @@ def create_collection(
241272
self,
242273
collection_name,
243274
description="",
275+
project="default",
244276
custom_event_type_schemas=[],
245277
custom_entity_type_schemas=[],
246278
builtin_event_types=[],
247279
builtin_entity_types=[],
248280
):
249281
params = {
250282
"CollectionName": collection_name,
283+
"ProjectName": project,
251284
"Description": description,
252285
"CustomEventTypeSchemas": custom_event_type_schemas,
253286
"CustomEntityTypeSchemas": custom_entity_type_schemas,

veadk/memory/long_term_memory_backends/vikingdb_memory_backend.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
from pydantic import Field
2323
from typing_extensions import override
24+
from vikingdb import IAM
25+
from vikingdb.memory import VikingMem
2426

2527
import veadk.config # noqa E401
2628
from veadk.auth.veauth.utils import get_credential_from_vefaas_iam
@@ -30,9 +32,6 @@
3032
from veadk.memory.long_term_memory_backends.base_backend import (
3133
BaseLongTermMemoryBackend,
3234
)
33-
from vikingdb import IAM
34-
from vikingdb.memory import VikingMem
35-
3635
from veadk.utils.logger import get_logger
3736

3837
logger = get_logger(__name__)
@@ -49,9 +48,16 @@ class VikingDBLTMBackend(BaseLongTermMemoryBackend):
4948

5049
session_token: str = ""
5150

52-
region: str = "cn-beijing"
51+
region: str = Field(
52+
default_factory=lambda: os.getenv("DATABASE_VIKINGMEM_REGION") or "cn-beijing"
53+
)
5354
"""VikingDB memory region"""
5455

56+
volcengine_project: str = Field(
57+
default_factory=lambda: os.getenv("DATABASE_VIKINGMEM_PROJECT") or "default"
58+
)
59+
"""VikingDB memory project"""
60+
5561
memory_type: list[str] = Field(default_factory=list)
5662

5763
def model_post_init(self, __context: Any) -> None:
@@ -86,8 +92,10 @@ def precheck_index_naming(self):
8692

8793
def _collection_exist(self) -> bool:
8894
try:
89-
client = self._get_client()
90-
client.get_collection(collection_name=self.index)
95+
client = self._get_sdk_client()
96+
client.get_collection(
97+
collection_name=self.index, project_name=self.volcengine_project
98+
)
9199
logger.info(f"Collection {self.index} exist.")
92100
return True
93101
except Exception:
@@ -101,6 +109,7 @@ def _create_collection(self) -> None:
101109
client = self._get_client()
102110
response = client.create_collection(
103111
collection_name=self.index,
112+
project=self.volcengine_project,
104113
description="Created by Volcengine Agent Development Kit VeADK",
105114
builtin_event_types=self.memory_type,
106115
)
@@ -156,7 +165,9 @@ def save_memory(self, user_id: str, event_strings: list[str], **kwargs) -> bool:
156165
)
157166

158167
client = self._get_sdk_client()
159-
collection = client.get_collection(collection_name=self.index)
168+
collection = client.get_collection(
169+
collection_name=self.index, project_name=self.volcengine_project
170+
)
160171
response = collection.add_session(
161172
session_id=session_id,
162173
messages=messages,
@@ -181,7 +192,9 @@ def search_memory(
181192
)
182193

183194
client = self._get_sdk_client()
184-
collection = client.get_collection(collection_name=self.index)
195+
collection = client.get_collection(
196+
collection_name=self.index, project_name=self.volcengine_project
197+
)
185198
response = collection.search_memory(
186199
query=query,
187200
filter=filter,

veadk/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
VERSION = "0.2.35"
15+
VERSION = "0.2.36"

0 commit comments

Comments
 (0)