Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
chore: add CLI CI test (#1858)
Matthew Zhou5769e0c8153d
diff --git a/.github/workflows/test_cli.yml b/.github/workflows/test_cli.ymlnew file mode 100644index 000000000..1257572c7--- /dev/null+++ b/.github/workflows/test_cli.yml@@ -0,0 +1,46 @@+name: Run CLI tests++env:+ OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}++on:+ push:+ branches: [ main ]+ pull_request:+ branches: [ main ]++jobs:+ test:+ runs-on: ubuntu-latest+ timeout-minutes: 15++ services:+ qdrant:+ image: qdrant/qdrant+ ports:+ - 6333:6333++ steps:+ - name: Checkout+ uses: actions/checkout@v4++ - name: Build and run container+ run: bash db/run_postgres.sh++ - name: "Setup Python, Poetry and Dependencies"+ uses: packetcoders/action-setup-cache-python-poetry@main+ with:+ python-version: "3.12"+ poetry-version: "1.8.2"+ install-args: "-E dev -E postgres -E tests"++ - name: Test `letta run` up until first message+ env:+ LETTA_PG_PORT: 8888+ LETTA_PG_USER: letta+ LETTA_PG_PASSWORD: letta+ LETTA_PG_DB: letta+ LETTA_PG_HOST: localhost+ LETTA_SERVER_PASS: test_server_token+ run: |+ poetry run pytest -s -vv tests/test_cli.py::test_letta_run_create_new_agentdiff --git a/.github/workflows/tests.yml b/.github/workflows/tests.ymlindex 1c3580414..33ab5ff55 100644--- a/.github/workflows/tests.yml+++ b/.github/workflows/tests.yml@@ -69,4 +69,4 @@ jobs:LETTA_SERVER_PASS: test_server_tokenPYTHONPATH: ${{ github.workspace }}:${{ env.PYTHONPATH }}run: |- poetry run pytest -s -vv -k "not test_tools.py and not test_concurrent_connections.py and not test_quickstart and not test_endpoints and not test_storage and not test_server and not test_openai_client and not test_providers" tests+ poetry run pytest -s -vv -k "not test_cli.py and not test_tools.py and not test_concurrent_connections.py and not test_quickstart and not test_endpoints and not test_storage and not test_server and not test_openai_client and not test_providers" testsdiff --git a/letta/cli/cli.py b/letta/cli/cli.pyindex 1517aa175..31a567e1d 100644--- a/letta/cli/cli.py+++ b/letta/cli/cli.py@@ -11,6 +11,7 @@ from letta import create_clientfrom letta.agent import Agent, save_agentfrom letta.config import LettaConfigfrom letta.constants import CLI_WARNING_PREFIX, LETTA_DIR+from letta.local_llm.constants import ASSISTANT_MESSAGE_CLI_SYMBOLfrom letta.log import get_loggerfrom letta.metadata import MetadataStorefrom letta.schemas.enums import OptionState@@ -276,7 +277,7 @@ def run(memory = ChatMemory(human=human_obj.value, persona=persona_obj.value, limit=core_memory_limit)metadata = {"human": human_obj.name, "persona": persona_obj.name}- typer.secho(f"-> π€ Using persona profile: '{persona_obj.name}'", fg=typer.colors.WHITE)+ typer.secho(f"-> {ASSISTANT_MESSAGE_CLI_SYMBOL} Using persona profile: '{persona_obj.name}'", fg=typer.colors.WHITE)typer.secho(f"-> π§ Using human profile: '{human_obj.name}'", fg=typer.colors.WHITE)# add toolsdiff --git a/letta/client/utils.py b/letta/client/utils.pyindex 254a82f15..bcec534c7 100644--- a/letta/client/utils.py+++ b/letta/client/utils.py@@ -2,6 +2,11 @@ from datetime import datetimefrom IPython.display import HTML, display+from letta.local_llm.constants import (+ ASSISTANT_MESSAGE_CLI_SYMBOL,+ INNER_THOUGHTS_CLI_SYMBOL,+)+def pprint(messages):"""Utility function for pretty-printing the output of client.send_message in notebooks"""@@ -47,13 +52,13 @@ def pprint(messages):html_content += f"<p><strong>π οΈ [{date_formatted}] Function Return ({return_status}):</strong></p>"html_content += f"<p class='function-return'>{return_string}</p>"elif "internal_monologue" in message:- html_content += f"<p><strong>π [{date_formatted}] Internal Monologue:</strong></p>"+ html_content += f"<p><strong>{INNER_THOUGHTS_CLI_SYMBOL} [{date_formatted}] Internal Monologue:</strong></p>"html_content += f"<p class='internal-monologue'>{message['internal_monologue']}</p>"elif "function_call" in message:html_content += f"<p><strong>π οΈ [[{date_formatted}] Function Call:</strong></p>"html_content += f"<p class='function-call'>{message['function_call']}</p>"elif "assistant_message" in message:- html_content += f"<p><strong>π€ [{date_formatted}] Assistant Message:</strong></p>"+ html_content += f"<p><strong>{ASSISTANT_MESSAGE_CLI_SYMBOL} [{date_formatted}] Assistant Message:</strong></p>"html_content += f"<p class='assistant-message'>{message['assistant_message']}</p>"html_content += "<br>"html_content += "</div>"diff --git a/letta/interface.py b/letta/interface.pyindex 1487db713..aac10453f 100644--- a/letta/interface.py+++ b/letta/interface.py@@ -5,6 +5,10 @@ from typing import List, Optionalfrom colorama import Fore, Style, initfrom letta.constants import CLI_WARNING_PREFIX+from letta.local_llm.constants import (+ ASSISTANT_MESSAGE_CLI_SYMBOL,+ INNER_THOUGHTS_CLI_SYMBOL,+)from letta.schemas.message import Messagefrom letta.utils import json_loads, printd@@ -79,14 +83,14 @@ class CLIInterface(AgentInterface):@staticmethoddef internal_monologue(msg: str, msg_obj: Optional[Message] = None):# ANSI escape code for italic is '\x1B[3m'- fstr = f"\x1B[3m{Fore.LIGHTBLACK_EX}π {{msg}}{Style.RESET_ALL}"+ fstr = f"\x1B[3m{Fore.LIGHTBLACK_EX}{INNER_THOUGHTS_CLI_SYMBOL} {{msg}}{Style.RESET_ALL}"if STRIP_UI:fstr = "{msg}"print(fstr.format(msg=msg))@staticmethoddef assistant_message(msg: str, msg_obj: Optional[Message] = None):- fstr = f"{Fore.YELLOW}{Style.BRIGHT}π€ {Fore.YELLOW}{{msg}}{Style.RESET_ALL}"+ fstr = f"{Fore.YELLOW}{Style.BRIGHT}{ASSISTANT_MESSAGE_CLI_SYMBOL} {Fore.YELLOW}{{msg}}{Style.RESET_ALL}"if STRIP_UI:fstr = "{msg}"print(fstr.format(msg=msg))diff --git a/letta/local_llm/constants.py b/letta/local_llm/constants.pyindex 1b3ab4e9f..ed07f4f1f 100644--- a/letta/local_llm/constants.py+++ b/letta/local_llm/constants.py@@ -29,3 +29,6 @@ DEFAULT_WRAPPER_NAME = "chatml"INNER_THOUGHTS_KWARG = "inner_thoughts"INNER_THOUGHTS_KWARG_DESCRIPTION = "Deep inner monologue private to you only."+INNER_THOUGHTS_CLI_SYMBOL = "π"++ASSISTANT_MESSAGE_CLI_SYMBOL = "π€"diff --git a/letta/streaming_interface.py b/letta/streaming_interface.pyindex 5ca5252b3..e21e5e73e 100644--- a/letta/streaming_interface.py+++ b/letta/streaming_interface.py@@ -9,6 +9,10 @@ from rich.live import Livefrom rich.markup import escapefrom letta.interface import CLIInterface+from letta.local_llm.constants import (+ ASSISTANT_MESSAGE_CLI_SYMBOL,+ INNER_THOUGHTS_CLI_SYMBOL,+)from letta.schemas.message import Messagefrom letta.schemas.openai.chat_completion_response import (ChatCompletionChunkResponse,@@ -296,7 +300,7 @@ class StreamingRefreshCLIInterface(AgentRefreshStreamingInterface):def process_refresh(self, response: ChatCompletionResponse):"""Process the response to rewrite the current output buffer."""if not response.choices:- self.update_output("π [italic]...[/italic]")+ self.update_output(f"{INNER_THOUGHTS_CLI_SYMBOL} [italic]...[/italic]")return # Early exit if there are no choiceschoice = response.choices[0]@@ -304,7 +308,7 @@ class StreamingRefreshCLIInterface(AgentRefreshStreamingInterface):tool_calls = choice.message.tool_calls if choice.message.tool_calls else []if self.fancy:- message_string = f"π [italic]{inner_thoughts}[/italic]" if inner_thoughts else ""+ message_string = f"{INNER_THOUGHTS_CLI_SYMBOL} [italic]{inner_thoughts}[/italic]" if inner_thoughts else ""else:message_string = "[inner thoughts] " + inner_thoughts if inner_thoughts else ""@@ -326,7 +330,7 @@ class StreamingRefreshCLIInterface(AgentRefreshStreamingInterface):message = function_args[len(prefix) :]else:message = function_args- message_string += f"π€ [bold yellow]{message}[/bold yellow]"+ message_string += f"{ASSISTANT_MESSAGE_CLI_SYMBOL} [bold yellow]{message}[/bold yellow]"else:message_string += f"{function_name}({function_args})"@@ -336,7 +340,7 @@ class StreamingRefreshCLIInterface(AgentRefreshStreamingInterface):if self.streaming:print()self.live.start() # Start the Live display context and keep it running- self.update_output("π [italic]...[/italic]")+ self.update_output(f"{INNER_THOUGHTS_CLI_SYMBOL} [italic]...[/italic]")def stream_end(self):if self.streaming:diff --git a/tests/test_cli.py b/tests/test_cli.pyindex 4e925c33d..107b6c6e5 100644--- a/tests/test_cli.py+++ b/tests/test_cli.py@@ -1,44 +1,73 @@-import subprocess+import os+import shutilimport sys-subprocess.check_call([sys.executable, "-m", "pip", "install", "pexpect"])-from prettytable.colortable import ColorTable+import pexpect+import pytest-from letta.cli.cli_config import ListChoice, add, delete-from letta.cli.cli_config import list as list_command+from letta.local_llm.constants import (+ ASSISTANT_MESSAGE_CLI_SYMBOL,+ INNER_THOUGHTS_CLI_SYMBOL,+)-# def test_configure_letta():-# configure_letta()+original_letta_path = os.path.expanduser("~/.letta")+backup_letta_path = os.path.expanduser("~/.letta_backup")-options = [ListChoice.agents, ListChoice.sources, ListChoice.humans, ListChoice.personas]+def swap_letta_config():+ if os.path.exists(backup_letta_path):+ print("\nDelete the backup ~/.letta directory\n")+ shutil.rmtree(backup_letta_path)-def test_cli_list():- for option in options:- output = list_command(arg=option)- # check if is a list- assert isinstance(output, ColorTable)+ if os.path.exists(original_letta_path):+ print("\nBackup the original ~/.letta directory\n")+ shutil.move(original_letta_path, backup_letta_path)+ try:+ # Run the test+ yield+ finally:+ # Ensure this runs no matter what+ print("\nClean up ~/.letta and restore the original directory\n")+ if os.path.exists(original_letta_path):+ shutil.rmtree(original_letta_path)-def test_cli_config():+ if os.path.exists(backup_letta_path):+ shutil.move(backup_letta_path, original_letta_path)- # test add- for option in ["human", "persona"]:- # create initial- add(option=option, name="test", text="test data")+def test_letta_run_create_new_agent(swap_letta_config):+ child = pexpect.spawn("poetry run letta run", encoding="utf-8")+ # Start the letta run command+ child.logfile = sys.stdout+ child.expect("Creating new agent", timeout=10)+ # Optional: LLM model selection+ try:+ child.expect("Select LLM model:", timeout=10)+ child.sendline("\033[B\033[B\033[B\033[B\033[B")+ except (pexpect.TIMEOUT, pexpect.EOF):+ print("[WARNING] LLM model selection step was skipped.")- ## update- # filename = "test.txt"- # open(filename, "w").write("test data new")- # child = pexpect.spawn(f"poetry run letta add --{str(option)} {filename} --name test --strip-ui")- # child.expect("Human test already exists. Overwrite?", timeout=TIMEOUT)- # child.sendline()- # child.expect(pexpect.EOF, timeout=TIMEOUT) # Wait for child to exit- # child.close()+ # Optional: Embedding model selection+ try:+ child.expect("Select embedding model:", timeout=10)+ child.sendline("text-embedding-ada-002")+ except (pexpect.TIMEOUT, pexpect.EOF):+ print("[WARNING] Embedding model selection step was skipped.")- for row in list_command(arg=ListChoice.humans if option == "human" else ListChoice.personas):- if row[0] == "test":- assert "test data" in row- # delete- delete(option=option, name="test")+ child.expect("Created new agent", timeout=10)+ child.sendline("")++ # Get initial response+ child.expect("Enter your message:", timeout=60)+ # Capture the output up to this point+ full_output = child.before+ # Count occurrences of inner thoughts+ cloud_emoji_count = full_output.count(INNER_THOUGHTS_CLI_SYMBOL)+ assert cloud_emoji_count == 1, f"It appears that there are multiple instances of inner thought outputted."+ # Count occurrences of assistant messages+ robot = full_output.count(ASSISTANT_MESSAGE_CLI_SYMBOL)+ assert robot == 1, f"It appears that there are multiple instances of assistant messages outputted."+ # Make sure the user name was repeated back at least once+ assert full_output.count("Chad") > 0, f"Chad was not mentioned...please manually inspect the outputs."