|
| 1 | +import argparse |
| 2 | +import os |
| 3 | +import sys |
| 4 | + |
| 5 | +import yaml |
| 6 | +import logging |
| 7 | + |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +from utils import LLMClient |
| 11 | + |
| 12 | +FILE = Path(__file__).resolve() |
| 13 | +ROOT = FILE.parents[0] |
| 14 | +if str(ROOT) not in sys.path: |
| 15 | + sys.path.append(str(ROOT)) |
| 16 | +ROOT = Path(os.path.relpath(ROOT, Path.cwd())) |
| 17 | + |
| 18 | +logging.basicConfig( |
| 19 | + level=logging.INFO, |
| 20 | + format="%(asctime)s - %(levelname)s - %(message)s", |
| 21 | + datefmt="%Y-%m-%d %H:%M:%S", |
| 22 | +) |
| 23 | + |
| 24 | +def parse_opt(known=False): |
| 25 | + """ |
| 26 | + Parse command-line options. |
| 27 | + """ |
| 28 | + parser = argparse.ArgumentParser() |
| 29 | + parser.add_argument("--host", type=str, default="127.0.0.1", help="ModuleLLM IP Address") |
| 30 | + parser.add_argument("--port", type=int, default=10001, help="ModuleLLM TCP Port") |
| 31 | + parser.add_argument("--test-items", type=str, default=ROOT / "default.yaml", help="testitems.yaml path") |
| 32 | + |
| 33 | + args = parser.parse_known_args()[0] if known else parser.parse_args() |
| 34 | + |
| 35 | + return args |
| 36 | + |
| 37 | +def read_yaml(file_path): |
| 38 | + """ |
| 39 | + Read a YAML file and return its content. |
| 40 | + """ |
| 41 | + if not os.path.exists(file_path): |
| 42 | + logging.error(f"YAML file '{file_path}' does not exist.") |
| 43 | + sys.exit(1) |
| 44 | + |
| 45 | + try: |
| 46 | + with open(file_path, "r") as file: |
| 47 | + data = yaml.safe_load(file) |
| 48 | + if data is None: |
| 49 | + logging.warning(f"YAML file '{file_path}' is empty.") |
| 50 | + return {} |
| 51 | + |
| 52 | + logging.info(f"YAML file '{file_path}' read successfully.") |
| 53 | + |
| 54 | + if "items" in data: |
| 55 | + return data["items"] |
| 56 | + else: |
| 57 | + logging.warning(f"'items' not found in YAML file.") |
| 58 | + return [] |
| 59 | + except Exception as e: |
| 60 | + logging.error(f"Failed to read YAML file '{file_path}': {e}") |
| 61 | + sys.exit(1) |
| 62 | + |
| 63 | +def write_yaml(file_path, data): |
| 64 | + """ |
| 65 | + Write data to a YAML file. |
| 66 | + """ |
| 67 | + try: |
| 68 | + with open(file_path, "w") as file: |
| 69 | + yaml.safe_dump(data, file) |
| 70 | + logging.info(f"YAML file '{file_path}' written successfully.") |
| 71 | + except Exception as e: |
| 72 | + logging.error(f"Failed to write YAML file '{file_path}': {e}") |
| 73 | + sys.exit(1) |
| 74 | + |
| 75 | +def categorize_and_deduplicate(items): |
| 76 | + """ |
| 77 | + Categorize items by 'type' and remove duplicate 'model_name'. |
| 78 | + """ |
| 79 | + categorized = {} |
| 80 | + for item in items: |
| 81 | + item_type = item.get("type") |
| 82 | + model_name = item.get("model_name") |
| 83 | + if not item_type or not model_name: |
| 84 | + continue |
| 85 | + |
| 86 | + if item_type not in categorized: |
| 87 | + categorized[item_type] = set() |
| 88 | + |
| 89 | + categorized[item_type].add(model_name) |
| 90 | + |
| 91 | + # Convert sets back to lists for easier usage |
| 92 | + return {key: list(value) for key, value in categorized.items()} |
| 93 | + |
| 94 | +def main(opt): |
| 95 | + items = read_yaml(opt.test_items) |
| 96 | + if not items: |
| 97 | + logging.warning(f"No items found in YAML file '{opt.test_items}'.") |
| 98 | + return |
| 99 | + |
| 100 | + categorized_items = categorize_and_deduplicate(items) |
| 101 | + |
| 102 | + logging.info("Categorized items:") |
| 103 | + for item_type, models in categorized_items.items(): |
| 104 | + logging.info(f"Type: {item_type}, Models: {models}") |
| 105 | + |
| 106 | + if item_type == "llm": |
| 107 | + logging.info("Initializing LLMClient...") |
| 108 | + llm_client = LLMClient(opt.host, opt.port) |
| 109 | + |
| 110 | + for model_name in models: |
| 111 | + logging.info(f"Testing model: {model_name}") |
| 112 | + input_text = "Tell me an adventure story." |
| 113 | + try: |
| 114 | + result = llm_client.test(model_name, input_text) |
| 115 | + logging.info(f"Test result for model '{model_name}': {result}") |
| 116 | + except Exception as e: |
| 117 | + logging.error(f"Error testing model '{model_name}': {e}") |
| 118 | + |
| 119 | + del llm_client |
| 120 | + logging.info("LLMClient deleted successfully.") |
| 121 | + |
| 122 | + return categorized_items |
| 123 | + |
| 124 | +if __name__ == "__main__": |
| 125 | + opt = parse_opt() |
| 126 | + main(opt) |
0 commit comments