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: move to `TokenTextSplitter` for more reliable chunking (#1098)

Sarah Wooders5e754df9d78d
diff --git a/configs/memgpt_hosted.json b/configs/memgpt_hosted.json
index c1203b484..fb575c106 100644
--- a/configs/memgpt_hosted.json
+++ b/configs/memgpt_hosted.json
@@ -9,4 +9,4 @@
"embedding_model": "BAAI/bge-large-en-v1.5",
"embedding_dim": 1024,
"embedding_chunk_size": 300
-}
\ No newline at end of file
+}
diff --git a/memgpt/cli/cli_load.py b/memgpt/cli/cli_load.py
index 105ec9822..55cbac5e1 100644
--- a/memgpt/cli/cli_load.py
+++ b/memgpt/cli/cli_load.py
@@ -98,19 +98,22 @@ def load_directory(
ms = MetadataStore(config)
source = Source(name=name, user_id=user_id)
ms.create_source(source)
- print("created source", name, str(user_id))
- print("listing source", user_id, ms.list_sources(user_id=user_id))
passage_storage = StorageConnector.get_storage_connector(TableType.PASSAGES, config, user_id)
# TODO: also get document store
# ingest data into passage/document store
- num_passages, num_documents = load_data(
- connector=connector,
- source=source,
- embedding_config=config.default_embedding_config,
- document_store=None,
- passage_store=passage_storage,
- )
+ try:
+ num_passages, num_documents = load_data(
+ connector=connector,
+ source=source,
+ embedding_config=config.default_embedding_config,
+ document_store=None,
+ passage_store=passage_storage,
+ )
+ except Exception as e:
+ typer.secho(f"Failed to load data from provided information.\n{e}", fg=typer.colors.RED)
+ ms.delete_source(source_id=source.id)
+
print(f"Loaded {num_passages} passages and {num_documents} documents from {name}")
except ValueError as e:
@@ -212,15 +215,18 @@ def load_vector_database(
# TODO: also get document store
# ingest data into passage/document store
- num_passages, num_documents = load_data(
- connector=connector,
- source=source,
- embedding_config=config.default_embedding_config,
- document_store=None,
- passage_store=passage_storage,
- chunk_size=1000,
- )
- print(f"Loaded {num_passages} passages and {num_documents} documents from {name}")
+ try:
+ num_passages, num_documents = load_data(
+ connector=connector,
+ source=source,
+ embedding_config=config.default_embedding_config,
+ document_store=None,
+ passage_store=passage_storage,
+ )
+ print(f"Loaded {num_passages} passages and {num_documents} documents from {name}")
+ except Exception as e:
+ typer.secho(f"Failed to load data from provided information.\n{e}", fg=typer.colors.RED)
+ ms.delete_source(source_id=source.id)
except ValueError as e:
typer.secho(f"Failed to load VectorDB from provided information.\n{e}", fg=typer.colors.RED)
diff --git a/memgpt/configs/memgpt_hosted.json b/memgpt/configs/memgpt_hosted.json
index c1203b484..fb575c106 100644
--- a/memgpt/configs/memgpt_hosted.json
+++ b/memgpt/configs/memgpt_hosted.json
@@ -9,4 +9,4 @@
"embedding_model": "BAAI/bge-large-en-v1.5",
"embedding_dim": 1024,
"embedding_chunk_size": 300
-}
\ No newline at end of file
+}
diff --git a/memgpt/data_sources/connectors.py b/memgpt/data_sources/connectors.py
index ce55fc8ab..eaef6cc11 100644
--- a/memgpt/data_sources/connectors.py
+++ b/memgpt/data_sources/connectors.py
@@ -46,7 +46,7 @@ def load_data(
document_store.insert(document)
# generate passages
- for passage_text, passage_metadata in connector.generate_passages([document]):
+ for passage_text, passage_metadata in connector.generate_passages([document], chunk_size=embedding_config.embedding_chunk_size):
embedding = embed_model.get_text_embedding(passage_text)
passage = Passage(
id=create_uuid_from_string(f"{str(source.id)}_{passage_text}"),
@@ -109,9 +109,10 @@ class DirectoryConnector(DataConnector):
def generate_passages(self, documents: List[Document], chunk_size: int = 1024) -> Iterator[Tuple[str, Dict]]: # -> Iterator[Passage]:
# use llama index to run embeddings code
- from llama_index.core.node_parser import SentenceSplitter
+ # from llama_index.core.node_parser import SentenceSplitter
+ from llama_index.core.node_parser import TokenTextSplitter
- parser = SentenceSplitter(chunk_size=chunk_size)
+ parser = TokenTextSplitter(chunk_size=chunk_size)
for document in documents:
llama_index_docs = [LlamaIndexDocument(text=document.text, metadata=document.metadata)]
nodes = parser.get_nodes_from_documents(llama_index_docs)
diff --git a/pyproject.toml b/pyproject.toml
index ad76e52b4..68771e975 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -51,7 +51,7 @@ pexpect = {version = "^4.9.0", optional = true}
pyright = {version = "^1.1.347", optional = true}
llama-index = "^0.10.6"
llama-index-embeddings-openai = "^0.1.1"
-llama-index-embeddings-huggingface = "^0.1.4"
+llama-index-embeddings-huggingface = {version = "^0.1.4", optional = true}
python-box = "^7.1.1"
pytest-order = {version = "^1.2.0", optional = true}
sqlmodel = "^0.0.16"
@@ -59,6 +59,7 @@ llama-index-embeddings-azure-openai = "^0.1.6"
autoflake = {version = "^2.3.0", optional = true}
[tool.poetry.extras]
+local = ["llama-index-embeddings-huggingface"]
postgres = ["pgvector", "pg8000"]
dev = ["pytest", "pytest-asyncio", "pexpect", "black", "pre-commit", "datasets", "pyright", "pytest-order", "autoflake"]
server = ["websockets", "fastapi", "uvicorn"]