Skip to content

Commit

Permalink
Added length checking to the persona/human loader functions, also upd…
Browse files Browse the repository at this point in the history
…ated the CLI error dump to look prettier (eg on over length errors) (#695)
  • Loading branch information
cpacker authored Dec 25, 2023
1 parent ccf7bf6 commit 10840fc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
24 changes: 14 additions & 10 deletions memgpt/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,23 +447,27 @@ def run(
agent_config.save()
typer.secho(f"-> 🤖 Using persona profile '{agent_config.persona}'", fg=typer.colors.WHITE)
typer.secho(f"-> 🧑 Using human profile '{agent_config.human}'", fg=typer.colors.WHITE)
typer.secho(f"🎉 Created new agent '{agent_config.name}'", fg=typer.colors.GREEN)

# Supress llama-index noise
with suppress_stdout():
# TODO: allow configrable state manager (only local is supported right now)
persistence_manager = LocalStateManager(agent_config) # TODO: insert dataset/pre-fill

# create agent
memgpt_agent = presets.use_preset(
agent_config.preset,
agent_config,
agent_config.model,
utils.get_persona_text(agent_config.persona),
utils.get_human_text(agent_config.human),
interface,
persistence_manager,
)
try:
memgpt_agent = presets.use_preset(
agent_config.preset,
agent_config,
agent_config.model,
utils.get_persona_text(agent_config.persona),
utils.get_human_text(agent_config.human),
interface,
persistence_manager,
)
except ValueError as e:
typer.secho(f"Failed to create agent from provided information:\n{e}", fg=typer.colors.RED)
sys.exit(1)
typer.secho(f"🎉 Created new agent '{agent_config.name}'", fg=typer.colors.GREEN)

# pretty print agent config
printd(json.dumps(vars(agent_config), indent=4, sort_keys=True))
Expand Down
31 changes: 23 additions & 8 deletions memgpt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@
import tiktoken

import memgpt
from memgpt.constants import MEMGPT_DIR, FUNCTION_RETURN_CHAR_LIMIT, CLI_WARNING_PREFIX
from memgpt.constants import (
MEMGPT_DIR,
FUNCTION_RETURN_CHAR_LIMIT,
CLI_WARNING_PREFIX,
CORE_MEMORY_HUMAN_CHAR_LIMIT,
CORE_MEMORY_PERSONA_CHAR_LIMIT,
)

from memgpt.openai_backcompat.openai_object import OpenAIObject

Expand Down Expand Up @@ -237,21 +243,30 @@ def list_persona_files():
return memgpt_defaults + user_added


def get_human_text(name: str):
def get_human_text(name: str, enforce_limit=True):
for file_path in list_human_files():
file = os.path.basename(file_path)
if f"{name}.txt" == file or name == file:
return open(file_path, "r").read().strip()
raise ValueError(f"Human {name} not found")
human_text = open(file_path, "r").read().strip()
if enforce_limit and len(human_text) > CORE_MEMORY_HUMAN_CHAR_LIMIT:
raise ValueError(f"Contents of {name}.txt is over the character limit ({len(human_text)} > {CORE_MEMORY_HUMAN_CHAR_LIMIT})")
return human_text

raise ValueError(f"Human {name}.txt not found")


def get_persona_text(name: str):
def get_persona_text(name: str, enforce_limit=True):
for file_path in list_persona_files():
file = os.path.basename(file_path)
if f"{name}.txt" == file or name == file:
return open(file_path, "r").read().strip()

raise ValueError(f"Persona {name} not found")
persona_text = open(file_path, "r").read().strip()
if enforce_limit and len(persona_text) > CORE_MEMORY_PERSONA_CHAR_LIMIT:
raise ValueError(
f"Contents of {name}.txt is over the character limit ({len(persona_text)} > {CORE_MEMORY_PERSONA_CHAR_LIMIT})"
)
return persona_text

raise ValueError(f"Persona {name}.txt not found")


def get_human_text(name: str):
Expand Down

0 comments on commit 10840fc

Please sign in to comment.