Run the meta-prompts and get new prompt suggestion, estimated prompt score and a set of challenging samples for the new prompts
(self)
| 92 | return {'prompt': sorted_history[-1]['prompt'], 'score': sorted_history[-1]['score']} |
| 93 | |
| 94 | def run_step_prompt(self): |
| 95 | """ |
| 96 | Run the meta-prompts and get new prompt suggestion, estimated prompt score and a set of challenging samples |
| 97 | for the new prompts |
| 98 | """ |
| 99 | step_num = len(self.eval.history) |
| 100 | if (step_num < self.config.meta_prompts.warmup) or (step_num % 3) > 0: |
| 101 | last_history = self.eval.history[-self.config.meta_prompts.history_length:] |
| 102 | else: |
| 103 | sorted_history = sorted(self.eval.history[self.config.meta_prompts.warmup - 1:], key=lambda x: x['score'], |
| 104 | reverse=False) |
| 105 | last_history = sorted_history[-self.config.meta_prompts.history_length:] |
| 106 | history_prompt = '\n'.join([self.eval.sample_to_text(sample, |
| 107 | num_errors_per_label=self.config.meta_prompts.num_err_prompt, |
| 108 | is_score=True) for sample in last_history]) |
| 109 | prompt_input = {"history": history_prompt, "task_description": self.task_description, |
| 110 | 'error_analysis': last_history[-1]['analysis']} |
| 111 | if 'label_schema' in self.config.dataset.keys(): |
| 112 | prompt_input["labels"] = json.dumps(self.config.dataset.label_schema) |
| 113 | prompt_suggestion = self.meta_chain.step_prompt_chain.invoke(prompt_input) |
| 114 | self.log_and_print(f'Previous prompt score:\n{self.eval.mean_score}\n#########\n') |
| 115 | self.log_and_print(f'Get new prompt:\n{prompt_suggestion["prompt"]}') |
| 116 | self.batch_id += 1 |
| 117 | if len(self.dataset) < self.config.dataset.max_samples: |
| 118 | batch_input = {"num_samples": self.config.meta_prompts.samples_generation_batch, |
| 119 | "task_description": self.task_description, |
| 120 | "prompt": prompt_suggestion['prompt']} |
| 121 | batch_inputs = self.generate_samples_batch(batch_input, self.config.meta_prompts.num_generated_samples, |
| 122 | self.config.meta_prompts.samples_generation_batch) |
| 123 | |
| 124 | if sum([len(t['errors']) for t in last_history]) > 0: |
| 125 | history_samples = '\n'.join([self.eval.sample_to_text(sample, |
| 126 | num_errors_per_label=self.config.meta_prompts.num_err_samples, |
| 127 | is_score=False) for sample in last_history]) |
| 128 | for batch in batch_inputs: |
| 129 | extra_samples = self.dataset.sample_records() |
| 130 | extra_samples_text = DatasetBase.samples_to_text(extra_samples) |
| 131 | batch['history'] = history_samples |
| 132 | batch['extra_samples'] = extra_samples_text |
| 133 | else: |
| 134 | for batch in batch_inputs: |
| 135 | extra_samples = self.dataset.sample_records() |
| 136 | extra_samples_text = DatasetBase.samples_to_text(extra_samples) |
| 137 | batch['history'] = 'No previous errors information' |
| 138 | batch['extra_samples'] = extra_samples_text |
| 139 | |
| 140 | samples_batches = self.meta_chain.step_samples.batch_invoke(batch_inputs, |
| 141 | self.config.meta_prompts.num_workers) |
| 142 | new_samples = [element for sublist in samples_batches for element in sublist['samples']] |
| 143 | new_samples = self.dataset.remove_duplicates(new_samples) |
| 144 | self.dataset.add(new_samples, self.batch_id) |
| 145 | logging.info('Get new samples') |
| 146 | self.cur_prompt = prompt_suggestion['prompt'] |
| 147 | |
| 148 | def stop_criteria(self): |
| 149 | """ |
no test coverage detected