Invoke the chain on a batch of inputs either async or not :param inputs: The list of all inputs :param num_workers: The number of workers :return: A list of results
(self, inputs: list[dict], num_workers: int)
| 121 | return [t.result() for t in list(all_res)] |
| 122 | |
| 123 | def batch_invoke(self, inputs: list[dict], num_workers: int): |
| 124 | """ |
| 125 | Invoke the chain on a batch of inputs either async or not |
| 126 | :param inputs: The list of all inputs |
| 127 | :param num_workers: The number of workers |
| 128 | :return: A list of results |
| 129 | """ |
| 130 | |
| 131 | def sample_generator(): |
| 132 | for sample in inputs: |
| 133 | yield sample |
| 134 | |
| 135 | def process_sample_with_progress(sample): |
| 136 | result = self.invoke(sample) |
| 137 | pbar.update(1) # Update the progress bar |
| 138 | return result |
| 139 | |
| 140 | if not ('async_params' in self.llm_config.keys()): # non async mode, use regular workers |
| 141 | with concurrent.futures.ThreadPoolExecutor(max_workers=num_workers) as executor: |
| 142 | with tqdm(total=len(inputs), desc="Processing samples") as pbar: |
| 143 | all_results = list(executor.map(process_sample_with_progress, sample_generator())) |
| 144 | else: |
| 145 | all_results = [] |
| 146 | for i in trange(0, len(inputs), num_workers, desc='Predicting'): |
| 147 | results = asyncio.run(self.async_batch_invoke(inputs[i:i + num_workers])) |
| 148 | all_results += results |
| 149 | all_results = [res for res in all_results if res is not None] |
| 150 | return all_results |
| 151 | |
| 152 | def build_chain(self): |
| 153 | """ |
no test coverage detected