|
| 1 | +import os |
| 2 | +from typing import Literal, Tuple |
| 3 | +from game_sdk.game.custom_types import Function, FunctionResultStatus, Argument |
| 4 | +from membase_plugin_gamesdk.membase_plugin_gamesdk import MembasePlugin |
| 5 | +from game_sdk.game.chat_agent import Chat, ChatAgent |
| 6 | + |
| 7 | + |
| 8 | +# set your own account and agent name |
| 9 | +# or set environment variables |
| 10 | +default_account = "game_sdk_test" |
| 11 | +default_agent_name = "game_sdk_test_agent" |
| 12 | +default_conversation_id = "67fd033a0740eed72502b65e" |
| 13 | + |
| 14 | +game_api_key = os.environ.get("GAME_API_KEY") |
| 15 | +chat_agent = ChatAgent( |
| 16 | + prompt="You are a helpful assistant.", |
| 17 | + api_key=game_api_key, |
| 18 | +) |
| 19 | + |
| 20 | +membase_plugin = MembasePlugin( |
| 21 | + account=default_account, |
| 22 | + agent_name=default_agent_name, |
| 23 | + auto_upload_to_hub=True, |
| 24 | + preload_from_hub=False, |
| 25 | +) |
| 26 | + |
| 27 | + |
| 28 | +def save_memory_executable(membase_plugin: MembasePlugin, memory: str, memory_type: Literal["user", "assistant"]) -> Tuple[FunctionResultStatus, str, dict]: |
| 29 | + try: |
| 30 | + membase_plugin.add_memory(memory, memory_type) |
| 31 | + return FunctionResultStatus.DONE, "Memory added successfully", {} |
| 32 | + except Exception as e: |
| 33 | + return FunctionResultStatus.FAILED, str(e), {} |
| 34 | + |
| 35 | +def get_memory_executable(membase_plugin: MembasePlugin, recent_n: int = 10) -> Tuple[FunctionResultStatus, str, dict]: |
| 36 | + try: |
| 37 | + memory = membase_plugin.get_memory(recent_n=recent_n) |
| 38 | + result = "" |
| 39 | + for msg in memory: |
| 40 | + result += f"{msg.role}: {msg.content}\n" |
| 41 | + return FunctionResultStatus.DONE, result, {} |
| 42 | + except Exception as e: |
| 43 | + return FunctionResultStatus.FAILED, str(e), {} |
| 44 | + |
| 45 | +action_space = [ |
| 46 | + Function( |
| 47 | + fn_name="save_memory", |
| 48 | + fn_description="Save a memory to the membase database", |
| 49 | + args=[ |
| 50 | + Argument(name="memory", type="str", description="The memory to save"), |
| 51 | + Argument(name="memory_type", type=["user", "assistant"], description="The type of memory to save"), |
| 52 | + ], |
| 53 | + executable=lambda memory, memory_type: save_memory_executable(membase_plugin, memory, memory_type), |
| 54 | + ), |
| 55 | + Function( |
| 56 | + fn_name="get_memory", |
| 57 | + fn_description="Get the last n memories from the membase database", |
| 58 | + args=[ |
| 59 | + Argument(name="recent_n", type="int", description="The number of memories to retrieve"), |
| 60 | + ], |
| 61 | + executable=lambda recent_n: get_memory_executable(membase_plugin, recent_n), |
| 62 | + ) |
| 63 | +] |
| 64 | + |
| 65 | +chat = chat_agent.create_chat( |
| 66 | + partner_id=default_account, |
| 67 | + partner_name=default_agent_name, |
| 68 | + action_space=action_space, |
| 69 | +) |
| 70 | + |
| 71 | +membase_plugin.switch_conversation_id(default_conversation_id) |
| 72 | +membase_plugin.reload_memory(default_conversation_id) |
| 73 | + |
| 74 | +chat_continue = True |
| 75 | +while chat_continue: |
| 76 | + |
| 77 | + user_message = input("Enter a message: ") |
| 78 | + |
| 79 | + response = chat.next(user_message) |
| 80 | + |
| 81 | + if response.function_call: |
| 82 | + print(f"Function call: {response.function_call.fn_name}") |
| 83 | + |
| 84 | + if response.message: |
| 85 | + print(f"Response: {response.message}") |
| 86 | + |
| 87 | + if response.is_finished: |
| 88 | + chat_continue = False |
| 89 | + break |
| 90 | + |
| 91 | +print("Chat ended") |
| 92 | + |
0 commit comments