Skip to content

Commit

Permalink
feat: migration script for version 0.3.18 (#1541)
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahwooders authored Jul 14, 2024
2 parents 02eae76 + 38e082c commit 8795e93
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 4 deletions.
12 changes: 8 additions & 4 deletions memgpt/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -700,9 +700,13 @@ def __init__(

# agents

def list_agents(self):
def list_agents(self) -> List[AgentState]:
self.interface.clear()
return self.server.list_agents(user_id=self.user_id)

# TODO: fix the server function
# return self.server.list_agents(user_id=self.user_id)

return self.server.ms.list_agents(user_id=self.user_id)

def agent_exists(self, agent_id: Optional[str] = None, agent_name: Optional[str] = None) -> bool:
if not (agent_id or agent_name):
Expand All @@ -711,9 +715,9 @@ def agent_exists(self, agent_id: Optional[str] = None, agent_name: Optional[str]
raise ValueError(f"Only one of agent_id or agent_name can be provided")
existing = self.list_agents()
if agent_id:
return agent_id in [agent["id"] for agent in existing["agents"]]
return str(agent_id) in [str(agent.id) for agent in existing]
else:
return agent_name in [agent["name"] for agent in existing["agents"]]
return agent_name in [str(agent.name) for agent in existing]

def create_agent(
self,
Expand Down
84 changes: 84 additions & 0 deletions scripts/migrate_0.3.18.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import os
import uuid

from sqlalchemy import MetaData, Table, create_engine

from memgpt import create_client
from memgpt.config import MemGPTConfig
from memgpt.data_types import AgentState, EmbeddingConfig, LLMConfig
from memgpt.metadata import MetadataStore
from memgpt.presets.presets import add_default_tools
from memgpt.prompts import gpt_system

# Replace this with your actual database connection URL
config = MemGPTConfig.load()
if config.recall_storage_type == "sqlite":
DATABASE_URL = "sqlite:///" + os.path.join(config.recall_storage_path, "sqlite.db")
else:
DATABASE_URL = config.recall_storage_uri
print(DATABASE_URL)
engine = create_engine(DATABASE_URL)
metadata = MetaData()

# defaults
system_prompt = gpt_system.get_system_text("memgpt_chat")

# Reflect the existing table
table = Table("agents", metadata, autoload_with=engine)


# get all agent rows
agent_states = []
with engine.connect() as conn:
agents = conn.execute(table.select()).fetchall()
for agent in agents:
# print(agent)
id = uuid.UUID(agent[0])
user_id = uuid.UUID(agent[1])
name = agent[2]
persona = agent[3]
human = agent[4]
system = agent[5]
preset = agent[6]
created_at = agent[7]
llm_config = LLMConfig(**agent[8])
embedding_config = EmbeddingConfig(**agent[9])
state = agent[10]
tools = agent[11]

state["memory"] = {"human": human, "persona": persona}

agent_state = AgentState(
id=id,
user_id=user_id,
name=name,
system=system,
created_at=created_at,
llm_config=llm_config,
embedding_config=embedding_config,
state=state,
tools=tools,
_metadata={"human": "migrated", "persona": "migrated"},
)

agent_states.append(agent_state)

# remove agents table
agents_model = Table("agents", metadata, autoload_with=engine)
agents_model.drop(engine)

# re-create tables and add default tools
ms = MetadataStore(config)
add_default_tools(None, ms)

for agent in agent_states:
ms.create_agent(agent)
print(f"Agent {agent.name} migrated successfully!")

# add another agent to create core memory tool
client = create_client()
dummy_agent = client.create_agent(name="dummy_agent")
tools = client.list_tools()
assert "core_memory_append" in [tool.name for tool in tools]

print("Migration completed successfully!")

0 comments on commit 8795e93

Please sign in to comment.