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

🙉Monke Good #13

Merged
merged 6 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
182 changes: 2 additions & 180 deletions bananalyzer/__init__.py
Original file line number Diff line number Diff line change
@@ -1,183 +1,5 @@
# Separate banana-lyzer args from pytest args
# Look for an instance of Banana-lyzer in the current directory
# If it doesn't exist, error
import argparse
import importlib.util
import os
import sys
from pathlib import Path
from typing import Any

from bananalyzer.data.examples import examples
from bananalyzer.data.schemas import GoalType
from bananalyzer.data.schemas import Example, GoalType
from bananalyzer.runner.agent_runner import AgentRunner
from bananalyzer.runner.runner import generate_test, run_tests
from bananalyzer.schema import Args, PytestArgs


def print_intro() -> None:
# https://www.asciiart.eu/food-and-drinks/bananas
print(
"""//\
V \
\ \_
\,'.`-.
|\ `. `.
( \ `. `-. _,.-:\
\ \ `. `-._ __..--' ,-';/
\ `. `-. `-..___..---' _.--' ,'/
`. `. `-._ __..--' ,' /
`. `-_ ``--..'' _.-' ,'
`-_ `-.___ __,--' ,'
`-.__ `----''' __.-'
`--..____..--'
"""
)
print("Bananalyzing... 🍌")


def parse_args() -> Args:
file_name = "bananalyzer-agent.py"
parser = argparse.ArgumentParser(
description=f"Run the agent inside a bananalyzer agent definition file against the benchmark",
)
parser.add_argument("path", type=str, help=f"Path to the {file_name} file")
parser.add_argument(
"--headless", action="store_true", help=f"Whether to run headless or not"
)
parser.add_argument(
"-s",
"--s",
action="store_true",
help="Shortcut for --capture=no in pytest. Will print stdout and stderr",
)
parser.add_argument(
"-id",
"--id",
type=str,
default=None,
help="Filter tests by id",
)
parser.add_argument(
"-i",
"--intent",
type=str,
default=None,
help="Filter tests by a particular intent",
)
parser.add_argument(
"-d",
"--domain",
type=str,
default=None,
help="Filter tests by a particular domain",
)
parser.add_argument(
"-n",
"--n",
type=str,
default=None,
help="Number of test workers to use. The default is 1",
)
parser.add_argument(
"-skip",
"--skip",
type=lambda s: s.split(","),
default=[],
help="A list of ids to skip tests on, separated by commas",
)

args = parser.parse_args()

file_name = os.path.basename(args.path)
if file_name != file_name:
raise RuntimeError(f"The provided file name must be {file_name}")

return Args(
path=args.path,
headless=args.headless,
intent=args.intent,
id=args.id,
domain=args.domain,
skip=args.skip,
pytest_args=PytestArgs(
s=args.s,
n=args.n,
),
)


def load_agent_from_path(file_path: str) -> Any:
path = Path(file_path)
module_name = path.stem

spec = importlib.util.spec_from_file_location(module_name, file_path)
if spec is None or spec.loader is None:
raise ImportError(f"Cannot load module from path {file_path}")
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
spec.loader.exec_module(module)

return getattr(module, "agent")


def validate_agent_instance_available(agent: Any) -> None:
# Ensures there is some "agent" variable in the global context
# This is a bit hacky, but it's the best way to ensure the agent is available
# without actually running the file
if not isinstance(agent, AgentRunner):
raise TypeError("User defined agent is is not an instance of AgentRunner")


def main() -> int:
"""
Load the agent from the provided path and run it against the benchmark

Note that pytest creates a new global context when running tests.
Because of this, we first load the agent and validate that it is of the correct type here.
Then we pass the path to the agent runner and let it load it within the pytest context.
Note your AgentRunner must be concurrency safe.
"""
print_intro()

# Load the agent
args = parse_args()
agent = load_agent_from_path(args.path)
validate_agent_instance_available(agent)

# Filter examples based on args
filtered_examples = examples[:]
if args.id:
filtered_examples = [
example for example in filtered_examples if example.id == args.id
]
if args.intent:
filtered_examples = [
example for example in filtered_examples if example.type == args.intent
]
if args.domain:
filtered_examples = [
example for example in filtered_examples if example.domain == args.domain
]
if args.skip:
filtered_examples = [
example for example in filtered_examples if example.id not in args.skip
]

# Test we actually have tests to run
if len(filtered_examples) == 0:
print()
print("=======================================================================")
print("🍌 No tests to run. Please ensure your filter parameters are correct 🍌")
print("=======================================================================")
return 0

# Load the desired tests
tests = [generate_test(example, args.headless) for example in filtered_examples]

# Run the tests
return run_tests(tests, args.path, args.pytest_args)


if __name__ == "__main__":
main()
__all__ = ["AgentRunner", "GoalType", "Example", "examples"]
Loading