-
Notifications
You must be signed in to change notification settings - Fork 87
feat: add membase plugin #99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| 3.10 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| # Membase Plugin for GAME SDK | ||
|
|
||
| A plugin for interacting with membase protocol through the GAME SDK. | ||
|
|
||
| ## Description | ||
|
|
||
| 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. | ||
|
|
||
| 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. | ||
|
|
||
| - support conversation switch | ||
| - support conversation pesistence, upload if auto_upload_to_hub is set, conversation content can be visit at: https://testnet.hub.membase.io/ | ||
| - support conversation preload from membase hub: https://testnet.hub.membase.io/ | ||
|
|
||
| ## Installation | ||
|
|
||
| ```bash | ||
| pip install -e plugins/membase | ||
| ``` | ||
|
|
||
| ## Configuration | ||
|
|
||
| The plugin requires the following environment variables to be set: | ||
|
|
||
| ```shell | ||
| MEMBASE_HUB=<Membase hub endpoint, default is 'https://testnet.hub.membase.io' > | ||
| MEMBASE_ACCOUNT=<Membase account address> | ||
| MEMBASE_ID=<your agent name> | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| ```python | ||
| import time | ||
| import uuid | ||
| from membase_plugin_gamesdk.membase_plugin_gamesdk import MembasePlugin | ||
|
|
||
| # set your own account and agent name | ||
| # or set environment variables | ||
| default_account = "game_sdk_test" | ||
| default_agent_name = "game_sdk_test_agent" | ||
| membase_plugin = MembasePlugin( | ||
| account=default_account, | ||
| agent_name=default_agent_name, | ||
| auto_upload_to_hub=True, | ||
| preload_from_hub=True, | ||
| ) | ||
|
|
||
| membase_plugin.add_memory("Hello, world!") | ||
| new_conversation_id = str(uuid.uuid4()) | ||
| membase_plugin.switch_conversation_id(new_conversation_id) | ||
| membase_plugin.add_memory("Hello, world! 2") | ||
| ``` | ||
|
|
||
| more in `test_membase.py` file |
45 changes: 45 additions & 0 deletions
45
plugins/membase/membase_plugin_gamesdk/membase_plugin_gamesdk.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import os | ||
| from typing import Literal, Optional | ||
| from membase.memory.multi_memory import MultiMemory | ||
| from membase.memory.message import Message | ||
|
|
||
| class MembasePlugin: | ||
| def __init__(self, account: Optional[str] = None, agent_name: Optional[str] = None, auto_upload_to_hub: bool = False, preload_from_hub: bool = False): | ||
| if not account: | ||
| self.account = os.getenv("MEMBASE_ACCOUNT") | ||
| else: | ||
| self.account = account | ||
| if not self.account: | ||
| raise ValueError("MEMBASE_ACCOUNT is not set and provided account is None") | ||
|
|
||
| if not agent_name: | ||
| self.id = os.getenv("MEMBASE_ID") | ||
| else: | ||
| self.id = agent_name | ||
| if not self.id: | ||
| self.id = self.account | ||
|
|
||
| self._multi_memory = MultiMemory( | ||
| membase_account=self.account, | ||
| auto_upload_to_hub=auto_upload_to_hub, | ||
| preload_from_hub=preload_from_hub, | ||
| ) | ||
|
|
||
| # memory_type: user,system,assistant | default: user | ||
| def add_memory(self, memory: str, memory_type: Literal["user", "system", "assistant"] = "user", conversation_id: Optional[str] = None): | ||
| msg = Message( | ||
| name=self.id, | ||
| content=memory, | ||
| role=memory_type, | ||
| ) | ||
| self._multi_memory.add(msg, conversation_id) | ||
|
|
||
| def get_memory(self, conversation_id: Optional[str] = None, recent_n: Optional[int] = None): | ||
| return self._multi_memory.get(conversation_id, recent_n) | ||
|
|
||
| def switch_conversation_id(self, conversation_id: Optional[str] = None): | ||
| self._multi_memory.update_conversation_id(conversation_id) | ||
|
|
||
| def reload_memory(self, conversation_id: str): | ||
| self._multi_memory.load_from_hub(conversation_id) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| [project] | ||
| name = "membase_plugin_gamesdk" | ||
| version = "0.1.0" | ||
| description = "Add your description here" | ||
| readme = "README.md" | ||
| requires-python = ">=3.10" | ||
| dependencies = [ | ||
| "membase>=0.1.5", | ||
| ] | ||
|
|
||
| [tool.uv.sources] | ||
| membase = { git = "https://github.com/unibaseio/membase.git" } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import time | ||
| import uuid | ||
| from membase_plugin_gamesdk.membase_plugin_gamesdk import MembasePlugin | ||
|
|
||
| # set your own account and agent name | ||
| # or set environment variables | ||
| default_account = "game_sdk_test" | ||
| default_agent_name = "game_sdk_test_agent" | ||
| membase_plugin = MembasePlugin( | ||
| account=default_account, | ||
| agent_name=default_agent_name, | ||
| auto_upload_to_hub=True, | ||
| preload_from_hub=True, | ||
| ) | ||
|
|
||
| membase_plugin.add_memory("Hello, world!") | ||
| new_conversation_id = str(uuid.uuid4()) | ||
| membase_plugin.switch_conversation_id(new_conversation_id) | ||
| membase_plugin.add_memory("Hello, world! 2") | ||
|
|
||
| time.sleep(3) | ||
|
|
||
| new_agent_name = "game_sdk_test_agent_new" | ||
| new_membase_plugin = MembasePlugin( | ||
| account=default_account, | ||
| agent_name=new_agent_name, | ||
| auto_upload_to_hub=True, | ||
| preload_from_hub=True, | ||
| ) | ||
|
|
||
| res = new_membase_plugin.get_memory(new_conversation_id, 1) | ||
| if res is None: | ||
| raise Exception("Failed to get memory none") | ||
| if len(res) != 1: | ||
| raise Exception("Failed to get memory") | ||
| if res[0].content != "Hello, world! 2": | ||
| raise Exception("Content is not correct") | ||
|
|
||
| new_membase_plugin.add_memory("Hello, world! 3") | ||
|
|
||
| print("Test passed") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.