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.

fix: Enhancements in CommonVector and test_server for Handling Data (#933)

Adam405b3429a0a8
diff --git a/memgpt/agent_store/db.py b/memgpt/agent_store/db.py
index f7f5e0204..38cfef8cf 100644
--- a/memgpt/agent_store/db.py
+++ b/memgpt/agent_store/db.py
@@ -1,4 +1,5 @@
import os
+import base64
from sqlalchemy import create_engine, Column, String, BIGINT, select, inspect, text, JSON, BLOB, BINARY, ARRAY, DateTime
from sqlalchemy import func, or_, and_
from sqlalchemy import desc, asc
@@ -62,17 +63,23 @@ class CommonVector(TypeDecorator):
return dialect.type_descriptor(BINARY())
def process_bind_param(self, value, dialect):
- if value:
- assert isinstance(value, np.ndarray) or isinstance(value, list), f"Value must be of type np.ndarray or list, got {type(value)}"
- assert isinstance(value[0], float), f"Value must be of type float, got {type(value[0])}"
- return np.array(value).tobytes()
- else:
+ if value is None:
return value
+ # Ensure value is a numpy array
+ if isinstance(value, list):
+ value = np.array(value, dtype=np.float32)
+ # Serialize numpy array to bytes, then encode to base64 for universal compatibility
+ return base64.b64encode(value.tobytes())
def process_result_value(self, value, dialect):
if not value:
return value
- return np.frombuffer(value)
+ # Check database type and deserialize accordingly
+ if dialect.name == "sqlite":
+ # Decode from base64 and convert back to numpy array
+ value = base64.b64decode(value)
+ # For PostgreSQL, value is already in bytes
+ return np.frombuffer(value, dtype=np.float32)
# Custom serialization / de-serialization for JSON columns
diff --git a/memgpt/metadata.py b/memgpt/metadata.py
index 4d0873b1c..2741e02e1 100644
--- a/memgpt/metadata.py
+++ b/memgpt/metadata.py
@@ -208,8 +208,11 @@ class MetadataStore:
else:
raise ValueError(f"Invalid metadata storage type: {config.metadata_storage_type}")
- # TODO: check to see if table(s) need to be greated or not
+ # Ensure valid URI
+ if not self.uri:
+ raise ValueError("Database URI is not provided or is invalid.")
+ # Check if tables need to be created
self.engine = create_engine(self.uri)
Base.metadata.create_all(
self.engine, tables=[UserModel.__table__, AgentModel.__table__, SourceModel.__table__, AgentSourceMappingModel.__table__]
diff --git a/tests/test_server.py b/tests/test_server.py
index e5071cf02..fd3b65f44 100644
--- a/tests/test_server.py
+++ b/tests/test_server.py
@@ -1,6 +1,7 @@
import uuid
import os
import memgpt.utils as utils
+from dotenv import load_dotenv
utils.DEBUG = True
from memgpt.config import MemGPTConfig
@@ -12,13 +13,17 @@ from .utils import wipe_config, wipe_memgpt_home
def test_server():
+ load_dotenv()
wipe_memgpt_home()
+ # Use os.getenv with a fallback to os.environ.get
+ db_url = os.getenv("PGVECTOR_TEST_DB_URL") or os.environ.get("PGVECTOR_TEST_DB_URL")
+
if os.getenv("OPENAI_API_KEY"):
config = MemGPTConfig(
- archival_storage_uri=os.getenv("PGVECTOR_TEST_DB_URL"),
- recall_storage_uri=os.getenv("PGVECTOR_TEST_DB_URL"),
- metadata_storage_uri=os.getenv("PGVECTOR_TEST_DB_URL"),
+ archival_storage_uri=db_url,
+ recall_storage_uri=db_url,
+ metadata_storage_uri=db_url,
archival_storage_type="postgres",
recall_storage_type="postgres",
metadata_storage_type="postgres",
@@ -40,9 +45,9 @@ def test_server():
)
else: # hosted
config = MemGPTConfig(
- archival_storage_uri=os.getenv("PGVECTOR_TEST_DB_URL"),
- recall_storage_uri=os.getenv("PGVECTOR_TEST_DB_URL"),
- metadata_storage_uri=os.getenv("PGVECTOR_TEST_DB_URL"),
+ archival_storage_uri=db_url,
+ recall_storage_uri=db_url,
+ metadata_storage_uri=db_url,
archival_storage_type="postgres",
recall_storage_type="postgres",
metadata_storage_type="postgres",
@@ -61,6 +66,7 @@ def test_server():
),
)
credentials = MemGPTCredentials()
+
config.save()
credentials.save()