|
| 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 __future__ import annotations |
| 16 | + |
| 17 | +import json |
| 18 | +from collections.abc import Iterable |
| 19 | + |
| 20 | +from langgraph.store.base import ( |
| 21 | + BaseStore, |
| 22 | + GetOp, |
| 23 | + ListNamespacesOp, |
| 24 | + Op, |
| 25 | + PutOp, |
| 26 | + Result, |
| 27 | + SearchOp, |
| 28 | +) |
| 29 | + |
| 30 | +from veadk.memory.long_term_memory_backends.vikingdb_memory_backend import ( |
| 31 | + VikingDBLTMBackend, |
| 32 | +) |
| 33 | + |
| 34 | + |
| 35 | +# mem0 |
| 36 | +# viking db |
| 37 | +class VikingMemoryStore(BaseStore): |
| 38 | + def __init__(self, index: str): |
| 39 | + # index = (index, user_id) |
| 40 | + # key = session_id |
| 41 | + self.index = index |
| 42 | + self._backend = VikingDBLTMBackend(index=index) |
| 43 | + |
| 44 | + def batch(self, ops: Iterable[Op]) -> list[Result]: |
| 45 | + # The batch/abatch methods are treated as internal. |
| 46 | + # Users should access via put/search/get/list_namespaces/etc. |
| 47 | + results = [] |
| 48 | + for op in ops: |
| 49 | + if isinstance(op, PutOp): |
| 50 | + self._apply_put_op(op) |
| 51 | + elif isinstance(op, GetOp): |
| 52 | + self._apply_get_op(op) |
| 53 | + elif isinstance(op, SearchOp): |
| 54 | + results.extend(self._apply_search_op(op)) |
| 55 | + # elif isinstance(op, ListNamespacesOp): |
| 56 | + # self._apply_list_namespaces_op(op) |
| 57 | + else: |
| 58 | + raise ValueError(f"Unknown op type: {type(op)}") |
| 59 | + |
| 60 | + return results |
| 61 | + |
| 62 | + def abatch( |
| 63 | + self, ops: Iterable[GetOp | SearchOp | PutOp | ListNamespacesOp] |
| 64 | + ) -> list[Result]: ... |
| 65 | + |
| 66 | + def _apply_put_op(self, op: PutOp) -> None: |
| 67 | + index, user_id = op.namespace |
| 68 | + session_id = op.key |
| 69 | + |
| 70 | + assert index == self._backend.index, ( |
| 71 | + "index must be the same as the backend index" |
| 72 | + ) |
| 73 | + |
| 74 | + value = op.value |
| 75 | + |
| 76 | + event_strings = [] |
| 77 | + |
| 78 | + for _, event in value.items(): |
| 79 | + event_strings.append(json.dumps(event)) |
| 80 | + |
| 81 | + if self._backend.save_memory( |
| 82 | + user_id=user_id, |
| 83 | + session_id=session_id, |
| 84 | + event_strings=event_strings, |
| 85 | + ): |
| 86 | + return None |
| 87 | + |
| 88 | + def _apply_get_op(self, op: GetOp): |
| 89 | + return ["Not implemented"] |
| 90 | + |
| 91 | + def _apply_search_op(self, op: SearchOp): |
| 92 | + index, user_id = op.namespace_prefix |
| 93 | + assert index == self._backend.index, ( |
| 94 | + "index must be the same as the backend index" |
| 95 | + ) |
| 96 | + |
| 97 | + query = op.query |
| 98 | + if not query: |
| 99 | + return [] |
| 100 | + |
| 101 | + value = self._backend.search_memory(user_id=user_id, query=query, top_k=1) |
| 102 | + return value |
0 commit comments