Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Better errors on over length persona/human files #695

Merged
merged 1 commit into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading