Skip to content

armgit / letta

Public Imported from GitHub
0

Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.

cleanup source loading code

Sarah Wooders35aa6b46b43d
diff --git a/memgpt/agent_store/storage.py b/memgpt/agent_store/storage.py
index 87414672b..752082688 100644
--- a/memgpt/agent_store/storage.py
+++ b/memgpt/agent_store/storage.py
@@ -115,13 +115,11 @@ class StorageConnector:
raise NotImplementedError(f"Storage type {storage_type} not implemented")
@staticmethod
- def get_archival_storage_connector(user_id, agent_id):
- config = MemGPTConfig.load()
+ def get_archival_storage_connector(config: MemGPTConfig, user_id, agent_id):
return StorageConnector.get_storage_connector(TableType.ARCHIVAL_MEMORY, config, user_id, agent_id)
@staticmethod
- def get_recall_storage_connector(user_id, agent_id):
- config = MemGPTConfig.load()
+ def get_recall_storage_connector(config: MemGPTConfig, user_id, agent_id):
return StorageConnector.get_storage_connector(TableType.RECALL_MEMORY, config, user_id, agent_id)
@abstractmethod
diff --git a/memgpt/autogen/memgpt_agent.py b/memgpt/autogen/memgpt_agent.py
index 838f347a7..458411dea 100644
--- a/memgpt/autogen/memgpt_agent.py
+++ b/memgpt/autogen/memgpt_agent.py
@@ -200,6 +200,7 @@ class MemGPTAgent(ConversableAgent):
concat_other_agent_messages=False,
is_termination_msg: Optional[Callable[[Dict], bool]] = None,
default_auto_reply: Optional[Union[str, Dict, None]] = "",
+ # TODO: pass in MemGPT config (needed to create DB connections)
):
super().__init__(name)
self.agent = agent
@@ -234,6 +235,7 @@ class MemGPTAgent(ConversableAgent):
self.agent.config.attach_data_source(data_source)
# reload agent with new data source
+ # TODO: @charles we will need to pass in the MemGPT config here to get the DB URIs (not contained in agent)
self.agent.persistence_manager.archival_memory.storage = StorageConnector.get_archival_storage_connector(
agent_config=self.agent.config
)
diff --git a/memgpt/cli/cli_load.py b/memgpt/cli/cli_load.py
index af2d89332..fe9a82436 100644
--- a/memgpt/cli/cli_load.py
+++ b/memgpt/cli/cli_load.py
@@ -32,6 +32,33 @@ from llama_index import (
app = typer.Typer()
+def insert_passages_into_source(passages: List[Passage], source_name: str, user_id: uuid.UUID, config: MemGPTConfig):
+
+ """Insert a list of passages into a source by updating storage connectors and metadata store"""
+ storage = StorageConnector.get_storage_connector(TableType.PASSAGES, config, user_id)
+ orig_size = storage.size()
+
+ # insert metadata store
+ ms = MetadataStore(config)
+ source = ms.get_source(user_id=user_id, source_name=source_name)
+ if not source:
+ # create new
+ source = Source(user_id=user_id, name=source_name, created_at=get_local_time())
+ ms.create_source(source)
+
+ # make sure user_id is set for passages
+ for passage in passages:
+ # TODO: attach source IDs
+ # passage.source_id = source.id
+ passage.user_id = user_id
+ passage.data_source = source_name
+
+ # add and save all passages
+ storage.insert_many(passages)
+ assert orig_size + len(passages) == storage.size(), f"Expected {orig_size + len(passages)} passages, got {storage.size()}"
+ storage.save()
+
+
def store_docs(name, docs, user_id=None, show_progress=True):
"""Common function for embedding and storing documents"""
@@ -51,9 +78,7 @@ def store_docs(name, docs, user_id=None, show_progress=True):
print(f"Source {name} for user {user.id} already exists")
# compute and record passages
- storage = StorageConnector.get_storage_connector(TableType.PASSAGES, config, user.id)
embed_model = embedding_model(user.default_embedding_config)
- orig_size = storage.size()
# use llama index to run embeddings code
with suppress_stdout():
@@ -83,15 +108,14 @@ def store_docs(name, docs, user_id=None, show_progress=True):
)
)
- # insert into storage
- storage.insert_many(passages)
- assert orig_size + len(passages) == storage.size(), f"Expected {orig_size + len(passages)} passages, got {storage.size()}"
- storage.save()
+ insert_passages_into_source(passages, name, user_id, config)
@app.command("index")
def load_index(
- name: str = typer.Option(help="Name of dataset to load."), dir: str = typer.Option(help="Path to directory containing index.")
+ name: str = typer.Option(help="Name of dataset to load."),
+ dir: str = typer.Option(help="Path to directory containing index."),
+ user_id: uuid.UUID = None,
):
"""Load a LlamaIndex saved VectorIndex into MemGPT"""
try:
@@ -110,11 +134,11 @@ def load_index(
passages.append(Passage(text=node.text, embedding=vector))
# create storage connector
- storage = StorageConnector.get_archival_storage_connector(name=name)
+ config = MemGPTConfig.load()
+ if user_id is None:
+ user_id = uuid.UUID(config.anon_clientid)
- # add and save all passages
- storage.insert_many(passages)
- storage.save()
+ insert_passages_into_source(passages, name, user_id, config)
except ValueError as e:
typer.secho(f"Failed to load index from provided information.\n{e}", fg=typer.colors.RED)
@@ -220,6 +244,7 @@ def load_vector_database(
table_name: str = typer.Option(help="Name of table containing data."),
text_column: str = typer.Option(help="Name of column containing text."),
embedding_column: str = typer.Option(help="Name of column containing embedding."),
+ user_id: uuid.UUID = None,
):
"""Load pre-computed embeddings into MemGPT from a database."""
@@ -252,9 +277,12 @@ def load_vector_database(
passages.append(Passage(text=text, embedding=embedding))
assert config.embedding_dim == len(embedding), f"Expected embedding dimension {config.embedding_dim}, got {len(embedding)}"
- # insert into storage
- storage = StorageConnector.get_archival_storage_connector(name=name)
- storage.insert_many(passages)
+ # create storage connector
+ config = MemGPTConfig.load()
+ if user_id is None:
+ user_id = uuid.UUID(config.anon_clientid)
+
+ insert_passages_into_source(passages, name, user_id, config)
except ValueError as e:
typer.secho(f"Failed to load vector database from provided information.\n{e}", fg=typer.colors.RED)
diff --git a/memgpt/server/server.py b/memgpt/server/server.py
index 43ebd8f6e..bbceea58d 100644
--- a/memgpt/server/server.py
+++ b/memgpt/server/server.py
@@ -513,9 +513,10 @@ class SyncServer(LockingServer):
return agent_config
- def get_server_config(self, user_id: str) -> dict:
+ def get_server_config(self) -> dict:
"""Return the base config"""
- base_config = vars(MemGPTConfig.load())
+ # TODO: do we need a seperate server config?
+ base_config = vars(self.config)
def clean_keys(config):
config_copy = config.copy()