|
| 1 | +import argparse |
| 2 | +import json |
| 3 | + |
| 4 | +from oasst_data import read_message_list, write_messages |
| 5 | +from oasst_data.schemas import ExportMessageNode |
| 6 | +from oasst_data.writer import open_jsonl_write |
| 7 | + |
| 8 | + |
| 9 | +def parse_args(): |
| 10 | + parser = argparse.ArgumentParser(description="filter_messages") |
| 11 | + parser.add_argument( |
| 12 | + "input_file_name", |
| 13 | + type=str, |
| 14 | + help="path to input .jsonl or .jsonl.gz input file", |
| 15 | + ) |
| 16 | + parser.add_argument( |
| 17 | + "output_file_name", |
| 18 | + type=str, |
| 19 | + help="path to output .jsonl or .jsonl.gz file", |
| 20 | + ) |
| 21 | + parser.add_argument( |
| 22 | + "--include-deleted", |
| 23 | + action="store_true", |
| 24 | + help="Include deleted messages in export", |
| 25 | + ) |
| 26 | + parser.add_argument( |
| 27 | + "--deleted-only", |
| 28 | + action="store_true", |
| 29 | + help="Export only deleted messages (implies --include-deleted)", |
| 30 | + ) |
| 31 | + parser.add_argument( |
| 32 | + "--include-spam", |
| 33 | + action="store_true", |
| 34 | + help="Export including messages with no review or negative review result.", |
| 35 | + ) |
| 36 | + parser.add_argument( |
| 37 | + "--spam-only", |
| 38 | + action="store_true", |
| 39 | + help="Export only messages with negative review result (implies --include-spam).", |
| 40 | + ) |
| 41 | + parser.add_argument( |
| 42 | + "--exclude-normal", |
| 43 | + action="store_true", |
| 44 | + help="exclude non-deleted non-synthetic messages with positive review", |
| 45 | + default=False, |
| 46 | + ) |
| 47 | + parser.add_argument( |
| 48 | + "--include-synthetic", |
| 49 | + action="store_true", |
| 50 | + help="Include synthetic messages in export", |
| 51 | + ) |
| 52 | + parser.add_argument( |
| 53 | + "--synthetic-only", |
| 54 | + action="store_true", |
| 55 | + help="Export only synthetic messages (implies --include-synth)", |
| 56 | + ) |
| 57 | + parser.add_argument( |
| 58 | + "--user", |
| 59 | + type=str, |
| 60 | + help="Only export trees involving the user with the specified ID. Incompatible with --state.", |
| 61 | + ) |
| 62 | + parser.add_argument( |
| 63 | + "--state", |
| 64 | + type=str, |
| 65 | + help="all|prompt_lottery_waiting|growing|ready_for_export|aborted_low_grade|halted_by_moderator|backlog_ranking", |
| 66 | + ) |
| 67 | + parser.add_argument( |
| 68 | + "--lang", |
| 69 | + type=str, |
| 70 | + help="Filter message trees by language code (BCP 47)", |
| 71 | + ) |
| 72 | + parser.add_argument( |
| 73 | + "--prompts-only", |
| 74 | + action="store_true", |
| 75 | + help="Export a list of initial prompt messages", |
| 76 | + ) |
| 77 | + parser.add_argument( |
| 78 | + "--export-text-only", |
| 79 | + action="store_true", |
| 80 | + help="Write jsonl file with message text strings only", |
| 81 | + ) |
| 82 | + parser.add_argument("--exclude-nulls", action="store_true", default=False) |
| 83 | + args = parser.parse_args() |
| 84 | + return args |
| 85 | + |
| 86 | + |
| 87 | +def main(): |
| 88 | + args = parse_args() |
| 89 | + |
| 90 | + deleted: bool | None = False |
| 91 | + spam: bool | None = False |
| 92 | + synthetic: bool | None = False |
| 93 | + langs: list[str] | None = None |
| 94 | + states: list[str] | None = None |
| 95 | + prompts_only: bool = args.prompts_only |
| 96 | + exclude_normal: bool = args.exclude_normal |
| 97 | + |
| 98 | + if args.include_deleted: |
| 99 | + deleted = None |
| 100 | + elif args.deleted_only: |
| 101 | + deleted = True |
| 102 | + |
| 103 | + if args.include_spam: |
| 104 | + spam = None |
| 105 | + elif args.spam_only: |
| 106 | + spam = True |
| 107 | + |
| 108 | + if args.include_synthetic: |
| 109 | + synthetic = None |
| 110 | + elif args.synthetic_only: |
| 111 | + synthetic = True |
| 112 | + |
| 113 | + if args.lang: |
| 114 | + langs = args.lang.split(",") |
| 115 | + |
| 116 | + if args.state: |
| 117 | + states = args.state.split(",") |
| 118 | + |
| 119 | + def approve_message(msg: ExportMessageNode) -> bool: |
| 120 | + if ( |
| 121 | + (deleted is not None and msg.deleted != deleted) |
| 122 | + or (synthetic is not None and msg.synthetic != synthetic) |
| 123 | + or (prompts_only and msg.parent_id) |
| 124 | + or (langs is not None and msg.lang not in langs) |
| 125 | + or (states is not None and msg.tree_state not in states) |
| 126 | + ): |
| 127 | + return False |
| 128 | + |
| 129 | + if exclude_normal is True and not msg.deleted and not msg.synthetic and msg.review_result: |
| 130 | + return False |
| 131 | + |
| 132 | + if spam is not None and spam != (not msg.review_result): |
| 133 | + return False |
| 134 | + |
| 135 | + return True |
| 136 | + |
| 137 | + print(f"Reading: {args.input_file_name}") |
| 138 | + messages = read_message_list(args.input_file_name, approve_message) |
| 139 | + |
| 140 | + print(f"Found {len(messages)} matching messages.") |
| 141 | + |
| 142 | + print(f"Writing: {args.output_file_name}") |
| 143 | + if args.export_text_only: |
| 144 | + with open_jsonl_write(args.output_file_name) as file: |
| 145 | + for msg in messages: |
| 146 | + json.dump(msg.text, file) |
| 147 | + file.write("\n") |
| 148 | + else: |
| 149 | + write_messages(args.output_file_name, messages, args.exclude_nulls) |
| 150 | + |
| 151 | + |
| 152 | +if __name__ == "__main__": |
| 153 | + main() |
0 commit comments