Skip to content

Commit 978e799

Browse files
author
mossv2
committed
add membase plugin
1 parent 4844726 commit 978e799

File tree

7 files changed

+2928
-1
lines changed

7 files changed

+2928
-1
lines changed

plugins/membase/.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.10

plugins/membase/README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Membase Plugin for GAME SDK
2+
3+
A plugin for interacting with membase protocol through the GAME SDK.
4+
5+
## Description
6+
7+
Membase is a core component of the Unibase ecosystem. It stores historical information, interaction records, and persistent data of Agents, ensuring their continuity and traceability.
8+
9+
The membase plugin enables seamless integration with the membase protocol for decentralized storage. It provides functionality to upload memory to and reload it from the Unibase DA network.
10+
11+
- support conversation switch
12+
- support conversation pesistence, upload if auto_upload_to_hub is set, conversation content can be visit at: https://testnet.hub.membase.io/
13+
- support conversation preload from membase hub: https://testnet.hub.membase.io/
14+
15+
## Installation
16+
17+
```bash
18+
pip install -e plugins/membase
19+
```
20+
21+
## Configuration
22+
23+
The plugin requires the following environment variables to be set:
24+
25+
```shell
26+
MEMBASE_HUB=<Membase hub endpoint, default is 'https://testnet.hub.membase.io' >
27+
MEMBASE_ACCOUNT=<Membase account address>
28+
MEMBASE_ID=<your agent name>
29+
```
30+
31+
## Usage
32+
33+
```python
34+
import time
35+
import uuid
36+
from membase_plugin_gamesdk.membase_plugin_gamesdk import MembasePlugin
37+
38+
# set your own account and agent name
39+
# or set environment variables
40+
default_account = "game_sdk_test"
41+
default_agent_name = "game_sdk_test_agent"
42+
membase_plugin = MembasePlugin(
43+
account=default_account,
44+
agent_name=default_agent_name,
45+
auto_upload_to_hub=True,
46+
preload_from_hub=True,
47+
)
48+
49+
membase_plugin.add_memory("Hello, world!")
50+
new_conversation_id = str(uuid.uuid4())
51+
membase_plugin.switch_conversation_id(new_conversation_id)
52+
membase_plugin.add_memory("Hello, world! 2")
53+
```
54+
55+
more in `test_membase.py` file
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import os
2+
from typing import Literal, Optional
3+
from membase.memory.multi_memory import MultiMemory
4+
from membase.memory.message import Message
5+
6+
class MembasePlugin:
7+
def __init__(self, account: Optional[str] = None, agent_name: Optional[str] = None, auto_upload_to_hub: bool = False, preload_from_hub: bool = False):
8+
if not account:
9+
self.account = os.getenv("MEMBASE_ACCOUNT")
10+
else:
11+
self.account = account
12+
if not self.account:
13+
raise ValueError("MEMBASE_ACCOUNT is not set and provided account is None")
14+
15+
if not agent_name:
16+
self.id = os.getenv("MEMBASE_ID")
17+
else:
18+
self.id = agent_name
19+
if not self.id:
20+
self.id = self.account
21+
22+
self._multi_memory = MultiMemory(
23+
membase_account=self.account,
24+
auto_upload_to_hub=auto_upload_to_hub,
25+
preload_from_hub=preload_from_hub,
26+
)
27+
28+
# memory_type: user,system,assistant | default: user
29+
def add_memory(self, memory: str, memory_type: Literal["user", "system", "assistant"] = "user", conversation_id: Optional[str] = None):
30+
msg = Message(
31+
name=self.id,
32+
content=memory,
33+
role=memory_type,
34+
)
35+
self._multi_memory.add(msg, conversation_id)
36+
37+
def get_memory(self, conversation_id: Optional[str] = None, recent_n: Optional[int] = None):
38+
return self._multi_memory.get(conversation_id, recent_n)
39+
40+
def switch_conversation_id(self, conversation_id: Optional[str] = None):
41+
self._multi_memory.update_conversation_id(conversation_id)
42+
43+
def reload_memory(self, conversation_id: str):
44+
self._multi_memory.load_from_hub(conversation_id)
45+

plugins/membase/pyproject.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[project]
2+
name = "membase_plugin_gamesdk"
3+
version = "0.1.0"
4+
description = "Add your description here"
5+
readme = "README.md"
6+
requires-python = ">=3.10"
7+
dependencies = [
8+
"membase>=0.1.5",
9+
]
10+
11+
[tool.uv.sources]
12+
membase = { git = "https://github.com/unibaseio/membase.git" }

plugins/membase/test_membase.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import time
2+
import uuid
3+
from membase_plugin_gamesdk.membase_plugin_gamesdk import MembasePlugin
4+
5+
# set your own account and agent name
6+
# or set environment variables
7+
default_account = "game_sdk_test"
8+
default_agent_name = "game_sdk_test_agent"
9+
membase_plugin = MembasePlugin(
10+
account=default_account,
11+
agent_name=default_agent_name,
12+
auto_upload_to_hub=True,
13+
preload_from_hub=True,
14+
)
15+
16+
membase_plugin.add_memory("Hello, world!")
17+
new_conversation_id = str(uuid.uuid4())
18+
membase_plugin.switch_conversation_id(new_conversation_id)
19+
membase_plugin.add_memory("Hello, world! 2")
20+
21+
time.sleep(3)
22+
23+
new_agent_name = "game_sdk_test_agent_new"
24+
new_membase_plugin = MembasePlugin(
25+
account=default_account,
26+
agent_name=new_agent_name,
27+
auto_upload_to_hub=True,
28+
preload_from_hub=True,
29+
)
30+
31+
res = new_membase_plugin.get_memory(new_conversation_id, 1)
32+
if res is None:
33+
raise Exception("Failed to get memory none")
34+
if len(res) != 1:
35+
raise Exception("Failed to get memory")
36+
if res[0].content != "Hello, world! 2":
37+
raise Exception("Content is not correct")
38+
39+
new_membase_plugin.add_memory("Hello, world! 3")
40+
41+
print("Test passed")

pyproject.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,7 @@ dependencies = [
3131

3232
[project.urls]
3333
"Homepage" = "https://github.com/game-by-virtuals/game-python"
34-
"Bug Tracker" = "https://github.com/game-by-virtuals/game-python"
34+
"Bug Tracker" = "https://github.com/game-by-virtuals/game-python"
35+
36+
[tool.uv.workspace]
37+
members = ["plugins/membase"]

0 commit comments

Comments
 (0)