1313# limitations under the License.
1414
1515import json
16+ import os
1617import threading
17- from veadk . utils . misc import getenv
18+
1819from volcengine .ApiInfo import ApiInfo
1920from volcengine .auth .SignerV4 import SignerV4
2021from volcengine .base .Service import Service
2122from volcengine .Credentials import Credentials
2223from volcengine .ServiceInfo import ServiceInfo
2324
25+ from veadk .utils .misc import getenv
26+
2427
2528class VikingDBMemoryException (Exception ):
2629 def __init__ (self , code , request_id , message = None ):
@@ -56,7 +59,9 @@ def __init__(
5659 socket_timeout = 30 ,
5760 ):
5861 env_host = getenv (
59- "DATABASE_VIKINGMEM_BASE_URL" , default_value = None , allow_false_values = True
62+ "DATABASE_VIKINGMEM_BASE_URL" ,
63+ default_value = None ,
64+ allow_false_values = True ,
6065 )
6166 if env_host :
6267 if env_host .startswith ("http://" ):
@@ -85,7 +90,9 @@ def __init__(
8590 self .get_body ("Ping" , {}, json .dumps ({}))
8691 except Exception as e :
8792 raise VikingDBMemoryException (
88- 1000028 , "missed" , "host or region is incorrect: {}" .format (str (e ))
93+ 1000028 ,
94+ "missed" ,
95+ "host or region is incorrect: {}" .format (str (e )),
8996 ) from None
9097
9198 def setHeader (self , header ):
@@ -118,49 +125,70 @@ def get_api_info():
118125 "/api/memory/collection/create" ,
119126 {},
120127 {},
121- {"Accept" : "application/json" , "Content-Type" : "application/json" },
128+ {
129+ "Accept" : "application/json" ,
130+ "Content-Type" : "application/json" ,
131+ },
122132 ),
123133 "GetCollection" : ApiInfo (
124134 "POST" ,
125135 "/api/memory/collection/info" ,
126136 {},
127137 {},
128- {"Accept" : "application/json" , "Content-Type" : "application/json" },
138+ {
139+ "Accept" : "application/json" ,
140+ "Content-Type" : "application/json" ,
141+ },
129142 ),
130143 "DropCollection" : ApiInfo (
131144 "POST" ,
132145 "/api/memory/collection/delete" ,
133146 {},
134147 {},
135- {"Accept" : "application/json" , "Content-Type" : "application/json" },
148+ {
149+ "Accept" : "application/json" ,
150+ "Content-Type" : "application/json" ,
151+ },
136152 ),
137153 "UpdateCollection" : ApiInfo (
138154 "POST" ,
139155 "/api/memory/collection/update" ,
140156 {},
141157 {},
142- {"Accept" : "application/json" , "Content-Type" : "application/json" },
158+ {
159+ "Accept" : "application/json" ,
160+ "Content-Type" : "application/json" ,
161+ },
143162 ),
144163 "SearchMemory" : ApiInfo (
145164 "POST" ,
146165 "/api/memory/search" ,
147166 {},
148167 {},
149- {"Accept" : "application/json" , "Content-Type" : "application/json" },
168+ {
169+ "Accept" : "application/json" ,
170+ "Content-Type" : "application/json" ,
171+ },
150172 ),
151173 "AddMessages" : ApiInfo (
152174 "POST" ,
153175 "/api/memory/messages/add" ,
154176 {},
155177 {},
156- {"Accept" : "application/json" , "Content-Type" : "application/json" },
178+ {
179+ "Accept" : "application/json" ,
180+ "Content-Type" : "application/json" ,
181+ },
157182 ),
158183 "Ping" : ApiInfo (
159184 "GET" ,
160185 "/api/memory/ping" ,
161186 {},
162187 {},
163- {"Accept" : "application/json" , "Content-Type" : "application/json" },
188+ {
189+ "Accept" : "application/json" ,
190+ "Content-Type" : "application/json" ,
191+ },
164192 ),
165193 }
166194 return api_info
@@ -199,7 +227,9 @@ def get_body_exception(self, api, params, body):
199227 res_json = json .loads (e .args [0 ].decode ("utf-8" ))
200228 except Exception as e :
201229 raise VikingDBMemoryException (
202- 1000028 , "missed" , "json load res error, res:{}" .format (str (e ))
230+ 1000028 ,
231+ "missed" ,
232+ "json load res error, res:{}" .format (str (e )),
203233 ) from None
204234 code = res_json .get ("code" , 1000028 )
205235 request_id = res_json .get ("request_id" , 1000028 )
@@ -223,7 +253,9 @@ def get_exception(self, api, params):
223253 res_json = json .loads (e .args [0 ].decode ("utf-8" ))
224254 except Exception as e :
225255 raise VikingDBMemoryException (
226- 1000028 , "missed" , "json load res error, res:{}" .format (str (e ))
256+ 1000028 ,
257+ "missed" ,
258+ "json load res error, res:{}" .format (str (e )),
227259 ) from None
228260 code = res_json .get ("code" , 1000028 )
229261 request_id = res_json .get ("request_id" , 1000028 )
@@ -241,13 +273,18 @@ def create_collection(
241273 self ,
242274 collection_name ,
243275 description = "" ,
276+ project = "default" ,
244277 custom_event_type_schemas = [],
245278 custom_entity_type_schemas = [],
246279 builtin_event_types = [],
247280 builtin_entity_types = [],
248281 ):
249282 params = {
250283 "CollectionName" : collection_name ,
284+ "ProjectName" : project ,
285+ "CollectionType" : os .getenv (
286+ "DATABASE_VIKINGMEM_COLLECTION_TYPE" , "standard"
287+ ),
251288 "Description" : description ,
252289 "CustomEventTypeSchemas" : custom_event_type_schemas ,
253290 "CustomEntityTypeSchemas" : custom_entity_type_schemas ,
@@ -257,8 +294,8 @@ def create_collection(
257294 res = self .json ("CreateCollection" , {}, json .dumps (params ))
258295 return json .loads (res )
259296
260- def get_collection (self , collection_name ):
261- params = {"CollectionName" : collection_name }
297+ def get_collection (self , collection_name , project = "default" ):
298+ params = {"CollectionName" : collection_name , "ProjectName" : project }
262299 res = self .json ("GetCollection" , {}, json .dumps (params ))
263300 return json .loads (res )
264301
0 commit comments