Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
Avoid usage of Vector type for when not using postgres
Sarah Wooders8b5fe4f38373
diff --git a/memgpt/connectors/db.py b/memgpt/connectors/db.pyindex 7ae77eed2..611e52152 100644--- a/memgpt/connectors/db.py+++ b/memgpt/connectors/db.py@@ -1,10 +1,8 @@-from pgvector.psycopg import register_vectorimport os-from pgvector.sqlalchemy import Vectorimport psycopg-from sqlalchemy import create_engine, Column, String, BIGINT, select, inspect, text, JSON+from sqlalchemy import create_engine, Column, String, BIGINT, select, inspect, text, JSON, BLOBfrom sqlalchemy import funcfrom sqlalchemy.orm import sessionmaker, mapped_columnfrom sqlalchemy.ext.declarative import declarative_base@@ -55,10 +53,26 @@ class CommonUUID(TypeDecorator):return uuid.UUID(value)+class CommonVector(TypeDecorator):++ """Common type for representing vectors in SQLite"""++ impl = BLOB++ def load_dialect_impl(self, dialect):+ return dialect.type_descriptor(BLOB())++ def process_bind_param(self, value, dialect):+ return np.array(value).tobytes()++ def process_result_value(self, value, dialect):+ return np.frombuffer(value, dtype=np.float32)++Base = declarative_base()-def get_db_model(table_name: str, table_type: TableType):+def get_db_model(table_name: str, table_type: TableType, dialect="postgresql"):config = MemGPTConfig.load()if table_type == TableType.ARCHIVAL_MEMORY or table_type == TableType.PASSAGES:@@ -77,7 +91,14 @@ def get_db_model(table_name: str, table_type: TableType):doc_id = Column(String)agent_id = Column(String)data_source = Column(String) # agent_name if agent, data_source name if from data source- embedding = mapped_column(Vector(config.embedding_dim))+ # embedding = mapped_column(CommonVector(config.embedding_dim))++ if dialect == "postgresql":+ from pgvector.sqlalchemy import Vector++ embedding = mapped_column(Vector(config.embedding_dim))+ else:+ embedding = Column(CommonVector)# metadata_ = Column(mutable_json_type(dbtype=JSONB, nested=True)) # more performance, but not supported for sqlitemetadata_ = Column(MutableJson)@@ -127,7 +148,14 @@ def get_db_model(table_name: str, table_type: TableType):# tool call response infotool_call_id = Column(String)- embedding = mapped_column(Vector(config.embedding_dim))+ # embedding = mapped_column(CommonVector(config.embedding_dim))+ # embedding= Column(CommonVector)+ if dialect == "postgresql":+ from pgvector.sqlalchemy import Vector++ embedding = mapped_column(Vector(config.embedding_dim))+ else:+ embedding = Column(CommonVector)# Add a datetime column, with default value as the current timecreated_at = Column(DateTime(timezone=True), server_default=func.now())@@ -366,7 +394,7 @@ class SQLLiteStorageConnector(SQLStorageConnector):self.db_model = get_db_model(self.table_name, table_type)# Create the SQLAlchemy engine- self.db_model = get_db_model(self.table_name, table_type)+ self.db_model = get_db_model(self.table_name, table_type, dialect="sqlite")self.engine = create_engine(f"sqlite:///{self.path}")Base.metadata.create_all(self.engine) # Create the table if it doesn't existself.Session = sessionmaker(bind=self.engine)diff --git a/tests/test_storage.py b/tests/test_storage.pyindex 8aea809c3..0aaae5af5 100644--- a/tests/test_storage.py+++ b/tests/test_storage.py@@ -44,11 +44,16 @@ def generate_passages(embed_model):# Data generation functions: Messages-def generate_messages():+def generate_messages(embed_model):"""Generate list of 3 Message objects"""messages = []for text, date, role, agent_id, id in zip(texts, dates, roles, agent_ids, ids):- messages.append(Message(user_id=user_id, text=text, agent_id=agent_id, role=role, created_at=date, id=id, model="gpt4"))+ embedding = None+ if embed_model:+ embedding = embed_model.get_text_embedding(text)+ messages.append(+ Message(user_id=user_id, text=text, agent_id=agent_id, role=role, created_at=date, id=id, model="gpt4", embedding=embedding)+ )print(messages[-1].text)return messages@@ -124,7 +129,7 @@ def test_storage(storage_connector, table_type):if table_type == TableType.ARCHIVAL_MEMORY:records = generate_passages(embed_model)elif table_type == TableType.RECALL_MEMORY:- records = generate_messages()+ records = generate_messages(embed_model)else:raise NotImplementedError(f"Table type {table_type} not implemented")