|
| 1 | +import json |
| 2 | +import yaml |
| 3 | +import random |
| 4 | + |
| 5 | +from pathlib import Path |
| 6 | +from pydantic import SecretStr |
| 7 | +from textwrap import wrap |
| 8 | + |
| 9 | +from docling_core.transforms.chunker.hierarchical_chunker import DocChunk, DocMeta |
| 10 | +from docling_sdg.qa.utils import get_qa_chunks |
| 11 | +from docling_sdg.qa.generate import Generator |
| 12 | +from docling_sdg.qa.base import GenerateOptions, LlmProvider |
| 13 | + |
| 14 | +chunk_filter = [ |
| 15 | + lambda chunk: len(str(chunk.text)) > 500 |
| 16 | +] |
| 17 | + |
| 18 | +def str_presenter(dumper, data): |
| 19 | + if len(data.splitlines()) > 1: # check for multiline string |
| 20 | + return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='|') |
| 21 | + elif len(data) > 80: |
| 22 | + data = "\n".join(wrap(data, 80)) |
| 23 | + return dumper.represent_scalar('tag:yaml.org,2002:str', data, style='|') |
| 24 | + return dumper.represent_scalar('tag:yaml.org,2002:str', data) |
| 25 | + |
| 26 | +yaml.add_representer(str, str_presenter) |
| 27 | + |
| 28 | +# to use with safe_dump: |
| 29 | +yaml.representer.SafeRepresenter.add_representer(str, str_presenter) |
| 30 | + |
| 31 | +class IndentedDumper(yaml.Dumper): |
| 32 | + def increase_indent(self, flow=False, indentless=False): |
| 33 | + return super(IndentedDumper, self).increase_indent(flow, False) |
| 34 | + |
| 35 | +def generate_seed_examples(contribution_name: str, chunks_jsonl_path: Path, output_dir: Path, domain: str, summary: str, num_seed_examples: int, api_key: str, api_url: str, model_id: str) -> Path: |
| 36 | + """ |
| 37 | + Creates a seed dataset from a path |
| 38 | + Args: |
| 39 | + contribution_name (str): Name of the contribution |
| 40 | + chunks_jsonl_path (Path): Path to the chunks/chunks.jsonl file |
| 41 | + output_dir (Path): Path to output dir for the qna.yaml and intermediate outputs by docling-sdg |
| 42 | + contribution_metadata (dict): Dictionary with the domain and summary of the contribution |
| 43 | + num_seed_examples (str): Number of seed examples user wishes to generate for the contribution |
| 44 | + api_key (str): API key for the model used to generate questions and answers from contexts |
| 45 | + api_url (str): Endpoint for the model used to generate questions and answers from contexts |
| 46 | + model_id (str): Name of the model used to generate questions and answers from contexts |
| 47 | + Returns: |
| 48 | + qna_output_path (pathlib.Path): Path to the generated seed example file |
| 49 | + """ |
| 50 | + dataset = {} |
| 51 | + dataset[contribution_name] = {} |
| 52 | + dataset[contribution_name]["chunks"] = [] |
| 53 | + |
| 54 | + if not chunks_jsonl_path.exists(): |
| 55 | + raise ValueError(f"chunks.jsonl does not exist but should at {chunks_jsonl_path}") |
| 56 | + |
| 57 | + docs = [] |
| 58 | + |
| 59 | + with open(chunks_jsonl_path, 'r') as file: # khaled was here |
| 60 | + for line in file: |
| 61 | + file_in_docs = False |
| 62 | + entry = json.loads(line) |
| 63 | + #entry = yaml.safe_load(line) |
| 64 | + meta = DocMeta(**entry['metadata']) |
| 65 | + chunk = DocChunk(text=entry['chunk'], meta=meta) |
| 66 | + for doc in docs: |
| 67 | + if doc["file"] == entry['file']: |
| 68 | + doc["chunk_objs"].append(chunk) |
| 69 | + file_in_docs = True |
| 70 | + break |
| 71 | + |
| 72 | + if file_in_docs == False: |
| 73 | + doc = dict(file=entry['file'], chunk_objs=[chunk]) |
| 74 | + docs.append(doc) |
| 75 | + |
| 76 | + for doc in docs: |
| 77 | + print(f"Filtering smaller chunks out of document {doc['file']}") |
| 78 | + |
| 79 | + qa_chunks = get_qa_chunks(doc["file"], doc["chunk_objs"], chunk_filter) |
| 80 | + dataset[contribution_name]["chunks"].extend(list(qa_chunks)) |
| 81 | + |
| 82 | + |
| 83 | + l = dataset[contribution_name]["chunks"] |
| 84 | + selected_chunks = random.sample(l, num_seed_examples) |
| 85 | + |
| 86 | + generate_options = GenerateOptions(project_id="project_id") |
| 87 | + generate_options.provider = LlmProvider.OPENAI_LIKE |
| 88 | + generate_options.api_key = SecretStr(api_key) |
| 89 | + generate_options.url = api_url |
| 90 | + generate_options.model_id = model_id |
| 91 | + generate_options.generated_file = output_dir / f"qagen-{contribution_name}.json" |
| 92 | + gen = Generator(generate_options=generate_options) |
| 93 | + |
| 94 | + Path.unlink(generate_options.generated_file, missing_ok=True) |
| 95 | + results = gen.generate_from_chunks(selected_chunks) # automatically saves to file |
| 96 | + |
| 97 | + print(f"Status for Q&A generation for {contribution_name} is: {results.status}") |
| 98 | + |
| 99 | + qnas = {} |
| 100 | + chunk_id_to_text = {} |
| 101 | + with open(generate_options.generated_file, "rt") as f: |
| 102 | + for line in f.readlines(): |
| 103 | + entry = json.loads(line) |
| 104 | + chunk_id = entry['chunk_id'] |
| 105 | + if chunk_id not in chunk_id_to_text: |
| 106 | + chunk_id_to_text[chunk_id] = entry['context'] |
| 107 | + if chunk_id not in qnas: |
| 108 | + qnas[chunk_id] = [] |
| 109 | + qnas[chunk_id].append({'question': entry['question'], 'answer': entry['answer']}) |
| 110 | + |
| 111 | + qna_output_path = output_dir / "qna.yaml" |
| 112 | + |
| 113 | + data = {'seed_examples': []} |
| 114 | + for chunk_id, context in chunk_id_to_text.items(): |
| 115 | + data['seed_examples'].append({ |
| 116 | + 'context': context, |
| 117 | + 'questions_and_answers': [ |
| 118 | + { |
| 119 | + 'question': example['question'], |
| 120 | + 'answer': example['answer'], |
| 121 | + } for example in qnas[chunk_id] |
| 122 | + ] |
| 123 | + }) |
| 124 | + |
| 125 | + |
| 126 | + data['document_outline'] = summary |
| 127 | + data['domain'] = domain |
| 128 | + |
| 129 | + Path.unlink(qna_output_path, missing_ok=True) # shouldn't be necessary but was. jupyter caching thing? |
| 130 | + with open(qna_output_path, 'w') as yaml_file: |
| 131 | + yaml.dump(data, yaml_file, Dumper=IndentedDumper, default_flow_style=False, sort_keys=False, width=80) |
| 132 | + |
| 133 | + return qna_output_path |
| 134 | + |
| 135 | +def review_seed_examples_file(seed_examples_path: Path, min_seed_examples: int = 5, num_qa_pairs: int = 3) -> None: |
| 136 | + with open(seed_examples_path, 'r') as yaml_file: |
| 137 | + yaml_data = yaml.safe_load(yaml_file) |
| 138 | + errors = [] |
| 139 | + print(f"Reviewing seed examples file at {seed_examples_path.resolve()}") |
| 140 | + |
| 141 | + # Check for document_outline |
| 142 | + if 'document_outline' not in yaml_data: |
| 143 | + errors.append("Missing contribution summary in 'document_outline'") |
| 144 | + else: |
| 145 | + # contribution summary is called document_outline internally |
| 146 | + print(f"Found contribution summary...") |
| 147 | + |
| 148 | + # Check for domain |
| 149 | + if 'domain' not in yaml_data: |
| 150 | + errors.append("Missing 'domain'") |
| 151 | + else: |
| 152 | + print(f"Found 'domain'...") |
| 153 | + |
| 154 | + # Check seed_examples |
| 155 | + seed_examples = yaml_data.get('seed_examples') |
| 156 | + if not seed_examples: |
| 157 | + errors.append("'seed_examples' section is missing or empty.") |
| 158 | + elif len(seed_examples) < min_seed_examples: |
| 159 | + errors.append(f"'seed_examples' should contain at least {min_seed_examples} examples, found {len(seed_examples)}. Please add {min_seed_examples - len(seed_examples)} more seed example(s)") |
| 160 | + else: |
| 161 | + print(f"Found {len(seed_examples)} 'contexts' in 'sed_examples'. Minimum expected number is {min_seed_examples}...") |
| 162 | + |
| 163 | + if seed_examples: |
| 164 | + for i, example in enumerate(seed_examples, start=1): |
| 165 | + qa_pairs = example.get('questions_and_answers') |
| 166 | + if not qa_pairs: |
| 167 | + errors.append(f"Seed Example {i} is missing 'questions_and_answers' section.") |
| 168 | + elif len(qa_pairs) != num_qa_pairs: |
| 169 | + errors.append(f"Seed Example {i} should contain {num_qa_pairs} question-answer pairs, found {len(qa_pairs)}. Please add {num_qa_pairs - len(qa_pairs)} more question-answer pair(s) to seed example {i}") |
| 170 | + else: |
| 171 | + print(f"Seed Example {i} contains expected number ({num_qa_pairs}) of 'question_and_answers'...") |
| 172 | + |
| 173 | + if errors: |
| 174 | + print("\n\033[31mERROR! Seed Examples validation failed with the following issues:\033[0m") |
| 175 | + for err in errors: |
| 176 | + print(f"- {err}") |
| 177 | + else: |
| 178 | + print(f"Seed Examples YAML {seed_examples_path.resolve()} is valid :)") |
| 179 | + print(f"\n") |
0 commit comments