Main method to generate responses. Uses asyncio.run to execute the async generation pipeline. Pre-encodes images before async processing. Args: messages: List of input messages. **kwargs: Additional generation arguments. Returns:
(self, messages: List[Any], **kwargs)
| 488 | return final_results |
| 489 | |
| 490 | def generate(self, messages: List[Any], **kwargs) -> List[str]: |
| 491 | """Main method to generate responses. |
| 492 | |
| 493 | Uses asyncio.run to execute the async generation pipeline. |
| 494 | Pre-encodes images before async processing. |
| 495 | |
| 496 | Args: |
| 497 | messages: List of input messages. |
| 498 | **kwargs: Additional generation arguments. |
| 499 | |
| 500 | Returns: |
| 501 | List of response strings, in same order as input. |
| 502 | """ |
| 503 | if not messages: |
| 504 | return [] |
| 505 | |
| 506 | # Phase 1: image pre-encoding |
| 507 | t_encode = time.time() |
| 508 | self._pre_encode_images(messages) |
| 509 | encode_elapsed = time.time() - t_encode |
| 510 | |
| 511 | # Phase 2: request preprocessing |
| 512 | t_preproc = time.time() |
| 513 | processed_messages = [] |
| 514 | for msg in messages: |
| 515 | processed_messages.append(self.pre_process(msg)) |
| 516 | preproc_elapsed = time.time() - t_preproc |
| 517 | |
| 518 | # Phase 3: drop redundant original buffers |
| 519 | t_cleanup = time.time() |
| 520 | self._clear_message_buffers(messages) |
| 521 | cleanup_elapsed = time.time() - t_cleanup |
| 522 | |
| 523 | if self.logger: |
| 524 | self.logger.info( |
| 525 | "[OpenAIAPI] prepare phases: " |
| 526 | f"pre_encode={encode_elapsed:.2f}s, " |
| 527 | f"preprocess={preproc_elapsed:.2f}s, " |
| 528 | f"clear_message_buffers={cleanup_elapsed:.2f}s" |
| 529 | ) |
| 530 | |
| 531 | all_kwargs = dict(self.default_kwargs) |
| 532 | all_kwargs.update(kwargs) |
| 533 | |
| 534 | # Run async pipeline |
| 535 | try: |
| 536 | # Check if there's already a running event loop |
| 537 | loop = asyncio.get_running_loop() |
| 538 | except RuntimeError: |
| 539 | loop = None |
| 540 | |
| 541 | if loop and loop.is_running(): |
| 542 | # We're inside an existing event loop (e.g., Jupyter notebook) |
| 543 | # Use nest_asyncio or create a new thread |
| 544 | import threading |
| 545 | |
| 546 | self.logger.warning("Running in existing event loop, using thread") |
| 547 | result = [None] |
no test coverage detected