peter2000/audit_uganda_config_space
0
1import json2from typing import Any, Dict, List3 4from distilabel.steps.tasks.typing import ChatType5from distilabel.steps.tasks.text_generation import TextGeneration6from distilabel.steps import StepInput, StepOutput, Step7 8from dotenv import load_dotenv9 10from defaults import (11 DEFAULT_DOMAIN,12 DEFAULT_PERSPECTIVES,13 DEFAULT_TOPICS,14 DEFAULT_EXAMPLES,15 DEFAULT_SYSTEM_PROMPT,16 N_PERSPECTIVES,17 N_TOPICS,18 N_EXAMPLES,19)20 21load_dotenv()22 23# Application description used for SelfInstruct24APPLICATION_DESCRIPTION = f"""You are an AI assistant than generates queries around the domain of {DEFAULT_DOMAIN}.25Your should not expect basic but profound questions from your users.26The queries should reflect a diversity of vision and economic positions and political positions.27The queries may know about different methods of {DEFAULT_DOMAIN}.28The queries can be positioned politically, economically, socially, or practically.29Also take into account the impact of diverse causes on diverse domains."""30 31 32TOPICS = DEFAULT_TOPICS[:N_TOPICS]33PERSPECTIVES = DEFAULT_PERSPECTIVES[:N_PERSPECTIVES]34EXAMPLES = DEFAULT_EXAMPLES[:N_EXAMPLES]35 36 37def create_examples_template(examples: List[Dict[str, str]]) -> List[str]:38 questions = """ Examples of high quality questions:"""39 answers = """ Examples of high quality answers:"""40 for example in examples:41 questions += f"""\n- Question: {example["question"]}\n"""42 answers += f"""\n- Answer: {example["answer"]}\n"""43 44 _template: str = (45 """{instruction}\nThis is the the instruction.\n Examples: """46 + questions47 + answers48 )49 return _template50 51 52def create_topics(topics: List[str], positions: List[str]) -> List[str]:53 return [54 f"{topic} from a {position} perspective"55 for topic in topics56 for position in positions57 ]58 59 60class DomainExpert(TextGeneration):61 """A customized task to generate text as a domain expert in the domain of farming and agriculture."""62 63 _system_prompt: (str) = DEFAULT_SYSTEM_PROMPT64 _template: str = """{instruction}\nThis is the the instruction.\n Examples: """65 66 def format_input(self, input: Dict[str, Any]) -> "ChatType":67 return [68 {69 "role": "system",70 "content": self._system_prompt,71 },72 {73 "role": "user",74 "content": self._template.format(**input),75 },76 ]77 78 79class CleanNumberedList(Step):80 """A step to clean the numbered list of questions."""81 82 def process(self, inputs: StepInput) -> StepOutput:83 import re84 85 pattern = r"^\d+\.\s"86 87 for input in inputs:88 input["question"] = re.sub(pattern, "", input["question"])89 yield inputs90 