| 9 | |
| 10 | |
| 11 | class ModelClient: |
| 12 | def __init__(self, |
| 13 | model_config: ModelConfig, |
| 14 | answer_config: AnswerExtractionConfig = None, |
| 15 | postprocess_config: PostProcessConfig = None |
| 16 | ) -> None: |
| 17 | self.model_config = model_config |
| 18 | self.model = PostProcessor( |
| 19 | model=BaseLanguageModel.get_model_from_config(self.model_config), |
| 20 | config=postprocess_config |
| 21 | ) |
| 22 | self.answer_extractor = AnswerExtractor(answer_config) |
| 23 | self.token_usage: int = 0 |
| 24 | |
| 25 | def generate(self, |
| 26 | prompts: Union[str, List[str]], |
| 27 | n: int = 1, |
| 28 | processor_args: ProcessorArgs = ProcessorArgs(), |
| 29 | usage_counter: ModelUsageCounter = None, |
| 30 | **kwargs |
| 31 | ) -> Union[str, List[str], List[List[str]]]: |
| 32 | """ |
| 33 | Generate responses from LLM model. |
| 34 | |
| 35 | Args: |
| 36 | prompts: Single prompt string or list of prompts |
| 37 | n: Number of responses to generate per prompt |
| 38 | processor_args: Arguments for post-processing (e.g., majority voting) |
| 39 | usage_counter: Instance to count and estimate token/time usage |
| 40 | **kwargs: Additional arguments for LLM inference |
| 41 | |
| 42 | Returns: |
| 43 | Response string(s) from LLM: |
| 44 | - Single prompt, n=1: str |
| 45 | - Batch prompts, n=1: List[str] |
| 46 | - Any prompts, n>1: List[List[str]] |
| 47 | """ |
| 48 | if processor_args.answer_extraction is None or not processor_args.answer_extraction.enable: |
| 49 | answer_extractor = None |
| 50 | else: |
| 51 | prompts = self.answer_extractor.format_prompts(prompts) |
| 52 | answer_extractor = self.answer_extractor |
| 53 | |
| 54 | responses = self.model.generate(prompts, n, answer_extractor, processor_args, usage_counter, **kwargs) |
| 55 | return responses |
| 56 | |
| 57 | def generate_with_images(self, |
| 58 | prompts: Union[str, List[str]], |
| 59 | images: Union[str, List[str]], |
| 60 | n: int = 1, |
| 61 | processor_args: ProcessorArgs = ProcessorArgs(), |
| 62 | usage_counter: ModelUsageCounter = None, |
| 63 | **kwargs |
| 64 | ) -> Union[str, List[str], List[List[str]]]: |
| 65 | """ |
| 66 | Generate responses from VLM model with image inputs. |
| 67 | |
| 68 | Args: |