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: Fix load bug (#795)

Jim Lloyd465f7af84751
diff --git a/memgpt/cli/cli_load.py b/memgpt/cli/cli_load.py
index ca832ee60..37453a7e7 100644
--- a/memgpt/cli/cli_load.py
+++ b/memgpt/cli/cli_load.py
@@ -171,12 +171,16 @@ def load_index(
typer.secho(f"Failed to load index from provided information.\n{e}", fg=typer.colors.RED)
+default_extensions = ".txt,.md,.pdf"
+
+
@app.command("directory")
def load_directory(
name: str = typer.Option(help="Name of dataset to load."),
input_dir: str = typer.Option(None, help="Path to directory containing dataset."),
input_files: List[str] = typer.Option(None, help="List of paths to files containing dataset."),
recursive: bool = typer.Option(False, help="Recursively search for files in directory."),
+ extensions: str = typer.Option(default_extensions, help="Comma separated list of file extensions to load"),
user_id: str = typer.Option(None, help="User ID to associate with dataset."),
):
try:
@@ -189,6 +193,7 @@ def load_directory(
reader = SimpleDirectoryReader(
input_dir=input_dir,
recursive=recursive,
+ required_exts=[ext.strip() for ext in extensions.split(",")],
)
else:
reader = SimpleDirectoryReader(input_files=input_files)
diff --git a/memgpt/embeddings.py b/memgpt/embeddings.py
index 299266b4f..c9682596f 100644
--- a/memgpt/embeddings.py
+++ b/memgpt/embeddings.py
@@ -7,11 +7,10 @@ from memgpt.utils import is_valid_url
from memgpt.data_types import EmbeddingConfig
from llama_index.embeddings import OpenAIEmbedding, AzureOpenAIEmbedding
-from llama_index.embeddings import TextEmbeddingsInference
from llama_index.bridge.pydantic import PrivateAttr
-
from llama_index.embeddings.base import BaseEmbedding
-from llama_index.embeddings.huggingface_utils import format_query, format_text
+from llama_index.embeddings.huggingface_utils import format_text
+import tiktoken
class EmbeddingEndpoint(BaseEmbedding):
@@ -38,6 +37,7 @@ class EmbeddingEndpoint(BaseEmbedding):
self._user = user
self._base_url = base_url
self._timeout = timeout
+ self._encoding = tiktoken.get_encoding(model)
super().__init__(
model_name=model,
)
@@ -46,6 +46,10 @@ class EmbeddingEndpoint(BaseEmbedding):
def class_name(cls) -> str:
return "EmbeddingEndpoint"
+ def count_tokens(self, text: str) -> int:
+ """Count tokens using the embedding model's tokenizer"""
+ return len(self._encoding.encode(text))
+
def _call_api(self, text: str) -> List[float]:
if not is_valid_url(self._base_url):
raise ValueError(
@@ -53,6 +57,12 @@ class EmbeddingEndpoint(BaseEmbedding):
)
import httpx
+ # If necessary, truncate text to fit in the embedding model's max sequence length (usually 512)
+ num_tokens = self.count_tokens(text)
+ max_length = self._encoding.max_length
+ if num_tokens > max_length:
+ text = format_text(text, self.model_name, max_length=max_length)
+
headers = {"Content-Type": "application/json"}
json_data = {"input": text, "model": self.model_name, "user": self._user}
@@ -136,6 +146,16 @@ class EmbeddingEndpoint(BaseEmbedding):
return self._get_text_embedding(text)
+def default_embedding_model():
+ # default to hugging face model running local
+ # warning: this is a terrible model
+ from llama_index.embeddings import HuggingFaceEmbedding
+
+ os.environ["TOKENIZERS_PARALLELISM"] = "False"
+ model = "BAAI/bge-small-en-v1.5"
+ return HuggingFaceEmbedding(model_name=model)
+
+
def embedding_model(config: EmbeddingConfig, user_id: Optional[uuid.UUID] = None):
"""Return LlamaIndex embedding model to use for embeddings"""
@@ -157,13 +177,10 @@ def embedding_model(config: EmbeddingConfig, user_id: Optional[uuid.UUID] = None
api_version=config.azure_version,
)
elif endpoint_type == "hugging-face":
- embed_model = EmbeddingEndpoint(model=config.embedding_model, base_url=config.embedding_endpoint, user=user_id)
+ try:
+ embed_model = EmbeddingEndpoint(model=config.embedding_model, base_url=config.embedding_endpoint, user=user_id)
+ except:
+ embed_model = default_embedding_model()
return embed_model
else:
- # default to hugging face model running local
- # warning: this is a terrible model
- from llama_index.embeddings import HuggingFaceEmbedding
-
- os.environ["TOKENIZERS_PARALLELISM"] = "False"
- model = "BAAI/bge-small-en-v1.5"
- return HuggingFaceEmbedding(model_name=model)
+ return default_embedding_model()