Skip to content

Commit

Permalink
test: improved testing for utils, removed unused functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBjare committed Nov 5, 2023
1 parent cbc0ffe commit bfcb876
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 28 deletions.
2 changes: 1 addition & 1 deletion gptme/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
from .prompts import get_prompt
from .tabcomplete import register_tabcomplete
from .tools import execute_msg, init_tools
from .util import epoch_to_age, generate_unique_name
from .util import epoch_to_age, generate_name

logger = logging.getLogger(__name__)
print_builtin = __builtins__["print"] # type: ignore
Expand Down
64 changes: 37 additions & 27 deletions gptme/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,6 @@ def get_tokenizer(model: str):
return tiktoken.get_encoding("cl100k_base")


def len_tokens_approx(content: str | list[Message]) -> int:
"""Approximate the number of tokens in a string by assuming tokens have len 3 (lol)."""
if isinstance(content, list):
return sum(len_tokens_approx(msg.content) for msg in content)
return len(content) // 3


def msgs2text(msgs: list[Message]) -> str:
output = ""
for msg in msgs:
output += f"{msg.role.capitalize()}: {msg.content}\n\n"
return output


def msgs2dicts(msgs: list[Message]) -> list[dict]:
"""Convert a list of Message objects to a list of dicts ready to pass to an LLM."""
return [msg.to_dict(keys=["role", "content"]) for msg in msgs]
Expand All @@ -66,10 +52,13 @@ def msgs2dicts(msgs: list[Message]) -> list[dict]:
"crawling",
"sneaking",
"sprinting",
"sneaking",
"dancing",
"singing",
"laughing",
]
adjectives = [
"funny",
"red",
"happy",
"sad",
"angry",
Expand All @@ -78,34 +67,55 @@ def msgs2dicts(msgs: list[Message]) -> list[dict]:
"sneaky",
"sleepy",
"hungry",
# colors
"red",
"blue",
"green",
"pink",
"purple",
"yellow",
"orange",
]
nouns = [
"walrus",
"pelican",
"cat",
"dog",
"elephant",
"rat",
"mouse",
"bird",
"fish",
"elephant",
"dinosaur",
# birds
"bird",
"pelican",
# fictional
"dragon",
"unicorn",
"dinosaur",
"mermaid",
"monster",
"alien",
"robot",
# sea creatures
"whale",
"shark",
"walrus",
"octopus",
"squid",
"jellyfish",
"starfish",
"penguin",
"seal",
]


def generate_unique_name():
def generate_name():
action = random.choice(actions)
adjective = random.choice(adjectives)
noun = random.choice(nouns)
unique_name = f"{action}-{adjective}-{noun}"
assert is_generated_name(unique_name)
return unique_name
return f"{action}-{adjective}-{noun}"


def is_generated_name(name: str) -> bool:
"""if name is a name generated by generate_unique_name"""
"""if name is a name generated by generate_name"""
all_words = actions + adjectives + nouns
return name.count("-") == 2 and all(word in all_words for word in name.split("-"))

Expand All @@ -125,14 +135,14 @@ def epoch_to_age(epoch):
return f"{age.days} days ago ({datetime.fromtimestamp(epoch).strftime('%Y-%m-%d')})"


def print_preview(code: str, lang: str):
def print_preview(code: str, lang: str): # pragma: no cover
print()
print("[bold white]Preview[/bold white]")
print(Syntax(code.strip(), lang))
print()


def ask_execute(question="Execute code?", default=True) -> bool:
def ask_execute(question="Execute code?", default=True) -> bool: # pragma: no cover
# TODO: add a way to outsource ask_execute decision to another agent/LLM
console = Console()
choicestr = f"({'Y' if default else 'y'}/{'n' if default else 'N'})"
Expand Down
15 changes: 15 additions & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
from datetime import datetime

from gptme.tools import is_supported_codeblock
from gptme.util import epoch_to_age, generate_name, is_generated_name


def test_generate_name():
name = generate_name()
assert is_generated_name(name)


def test_epoch_to_age():
epoch_today = datetime.now().timestamp()
assert epoch_to_age(epoch_today) == "just now"
epoch_yesterday = epoch_today - 24 * 60 * 60
assert epoch_to_age(epoch_yesterday) == "yesterday"


def test_is_supported_codeblock():
Expand Down

0 comments on commit bfcb876

Please sign in to comment.