in src/models/struxgpt_v2.py [0:0]
def gen_struct_qa(self, qa_gen_template: str,
ctx_struct: Union[StructItem, dict],
chunk_titles: List[str], chunk_contents: List[str], idx: str,
demonstrations: str = "", response: str = "",
prompt_only: bool = False, parse_only: bool = False,
**prompt_kwargs):
if not parse_only:
#### generate qa data
ctx_struct = self.check_struct_item(ctx_struct, StructItem)
ctx_mindmap = self.dump_mindmap(ctx_struct, highlights=chunk_titles)
contents = ''
for title, content in zip(chunk_titles, chunk_contents):
contents += f'\n* **{title}**\n```\n{content}\n```\n'
chunk_num = len(chunk_titles)
prompt = qa_gen_template.format(mindmap=ctx_mindmap, field=ctx_struct.scope,
passage_num=chunk_num, contents=contents,
demos=demonstrations, **prompt_kwargs)
if prompt_only:
return {'idx': idx, 'question': prompt,
'ctx_struct': ctx_struct, 'ctx_mindmap': ctx_mindmap}
else:
response = self.call_model(prompt)
#### parse qa data
try:
invalid_keywords = ['test', 'exam', 'mindmap', 'segment',
'考', '思维导图', '片段']
sections = response.strip().split('\n\n')
assert len(sections) == 3
assert sections[0].splitlines()[0].startswith('# Question')
assert sections[1].splitlines()[0].startswith('# Answer')
assert sections[2].splitlines()[0].startswith('# Explanation')
question, answer, explanation = [
'\n'.join(section.splitlines()[1:]) for section in sections
]
explanation = '\n'.join(line for line in explanation.splitlines() \
if all(flag not in line.lower() for flag in invalid_keywords))
for flag in ['**Integrating Mindmap Elements:**', '**Logical Deduction:**', '**Conclusion:**']:
explanation = explanation.replace(flag, '')
explanation = explanation[1:] if explanation[0] in PUNCTUATION else explanation
explanation = explanation.strip().replace('given', 'related')
return {'idx': idx, 'question': question, 'answer': answer, 'explanation': explanation,
'ctx_content': chunk_contents, 'chunk_titles': chunk_titles}
except:
return {}